axmol/extensions/CocoStudio/Armature/physics/CCPhysicsWorld.cpp

166 lines
4.2 KiB
C++
Raw Normal View History

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
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) override
2013-06-07 18:29:47 +08:00
{
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) override
2013-06-07 18:29:47 +08:00
{
contact_list.clear();
B2_NOT_USED(contact);
}
virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
2013-06-07 18:29:47 +08:00
{
B2_NOT_USED(contact);
B2_NOT_USED(oldManifold);
}
virtual void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse) override
2013-06-07 18:29:47 +08:00
{
B2_NOT_USED(contact);
B2_NOT_USED(impulse);
}
public:
std::list<Contact> contact_list;
};
PhysicsWorld *PhysicsWorld::s_PhysicsWorld = NULL;
2013-06-06 12:02:54 +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)
{
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
}
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
}
PhysicsWorld::PhysicsWorld()
: _noGravityWorld(NULL)
, _debugDraw(NULL)
2013-06-06 12:02:54 +08:00
{
}
PhysicsWorld::~PhysicsWorld()
2013-06-06 12:02:54 +08:00
{
CC_SAFE_DELETE(_debugDraw);
CC_SAFE_DELETE(_noGravityWorld);
CC_SAFE_DELETE(_contactListener);
2013-06-06 12:02:54 +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
_noGravityWorld = new b2World(noGravity);
_noGravityWorld->SetAllowSleeping(true);
2013-06-06 12:02:54 +08:00
_contactListener = new ContactListener();
_noGravityWorld->SetContactListener(_contactListener);
2013-06-06 12:02:54 +08:00
#if ENABLE_PHYSICS_DEBUG
_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;
_debugDraw->SetFlags(flags);
2013-06-06 12:02:54 +08:00
#endif
}
b2World *PhysicsWorld::getNoGravityWorld()
2013-06-06 12:02:54 +08:00
{
return _noGravityWorld;
2013-06-06 12:02:54 +08:00
}
void PhysicsWorld::update(float dt)
2013-06-06 12:02:54 +08:00
{
_noGravityWorld->Step(dt, 0, 0);
2013-06-06 12:02:54 +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
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
}
void PhysicsWorld::drawDebug()
2013-06-06 12:02:54 +08:00
{
_noGravityWorld->DrawDebugData();
2013-06-06 12:02:54 +08:00
}
}}} // namespace cocos2d { namespace extension { namespace armature {