character_collision.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 
28 class CharacterCollision : public Test
29 {
30 public:
32  {
33  // Ground body
34  {
35  b2BodyDef bd;
36  b2Body* ground = m_world->CreateBody(&bd);
37 
38  b2EdgeShape shape;
39  shape.SetTwoSided(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
40  ground->CreateFixture(&shape, 0.0f);
41  }
42 
43  // Collinear edges with no adjacency information.
44  // This shows the problematic case where a box shape can hit
45  // an internal vertex.
46  {
47  b2BodyDef bd;
48  b2Body* ground = m_world->CreateBody(&bd);
49 
50  b2EdgeShape shape;
51  shape.SetTwoSided(b2Vec2(-8.0f, 1.0f), b2Vec2(-6.0f, 1.0f));
52  ground->CreateFixture(&shape, 0.0f);
53  shape.SetTwoSided(b2Vec2(-6.0f, 1.0f), b2Vec2(-4.0f, 1.0f));
54  ground->CreateFixture(&shape, 0.0f);
55  shape.SetTwoSided(b2Vec2(-4.0f, 1.0f), b2Vec2(-2.0f, 1.0f));
56  ground->CreateFixture(&shape, 0.0f);
57  }
58 
59  // Chain shape
60  {
61  b2BodyDef bd;
62  bd.angle = 0.25f * b2_pi;
63  b2Body* ground = m_world->CreateBody(&bd);
64 
65  b2Vec2 vs[4];
66  vs[0].Set(5.0f, 7.0f);
67  vs[1].Set(6.0f, 8.0f);
68  vs[2].Set(7.0f, 8.0f);
69  vs[3].Set(8.0f, 7.0f);
70  b2ChainShape shape;
71  shape.CreateLoop(vs, 4);
72  ground->CreateFixture(&shape, 0.0f);
73  }
74 
75  // Square tiles. This shows that adjacency shapes may
76  // have non-smooth collision. There is no solution
77  // to this problem.
78  {
79  b2BodyDef bd;
80  b2Body* ground = m_world->CreateBody(&bd);
81 
82  b2PolygonShape shape;
83  shape.SetAsBox(1.0f, 1.0f, b2Vec2(4.0f, 3.0f), 0.0f);
84  ground->CreateFixture(&shape, 0.0f);
85  shape.SetAsBox(1.0f, 1.0f, b2Vec2(6.0f, 3.0f), 0.0f);
86  ground->CreateFixture(&shape, 0.0f);
87  shape.SetAsBox(1.0f, 1.0f, b2Vec2(8.0f, 3.0f), 0.0f);
88  ground->CreateFixture(&shape, 0.0f);
89  }
90 
91  // Square made from an edge loop. Collision should be smooth.
92  {
93  b2BodyDef bd;
94  b2Body* ground = m_world->CreateBody(&bd);
95 
96  b2Vec2 vs[4];
97  vs[0].Set(-1.0f, 3.0f);
98  vs[1].Set(1.0f, 3.0f);
99  vs[2].Set(1.0f, 5.0f);
100  vs[3].Set(-1.0f, 5.0f);
101  b2ChainShape shape;
102  shape.CreateLoop(vs, 4);
103  ground->CreateFixture(&shape, 0.0f);
104  }
105 
106  // Edge loop. Collision should be smooth.
107  {
108  b2BodyDef bd;
109  bd.position.Set(-10.0f, 4.0f);
110  b2Body* ground = m_world->CreateBody(&bd);
111 
112  b2Vec2 vs[10];
113  vs[0].Set(0.0f, 0.0f);
114  vs[1].Set(6.0f, 0.0f);
115  vs[2].Set(6.0f, 2.0f);
116  vs[3].Set(4.0f, 1.0f);
117  vs[4].Set(2.0f, 2.0f);
118  vs[5].Set(0.0f, 2.0f);
119  vs[6].Set(-2.0f, 2.0f);
120  vs[7].Set(-4.0f, 3.0f);
121  vs[8].Set(-6.0f, 2.0f);
122  vs[9].Set(-6.0f, 0.0f);
123  b2ChainShape shape;
124  shape.CreateLoop(vs, 10);
125  ground->CreateFixture(&shape, 0.0f);
126  }
127 
128  // Square character 1
129  {
130  b2BodyDef bd;
131  bd.position.Set(-3.0f, 8.0f);
132  bd.type = b2_dynamicBody;
133  bd.fixedRotation = true;
134  bd.allowSleep = false;
135 
136  b2Body* body = m_world->CreateBody(&bd);
137 
138  b2PolygonShape shape;
139  shape.SetAsBox(0.5f, 0.5f);
140 
141  b2FixtureDef fd;
142  fd.shape = &shape;
143  fd.density = 20.0f;
144  body->CreateFixture(&fd);
145  }
146 
147  // Square character 2
148  {
149  b2BodyDef bd;
150  bd.position.Set(-5.0f, 5.0f);
151  bd.type = b2_dynamicBody;
152  bd.fixedRotation = true;
153  bd.allowSleep = false;
154 
155  b2Body* body = m_world->CreateBody(&bd);
156 
157  b2PolygonShape shape;
158  shape.SetAsBox(0.25f, 0.25f);
159 
160  b2FixtureDef fd;
161  fd.shape = &shape;
162  fd.density = 20.0f;
163  body->CreateFixture(&fd);
164  }
165 
166  // Hexagon character
167  {
168  b2BodyDef bd;
169  bd.position.Set(-5.0f, 8.0f);
170  bd.type = b2_dynamicBody;
171  bd.fixedRotation = true;
172  bd.allowSleep = false;
173 
174  b2Body* body = m_world->CreateBody(&bd);
175 
176  float angle = 0.0f;
177  float delta = b2_pi / 3.0f;
178  b2Vec2 vertices[6];
179  for (int32 i = 0; i < 6; ++i)
180  {
181  vertices[i].Set(0.5f * cosf(angle), 0.5f * sinf(angle));
182  angle += delta;
183  }
184 
185  b2PolygonShape shape;
186  shape.Set(vertices, 6);
187 
188  b2FixtureDef fd;
189  fd.shape = &shape;
190  fd.density = 20.0f;
191  body->CreateFixture(&fd);
192  }
193 
194  // Circle character
195  {
196  b2BodyDef bd;
197  bd.position.Set(3.0f, 5.0f);
198  bd.type = b2_dynamicBody;
199  bd.fixedRotation = true;
200  bd.allowSleep = false;
201 
202  b2Body* body = m_world->CreateBody(&bd);
203 
204  b2CircleShape shape;
205  shape.m_radius = 0.5f;
206 
207  b2FixtureDef fd;
208  fd.shape = &shape;
209  fd.density = 20.0f;
210  body->CreateFixture(&fd);
211  }
212 
213  // Circle character
214  {
215  b2BodyDef bd;
216  bd.position.Set(-7.0f, 6.0f);
217  bd.type = b2_dynamicBody;
218  bd.allowSleep = false;
219 
221 
222  b2CircleShape shape;
223  shape.m_radius = 0.25f;
224 
225  b2FixtureDef fd;
226  fd.shape = &shape;
227  fd.density = 20.0f;
228  fd.friction = 1.0f;
230  }
231  }
232 
233  void Step(Settings& settings) override
234  {
236  v.x = -5.0f;
238 
239  Test::Step(settings);
240  g_debugDraw.DrawString(5, m_textLine, "This tests various character collision shapes.");
242  g_debugDraw.DrawString(5, m_textLine, "Limitation: square and hexagon can snag on aligned boxes.");
244  g_debugDraw.DrawString(5, m_textLine, "Feature: edge chains have smooth collision inside and out.");
246  }
247 
248  static Test* Create()
249  {
250  return new CharacterCollision;
251  }
252 
254 };
255 
256 static int testIndex = RegisterTest("Examples", "Character Collision", CharacterCollision::Create);
const b2Shape * shape
Definition: b2_fixture.h:76
bool fixedRotation
Should this body be prevented from rotating? Useful for characters.
Definition: b2_body.h:109
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
TF2SIMD_FORCE_INLINE tf2Scalar angle(const Quaternion &q1, const Quaternion &q2)
f
float x
Definition: b2_math.h:128
int32 m_textLine
Definition: test.h:127
Definition: test.h:80
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
const b2Vec2 & GetLinearVelocity() const
Definition: b2_body.h:519
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
void CreateLoop(const b2Vec2 *vertices, int32 count)
int32 m_textIncrement
Definition: test.h:135
b2World * m_world
Definition: test.h:128
float angle
The world angle of the body in radians.
Definition: b2_body.h:81
#define b2_pi
Definition: b2_common.h:41
b2Vec2 position
Definition: b2_body.h:78
void Set(const b2Vec2 *points, int32 count)
void Step(Settings &settings) override
static int testIndex
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
bool allowSleep
Definition: b2_body.h:103
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
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