Merge pull request #4348 from boyu0/iss2771_physical

issue #2771: physical bug fix.
This commit is contained in:
James Chen 2013-11-20 00:24:07 -08:00
commit bffc5bb4d5
2 changed files with 59 additions and 8 deletions

View File

@ -293,19 +293,32 @@ void PhysicsBody::setDynamic(bool dynamic)
if (dynamic) if (dynamic)
{ {
cpBodySetMass(_info->getBody(), _mass); cpBodySetMass(_info->getBody(), _mass);
cpBodySetMoment(_info->getBody(), _moment);
if (_world != nullptr) if (_world != nullptr)
{ {
// reset the gravity enable
if (isGravityEnabled())
{
_gravityEnable = false;
setGravityEnable(true);
}
cpSpaceAddBody(_world->_info->getSpace(), _info->getBody()); cpSpaceAddBody(_world->_info->getSpace(), _info->getBody());
} }
}else }
else
{ {
cpBodySetMass(_info->getBody(), PHYSICS_INFINITY);
if (_world != nullptr) if (_world != nullptr)
{ {
cpSpaceRemoveBody(_world->_info->getSpace(), _info->getBody()); cpSpaceRemoveBody(_world->_info->getSpace(), _info->getBody());
} }
// avoid incorrect collion simulation.
cpBodySetMass(_info->getBody(), PHYSICS_INFINITY);
cpBodySetMoment(_info->getBody(), PHYSICS_INFINITY);
cpBodySetVel(_info->getBody(), cpvzero);
cpBodySetAngVel(_info->getBody(), 0.0f);
} }
} }
@ -456,7 +469,11 @@ void PhysicsBody::setMass(float mass)
} }
} }
// the static body's mass and moment is always infinity
if (_dynamic)
{
cpBodySetMass(_info->getBody(), PhysicsHelper::float2cpfloat(_mass)); cpBodySetMass(_info->getBody(), PhysicsHelper::float2cpfloat(_mass));
}
} }
void PhysicsBody::addMass(float mass) void PhysicsBody::addMass(float mass)
@ -498,7 +515,11 @@ void PhysicsBody::addMass(float mass)
} }
} }
// the static body's mass and moment is always infinity
if (_dynamic)
{
cpBodySetMass(_info->getBody(), PhysicsHelper::float2cpfloat(_mass)); cpBodySetMass(_info->getBody(), PhysicsHelper::float2cpfloat(_mass));
}
} }
void PhysicsBody::addMoment(float moment) void PhysicsBody::addMoment(float moment)
@ -537,7 +558,8 @@ void PhysicsBody::addMoment(float moment)
} }
} }
if (_rotationEnable) // the static body's mass and moment is always infinity
if (_rotationEnable && _dynamic)
{ {
cpBodySetMoment(_info->getBody(), PhysicsHelper::float2cpfloat(_moment)); cpBodySetMoment(_info->getBody(), PhysicsHelper::float2cpfloat(_moment));
} }
@ -545,6 +567,12 @@ void PhysicsBody::addMoment(float moment)
void PhysicsBody::setVelocity(const Point& velocity) void PhysicsBody::setVelocity(const Point& velocity)
{ {
if (!_dynamic)
{
CCLOG("physics warning: your cann't set velocity for a static body.");
return;
}
cpBodySetVel(_info->getBody(), PhysicsHelper::point2cpv(velocity)); cpBodySetVel(_info->getBody(), PhysicsHelper::point2cpv(velocity));
} }
@ -565,6 +593,12 @@ Point PhysicsBody::getVelocityAtWorldPoint(const Point& point)
void PhysicsBody::setAngularVelocity(float velocity) void PhysicsBody::setAngularVelocity(float velocity)
{ {
if (!_dynamic)
{
CCLOG("physics warning: your cann't set angular velocity for a static body.");
return;
}
cpBodySetAngVel(_info->getBody(), PhysicsHelper::float2cpfloat(velocity)); cpBodySetAngVel(_info->getBody(), PhysicsHelper::float2cpfloat(velocity));
} }
@ -598,7 +632,8 @@ void PhysicsBody::setMoment(float moment)
_moment = moment; _moment = moment;
_momentDefault = false; _momentDefault = false;
if (_rotationEnable) // the static body's mass and moment is always infinity
if (_rotationEnable && _dynamic)
{ {
cpBodySetMoment(_info->getBody(), PhysicsHelper::float2cpfloat(_moment)); cpBodySetMoment(_info->getBody(), PhysicsHelper::float2cpfloat(_moment));
} }

View File

@ -481,8 +481,24 @@ void PhysicsWorld::removeBody(PhysicsBody* body)
// destory the body's joints // destory the body's joints
for (auto joint : body->_joints) for (auto joint : body->_joints)
{ {
removeJoint(joint, true); // set destroy param to false to keep the iterator available
removeJoint(joint, false);
PhysicsBody* other = (joint->getBodyA() == body ? joint->getBodyB() : body);
other->removeJoint(joint);
// test the distraction is delaied or not
if (_delayRemoveJoints.size() > 0 && _delayRemoveJoints.back() == joint)
{
joint->_destoryMark = true;
} }
else
{
delete joint;
}
}
body->_joints.clear();
removeBodyOrDelay(body); removeBodyOrDelay(body);
_bodies->removeObject(body); _bodies->removeObject(body);