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;
94  b2_toiMaxRootIters = 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",
108  b2_gjkCalls, b2_gjkIters / float(b2_gjkCalls), b2_gjkMaxIters);
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",
115  b2_toiCalls, b2_toiIters / float(b2_toiCalls), b2_toiMaxRootIters);
117 
118  g_debugDraw.DrawString(5, m_textLine, "ave toi root iters = %3.1f, max toi root iters = %d",
119  b2_toiRootIters / float(b2_toiCalls), b2_toiMaxRootIters);
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);
static Test * Create()
b2Body * m_bullet
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
B2_API const b2Vec2 b2Vec2_zero
Useful constant.
f
#define B2_API
Definition: b2_api.h:49
B2_API int32 b2_toiCalls
B2_API int32 b2_gjkMaxIters
Definition: b2_distance.cpp:30
void SetTransform(const b2Vec2 &position, float angle)
Definition: b2_body.cpp:415
int32 m_textLine
Definition: test.h:127
B2_API int32 b2_gjkCalls
Definition: b2_distance.cpp:30
Definition: test.h:80
bool bullet
Definition: b2_body.h:115
float RandomFloat()
Random number in range [-1,1].
Definition: test.h:37
A 2D column vector.
Definition: b2_math.h:41
signed int int32
Definition: b2_types.h:28
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
void SetAsBox(float hx, float hy)
B2_API int32 b2_toiMaxRootIters
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
void SetLinearVelocity(const b2Vec2 &v)
Definition: b2_body.h:504
static int testIndex
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
void Step(Settings &settings) override
Definition: bullet_test.cpp:97
B2_API int32 b2_gjkIters
Definition: b2_distance.cpp:30
int32 m_textIncrement
Definition: test.h:135
b2World * m_world
Definition: test.h:128
B2_API int32 b2_toiRootIters
int32 m_stepCount
Definition: test.h:134
b2Body * m_body
b2Vec2 position
Definition: b2_body.h:78
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
void SetAngularVelocity(float omega)
Definition: b2_body.h:524
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
B2_API int32 b2_toiIters
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
void Launch()
Definition: bullet_test.cpp:71
B2_API int32 b2_toiMaxIters


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