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.
22
class EdgeShapesCallback : public b2RayCastCallback
30
float32 ReportFixture( b2Fixture* fixture, const b2Vec2& point,
31
const b2Vec2& normal, float32 fraction)
45
class EdgeShapes : public Test
59
b2Body* ground = m_world->CreateBody(&bd);
62
float32 y1 = 2.0f * cosf(x1 / 10.0f * b2_pi);
63
for (int32 i = 0; i < 80; ++i)
65
float32 x2 = x1 + 0.5f;
66
float32 y2 = 2.0f * cosf(x2 / 10.0f * b2_pi);
69
shape.Set(b2Vec2(x1, y1), b2Vec2(x2, y2));
70
ground->CreateFixture(&shape, 0.0f);
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);
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);
95
float32 b = w / (2.0f + b2Sqrt(2.0f));
96
float32 s = b2Sqrt(2.0f) * b;
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);
108
m_polygons[2].Set(vertices, 8);
112
m_polygons[3].SetAsBox(0.5f, 0.5f);
116
m_circle.m_radius = 0.5f;
120
memset(m_bodies, 0, sizeof(m_bodies));
125
void Create(int32 index)
127
if (m_bodies[m_bodyIndex] != NULL)
129
m_world->DestroyBody(m_bodies[m_bodyIndex]);
130
m_bodies[m_bodyIndex] = NULL;
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;
143
bd.angularDamping = 0.02f;
146
m_bodies[m_bodyIndex] = m_world->CreateBody(&bd);
151
fd.shape = m_polygons + index;
154
m_bodies[m_bodyIndex]->CreateFixture(&fd);
159
fd.shape = &m_circle;
162
m_bodies[m_bodyIndex]->CreateFixture(&fd);
165
m_bodyIndex = (m_bodyIndex + 1) % e_maxBodies;
170
for (int32 i = 0; i < e_maxBodies; ++i)
172
if (m_bodies[i] != NULL)
174
m_world->DestroyBody(m_bodies[i]);
181
void Keyboard(unsigned char key)
199
void Step(Settings* settings)
201
bool advanceRay = settings->pause == 0 || settings->singleStep;
203
Test::Step(settings);
204
m_debugDraw.DrawString(5, m_textLine, "Press 1-5 to drop stuff");
208
b2Vec2 point1(0.0f, 10.0f);
209
b2Vec2 d(L * cosf(m_angle), -L * b2Abs(sinf(m_angle)));
210
b2Vec2 point2 = point1 + d;
212
EdgeShapesCallback callback;
214
m_world->RayCast(&callback, point1, point2);
216
if (callback.m_fixture)
218
m_debugDraw.DrawPoint(callback.m_point, 5.0f, b2Color(0.4f, 0.9f, 0.4f));
220
m_debugDraw.DrawSegment(point1, callback.m_point, b2Color(0.8f, 0.8f, 0.8f));
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));
227
m_debugDraw.DrawSegment(point1, point2, b2Color(0.8f, 0.8f, 0.8f));
232
m_angle += 0.25f * b2_pi / 180.0f;
236
static Test* Create()
238
return new EdgeShapes;
242
b2Body* m_bodies[e_maxBodies];
243
b2PolygonShape m_polygons[4];
244
b2CircleShape m_circle;