-
Notifications
You must be signed in to change notification settings - Fork 0
/
box2dengine.cpp
339 lines (291 loc) · 7.49 KB
/
box2dengine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <iostream>
#include <QtCore/QTimerEvent>
#include <QtCore/QPointF>
#include <Box2D/Box2D.h>
#include "box2dengine.h"
/*!
\class QtBox2DQueryCallback
\brief Box2D query callback. Emits the bodyFound() signal when
a body is detected by the query.
*/
/*!
Constructs a query callback object.
*/
QtBox2DQueryCallback::QtBox2DQueryCallback(QObject *parent)
: QObject(parent),
m_type(b2_staticBody),
m_repeat(true),
m_filter(false)
{
}
/*!
Constructs a query callback object that only reports bodies of the type
specified by \a type.
*/
QtBox2DQueryCallback::QtBox2DQueryCallback(b2BodyType type, QObject *parent)
: QObject(parent),
m_type(type),
m_repeat(true),
m_filter(true)
{
}
/*!
\reimp
*/
bool QtBox2DQueryCallback::ReportFixture(b2Fixture *fixture)
{
b2Body *body = fixture->GetBody();
if (!m_filter || (m_filter && m_type == body->GetType())) {
emit bodyFound(body);
return m_repeat;
}
return true;
}
/*!
\fn void QtBox2DQueryCallback::setTerminate(bool terminate)
This property holds whether the query terminates when a body is found.
*/
/*!
\class QtBox2DEngine
\brief Main class for Box2D simulations. Essentially a QObject-based wrapper
for the b2World class with a more Qt-friendly interface
This class also has some added signals and slots and can be used together
with QtBox2DWidget to visualize Box2D simulations.
*/
/*!
Constructs a QtBox2DEngine.
*/
QtBox2DEngine::QtBox2DEngine(QObject *parent)
: QObject(parent),
m_timer(0),
m_running(false),
m_hertz(58),
m_velocityIter(8),
m_positionIter(3),
m_step(5),
m_world(new b2World(b2Vec2(0, -10)))
{
}
/*!
Destroys the QtBox2DEngine object.
*/
QtBox2DEngine::~QtBox2DEngine()
{
stop();
delete m_world;
}
/*!
Creates a world body using \a definition.
*/
b2Body *QtBox2DEngine::createBody(const b2BodyDef *definition) const
{
return m_world->CreateBody(definition);
}
/*!
Creates a body of b2BodyType \a type at position \a x, \a y.
\overload
*/
b2Body *QtBox2DEngine::createBody(b2BodyType type, qreal x, qreal y, qreal angle, bool bullet) const
{
b2BodyDef body;
body.type = type;
body.position.Set(x, y);
body.angle = angle;
body.bullet = bullet;
return createBody(&body);
}
/*!
Remove and destroy \a body.
*/
void QtBox2DEngine::destroyBody(b2Body *body)
{
m_world->DestroyBody(body);
}
/*!
Returns a pointer to the head of the world body list. Use b2Body::GetNext
to get the next body in the list. See Box2D's documentation for more info.
\sa QtBox2DEngine::bodylist()
*/
b2Body *QtBox2DEngine::bodies() const
{
return m_world->GetBodyList();
}
/*!
Returns a list of pointers to all bodies in the world.
\sa QtBox2DEngine::bodies()
*/
QList<b2Body *> QtBox2DEngine::bodyList() const
{
QList<b2Body *> list;
b2Body *b = m_world->GetBodyList();
while (b) {
list.push_back(b);
b = b->GetNext();
}
return list;
}
/*!
\fn bool QtBox2DEngine::worldLocked() const
Returns true if the world is locked, i.e., a time step is in progress.
*/
/*!
Creates a fixture with \a shape and attaches it to \a body.
*/
b2Fixture *QtBox2DEngine::createFixture(b2Body *body, const b2Shape *shape, qreal density, qreal friction) const
{
b2FixtureDef fixture;
fixture.shape = shape;
fixture.density = density;
fixture.friction = friction;
return body->CreateFixture(&fixture);
}
/*!
Creates a sensor and attaches it to \a body. A sensor is a fixture that
detects collision but does not produce a response.
*/
b2Fixture *QtBox2DEngine::createSensor(b2Body *body, const b2Shape *shape) const
{
b2FixtureDef fixture;
fixture.shape = shape;
fixture.isSensor = true;
return body->CreateFixture(&fixture);
}
/*!
Creates a joint defined by \a definition. See Box2D's documentation for
more info about joints.
*/
b2Joint *QtBox2DEngine::createJoint(const b2JointDef *definition) const
{
return m_world->CreateJoint(definition);
}
/*!
Destroys \a joint.
*/
void QtBox2DEngine::destroyJoint(b2Joint *joint)
{
m_world->DestroyJoint(joint);
}
/*!
Performs a log(N) time complexity region query to determine all the shapes
between points \a p1 and \a p2, using \a callback to report when a body is
found.
\sa QtBox2DQueryCallback
*/
void QtBox2DEngine::query(QtBox2DQueryCallback *callback, const QPointF &p1, const QPointF &p2)
{
b2AABB aabb;
aabb.lowerBound = b2Vec2(p1.x(), p1.y());
aabb.upperBound = b2Vec2(p2.x(), p2.y());
m_world->QueryAABB(callback, aabb);
}
/*!
Set friction of all fixtures on \a body to \a friction.
*/
void QtBox2DEngine::setFriction(b2Body *body, qreal friction) const
{
assignFixtureProperty(body, &b2Fixture::SetFriction, friction);
}
/*!
Set density of all fixtures on \a body to \a density.
*/
void QtBox2DEngine::setDensity(b2Body *body, qreal density) const
{
assignFixtureProperty(body, &b2Fixture::SetDensity, density);
}
/*!
Set restitution of all fixtures on \a body to \a restitution.
*/
void QtBox2DEngine::setRestitution(b2Body *body, qreal restitution) const
{
assignFixtureProperty(body, &b2Fixture::SetRestitution, restitution);
}
/*!
\fn void QtBox2DEngine::setContactListener(b2ContactListener *listener)
Assign a contact listener for this engine.
\sa QtBox2DContactListener
*/
/*!
Starts simulation.
*/
void QtBox2DEngine::start()
{
if (!m_running) {
m_running = true;
setInterval(m_hertz);
}
}
/*!
Stops simulation.
*/
void QtBox2DEngine::stop()
{
if (m_running) {
m_running = false;
if (m_timer)
killTimer(m_timer);
m_timer = 0;
}
}
/*!
Set the simulation update interval in hertz.
*/
void QtBox2DEngine::setInterval(double hertz)
{
if (m_timer)
killTimer(m_timer);
m_hertz = hertz;
m_timer = startTimer(m_hertz/m_step);
}
/*!
Set timestep ratio. The step is performed interval/ratio times per second.
*/
void QtBox2DEngine::setTimestepRatio(double ratio)
{
m_step = ratio;
setInterval(m_hertz);
}
/*!
Specifies the world gravity using a vector with coordinates \a x and \a y.
*/
void QtBox2DEngine::setGravity(float32 x, float32 y)
{
m_world->SetGravity(b2Vec2(x, y));
}
/*!
Assign a gravity vector pointing downward with length g, i.e., (0, -g).
\overload
*/
void QtBox2DEngine::setGravity(float32 g)
{
m_world->SetGravity(b2Vec2(0, -g));
}
/*!
\fn void QtBox2DEngine::setVelocityIterations(int i)
Set iteration count for the velocity phase in the constraint solver. The
default setting is 8. More iterations decreases performance but improves
the quality of simulation. See Box2D's documentation.
*/
/*!
\fn void QtBox2DEngine::setPositionIterations(int i)
Set position iteration count. Default setting is 3. More iterations
decreases performance but improves the quality of simulation. See Box2D's
documentation.
*/
/*!
\reimp
*/
void QtBox2DEngine::timerEvent(QTimerEvent *event)
{
if (m_running && event->timerId() == m_timer) {
m_world->Step(1.0/m_hertz, m_velocityIter, m_positionIter);
emit step();
}
}
void QtBox2DEngine::assignFixtureProperty(b2Body *body, const FixtureMember &member, qreal arg) const
{
b2Fixture* fixture = body->GetFixtureList();
while (fixture) {
(fixture->*member)(arg);
fixture = fixture->GetNext();
}
}