forked from vanderlin/ofxBox2d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofxBox2d.cpp
More file actions
484 lines (401 loc) · 12.2 KB
/
ofxBox2d.cpp
File metadata and controls
484 lines (401 loc) · 12.2 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include "ofxBox2d.h"
// ------------------------------------------------------
ofxBox2d::ofxBox2d() {
enableContactEvents = false;
world = NULL;
m_bomb = NULL;
#ifdef TARGET_OPENGLES
// touch grabbing
for( int i=0; i<OF_MAX_TOUCH_JOINTS; i++ )
touchJoints[ i ] = NULL;
for( int i=0; i<OF_MAX_TOUCH_JOINTS; i++ )
touchBodies[ i ] = NULL;
#else
// mouse grabbing
mouseJoint = NULL;
mouseBody = NULL;
#endif
ground = NULL;
mainBody = NULL;
}
// ------------------------------------------------------
ofxBox2d::~ofxBox2d() {
#ifdef TARGET_OPENGLES
// destroy touch grabbing bodies
for(int i=0; i<OF_MAX_TOUCH_JOINTS; i++) {
if(touchBodies[i]) {
if(world) world->DestroyBody(touchBodies[i]);
}
}
#else
// destroy mouse grabbing body
if(mouseBody) {
if(world) world->DestroyBody(mouseBody);
}
#endif
if(world) {
for (b2Body* f = world->GetBodyList(); f; f = f->GetNext()) {
world->DestroyBody(f);
}
for (b2Joint* f = world->GetJointList(); f; f = f->GetNext()) {
world->DestroyJoint(f);
}
/*
// This is not safe...
delete world;
world = NULL;*/
}
}
// ------------------------------------------------------ init
void ofxBox2d::init() {
// settings
bHasContactListener = false;
bCheckBounds = false;
bEnableGrabbing = true;
scale = OFX_BOX2D_SCALE;
doSleep = true;
// gravity
gravity.set(0, 5.0f);
setFPS(30.0);
velocityIterations = 40;
positionIterations = 20;
#ifdef TARGET_OPENGLES
// touch grabbing
for( int i=0; i<OF_MAX_TOUCH_JOINTS; i++ )
touchJoints[ i ] = NULL;
for( int i=0; i<OF_MAX_TOUCH_JOINTS; i++ )
touchBodies[ i ] = NULL;
#else
// mouse grabbing
mouseJoint = NULL;
mouseBody = NULL;
#endif
// ground/bounds
// debug drawer
debugRender.setScale(scale);
debugRender.SetFlags(1);
//worldAABB.lowerBound.Set(-100.0f, -100.0f);
//worldAABB.upperBound.Set(100.0f, 100.0f);
delete world;
world = NULL;
world = new b2World(b2Vec2(gravity.x, gravity.y));
world->SetAllowSleeping(doSleep);
//world->SetDebugDraw(&debugRender);
if(ground!=NULL) {
world->DestroyBody(ground);
ground = NULL;
}
ofLog(OF_LOG_NOTICE, "ofxBox2d:: - world created -");
}
// ------------------------------------------------------ enable events
void ofxBox2d::enableEvents() {
if(world!=NULL) {
world->SetContactListener(this);
}
}
// ------------------------------------------------------ disable events
void ofxBox2d::disableEvents() {
if(world!=NULL) {
world->SetContactListener(NULL);
}
}
// ------------------------------------------------------ grab shapes
void ofxBox2d::setContactListener(ofxBox2dContactListener * listener) {
if(world != NULL) {
bHasContactListener = true;
world->SetContactListener(listener);
}
else {
ofLog(OF_LOG_WARNING, "ofxBox2d:: - world not inited -");
}
}
// ------------------------------------------------------ grab shapes Events
void ofxBox2d::registerGrabbing() {
#ifdef TARGET_OPENGLES
ofAddListener(ofEvents().touchDown, this, &ofxBox2d::touchDown);
ofAddListener(ofEvents().touchMoved, this, &ofxBox2d::touchMoved);
ofAddListener(ofEvents().touchUp, this, &ofxBox2d::touchUp);
#else
ofAddListener(ofEvents().mousePressed, this, &ofxBox2d::mousePressed);
ofAddListener(ofEvents().mouseDragged, this, &ofxBox2d::mouseDragged);
ofAddListener(ofEvents().mouseReleased, this, &ofxBox2d::mouseReleased);
#endif
}
#ifdef TARGET_OPENGLES
void ofxBox2d::touchDown(ofTouchEventArgs &touch) {
grabShapeDown(touch.x, touch.y, touch.id);
}
void ofxBox2d::touchMoved(ofTouchEventArgs &touch) {
grabShapeDragged(touch.x, touch.y, touch.id);
}
void ofxBox2d::touchUp(ofTouchEventArgs &touch) {
grabShapeUp(touch.x, touch.y, touch.id);
}
#else
void ofxBox2d::mousePressed(ofMouseEventArgs &e) {
grabShapeDown(e.x, e.y);
}
void ofxBox2d::mouseDragged(ofMouseEventArgs &e) {
grabShapeDragged(e.x, e.y);
}
void ofxBox2d::mouseReleased(ofMouseEventArgs &e) {
grabShapeUp(e.x, e.y);
}
#endif
// ------------------------------------------------------
void ofxBox2d::grabShapeDown(float x, float y, int id) {
if(world == NULL) {
ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -");
return;
}
if(bEnableGrabbing) {
b2Vec2 p(x/OFX_BOX2D_SCALE, y/OFX_BOX2D_SCALE);
#ifdef TARGET_OPENGLES
if(id >= 0 && id < OF_MAX_TOUCH_JOINTS)
{
if(touchJoints[id] != NULL)
return;
if( touchBodies[id] == NULL) {
b2BodyDef bd;
touchBodies[id] = world->CreateBody(&bd);
}
}
else
return; // invalid mouse / touch id.
#else
if (mouseJoint != NULL) {
return;
}
if(mouseBody == NULL) {
b2BodyDef bd;
mouseBody = world->CreateBody(&bd);
}
#endif
// Make a small box.
b2AABB aabb;
b2Vec2 d;
d.Set(0.001f, 0.001f);
aabb.lowerBound = p - d;
aabb.upperBound = p + d;
// Query the world for overlapping shapes.
QueryCallback callback(p);
world->QueryAABB(&callback, aabb);
if (callback.m_fixture) {
b2Body* body = callback.m_fixture->GetBody();
b2MouseJointDef md;
md.bodyB = body;
md.target = p;
md.maxForce = 1000.0f * body->GetMass();
#ifdef TARGET_OPENGLES
md.bodyA = touchBodies[id];
touchJoints[id] = (b2MouseJoint*)world->CreateJoint(&md);
#else
md.bodyA = mouseBody;
mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
#endif
body->SetAwake(true);
}
}
}
// ------------------------------------------------------
void ofxBox2d::grabShapeUp(float x, float y, int id) {
#ifdef TARGET_OPENGLES
if(id >= 0 && id < OF_MAX_TOUCH_JOINTS) {
if(touchJoints[id] && bEnableGrabbing){
if(world == NULL) {
ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -");
return;
}
world->DestroyJoint(touchJoints[id]);
touchJoints[id] = NULL;
}
}
#else
if(mouseJoint && bEnableGrabbing) {
if(world == NULL) {
ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -");
return;
}
world->DestroyJoint(mouseJoint);
mouseJoint = NULL;
}
#endif
}
// ------------------------------------------------------
void ofxBox2d::grabShapeDragged(float x, float y, int id) {
b2Vec2 p(x/OFX_BOX2D_SCALE, y/OFX_BOX2D_SCALE);
#ifdef TARGET_OPENGLES
if(id >= 0 && id < OF_MAX_TOUCH_JOINTS) {
if (touchJoints[id] && bEnableGrabbing)
touchJoints[id]->SetTarget(p);
}
#else
if (mouseJoint && bEnableGrabbing)
mouseJoint->SetTarget(p);
#endif
}
// ------------------------------------------------------
int ofxBox2d::getBodyCount() {
if(world)
return world->GetBodyCount();
return 0;
}
// ------------------------------------------------------
int ofxBox2d::getJointCount() {
if(world)
return world->GetJointCount();
return 0;
}
// ------------------------------------------------------ wake up
void ofxBox2d::wakeupShapes() {
if(world == NULL) {
ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -");
return;
}
b2Body* bodies = world->GetBodyList();
while(bodies) {
b2Body* b = bodies;
if(b) {
if( !b->IsAwake() ) b->SetAwake(true);
}
bodies = bodies->GetNext();
}
}
// ------------------------------------------------------ set gravity
void ofxBox2d::setGravity(ofPoint pt) {
setGravity(pt.x, pt.y);
}
void ofxBox2d::setGravity(float x, float y) {
if(world == NULL) {
ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -");
return;
}
world->SetGravity(b2Vec2(x, y));
wakeupShapes();
}
ofPoint ofxBox2d::getGravity() {
if(world == NULL) {
ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -");
return ofPoint();
}
return ofPoint(world->GetGravity().x, world->GetGravity().y);
}
// ------------------------------------------------------ set bounds
void ofxBox2d::setBounds(ofPoint lowBounds, ofPoint upBounds) {
//TODO: still need to work on this...
}
// ------------------------------------------------------ create Ground
void ofxBox2d::createGround(float x1, float y1, float x2, float y2) {
if(world == NULL) {
ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -");
return;
}
if(ground!=NULL) world->DestroyBody(ground);
b2BodyDef bd;
ground = world->CreateBody(&bd);
b2EdgeShape shape;
shape.Set(b2Vec2(x1/OFX_BOX2D_SCALE, y1/OFX_BOX2D_SCALE), b2Vec2(x2/OFX_BOX2D_SCALE, y2/OFX_BOX2D_SCALE));
ground->CreateFixture(&shape, 0.0f);
}
// ------------------------------------------------------ create Ground
void ofxBox2d::createGround(const ofPoint & p1, const ofPoint & p2) {
createGround(p1.x, p1.y, p2.x, p2.y);
}
// ------------------------------------------------------ create bounds
void ofxBox2d::createBounds(ofRectangle rec) {
createBounds(rec.x, rec.y, rec.width, rec.height);
}
// ------------------------------------------------------ create bounds
void ofxBox2d::createBounds(float x, float y, float w, float h) {
if(world == NULL) {
ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -");
return;
}
if(ground!=NULL) world->DestroyBody(ground);
b2BodyDef bd;
bd.position.Set(0, 0);
ground = world->CreateBody(&bd);
b2EdgeShape shape;
ofRectangle rec(x/OFX_BOX2D_SCALE, y/OFX_BOX2D_SCALE, w/OFX_BOX2D_SCALE, h/OFX_BOX2D_SCALE);
//right wall
shape.Set(b2Vec2(rec.x+rec.width, rec.y), b2Vec2(rec.x+rec.width, rec.y+rec.height));
ground->CreateFixture(&shape, 0.0f);
//left wall
shape.Set(b2Vec2(rec.x, rec.y), b2Vec2(rec.x, rec.y+rec.height));
ground->CreateFixture(&shape, 0.0f);
// top wall
shape.Set(b2Vec2(rec.x, rec.y), b2Vec2(rec.x+rec.width, rec.y));
ground->CreateFixture(&shape, 0.0f);
// bottom wall
shape.Set(b2Vec2(rec.x, rec.y+rec.height), b2Vec2(rec.x+rec.width, rec.y+rec.height));
ground->CreateFixture(&shape, 0.0f);
}
// ------------------------------------------------------ check if shapes are out of bounds
void ofxBox2d::checkBounds(bool b) {
bCheckBounds = b;
}
// ------------------------------------------------------
void ofxBox2d::setIterations(int velocityTimes, int positionTimes) {
velocityIterations = velocityTimes;
positionIterations = positionTimes;
}
// ------------------------------------------------------
void ofxBox2d::update() {
if(world == NULL) {
ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -");
return;
}
// destroy the object if we are out of the bounds
if(bCheckBounds) {
/*
float top = 0;
float bottom = ofGetHeight();
float right = ofGetWidth();
float left = 0;
b2Body* node = world->GetBodyList();
while(node) {
b2Body* b = node;
node = node->GetNext();
b2Vec2 p = b->GetPosition();
ofxBox2dBaseShape* base = (ofxBox2dBaseShape*)b->GetUserData();
if(base) {
//printf("dead:%i\n", base->dead);
if(p.y*OFX_BOX2D_SCALE > bottom) {
base->dead = true;
world->DestroyBody(b);
}
if(p.y*OFX_BOX2D_SCALE < top) {
base->dead = true;
world->DestroyBody(b);
}
if(p.x*OFX_BOX2D_SCALE > right) {
base->dead = true;
world->DestroyBody(b);
}
if(p.x*OFX_BOX2D_SCALE < left) {
base->dead = true;
world->DestroyBody(b);
}
*/
}
float timeStep = (1.0f / fps);
world->Step(timeStep, velocityIterations, positionIterations);
//world->Validate();
}
// ------------------------------------------------------
void ofxBox2d::drawGround() {
if(ground == NULL) return;
const b2Transform& xf = ground->GetTransform();
for (b2Fixture* f = ground->GetFixtureList(); f; f = f->GetNext()) {
b2EdgeShape* edge = (b2EdgeShape*)f->GetShape();
if(edge) {
ofNoFill();
ofSetColor(120, 0, 120);
ofLine(worldPtToscreenPt(edge->m_vertex0), worldPtToscreenPt(edge->m_vertex1));
}
}
}
// ------------------------------------------------------
void ofxBox2d::draw() {
drawGround();
}