diff --git a/src/ofxBox2d.cpp b/src/ofxBox2d.cpp index 8042a1b..ff60045 100644 --- a/src/ofxBox2d.cpp +++ b/src/ofxBox2d.cpp @@ -5,17 +5,6 @@ ofxBox2d::ofxBox2d() { enableContactEvents = false; world = NULL; m_bomb = NULL; -#ifdef TARGET_OPENGLES - // touch grabbing - for( int i=0; iDestroyBody(touchBodies[i]); - } + for(int i=0; iDestroyJoint(mouseJoint.joint); + world->DestroyBody(mouseJoint.body); } -#else - // destroy mouse grabbing body - if(mouseBody) { - if(world) world->DestroyBody(mouseBody); - } -#endif + mouseJoints.clear(); + if(world) { // Fix from: https://github.com/vanderlin/ofxBox2d/issues/62 b2Body* f = world->GetBodyList(); @@ -73,17 +56,6 @@ void ofxBox2d::init() { velocityIterations = 40; positionIterations = 20; -#ifdef TARGET_OPENGLES - // touch grabbing - for( int i=0; i= 0 && id < OF_MAX_TOUCH_JOINTS) - { - if(touchJoints[id] != NULL) - return; - - if( touchBodies[id] == NULL) { - b2BodyDef bd; - touchBodies[id] = world->CreateBody(&bd); - } + + for(int i=0; iCreateBody(&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); - } - - - } + } + b2Vec2 p(x/OFX_BOX2D_SCALE, y/OFX_BOX2D_SCALE); + + // 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 == NULL) { + return; + } + + mouseJoints.push_back(ofxBox2dMouseJoint()); + ofxBox2dMouseJoint & mouseJoint = mouseJoints.back(); + mouseJoint.mouseID = id; + + b2BodyDef bd; + mouseJoint.body = world->CreateBody(&bd); + + b2Body* body = callback.m_fixture->GetBody(); + b2MouseJointDef md; + md.bodyB = body; + md.target = p; + md.maxForce = 1000.0f * body->GetMass(); + md.bodyA = mouseJoint.body; + mouseJoint.joint = (b2MouseJoint*)world->CreateJoint(&md); + + 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; - } + + if(bEnableGrabbing == false) { + return; } -#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 + if(world == NULL) { + ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -"); + return; + } + for(int i=0; iDestroyJoint(mouseJoint.joint); + world->DestroyBody(mouseJoint.body); + mouseJoints.erase(mouseJoints.begin()+i); + break; + } + } } // ------------------------------------------------------ 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 + if(bEnableGrabbing == false) { + return; + } + b2Vec2 p(x/OFX_BOX2D_SCALE, y/OFX_BOX2D_SCALE); + + for(int i=0; iSetTarget(p); + break; + } + } } // ------------------------------------------------------ diff --git a/src/ofxBox2d.h b/src/ofxBox2d.h index 911c5b9..4a8d6bc 100644 --- a/src/ofxBox2d.h +++ b/src/ofxBox2d.h @@ -13,10 +13,6 @@ #include "ofxBox2dRender.h" #include "ofxBox2dContactListener.h" -#ifdef TARGET_OPENGLES -#define OF_MAX_TOUCH_JOINTS 5 // max number of touch points on iPhone + iPad (this may change in the future though). -#endif - class ofxBox2dContactArgs : public ofEventArgs { public: @@ -24,6 +20,19 @@ class ofxBox2dContactArgs : public ofEventArgs { b2Fixture * b; }; +class ofxBox2dMouseJoint { +public: + ofxBox2dMouseJoint() { + mouseID = -1; + joint = NULL; + body = NULL; + } + + int mouseID; + b2MouseJoint * joint; + b2Body * body; +}; + class ofxBox2d : public b2ContactListener { private: @@ -66,13 +75,8 @@ class ofxBox2d : public b2ContactListener { b2BodyDef bd; b2Body* m_bomb; -#ifdef TARGET_OPENGLES - b2MouseJoint* touchJoints[ OF_MAX_TOUCH_JOINTS ]; - b2Body* touchBodies[ OF_MAX_TOUCH_JOINTS ]; -#else - b2MouseJoint* mouseJoint; - b2Body* mouseBody; -#endif + vector mouseJoints; + b2Body* ground; b2Body* mainBody;