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

« back to all changes in this revision

Viewing changes to tests/box2d/Testbed/Tests/EdgeShapes.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 EDGE_SHAPES_H
 
20
#define EDGE_SHAPES_H
 
21
 
 
22
class EdgeShapesCallback : public b2RayCastCallback
 
23
{
 
24
public:
 
25
        EdgeShapesCallback()
 
26
        {
 
27
                m_fixture = NULL;
 
28
        }
 
29
 
 
30
        float32 ReportFixture(  b2Fixture* fixture, const b2Vec2& point,
 
31
                const b2Vec2& normal, float32 fraction)
 
32
        {
 
33
                m_fixture = fixture;
 
34
                m_point = point;
 
35
                m_normal = normal;
 
36
 
 
37
                return fraction;
 
38
        }
 
39
 
 
40
        b2Fixture* m_fixture;
 
41
        b2Vec2 m_point;
 
42
        b2Vec2 m_normal;
 
43
};
 
44
 
 
45
class EdgeShapes : public Test
 
46
{
 
47
public:
 
48
 
 
49
        enum
 
50
        {
 
51
                e_maxBodies = 256
 
52
        };
 
53
 
 
54
        EdgeShapes()
 
55
        {
 
56
                // Ground body
 
57
                {
 
58
                        b2BodyDef bd;
 
59
                        b2Body* ground = m_world->CreateBody(&bd);
 
60
 
 
61
                        float32 x1 = -20.0f;
 
62
                        float32 y1 = 2.0f * cosf(x1 / 10.0f * b2_pi);
 
63
                        for (int32 i = 0; i < 80; ++i)
 
64
                        {
 
65
                                float32 x2 = x1 + 0.5f;
 
66
                                float32 y2 = 2.0f * cosf(x2 / 10.0f * b2_pi);
 
67
 
 
68
                                b2EdgeShape shape;
 
69
                                shape.Set(b2Vec2(x1, y1), b2Vec2(x2, y2));
 
70
                                ground->CreateFixture(&shape, 0.0f);
 
71
 
 
72
                                x1 = x2;
 
73
                                y1 = y2;
 
74
                        }
 
75
                }
 
76
 
 
77
                {
 
78
                        b2Vec2 vertices[3];
 
79
                        vertices[0].Set(-0.5f, 0.0f);
 
80
                        vertices[1].Set(0.5f, 0.0f);
 
81
                        vertices[2].Set(0.0f, 1.5f);
 
82
                        m_polygons[0].Set(vertices, 3);
 
83
                }
 
84
 
 
85
                {
 
86
                        b2Vec2 vertices[3];
 
87
                        vertices[0].Set(-0.1f, 0.0f);
 
88
                        vertices[1].Set(0.1f, 0.0f);
 
89
                        vertices[2].Set(0.0f, 1.5f);
 
90
                        m_polygons[1].Set(vertices, 3);
 
91
                }
 
92
 
 
93
                {
 
94
                        float32 w = 1.0f;
 
95
                        float32 b = w / (2.0f + b2Sqrt(2.0f));
 
96
                        float32 s = b2Sqrt(2.0f) * b;
 
97
 
 
98
                        b2Vec2 vertices[8];
 
99
                        vertices[0].Set(0.5f * s, 0.0f);
 
100
                        vertices[1].Set(0.5f * w, b);
 
101
                        vertices[2].Set(0.5f * w, b + s);
 
102
                        vertices[3].Set(0.5f * s, w);
 
103
                        vertices[4].Set(-0.5f * s, w);
 
104
                        vertices[5].Set(-0.5f * w, b + s);
 
105
                        vertices[6].Set(-0.5f * w, b);
 
106
                        vertices[7].Set(-0.5f * s, 0.0f);
 
107
 
 
108
                        m_polygons[2].Set(vertices, 8);
 
109
                }
 
110
 
 
111
                {
 
112
                        m_polygons[3].SetAsBox(0.5f, 0.5f);
 
113
                }
 
114
 
 
115
                {
 
116
                        m_circle.m_radius = 0.5f;
 
117
                }
 
118
 
 
119
                m_bodyIndex = 0;
 
120
                memset(m_bodies, 0, sizeof(m_bodies));
 
121
 
 
122
                m_angle = 0.0f;
 
123
        }
 
124
 
 
125
        void Create(int32 index)
 
126
        {
 
127
                if (m_bodies[m_bodyIndex] != NULL)
 
128
                {
 
129
                        m_world->DestroyBody(m_bodies[m_bodyIndex]);
 
130
                        m_bodies[m_bodyIndex] = NULL;
 
131
                }
 
132
 
 
133
                b2BodyDef bd;
 
134
 
 
135
                float32 x = RandomFloat(-10.0f, 10.0f);
 
136
                float32 y = RandomFloat(10.0f, 20.0f);
 
137
                bd.position.Set(x, y);
 
138
                bd.angle = RandomFloat(-b2_pi, b2_pi);
 
139
                bd.type = b2_dynamicBody;
 
140
 
 
141
                if (index == 4)
 
142
                {
 
143
                        bd.angularDamping = 0.02f;
 
144
                }
 
145
 
 
146
                m_bodies[m_bodyIndex] = m_world->CreateBody(&bd);
 
147
 
 
148
                if (index < 4)
 
149
                {
 
150
                        b2FixtureDef fd;
 
151
                        fd.shape = m_polygons + index;
 
152
                        fd.friction = 0.3f;
 
153
                        fd.density = 20.0f;
 
154
                        m_bodies[m_bodyIndex]->CreateFixture(&fd);
 
155
                }
 
156
                else
 
157
                {
 
158
                        b2FixtureDef fd;
 
159
                        fd.shape = &m_circle;
 
160
                        fd.friction = 0.3f;
 
161
                        fd.density = 20.0f;
 
162
                        m_bodies[m_bodyIndex]->CreateFixture(&fd);
 
163
                }
 
164
 
 
165
                m_bodyIndex = (m_bodyIndex + 1) % e_maxBodies;
 
166
        }
 
167
 
 
168
        void DestroyBody()
 
169
        {
 
170
                for (int32 i = 0; i < e_maxBodies; ++i)
 
171
                {
 
172
                        if (m_bodies[i] != NULL)
 
173
                        {
 
174
                                m_world->DestroyBody(m_bodies[i]);
 
175
                                m_bodies[i] = NULL;
 
176
                                return;
 
177
                        }
 
178
                }
 
179
        }
 
180
 
 
181
        void Keyboard(unsigned char key)
 
182
        {
 
183
                switch (key)
 
184
                {
 
185
                case '1':
 
186
                case '2':
 
187
                case '3':
 
188
                case '4':
 
189
                case '5':
 
190
                        Create(key - '1');
 
191
                        break;
 
192
 
 
193
                case 'd':
 
194
                        DestroyBody();
 
195
                        break;
 
196
                }
 
197
        }
 
198
 
 
199
        void Step(Settings* settings)
 
200
        {
 
201
                bool advanceRay = settings->pause == 0 || settings->singleStep;
 
202
 
 
203
                Test::Step(settings);
 
204
                m_debugDraw.DrawString(5, m_textLine, "Press 1-5 to drop stuff");
 
205
                m_textLine += 15;
 
206
 
 
207
                float32 L = 25.0f;
 
208
                b2Vec2 point1(0.0f, 10.0f);
 
209
                b2Vec2 d(L * cosf(m_angle), -L * b2Abs(sinf(m_angle)));
 
210
                b2Vec2 point2 = point1 + d;
 
211
 
 
212
                EdgeShapesCallback callback;
 
213
 
 
214
                m_world->RayCast(&callback, point1, point2);
 
215
 
 
216
                if (callback.m_fixture)
 
217
                {
 
218
                        m_debugDraw.DrawPoint(callback.m_point, 5.0f, b2Color(0.4f, 0.9f, 0.4f));
 
219
 
 
220
                        m_debugDraw.DrawSegment(point1, callback.m_point, b2Color(0.8f, 0.8f, 0.8f));
 
221
 
 
222
                        b2Vec2 head = callback.m_point + 0.5f * callback.m_normal;
 
223
                        m_debugDraw.DrawSegment(callback.m_point, head, b2Color(0.9f, 0.9f, 0.4f));
 
224
                }
 
225
                else
 
226
                {
 
227
                        m_debugDraw.DrawSegment(point1, point2, b2Color(0.8f, 0.8f, 0.8f));
 
228
                }
 
229
 
 
230
                if (advanceRay)
 
231
                {
 
232
                        m_angle += 0.25f * b2_pi / 180.0f;
 
233
                }
 
234
        }
 
235
 
 
236
        static Test* Create()
 
237
        {
 
238
                return new EdgeShapes;
 
239
        }
 
240
 
 
241
        int32 m_bodyIndex;
 
242
        b2Body* m_bodies[e_maxBodies];
 
243
        b2PolygonShape m_polygons[4];
 
244
        b2CircleShape m_circle;
 
245
 
 
246
        float32 m_angle;
 
247
};
 
248
 
 
249
#endif