bullet_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 "test.h"
24 
25 class BulletTest : public Test
26 {
27 public:
28 
30  {
31  {
32  b2BodyDef bd;
33  bd.position.Set(0.0f, 0.0f);
34  b2Body* body = m_world->CreateBody(&bd);
35 
36  b2EdgeShape edge;
37 
38  edge.SetTwoSided(b2Vec2(-10.0f, 0.0f), b2Vec2(10.0f, 0.0f));
39  body->CreateFixture(&edge, 0.0f);
40 
41  b2PolygonShape shape;
42  shape.SetAsBox(0.2f, 1.0f, b2Vec2(0.5f, 1.0f), 0.0f);
43  body->CreateFixture(&shape, 0.0f);
44  }
45 
46  {
47  b2BodyDef bd;
48  bd.type = b2_dynamicBody;
49  bd.position.Set(0.0f, 4.0f);
50 
51  b2PolygonShape box;
52  box.SetAsBox(2.0f, 0.1f);
53 
54  m_body = m_world->CreateBody(&bd);
55  m_body->CreateFixture(&box, 1.0f);
56 
57  box.SetAsBox(0.25f, 0.25f);
58 
59  //m_x = RandomFloat(-1.0f, 1.0f);
60  m_x = 0.20352793f;
61  bd.position.Set(m_x, 10.0f);
62  bd.bullet = true;
63 
64  m_bullet = m_world->CreateBody(&bd);
65  m_bullet->CreateFixture(&box, 100.0f);
66 
67  m_bullet->SetLinearVelocity(b2Vec2(0.0f, -50.0f));
68  }
69  }
70 
71  void Launch()
72  {
73  m_body->SetTransform(b2Vec2(0.0f, 4.0f), 0.0f);
76 
77  m_x = RandomFloat(-1.0f, 1.0f);
78  m_bullet->SetTransform(b2Vec2(m_x, 10.0f), 0.0f);
79  m_bullet->SetLinearVelocity(b2Vec2(0.0f, -50.0f));
81 
85 
86  b2_gjkCalls = 0;
87  b2_gjkIters = 0;
88  b2_gjkMaxIters = 0;
89 
90  b2_toiCalls = 0;
91  b2_toiIters = 0;
92  b2_toiMaxIters = 0;
93  b2_toiRootIters = 0;
95  }
96 
97  void Step(Settings& settings) override
98  {
99  Test::Step(settings);
100 
104 
105  if (b2_gjkCalls > 0)
106  {
107  g_debugDraw.DrawString(5, m_textLine, "gjk calls = %d, ave gjk iters = %3.1f, max gjk iters = %d",
110  }
111 
112  if (b2_toiCalls > 0)
113  {
114  g_debugDraw.DrawString(5, m_textLine, "toi calls = %d, ave toi iters = %3.1f, max toi iters = %d",
117 
118  g_debugDraw.DrawString(5, m_textLine, "ave toi root iters = %3.1f, max toi root iters = %d",
121  }
122 
123  if (m_stepCount % 60 == 0)
124  {
125  Launch();
126  }
127  }
128 
129  static Test* Create()
130  {
131  return new BulletTest;
132  }
133 
136  float m_x;
137 };
138 
139 static int testIndex = RegisterTest("Continuous", "Bullet Test", BulletTest::Create);
Test::m_textIncrement
int32 m_textIncrement
Definition: test.h:135
b2_gjkIters
B2_API int32 b2_gjkIters
Definition: b2_distance.cpp:30
b2EdgeShape
Definition: b2_edge_shape.h:32
b2BodyDef::bullet
bool bullet
Definition: b2_body.h:115
g_debugDraw
DebugDraw g_debugDraw
Definition: draw.cpp:32
BulletTest::m_bullet
b2Body * m_bullet
Definition: bullet_test.cpp:135
BulletTest
Definition: bullet_test.cpp:25
b2_toiCalls
B2_API int32 b2_toiCalls
Definition: b2_time_of_impact.cpp:33
b2Vec2_zero
const B2_API b2Vec2 b2Vec2_zero
Useful constant.
b2Body::SetTransform
void SetTransform(const b2Vec2 &position, float angle)
Definition: b2_body.cpp:415
Test::m_stepCount
int32 m_stepCount
Definition: test.h:134
BulletTest::Launch
void Launch()
Definition: bullet_test.cpp:71
b2_toiMaxIters
B2_API int32 b2_toiMaxIters
Definition: b2_time_of_impact.cpp:33
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
B2_API
#define B2_API
Definition: b2_api.h:49
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
BulletTest::Step
void Step(Settings &settings) override
Definition: bullet_test.cpp:97
b2_toiRootIters
B2_API int32 b2_toiRootIters
Definition: b2_time_of_impact.cpp:34
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
b2Body::SetAngularVelocity
void SetAngularVelocity(float omega)
Definition: b2_body.h:524
b2EdgeShape::SetTwoSided
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
Definition: b2_edge_shape.cpp:36
b2Body::SetLinearVelocity
void SetLinearVelocity(const b2Vec2 &v)
Definition: b2_body.h:504
f
f
b2BodyDef::type
b2BodyType type
Definition: b2_body.h:74
BulletTest::m_body
b2Body * m_body
Definition: bullet_test.cpp:134
b2_toiMaxRootIters
B2_API int32 b2_toiMaxRootIters
Definition: b2_time_of_impact.cpp:34
b2_dynamicBody
@ b2_dynamicBody
Definition: b2_body.h:47
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
BulletTest::BulletTest
BulletTest()
Definition: bullet_test.cpp:29
RandomFloat
float RandomFloat()
Random number in range [-1,1].
Definition: test.h:37
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition: b2_polygon_shape.cpp:36
b2_gjkCalls
B2_API int32 b2_gjkCalls
Definition: b2_distance.cpp:30
BulletTest::m_x
float m_x
Definition: bullet_test.cpp:136
b2BodyDef
Definition: b2_body.h:52
b2_gjkMaxIters
B2_API int32 b2_gjkMaxIters
Definition: b2_distance.cpp:30
Settings
Definition: settings.h:25
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
b2PolygonShape
Definition: b2_polygon_shape.h:32
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
Test::m_textLine
int32 m_textLine
Definition: test.h:127
int32
signed int int32
Definition: b2_types.h:28
Test
Definition: test.h:80
b2BodyDef::position
b2Vec2 position
Definition: b2_body.h:78
testIndex
static int testIndex
Definition: bullet_test.cpp:139
Test::Step
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2_toiIters
B2_API int32 b2_toiIters
Definition: b2_time_of_impact.cpp:33
Test::m_world
b2World * m_world
Definition: test.h:128
BulletTest::Create
static Test * Create()
Definition: bullet_test.cpp:129
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:07