2
* Copyright (c) 2006-2010 Erin Catto http://www.box2d.org
4
* This software is provided 'as-is', without any express or implied
5
* warranty. In no event will the authors be held liable for any damages
6
* arising from the use of this software.
7
* Permission is granted to anyone to use this software for any purpose,
8
* including commercial applications, and to alter it and redistribute it
9
* freely, subject to the following restrictions:
10
* 1. The origin of this software must not be misrepresented; you must not
11
* claim that you wrote the original software. If you use this software
12
* in a product, an acknowledgment in the product documentation would be
13
* appreciated but is not required.
14
* 2. Altered source versions must be plainly marked as such, and must not be
15
* misrepresented as being the original software.
16
* 3. This notice may not be removed or altered from any source distribution.
19
#ifndef CHARACTER_COLLISION_H
20
#define CHARACTER_COLLISION_H
22
/// This is a test of typical character collision scenarios. This does not
23
/// show how you should implement a character in your application.
24
/// Instead this is used to test smooth collision on edge chains.
25
class CharacterCollision : public Test
33
b2Body* ground = m_world->CreateBody(&bd);
36
shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
37
ground->CreateFixture(&shape, 0.0f);
40
// Collinear edges with no adjacency information.
41
// This shows the problematic case where a box shape can hit
42
// an internal vertex.
45
b2Body* ground = m_world->CreateBody(&bd);
48
shape.Set(b2Vec2(-8.0f, 1.0f), b2Vec2(-6.0f, 1.0f));
49
ground->CreateFixture(&shape, 0.0f);
50
shape.Set(b2Vec2(-6.0f, 1.0f), b2Vec2(-4.0f, 1.0f));
51
ground->CreateFixture(&shape, 0.0f);
52
shape.Set(b2Vec2(-4.0f, 1.0f), b2Vec2(-2.0f, 1.0f));
53
ground->CreateFixture(&shape, 0.0f);
59
bd.angle = 0.25f * b2_pi;
60
b2Body* ground = m_world->CreateBody(&bd);
63
vs[0].Set(5.0f, 7.0f);
64
vs[1].Set(6.0f, 8.0f);
65
vs[2].Set(7.0f, 8.0f);
66
vs[3].Set(8.0f, 7.0f);
68
shape.CreateChain(vs, 4);
69
ground->CreateFixture(&shape, 0.0f);
72
// Square tiles. This shows that adjacency shapes may
73
// have non-smooth collision. There is no solution
77
b2Body* ground = m_world->CreateBody(&bd);
80
shape.SetAsBox(1.0f, 1.0f, b2Vec2(4.0f, 3.0f), 0.0f);
81
ground->CreateFixture(&shape, 0.0f);
82
shape.SetAsBox(1.0f, 1.0f, b2Vec2(6.0f, 3.0f), 0.0f);
83
ground->CreateFixture(&shape, 0.0f);
84
shape.SetAsBox(1.0f, 1.0f, b2Vec2(8.0f, 3.0f), 0.0f);
85
ground->CreateFixture(&shape, 0.0f);
88
// Square made from an edge loop. Collision should be smooth.
91
b2Body* ground = m_world->CreateBody(&bd);
94
vs[0].Set(-1.0f, 3.0f);
95
vs[1].Set(1.0f, 3.0f);
96
vs[2].Set(1.0f, 5.0f);
97
vs[3].Set(-1.0f, 5.0f);
99
shape.CreateLoop(vs, 4);
100
ground->CreateFixture(&shape, 0.0f);
103
// Edge loop. Collision should be smooth.
106
bd.position.Set(-10.0f, 4.0f);
107
b2Body* ground = m_world->CreateBody(&bd);
110
vs[0].Set(0.0f, 0.0f);
111
vs[1].Set(6.0f, 0.0f);
112
vs[2].Set(6.0f, 2.0f);
113
vs[3].Set(4.0f, 1.0f);
114
vs[4].Set(2.0f, 2.0f);
115
vs[5].Set(0.0f, 2.0f);
116
vs[6].Set(-2.0f, 2.0f);
117
vs[7].Set(-4.0f, 3.0f);
118
vs[8].Set(-6.0f, 2.0f);
119
vs[9].Set(-6.0f, 0.0f);
121
shape.CreateLoop(vs, 10);
122
ground->CreateFixture(&shape, 0.0f);
125
// Square character 1
128
bd.position.Set(-3.0f, 8.0f);
129
bd.type = b2_dynamicBody;
130
bd.fixedRotation = true;
131
bd.allowSleep = false;
133
b2Body* body = m_world->CreateBody(&bd);
135
b2PolygonShape shape;
136
shape.SetAsBox(0.5f, 0.5f);
141
body->CreateFixture(&fd);
144
// Square character 2
147
bd.position.Set(-5.0f, 5.0f);
148
bd.type = b2_dynamicBody;
149
bd.fixedRotation = true;
150
bd.allowSleep = false;
152
b2Body* body = m_world->CreateBody(&bd);
154
b2PolygonShape shape;
155
shape.SetAsBox(0.25f, 0.25f);
160
body->CreateFixture(&fd);
166
bd.position.Set(-5.0f, 8.0f);
167
bd.type = b2_dynamicBody;
168
bd.fixedRotation = true;
169
bd.allowSleep = false;
171
b2Body* body = m_world->CreateBody(&bd);
173
float32 angle = 0.0f;
174
float32 delta = b2_pi / 3.0f;
176
for (int32 i = 0; i < 6; ++i)
178
vertices[i].Set(0.5f * cosf(angle), 0.5f * sinf(angle));
182
b2PolygonShape shape;
183
shape.Set(vertices, 6);
188
body->CreateFixture(&fd);
194
bd.position.Set(3.0f, 5.0f);
195
bd.type = b2_dynamicBody;
196
bd.fixedRotation = true;
197
bd.allowSleep = false;
199
b2Body* body = m_world->CreateBody(&bd);
202
shape.m_radius = 0.5f;
207
body->CreateFixture(&fd);
213
bd.position.Set(-7.0f, 6.0f);
214
bd.type = b2_dynamicBody;
215
bd.allowSleep = false;
217
m_character = m_world->CreateBody(&bd);
220
shape.m_radius = 0.25f;
226
m_character->CreateFixture(&fd);
230
void Step(Settings* settings)
232
b2Vec2 v = m_character->GetLinearVelocity();
234
m_character->SetLinearVelocity(v);
236
Test::Step(settings);
237
m_debugDraw.DrawString(5, m_textLine, "This tests various character collision shapes.");
239
m_debugDraw.DrawString(5, m_textLine, "Limitation: square and hexagon can snag on aligned boxes.");
241
m_debugDraw.DrawString(5, m_textLine, "Feature: edge chains have smooth collision inside and out.");
245
static Test* Create()
247
return new CharacterCollision;