2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "CCPhysicsWorld.h"
|
|
|
|
#include "../utils/CCArmatureDefine.h"
|
2013-06-07 10:52:32 +08:00
|
|
|
#include "Box2D/Box2D.h"
|
2013-06-07 18:29:47 +08:00
|
|
|
#include "../external_tool/GLES-Render.h"
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-17 21:32:15 +08:00
|
|
|
namespace cocos2d { namespace extension { namespace armature {
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 18:29:47 +08:00
|
|
|
|
|
|
|
class Contact
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
b2Fixture *fixtureA;
|
|
|
|
b2Fixture *fixtureB;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ContactListener : public b2ContactListener
|
|
|
|
{
|
|
|
|
//! Callbacks for derived classes.
|
|
|
|
virtual void BeginContact(b2Contact *contact)
|
|
|
|
{
|
|
|
|
if (contact)
|
|
|
|
{
|
|
|
|
Contact c;
|
|
|
|
c.fixtureA = contact->GetFixtureA();
|
|
|
|
c.fixtureB = contact->GetFixtureB();
|
|
|
|
|
|
|
|
contact_list.push_back(c);
|
|
|
|
}
|
|
|
|
B2_NOT_USED(contact);
|
|
|
|
}
|
|
|
|
virtual void EndContact(b2Contact *contact)
|
|
|
|
{
|
|
|
|
contact_list.clear();
|
|
|
|
B2_NOT_USED(contact);
|
|
|
|
}
|
|
|
|
virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold)
|
|
|
|
{
|
|
|
|
B2_NOT_USED(contact);
|
|
|
|
B2_NOT_USED(oldManifold);
|
|
|
|
}
|
|
|
|
virtual void PostSolve(const b2Contact *contact, const b2ContactImpulse *impulse)
|
|
|
|
{
|
|
|
|
B2_NOT_USED(contact);
|
|
|
|
B2_NOT_USED(impulse);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::list<Contact> contact_list;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsWorld *PhysicsWorld::s_PhysicsWorld = NULL;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsWorld *PhysicsWorld::sharedPhysicsWorld()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
if (s_PhysicsWorld == NULL)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
s_PhysicsWorld = new PhysicsWorld();
|
2013-06-07 10:52:32 +08:00
|
|
|
s_PhysicsWorld->initNoGravityWorld();
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
return s_PhysicsWorld;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsWorld::purgePhysicsWorld()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
delete s_PhysicsWorld;
|
|
|
|
s_PhysicsWorld = NULL;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsWorld::PhysicsWorld()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _noGravityWorld(NULL)
|
|
|
|
, _debugDraw(NULL)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsWorld::~PhysicsWorld()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_debugDraw);
|
|
|
|
CC_SAFE_DELETE(_noGravityWorld);
|
|
|
|
CC_SAFE_DELETE(_contactListener);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsWorld::initNoGravityWorld()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
b2Vec2 noGravity(0, 0);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_noGravityWorld = new b2World(noGravity);
|
|
|
|
_noGravityWorld->SetAllowSleeping(true);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_contactListener = new ContactListener();
|
|
|
|
_noGravityWorld->SetContactListener(_contactListener);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
#if ENABLE_PHYSICS_DEBUG
|
2013-06-15 14:03:30 +08:00
|
|
|
_debugDraw = new GLESDebugDraw( PT_RATIO );
|
|
|
|
_noGravityWorld->SetDebugDraw(_debugDraw);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
uint32 flags = 0;
|
|
|
|
flags += b2Draw::e_shapeBit;
|
|
|
|
// flags += b2Draw::e_jointBit;
|
|
|
|
// flags += b2Draw::e_aabbBit;
|
|
|
|
// flags += b2Draw::e_pairBit;
|
|
|
|
// flags += b2Draw::e_centerOfMassBit;
|
2013-06-15 14:03:30 +08:00
|
|
|
_debugDraw->SetFlags(flags);
|
2013-06-06 12:02:54 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
b2World *PhysicsWorld::getNoGravityWorld()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _noGravityWorld;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsWorld::update(float dt)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_noGravityWorld->Step(dt, 0, 0);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (std::list<Contact>::iterator it = _contactListener->contact_list.begin(); it != _contactListener->contact_list.end(); ++it)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
Contact &contact = *it;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
b2Body *b2a = contact.fixtureA->GetBody();
|
|
|
|
b2Body *b2b = contact.fixtureB->GetBody();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone *ba = (Bone *)b2a->GetUserData();
|
|
|
|
Bone *bb = (Bone *)b2b->GetUserData();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
BoneColliderSignal.emit(ba, bb);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsWorld::drawDebug()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_noGravityWorld->DrawDebugData();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-17 21:32:15 +08:00
|
|
|
}}} // namespace cocos2d { namespace extension { namespace armature {
|