diff --git a/cocos/physics/CCPhysicsBody.cpp b/cocos/physics/CCPhysicsBody.cpp index 4d69d5c52095..69772073ac8e 100644 --- a/cocos/physics/CCPhysicsBody.cpp +++ b/cocos/physics/CCPhysicsBody.cpp @@ -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); } } @@ -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) @@ -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) @@ -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)); } @@ -545,6 +567,12 @@ void PhysicsBody::addMoment(float 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)); } @@ -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)); } @@ -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)); } diff --git a/cocos/physics/CCPhysicsWorld.cpp b/cocos/physics/CCPhysicsWorld.cpp index 48023afb0427..1e1e06c4c6e1 100644 --- a/cocos/physics/CCPhysicsWorld.cpp +++ b/cocos/physics/CCPhysicsWorld.cpp @@ -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;