Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions cocos/physics/CCPhysicsBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,19 +293,32 @@ void PhysicsBody::setDynamic(bool dynamic)
if (dynamic)
{
cpBodySetMass(_info->getBody(), _mass);
cpBodySetMoment(_info->getBody(), _moment);

if (_world != nullptr)
{
// reset the gravity enable
if (isGravityEnabled())
{
_gravityEnable = false;
setGravityEnable(true);
}

cpSpaceAddBody(_world->_info->getSpace(), _info->getBody());
}
}else
}
else
{
cpBodySetMass(_info->getBody(), PHYSICS_INFINITY);

if (_world != nullptr)
{
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);
}

}
Expand Down Expand Up @@ -456,7 +469,11 @@ void PhysicsBody::setMass(float mass)
}
}

cpBodySetMass(_info->getBody(), PhysicsHelper::float2cpfloat(_mass));
// the static body's mass and moment is always infinity
if (_dynamic)
{
cpBodySetMass(_info->getBody(), PhysicsHelper::float2cpfloat(_mass));
}
}

void PhysicsBody::addMass(float mass)
Expand Down Expand Up @@ -498,7 +515,11 @@ void PhysicsBody::addMass(float mass)
}
}

cpBodySetMass(_info->getBody(), PhysicsHelper::float2cpfloat(_mass));
// the static body's mass and moment is always infinity
if (_dynamic)
{
cpBodySetMass(_info->getBody(), PhysicsHelper::float2cpfloat(_mass));
}
}

void PhysicsBody::addMoment(float moment)
Expand Down Expand Up @@ -537,14 +558,21 @@ 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));
}
}

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));
}

Expand All @@ -565,6 +593,12 @@ Point PhysicsBody::getVelocityAtWorldPoint(const Point& point)

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));
}

Expand Down Expand Up @@ -598,7 +632,8 @@ void PhysicsBody::setMoment(float moment)
_moment = moment;
_momentDefault = false;

if (_rotationEnable)
// the static body's mass and moment is always infinity
if (_rotationEnable && _dynamic)
{
cpBodySetMoment(_info->getBody(), PhysicsHelper::float2cpfloat(_moment));
}
Expand Down
18 changes: 17 additions & 1 deletion cocos/physics/CCPhysicsWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,25 @@ void PhysicsWorld::removeBody(PhysicsBody* body)
// destory the body's 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);
_bodies->removeObject(body);
body->_world = nullptr;
Expand Down