forked from qml-box2d/qml-box2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
box2dworld.cpp
339 lines (288 loc) · 8.77 KB
/
box2dworld.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
339
/*
* box2dworld.cpp
* Copyright (c) 2010-2011 Thorbjørn Lindeijer <[email protected]>
* Copyright (c) 2011 Joonas Erkinheimo <[email protected]>
* Copyright (c) 2012 Adriano Rezende <[email protected]>
*
* This file is part of the Box2D QML plugin.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in
* a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "box2dworld.h"
#include "box2dbody.h"
#include "box2dcontact.h"
#include "box2dfixture.h"
#include "box2djoint.h"
#include "box2draycast.h"
StepDriver::StepDriver(Box2DWorld *world)
: QAbstractAnimation(world)
, mWorld(world)
{
setLoopCount(-1); // loop forever
}
int StepDriver::duration() const
{
return 1000;
}
void StepDriver::updateCurrentTime(int)
{
mWorld->step();
}
class ContactEvent
{
public:
enum Type {
BeginContact,
EndContact
};
Type type;
Box2DFixture *fixtureA;
Box2DFixture *fixtureB;
};
class ContactListener : public b2ContactListener
{
public:
explicit ContactListener(Box2DWorld *world);
void BeginContact(b2Contact *contact);
void EndContact(b2Contact *contact);
void PreSolve(b2Contact *contact, const b2Manifold *oldManifold);
void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse);
void removeEvent(int index) { mEvents.removeAt(index); }
void clearEvents() { mEvents.clear(); }
const QList<ContactEvent> &events() { return mEvents; }
private:
QList<ContactEvent> mEvents;
Box2DWorld *mWorld;
Box2DContact mContact;
};
ContactListener::ContactListener(Box2DWorld *world) :
mWorld(world)
{
}
void ContactListener::BeginContact(b2Contact *contact)
{
ContactEvent event;
event.type = ContactEvent::BeginContact;
event.fixtureA = toBox2DFixture(contact->GetFixtureA());
event.fixtureB = toBox2DFixture(contact->GetFixtureB());
mEvents.append(event);
}
void ContactListener::EndContact(b2Contact *contact)
{
ContactEvent event;
event.type = ContactEvent::EndContact;
event.fixtureA = toBox2DFixture(contact->GetFixtureA());
event.fixtureB = toBox2DFixture(contact->GetFixtureB());
mEvents.append(event);
}
void ContactListener::PreSolve(b2Contact *contact, const b2Manifold *oldManifold)
{
Q_UNUSED(oldManifold)
mContact.setContact(contact);
emit mWorld->preSolve(&mContact);
}
void ContactListener::PostSolve(b2Contact *contact, const b2ContactImpulse *impulse)
{
Q_UNUSED(impulse)
mContact.setContact(contact);
emit mWorld->postSolve(&mContact);
}
Box2DWorld::Box2DWorld(QObject *parent) :
QObject(parent),
mWorld(b2Vec2(0.0f, -10.0f)),
mContactListener(0),
mTimeStep(1.0f / 60.0f),
mVelocityIterations(8),
mPositionIterations(3),
mComponentComplete(false),
mIsRunning(true),
mSynchronizing(false),
mStepDriver(new StepDriver(this)),
mProfile(new Box2DProfile(&mWorld, this)),
mEnableContactEvents(true),
mPixelsPerMeter(32.0f)
{
mWorld.SetDestructionListener(this);
}
Box2DWorld::~Box2DWorld()
{
// The bodies and joints will be deleted as part of the world, so it's
// important that they are no longer referenced from the Box2DBody and
// Box2DJoint instances.
for (b2Body *body = mWorld.GetBodyList(); body; body = body->GetNext())
toBox2DBody(body)->nullifyBody();
for (b2Joint *joint = mWorld.GetJointList(); joint; joint = joint->GetNext())
toBox2DJoint(joint)->nullifyJoint();
enableContactListener(false);
}
void Box2DWorld::setTimeStep(float timeStep)
{
if (mTimeStep != timeStep) {
mTimeStep = timeStep;
emit timeStepChanged();
}
}
void Box2DWorld::setRunning(bool running)
{
if (mIsRunning == running)
return;
mIsRunning = running;
emit runningChanged();
if (mComponentComplete) {
if (running)
mStepDriver->start();
else
mStepDriver->stop();
}
}
void Box2DWorld::setVelocityIterations(int iterations)
{
if (mVelocityIterations != iterations) {
mVelocityIterations = iterations;
emit velocityIterationsChanged();
}
}
void Box2DWorld::setPositionIterations(int iterations)
{
if (mPositionIterations != iterations) {
mPositionIterations = iterations;
emit positionIterationsChanged();
}
}
QPointF Box2DWorld::gravity() const
{
return invertY(mWorld.GetGravity());
}
void Box2DWorld::setGravity(const QPointF &gravity)
{
const b2Vec2 invertedGravity = invertY(gravity);
if (mWorld.GetGravity() == invertedGravity)
return;
mWorld.SetGravity(invertedGravity);
emit gravityChanged();
}
void Box2DWorld::setAutoClearForces(bool autoClearForces)
{
if (mWorld.GetAutoClearForces() == autoClearForces)
return;
mWorld.SetAutoClearForces(autoClearForces);
emit autoClearForcesChanged();
}
void Box2DWorld::setEnableContactEvents(bool enableContactEvents)
{
if(enableContactEvents == mEnableContactEvents)
return;
mEnableContactEvents = enableContactEvents;
enableContactListener(mEnableContactEvents);
emit enableContactEventsChanged();
}
void Box2DWorld::enableContactListener(bool enable)
{
if (enable) {
mContactListener = new ContactListener(this);
mWorld.SetContactListener(mContactListener);
} else {
mWorld.SetContactListener(0);
delete mContactListener;
}
}
void Box2DWorld::setPixelsPerMeter(float pixelsPerMeter)
{
if (pixelsPerMeter <= 0.0f) {
qWarning("World: pixelsPerMeter must be > 0.0f");
return;
}
if (mPixelsPerMeter != pixelsPerMeter) {
mPixelsPerMeter = pixelsPerMeter;
pixelsPerMeterChanged();
}
}
void Box2DWorld::classBegin()
{
}
void Box2DWorld::componentComplete()
{
mComponentComplete = true;
enableContactListener(mEnableContactEvents);
if (mIsRunning)
mStepDriver->start();
}
void Box2DWorld::SayGoodbye(b2Joint *joint)
{
if (Box2DJoint *temp = toBox2DJoint(joint)) {
temp->nullifyJoint();
delete temp;
}
}
void Box2DWorld::SayGoodbye(b2Fixture *fixture)
{
if (mEnableContactEvents) {
Box2DFixture *f = toBox2DFixture(fixture);
QList<ContactEvent> events = mContactListener->events();
for (int i = events.count() - 1; i >= 0; i--) {
if(events.at(i).fixtureA == f || events.at(i).fixtureB == f)
mContactListener->removeEvent(i);
}
}
}
void Box2DWorld::step()
{
// Update Box2D state before stepping
for (b2Body *body = mWorld.GetBodyList(); body; body = body->GetNext()) {
Box2DBody *b = toBox2DBody(body);
if (b->transformDirty() && b->isActive())
b->updateTransform();
}
mWorld.Step(mTimeStep, mVelocityIterations, mPositionIterations);
b2Timer timer;
// Update QML state after stepping
mSynchronizing = true;
for (b2Body *body = mWorld.GetBodyList(); body; body = body->GetNext()) {
Box2DBody *b = toBox2DBody(body);
if (b->isActive() && b->bodyType() != Box2DBody::Static && b->target())
b->synchronize();
}
mSynchronizing = false;
mProfile->mSynchronize = timer.GetMilliseconds();
timer.Reset();
if (mEnableContactEvents) {
// Emit contact signals
foreach (const ContactEvent &event, mContactListener->events()) {
switch (event.type) {
case ContactEvent::BeginContact:
emit event.fixtureA->beginContact(event.fixtureB);
emit event.fixtureB->beginContact(event.fixtureA);
break;
case ContactEvent::EndContact:
emit event.fixtureA->endContact(event.fixtureB);
emit event.fixtureB->endContact(event.fixtureA);
break;
}
}
}
mContactListener->clearEvents();
mProfile->mEmitSignals = timer.GetMilliseconds();
emit stepped();
}
void Box2DWorld::rayCast(Box2DRayCast *rayCast,
const QPointF &point1,
const QPointF &point2)
{
mWorld.RayCast(rayCast, toMeters(point1), toMeters(point2));
}