polygon_shapes.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 
27 
32 {
33 public:
34 
35  enum
36  {
38  };
39 
41  {
42  m_count = 0;
43  }
44 
47  bool ReportFixture(b2Fixture* fixture) override
48  {
49  if (m_count == e_maxCount)
50  {
51  return false;
52  }
53 
54  b2Body* body = fixture->GetBody();
55  b2Shape* shape = fixture->GetShape();
56 
57  bool overlap = b2TestOverlap(shape, 0, &m_circle, 0, body->GetTransform(), m_transform);
58 
59  if (overlap)
60  {
61  b2Color color(0.95f, 0.95f, 0.6f);
62  b2Vec2 center = body->GetWorldCenter();
63  g_debugDraw->DrawPoint(center, 5.0f, color);
64  ++m_count;
65  }
66 
67  return true;
68  }
69 
74 };
75 
76 class PolygonShapes : public Test
77 {
78 public:
79 
80  enum
81  {
83  };
84 
86  {
87  // Ground body
88  {
89  b2BodyDef bd;
90  b2Body* ground = m_world->CreateBody(&bd);
91 
92  b2EdgeShape shape;
93  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
94  ground->CreateFixture(&shape, 0.0f);
95  }
96 
97  {
98  b2Vec2 vertices[3];
99  vertices[0].Set(-0.5f, 0.0f);
100  vertices[1].Set(0.5f, 0.0f);
101  vertices[2].Set(0.0f, 1.5f);
102  m_polygons[0].Set(vertices, 3);
103  }
104 
105  {
106  b2Vec2 vertices[3];
107  vertices[0].Set(-0.1f, 0.0f);
108  vertices[1].Set(0.1f, 0.0f);
109  vertices[2].Set(0.0f, 1.5f);
110  m_polygons[1].Set(vertices, 3);
111  }
112 
113  {
114  float w = 1.0f;
115  float b = w / (2.0f + b2Sqrt(2.0f));
116  float s = b2Sqrt(2.0f) * b;
117 
118  b2Vec2 vertices[8];
119  vertices[0].Set(0.5f * s, 0.0f);
120  vertices[1].Set(0.5f * w, b);
121  vertices[2].Set(0.5f * w, b + s);
122  vertices[3].Set(0.5f * s, w);
123  vertices[4].Set(-0.5f * s, w);
124  vertices[5].Set(-0.5f * w, b + s);
125  vertices[6].Set(-0.5f * w, b);
126  vertices[7].Set(-0.5f * s, 0.0f);
127 
128  m_polygons[2].Set(vertices, 8);
129  }
130 
131  {
132  m_polygons[3].SetAsBox(0.5f, 0.5f);
133  }
134 
135  {
136  m_circle.m_radius = 0.5f;
137  }
138 
139  m_bodyIndex = 0;
140  memset(m_bodies, 0, sizeof(m_bodies));
141  }
142 
143  void Create(int32 index)
144  {
145  if (m_bodies[m_bodyIndex] != NULL)
146  {
149  }
150 
151  b2BodyDef bd;
152  bd.type = b2_dynamicBody;
153 
154  float x = RandomFloat(-2.0f, 2.0f);
155  bd.position.Set(x, 10.0f);
156  bd.angle = RandomFloat(-b2_pi, b2_pi);
157 
158  if (index == 4)
159  {
160  bd.angularDamping = 0.02f;
161  }
162 
164 
165  if (index < 4)
166  {
167  b2FixtureDef fd;
168  fd.shape = m_polygons + index;
169  fd.density = 1.0f;
170  fd.friction = 0.3f;
172  }
173  else
174  {
175  b2FixtureDef fd;
176  fd.shape = &m_circle;
177  fd.density = 1.0f;
178  fd.friction = 0.3f;
179 
181  }
182 
184  }
185 
186  void DestroyBody()
187  {
188  for (int32 i = 0; i < e_maxBodies; ++i)
189  {
190  if (m_bodies[i] != NULL)
191  {
193  m_bodies[i] = NULL;
194  return;
195  }
196  }
197  }
198 
199  void Keyboard(int key) override
200  {
201  switch (key)
202  {
203  case GLFW_KEY_1:
204  case GLFW_KEY_2:
205  case GLFW_KEY_3:
206  case GLFW_KEY_4:
207  case GLFW_KEY_5:
208  Create(key - GLFW_KEY_1);
209  break;
210 
211  case GLFW_KEY_A:
212  for (int32 i = 0; i < e_maxBodies; i += 2)
213  {
214  if (m_bodies[i])
215  {
216  bool enabled = m_bodies[i]->IsEnabled();
217  m_bodies[i]->SetEnabled(!enabled);
218  }
219  }
220  break;
221 
222  case GLFW_KEY_D:
223  DestroyBody();
224  break;
225  }
226  }
227 
228  void Step(Settings& settings) override
229  {
230  Test::Step(settings);
231 
232  PolygonShapesCallback callback;
233  callback.m_circle.m_radius = 2.0f;
234  callback.m_circle.m_p.Set(0.0f, 1.1f);
235  callback.m_transform.SetIdentity();
236  callback.g_debugDraw = &g_debugDraw;
237 
238  b2AABB aabb;
239  callback.m_circle.ComputeAABB(&aabb, callback.m_transform, 0);
240 
241  m_world->QueryAABB(&callback, aabb);
242 
243  b2Color color(0.4f, 0.7f, 0.8f);
244  g_debugDraw.DrawCircle(callback.m_circle.m_p, callback.m_circle.m_radius, color);
245 
246  g_debugDraw.DrawString(5, m_textLine, "Press 1-5 to drop stuff, maximum of %d overlaps detected", PolygonShapesCallback::e_maxCount);
248  g_debugDraw.DrawString(5, m_textLine, "Press 'a' to enable/disable some bodies");
250  g_debugDraw.DrawString(5, m_textLine, "Press 'd' to destroy a body");
252  }
253 
254  static Test* Create()
255  {
256  return new PolygonShapes;
257  }
258 
263 };
264 
265 static int testIndex = RegisterTest("Geometry", "Polygon Shapes", PolygonShapes::Create);
Test::m_textIncrement
int32 m_textIncrement
Definition: test.h:135
b2EdgeShape
Definition: b2_edge_shape.h:32
PolygonShapes::m_circle
b2CircleShape m_circle
Definition: polygon_shapes.cpp:262
GLFW_KEY_2
#define GLFW_KEY_2
Definition: glfw3.h:368
g_debugDraw
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2World::QueryAABB
void QueryAABB(b2QueryCallback *callback, const b2AABB &aabb) const
Definition: b2_world.cpp:994
NULL
#define NULL
b2Body::SetEnabled
void SetEnabled(bool flag)
Definition: b2_body.cpp:466
GLFW_KEY_D
#define GLFW_KEY_D
Definition: glfw3.h:381
PolygonShapesCallback::PolygonShapesCallback
PolygonShapesCallback()
Definition: polygon_shapes.cpp:40
PolygonShapesCallback::ReportFixture
bool ReportFixture(b2Fixture *fixture) override
Definition: polygon_shapes.cpp:47
PolygonShapes::m_polygons
b2PolygonShape m_polygons[4]
Definition: polygon_shapes.cpp:261
b2BodyDef::angularDamping
float angularDamping
Definition: b2_body.h:99
s
XmlRpcServer s
PolygonShapes::e_maxBodies
@ e_maxBodies
Definition: polygon_shapes.cpp:82
GLFW_KEY_4
#define GLFW_KEY_4
Definition: glfw3.h:370
testIndex
static int testIndex
Definition: polygon_shapes.cpp:265
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
PolygonShapes::Create
void Create(int32 index)
Definition: polygon_shapes.cpp:143
PolygonShapesCallback::m_circle
b2CircleShape m_circle
Definition: polygon_shapes.cpp:70
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2Body::IsEnabled
bool IsEnabled() const
Get the active state of the body.
Definition: b2_body.h:666
GLFW_KEY_A
#define GLFW_KEY_A
Definition: glfw3.h:378
PolygonShapes::Keyboard
void Keyboard(int key) override
Definition: polygon_shapes.cpp:199
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
b2PolygonShape::Set
void Set(const b2Vec2 *points, int32 count)
Definition: b2_polygon_shape.cpp:118
f
f
b2Shape
Definition: b2_shape.h:48
GLFW_KEY_1
#define GLFW_KEY_1
Definition: glfw3.h:367
PolygonShapes::PolygonShapes
PolygonShapes()
Definition: polygon_shapes.cpp:85
b2BodyDef::type
b2BodyType type
Definition: b2_body.h:74
b2Draw::DrawPoint
virtual void DrawPoint(const b2Vec2 &p, float size, const b2Color &color)=0
Draw a point.
b2Draw
Definition: b2_draw.h:48
PolygonShapes::m_bodies
b2Body * m_bodies[e_maxBodies]
Definition: polygon_shapes.cpp:260
b2_pi
#define b2_pi
Definition: b2_common.h:41
b2FixtureDef::friction
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82
b2Transform
Definition: b2_math.h:338
b2_dynamicBody
@ b2_dynamicBody
Definition: b2_body.h:47
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
PolygonShapes::m_bodyIndex
int32 m_bodyIndex
Definition: polygon_shapes.cpp:259
PolygonShapes::Create
static Test * Create()
Definition: polygon_shapes.cpp:254
b2Body::GetWorldCenter
const b2Vec2 & GetWorldCenter() const
Get the world position of the center of mass.
Definition: b2_body.h:494
b2Shape::m_radius
float m_radius
Definition: b2_shape.h:102
b2BodyDef::angle
float angle
The world angle of the body in radians.
Definition: b2_body.h:81
b2Fixture
Definition: b2_fixture.h:116
b2Color
Color for debug drawing. Each value has the range [0,1].
Definition: b2_draw.h:30
RandomFloat
float RandomFloat()
Random number in range [-1,1].
Definition: test.h:37
b2TestOverlap
B2_API bool b2TestOverlap(const b2Shape *shapeA, int32 indexA, const b2Shape *shapeB, int32 indexB, const b2Transform &xfA, const b2Transform &xfB)
Determine if two generic shapes overlap.
Definition: b2_collision.cpp:239
PolygonShapesCallback::e_maxCount
@ e_maxCount
Definition: polygon_shapes.cpp:37
b2Sqrt
#define b2Sqrt(x)
Definition: b2_math.h:37
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
b2CircleShape::ComputeAABB
void ComputeAABB(b2AABB *aabb, const b2Transform &transform, int32 childIndex) const override
Definition: b2_circle_shape.cpp:89
b2BodyDef
Definition: b2_body.h:52
PolygonShapesCallback::g_debugDraw
b2Draw * g_debugDraw
Definition: polygon_shapes.cpp:72
Settings
Definition: settings.h:25
b2FixtureDef::shape
const b2Shape * shape
Definition: b2_fixture.h:76
PolygonShapesCallback
Definition: polygon_shapes.cpp:31
b2Transform::SetIdentity
void SetIdentity()
Set this to the identity transform.
Definition: b2_math.h:347
PolygonShapes::Step
void Step(Settings &settings) override
Definition: polygon_shapes.cpp:228
b2AABB
An axis aligned bounding box.
Definition: b2_collision.h:168
GLFW_KEY_3
#define GLFW_KEY_3
Definition: glfw3.h:369
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
int32
signed int int32
Definition: b2_types.h:28
DebugDraw::DrawCircle
void DrawCircle(const b2Vec2 &center, float radius, const b2Color &color) override
Draw a circle.
Definition: draw.cpp:674
Test
Definition: test.h:80
b2BodyDef::position
b2Vec2 position
Definition: b2_body.h:78
PolygonShapes
Definition: polygon_shapes.cpp:76
PolygonShapes::DestroyBody
void DestroyBody()
Definition: polygon_shapes.cpp:186
GLFW_KEY_5
#define GLFW_KEY_5
Definition: glfw3.h:371
PolygonShapesCallback::m_count
int32 m_count
Definition: polygon_shapes.cpp:73
Test::Step
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2Fixture::GetBody
b2Body * GetBody()
Definition: b2_fixture.h:283
b2QueryCallback
Definition: b2_world_callbacks.h:128
b2Body::GetTransform
const b2Transform & GetTransform() const
Definition: b2_body.h:479
b2CircleShape::m_p
b2Vec2 m_p
Position.
Definition: b2_circle_shape.h:57
Test::m_world
b2World * m_world
Definition: test.h:128
b2Fixture::GetShape
b2Shape * GetShape()
Definition: b2_fixture.h:258
PolygonShapesCallback::m_transform
b2Transform m_transform
Definition: polygon_shapes.cpp:71
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165


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