box_stack.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 extern B2_API bool g_blockSolve;
26 
27 class BoxStack : public Test
28 {
29 public:
30 
31  enum
32  {
35  //e_columnCount = 1,
36  //e_rowCount = 1
37  };
38 
40  {
41  {
42  b2BodyDef bd;
43  b2Body* ground = m_world->CreateBody(&bd);
44 
45  b2EdgeShape shape;
46  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
47  ground->CreateFixture(&shape, 0.0f);
48 
49  shape.SetTwoSided(b2Vec2(20.0f, 0.0f), b2Vec2(20.0f, 20.0f));
50  ground->CreateFixture(&shape, 0.0f);
51  }
52 
53  float xs[5] = {0.0f, -10.0f, -5.0f, 5.0f, 10.0f};
54 
55  for (int32 j = 0; j < e_columnCount; ++j)
56  {
57  b2PolygonShape shape;
58  shape.SetAsBox(0.5f, 0.5f);
59 
60  b2FixtureDef fd;
61  fd.shape = &shape;
62  fd.density = 1.0f;
63  fd.friction = 0.3f;
64 
65  for (int i = 0; i < e_rowCount; ++i)
66  {
67  b2BodyDef bd;
68  bd.type = b2_dynamicBody;
69 
70  int32 n = j * e_rowCount + i;
72  m_indices[n] = n;
73  bd.userData.pointer = n;
74 
75  float x = 0.0f;
76  //float x = RandomFloat(-0.02f, 0.02f);
77  //float x = i % 2 == 0 ? -0.01f : 0.01f;
78  bd.position.Set(xs[j] + x, 0.55f + 1.1f * i);
79  b2Body* body = m_world->CreateBody(&bd);
80 
81  m_bodies[n] = body;
82 
83  body->CreateFixture(&fd);
84  }
85  }
86 
87  m_bullet = NULL;
88  }
89 
90  void Keyboard(int key) override
91  {
92  switch (key)
93  {
94  case GLFW_KEY_COMMA:
95  if (m_bullet != NULL)
96  {
98  m_bullet = NULL;
99  }
100 
101  {
102  b2CircleShape shape;
103  shape.m_radius = 0.25f;
104 
105  b2FixtureDef fd;
106  fd.shape = &shape;
107  fd.density = 20.0f;
108  fd.restitution = 0.05f;
109 
110  b2BodyDef bd;
111  bd.type = b2_dynamicBody;
112  bd.bullet = true;
113  bd.position.Set(-31.0f, 5.0f);
114 
115  m_bullet = m_world->CreateBody(&bd);
116  m_bullet->CreateFixture(&fd);
117 
118  m_bullet->SetLinearVelocity(b2Vec2(400.0f, 0.0f));
119  }
120  break;
121 
122  case GLFW_KEY_B:
124  break;
125  }
126  }
127 
128  void Step(Settings& settings) override
129  {
130  Test::Step(settings);
131  g_debugDraw.DrawString(5, m_textLine, "Press: (,) to launch a bullet.");
133  g_debugDraw.DrawString(5, m_textLine, "Blocksolve = %d", g_blockSolve);
134  //if (m_stepCount == 300)
135  //{
136  // if (m_bullet != NULL)
137  // {
138  // m_world->DestroyBody(m_bullet);
139  // m_bullet = NULL;
140  // }
141 
142  // {
143  // b2CircleShape shape;
144  // shape.m_radius = 0.25f;
145 
146  // b2FixtureDef fd;
147  // fd.shape = &shape;
148  // fd.density = 20.0f;
149  // fd.restitution = 0.05f;
150 
151  // b2BodyDef bd;
152  // bd.type = b2_dynamicBody;
153  // bd.bullet = true;
154  // bd.position.Set(-31.0f, 5.0f);
155 
156  // m_bullet = m_world->CreateBody(&bd);
157  // m_bullet->CreateFixture(&fd);
158 
159  // m_bullet->SetLinearVelocity(b2Vec2(400.0f, 0.0f));
160  // }
161  //}
162  }
163 
164  static Test* Create()
165  {
166  return new BoxStack;
167  }
168 
172 };
173 
174 static int testIndex = RegisterTest("Stacking", "Boxes", BoxStack::Create);
Test::m_textIncrement
int32 m_textIncrement
Definition: test.h:135
b2EdgeShape
Definition: b2_edge_shape.h:32
b2BodyDef::bullet
bool bullet
Definition: b2_body.h:115
BoxStack::m_bullet
b2Body * m_bullet
Definition: box_stack.cpp:169
g_debugDraw
DebugDraw g_debugDraw
Definition: draw.cpp:32
NULL
#define NULL
BoxStack::Create
static Test * Create()
Definition: box_stack.cpp:164
GLFW_KEY_B
#define GLFW_KEY_B
Definition: glfw3.h:379
testIndex
static int testIndex
Definition: box_stack.cpp:174
BoxStack::e_columnCount
@ e_columnCount
Definition: box_stack.cpp:33
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2CircleShape
A solid circle shape.
Definition: b2_circle_shape.h:30
b2FixtureDef
Definition: b2_fixture.h:61
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
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
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
b2FixtureDef::restitution
float restitution
The restitution (elasticity) usually in the range [0,1].
Definition: b2_fixture.h:85
b2FixtureDef::friction
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82
b2_dynamicBody
@ b2_dynamicBody
Definition: b2_body.h:47
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
b2Shape::m_radius
float m_radius
Definition: b2_shape.h:102
BoxStack::Keyboard
void Keyboard(int key) override
Definition: box_stack.cpp:90
g_blockSolve
B2_API bool g_blockSolve
Definition: b2_contact_solver.cpp:34
BoxStack::m_bodies
b2Body * m_bodies[e_rowCount *e_columnCount]
Definition: box_stack.cpp:170
BoxStack::e_rowCount
@ e_rowCount
Definition: box_stack.cpp:34
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition: b2_polygon_shape.cpp:36
b2FixtureDef::density
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
b2BodyDef
Definition: b2_body.h:52
BoxStack::m_indices
int32 m_indices[e_rowCount *e_columnCount]
Definition: box_stack.cpp:171
GLFW_KEY_COMMA
#define GLFW_KEY_COMMA
Definition: glfw3.h:362
Settings
Definition: settings.h:25
b2FixtureDef::shape
const b2Shape * shape
Definition: b2_fixture.h:76
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
b2World::DestroyBody
void DestroyBody(b2Body *body)
Definition: b2_world.cpp:139
BoxStack
Definition: box_stack.cpp:27
int32
signed int int32
Definition: b2_types.h:28
BoxStack::BoxStack
BoxStack()
Definition: box_stack.cpp:39
Test
Definition: test.h:80
b2BodyDef::position
b2Vec2 position
Definition: b2_body.h:78
b2BodyUserData::pointer
uintptr_t pointer
For legacy compatibility.
Definition: b2_settings.h:66
Test::Step
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2Assert
#define b2Assert(A)
Definition: b2_common.h:37
BoxStack::Step
void Step(Settings &settings) override
Definition: box_stack.cpp:128
b2BodyDef::userData
b2BodyUserData userData
Use this to store application specific body data.
Definition: b2_body.h:121
Test::m_world
b2World * m_world
Definition: test.h:128
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165


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