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 
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.0f, -10.0f));
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.0f);
53  bodyB->CreateFixture(&circle, 0.0f);
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 }
void BeginContact(b2Contact *contact)
Called when two fixtures begin to touch.
Definition: world_test.cpp:32
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
f
void SetTransform(const b2Vec2 &position, float angle)
Definition: b2_body.cpp:415
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
static bool begin_contact
Definition: world_test.cpp:27
signed int int32
Definition: b2_types.h:28
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
float m_radius
Definition: b2_shape.h:102
b2Contact * GetContactList()
Definition: b2_world.h:284
void SetContactListener(b2ContactListener *listener)
Definition: b2_world.cpp:105
DOCTEST_TEST_CASE("begin contact")
Definition: world_test.cpp:38
#define CHECK
Definition: doctest.h:2471
void Step(float timeStep, int32 velocityIterations, int32 positionIterations)
Definition: b2_world.cpp:905
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:22