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;
71  b2Assert(n < e_rowCount * e_columnCount);
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);
const b2Shape * shape
Definition: b2_fixture.h:76
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
int32 m_indices[e_rowCount *e_columnCount]
Definition: box_stack.cpp:171
uintptr_t pointer
For legacy compatibility.
Definition: b2_settings.h:66
f
#define B2_API
Definition: b2_api.h:49
B2_API bool g_blockSolve
int32 m_textLine
Definition: test.h:127
Definition: test.h:80
bool bullet
Definition: b2_body.h:115
#define GLFW_KEY_B
Definition: glfw3.h:379
b2Body * m_bullet
Definition: box_stack.cpp:169
A solid circle shape.
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)
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
void SetLinearVelocity(const b2Vec2 &v)
Definition: b2_body.h:504
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
void Step(Settings &settings) override
Definition: box_stack.cpp:128
void Keyboard(int key) override
Definition: box_stack.cpp:90
int32 m_textIncrement
Definition: test.h:135
b2BodyUserData userData
Use this to store application specific body data.
Definition: b2_body.h:121
b2World * m_world
Definition: test.h:128
static Test * Create()
Definition: box_stack.cpp:164
b2Vec2 position
Definition: b2_body.h:78
#define GLFW_KEY_COMMA
Definition: glfw3.h:362
void DestroyBody(b2Body *body)
Definition: b2_world.cpp:139
static int testIndex
Definition: box_stack.cpp:174
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
b2Body * m_bodies[e_rowCount *e_columnCount]
Definition: box_stack.cpp:170
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
float restitution
The restitution (elasticity) usually in the range [0,1].
Definition: b2_fixture.h:85
#define b2Assert(A)
Definition: b2_common.h:37
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82


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