externals
box2d
unit-test
world_test.cpp
Go to the documentation of this file.
1
// MIT License
2
3
// Copyright (c) 2019 Erin Catto
4
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
12
// The above copyright notice and this permission notice shall be included in all
13
// copies or substantial portions of the Software.
14
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
// SOFTWARE.
22
23
#include "
box2d/box2d.h
"
24
#include "
doctest.h
"
25
#include <stdio.h>
26
27
static
bool
begin_contact
=
false
;
28
29
class
MyContactListener
:
public
b2ContactListener
30
{
31
public
:
32
void
BeginContact
(
b2Contact
* contact)
33
{
34
begin_contact
=
true
;
35
}
36
};
37
38
DOCTEST_TEST_CASE
(
"begin contact"
)
39
{
40
b2World
world =
b2World
(
b2Vec2
(0.0
f
, -10.0
f
));
41
MyContactListener
listener;
42
world.
SetContactListener
(&listener);
43
44
b2CircleShape
circle;
45
circle.
m_radius
= 5.f;
46
47
b2BodyDef
bodyDef;
48
bodyDef.
type
=
b2_dynamicBody
;
49
50
b2Body
* bodyA = world.
CreateBody
(&bodyDef);
51
b2Body
* bodyB = world.
CreateBody
(&bodyDef);
52
bodyA->
CreateFixture
(&circle, 0.0
f
);
53
bodyB->
CreateFixture
(&circle, 0.0
f
);
54
55
bodyA->
SetTransform
(
b2Vec2
(0.
f
, 0.
f
), 0.
f
);
56
bodyB->
SetTransform
(
b2Vec2
(100.
f
, 0.
f
), 0.
f
);
57
58
const
float
timeStep = 1.f / 60.f;
59
const
int32
velocityIterations = 6;
60
const
int32
positionIterations = 2;
61
62
world.
Step
(timeStep, velocityIterations, positionIterations);
63
64
CHECK
(world.
GetContactList
() ==
nullptr
);
65
CHECK
(
begin_contact
==
false
);
66
67
bodyB->
SetTransform
(
b2Vec2
(1.
f
, 0.
f
), 0.
f
);
68
69
world.
Step
(timeStep, velocityIterations, positionIterations);
70
71
CHECK
(world.
GetContactList
() !=
nullptr
);
72
CHECK
(
begin_contact
==
true
);
73
}
MyContactListener::BeginContact
void BeginContact(b2Contact *contact)
Called when two fixtures begin to touch.
Definition:
world_test.cpp:32
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition:
b2_body.cpp:165
MyContactListener
Definition:
world_test.cpp:29
f
f
b2Body::SetTransform
void SetTransform(const b2Vec2 &position, float angle)
Definition:
b2_body.cpp:415
b2BodyDef
Definition:
b2_body.h:52
b2Contact
Definition:
b2_contact.h:88
b2CircleShape
A solid circle shape.
Definition:
b2_circle_shape.h:30
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
begin_contact
static bool begin_contact
Definition:
world_test.cpp:27
int32
signed int int32
Definition:
b2_types.h:28
b2BodyDef::type
b2BodyType type
Definition:
b2_body.h:74
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition:
b2_body.h:128
b2Shape::m_radius
float m_radius
Definition:
b2_shape.h:102
b2World::GetContactList
b2Contact * GetContactList()
Definition:
b2_world.h:284
b2World::SetContactListener
void SetContactListener(b2ContactListener *listener)
Definition:
b2_world.cpp:105
b2ContactListener
Definition:
b2_world_callbacks.h:86
DOCTEST_TEST_CASE
DOCTEST_TEST_CASE("begin contact")
Definition:
world_test.cpp:38
CHECK
#define CHECK
Definition:
doctest.h:2471
box2d.h
b2World::Step
void Step(float timeStep, int32 velocityIterations, int32 positionIterations)
Definition:
b2_world.cpp:905
doctest.h
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition:
b2_world.cpp:115
b2World
Definition:
b2_world.h:46
b2_dynamicBody
Definition:
b2_body.h:47
mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:22