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  {
147  m_world->DestroyBody(m_bodies[m_bodyIndex]);
148  m_bodies[m_bodyIndex] = NULL;
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 
163  m_bodies[m_bodyIndex] = m_world->CreateBody(&bd);
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;
171  m_bodies[m_bodyIndex]->CreateFixture(&fd);
172  }
173  else
174  {
175  b2FixtureDef fd;
176  fd.shape = &m_circle;
177  fd.density = 1.0f;
178  fd.friction = 0.3f;
179 
180  m_bodies[m_bodyIndex]->CreateFixture(&fd);
181  }
182 
183  m_bodyIndex = (m_bodyIndex + 1) % e_maxBodies;
184  }
185 
186  void DestroyBody()
187  {
188  for (int32 i = 0; i < e_maxBodies; ++i)
189  {
190  if (m_bodies[i] != NULL)
191  {
192  m_world->DestroyBody(m_bodies[i]);
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);
247  m_textLine += m_textIncrement;
248  g_debugDraw.DrawString(5, m_textLine, "Press 'a' to enable/disable some bodies");
249  m_textLine += m_textIncrement;
250  g_debugDraw.DrawString(5, m_textLine, "Press 'd' to destroy a body");
251  m_textLine += m_textIncrement;
252  }
253 
254  static Test* Create()
255  {
256  return new PolygonShapes;
257  }
258 
260  b2Body* m_bodies[e_maxBodies];
261  b2PolygonShape m_polygons[4];
263 };
264 
265 static int testIndex = RegisterTest("Geometry", "Polygon Shapes", PolygonShapes::Create);
const b2Transform & GetTransform() const
Definition: b2_body.h:479
const b2Shape * shape
Definition: b2_fixture.h:76
#define b2Sqrt(x)
Definition: b2_math.h:37
virtual void DrawCircle(const b2Vec2 &center, float radius, const b2Color &color)=0
Draw a circle.
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
bool ReportFixture(b2Fixture *fixture) override
f
void Step(Settings &settings) override
void SetIdentity()
Set this to the identity transform.
Definition: b2_math.h:347
XmlRpcServer s
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.
virtual void DrawPoint(const b2Vec2 &p, float size, const b2Color &color)=0
Draw a point.
Definition: test.h:80
float angularDamping
Definition: b2_body.h:99
float RandomFloat()
Random number in range [-1,1].
Definition: test.h:37
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
#define GLFW_KEY_A
Definition: glfw3.h:378
signed int int32
Definition: b2_types.h:28
b2Vec2 m_p
Position.
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
Color for debug drawing. Each value has the range [0,1].
Definition: b2_draw.h:30
#define GLFW_KEY_1
Definition: glfw3.h:367
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
Definition: b2_draw.h:48
float m_radius
Definition: b2_shape.h:102
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
#define GLFW_KEY_4
Definition: glfw3.h:370
#define GLFW_KEY_2
Definition: glfw3.h:368
#define GLFW_KEY_3
Definition: glfw3.h:369
#define GLFW_KEY_D
Definition: glfw3.h:381
float angle
The world angle of the body in radians.
Definition: b2_body.h:81
void ComputeAABB(b2AABB *aabb, const b2Transform &transform, int32 childIndex) const override
#define b2_pi
Definition: b2_common.h:41
b2Vec2 position
Definition: b2_body.h:78
An axis aligned bounding box.
Definition: b2_collision.h:168
b2CircleShape m_circle
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
static Test * Create()
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2Shape * GetShape()
Definition: b2_fixture.h:258
const b2Vec2 & GetWorldCenter() const
Get the world position of the center of mass.
Definition: b2_body.h:494
b2Body * GetBody()
Definition: b2_fixture.h:283
void Create(int32 index)
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82
#define GLFW_KEY_5
Definition: glfw3.h:371
void Keyboard(int key) override
static int testIndex


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