~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/box2d/Testbed/Tests/CharacterCollision.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* Copyright (c) 2006-2010 Erin Catto http://www.box2d.org
 
3
*
 
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.
 
17
*/
 
18
 
 
19
#ifndef CHARACTER_COLLISION_H
 
20
#define CHARACTER_COLLISION_H
 
21
 
 
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
 
26
{
 
27
public:
 
28
        CharacterCollision()
 
29
        {
 
30
                // Ground body
 
31
                {
 
32
                        b2BodyDef bd;
 
33
                        b2Body* ground = m_world->CreateBody(&bd);
 
34
 
 
35
                        b2EdgeShape shape;
 
36
                        shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
 
37
                        ground->CreateFixture(&shape, 0.0f);
 
38
                }
 
39
 
 
40
                // Collinear edges with no adjacency information.
 
41
                // This shows the problematic case where a box shape can hit
 
42
                // an internal vertex.
 
43
                {
 
44
                        b2BodyDef bd;
 
45
                        b2Body* ground = m_world->CreateBody(&bd);
 
46
 
 
47
                        b2EdgeShape shape;
 
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);
 
54
                }
 
55
 
 
56
                // Chain shape
 
57
                {
 
58
                        b2BodyDef bd;
 
59
                        bd.angle = 0.25f * b2_pi;
 
60
                        b2Body* ground = m_world->CreateBody(&bd);
 
61
 
 
62
                        b2Vec2 vs[4];
 
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);
 
67
                        b2ChainShape shape;
 
68
                        shape.CreateChain(vs, 4);
 
69
                        ground->CreateFixture(&shape, 0.0f);
 
70
                }
 
71
 
 
72
                // Square tiles. This shows that adjacency shapes may
 
73
                // have non-smooth collision. There is no solution
 
74
                // to this problem.
 
75
                {
 
76
                        b2BodyDef bd;
 
77
                        b2Body* ground = m_world->CreateBody(&bd);
 
78
 
 
79
                        b2PolygonShape shape;
 
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);
 
86
                }
 
87
 
 
88
                // Square made from an edge loop. Collision should be smooth.
 
89
                {
 
90
                        b2BodyDef bd;
 
91
                        b2Body* ground = m_world->CreateBody(&bd);
 
92
 
 
93
                        b2Vec2 vs[4];
 
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);
 
98
                        b2ChainShape shape;
 
99
                        shape.CreateLoop(vs, 4);
 
100
                        ground->CreateFixture(&shape, 0.0f);
 
101
                }
 
102
 
 
103
                // Edge loop. Collision should be smooth.
 
104
                {
 
105
                        b2BodyDef bd;
 
106
                        bd.position.Set(-10.0f, 4.0f);
 
107
                        b2Body* ground = m_world->CreateBody(&bd);
 
108
 
 
109
                        b2Vec2 vs[10];
 
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);
 
120
                        b2ChainShape shape;
 
121
                        shape.CreateLoop(vs, 10);
 
122
                        ground->CreateFixture(&shape, 0.0f);
 
123
                }
 
124
 
 
125
                // Square character 1
 
126
                {
 
127
                        b2BodyDef bd;
 
128
                        bd.position.Set(-3.0f, 8.0f);
 
129
                        bd.type = b2_dynamicBody;
 
130
                        bd.fixedRotation = true;
 
131
                        bd.allowSleep = false;
 
132
 
 
133
                        b2Body* body = m_world->CreateBody(&bd);
 
134
 
 
135
                        b2PolygonShape shape;
 
136
                        shape.SetAsBox(0.5f, 0.5f);
 
137
 
 
138
                        b2FixtureDef fd;
 
139
                        fd.shape = &shape;
 
140
                        fd.density = 20.0f;
 
141
                        body->CreateFixture(&fd);
 
142
                }
 
143
 
 
144
                // Square character 2
 
145
                {
 
146
                        b2BodyDef bd;
 
147
                        bd.position.Set(-5.0f, 5.0f);
 
148
                        bd.type = b2_dynamicBody;
 
149
                        bd.fixedRotation = true;
 
150
                        bd.allowSleep = false;
 
151
 
 
152
                        b2Body* body = m_world->CreateBody(&bd);
 
153
 
 
154
                        b2PolygonShape shape;
 
155
                        shape.SetAsBox(0.25f, 0.25f);
 
156
 
 
157
                        b2FixtureDef fd;
 
158
                        fd.shape = &shape;
 
159
                        fd.density = 20.0f;
 
160
                        body->CreateFixture(&fd);
 
161
                }
 
162
 
 
163
                // Hexagon character
 
164
                {
 
165
                        b2BodyDef bd;
 
166
                        bd.position.Set(-5.0f, 8.0f);
 
167
                        bd.type = b2_dynamicBody;
 
168
                        bd.fixedRotation = true;
 
169
                        bd.allowSleep = false;
 
170
 
 
171
                        b2Body* body = m_world->CreateBody(&bd);
 
172
 
 
173
                        float32 angle = 0.0f;
 
174
                        float32 delta = b2_pi / 3.0f;
 
175
                        b2Vec2 vertices[6];
 
176
                        for (int32 i = 0; i < 6; ++i)
 
177
                        {
 
178
                                vertices[i].Set(0.5f * cosf(angle), 0.5f * sinf(angle));
 
179
                                angle += delta;
 
180
                        }
 
181
 
 
182
                        b2PolygonShape shape;
 
183
                        shape.Set(vertices, 6);
 
184
 
 
185
                        b2FixtureDef fd;
 
186
                        fd.shape = &shape;
 
187
                        fd.density = 20.0f;
 
188
                        body->CreateFixture(&fd);
 
189
                }
 
190
 
 
191
                // Circle character
 
192
                {
 
193
                        b2BodyDef bd;
 
194
                        bd.position.Set(3.0f, 5.0f);
 
195
                        bd.type = b2_dynamicBody;
 
196
                        bd.fixedRotation = true;
 
197
                        bd.allowSleep = false;
 
198
 
 
199
                        b2Body* body = m_world->CreateBody(&bd);
 
200
 
 
201
                        b2CircleShape shape;
 
202
                        shape.m_radius = 0.5f;
 
203
 
 
204
                        b2FixtureDef fd;
 
205
                        fd.shape = &shape;
 
206
                        fd.density = 20.0f;
 
207
                        body->CreateFixture(&fd);
 
208
                }
 
209
 
 
210
                // Circle character
 
211
                {
 
212
                        b2BodyDef bd;
 
213
                        bd.position.Set(-7.0f, 6.0f);
 
214
                        bd.type = b2_dynamicBody;
 
215
                        bd.allowSleep = false;
 
216
 
 
217
                        m_character = m_world->CreateBody(&bd);
 
218
 
 
219
                        b2CircleShape shape;
 
220
                        shape.m_radius = 0.25f;
 
221
 
 
222
                        b2FixtureDef fd;
 
223
                        fd.shape = &shape;
 
224
                        fd.density = 20.0f;
 
225
                        fd.friction = 1.0f;
 
226
                        m_character->CreateFixture(&fd);
 
227
                }
 
228
        }
 
229
 
 
230
        void Step(Settings* settings)
 
231
        {
 
232
                b2Vec2 v = m_character->GetLinearVelocity();
 
233
                v.x = -5.0f;
 
234
                m_character->SetLinearVelocity(v);
 
235
 
 
236
                Test::Step(settings);
 
237
                m_debugDraw.DrawString(5, m_textLine, "This tests various character collision shapes.");
 
238
                m_textLine += 15;
 
239
                m_debugDraw.DrawString(5, m_textLine, "Limitation: square and hexagon can snag on aligned boxes.");
 
240
                m_textLine += 15;
 
241
                m_debugDraw.DrawString(5, m_textLine, "Feature: edge chains have smooth collision inside and out.");
 
242
                m_textLine += 15;
 
243
        }
 
244
 
 
245
        static Test* Create()
 
246
        {
 
247
                return new CharacterCollision;
 
248
        }
 
249
 
 
250
        b2Body* m_character;
 
251
};
 
252
 
 
253
#endif