issue #2771: fix PhysicsWorld some bugs

This commit is contained in:
boyu0 2013-11-07 14:40:09 +08:00
parent f2cd5ca630
commit a02fd7c6da
2 changed files with 21 additions and 12 deletions

View File

@ -307,6 +307,11 @@ void PhysicsWorld::delayTestRemoveJoint(PhysicsJoint* joint)
void PhysicsWorld::addJoint(PhysicsJoint* joint)
{
if (joint->getWorld() != nullptr && joint->getWorld() != this)
{
joint->removeFormWorld();
}
delayTestAddJoint(joint);
_joints.push_back(joint);
joint->_world = this;
@ -388,14 +393,14 @@ void PhysicsWorld::removeAllJoints(bool destroy)
_joints.clear();
}
PhysicsShape* PhysicsWorld::addShape(PhysicsShape* shape)
void PhysicsWorld::addShape(PhysicsShape* shape)
{
for (auto cps : shape->_info->getShapes())
{
_info->addShape(cps);
}
return shape;
return;
}
void PhysicsWorld::realAddJoint(PhysicsJoint *joint)
@ -408,11 +413,6 @@ void PhysicsWorld::realAddJoint(PhysicsJoint *joint)
void PhysicsWorld::realAddBody(PhysicsBody* body)
{
if (body->getWorld() != this && body->getWorld() != nullptr)
{
body->removeFromWorld();
}
if (body->isEnabled())
{
//is gravity enable
@ -435,15 +435,23 @@ void PhysicsWorld::realAddBody(PhysicsBody* body)
}
}
PhysicsBody* PhysicsWorld::addBody(PhysicsBody* body)
void PhysicsWorld::addBody(PhysicsBody* body)
{
CCASSERT(body != nullptr, "the body can not be nullptr");
if (body->getWorld() == this)
{
return;
}
if (body->getWorld() != nullptr)
{
body->removeFromWorld();
}
delayTestAddBody(body);
_bodies->addObject(body);
body->_world = this;
return body;
}
void PhysicsWorld::removeBody(PhysicsBody* body)
@ -451,6 +459,7 @@ void PhysicsWorld::removeBody(PhysicsBody* body)
if (body->getWorld() != this)
{
CCLOG("Physics Warnning: this body doesn't belong to this world");
return;
}

View File

@ -136,8 +136,8 @@ protected:
static PhysicsWorld* create(Scene& scene);
bool init(Scene& scene);
virtual PhysicsBody* addBody(PhysicsBody* body);
virtual PhysicsShape* addShape(PhysicsShape* shape);
virtual void addBody(PhysicsBody* body);
virtual void addShape(PhysicsShape* shape);
virtual void removeShape(PhysicsShape* shape);
virtual void update(float delta);