Merge pull request #11383 from WenhaiLin/v3-physics-rotation

Physics: Fixed the rotation of PhysicsBody is wrong if the rotation of node is set before PhysicsBody attached.
This commit is contained in:
minggo 2015-04-10 18:12:43 +08:00
commit 9cc03407a8
3 changed files with 11 additions and 8 deletions

View File

@ -129,6 +129,7 @@ Node::Node(void)
, _updateTransformFromPhysics(true)
, _physicsWorld(nullptr)
, _physicsBodyAssociatedWith(0)
, _physicsRotationOffset(0.0f)
#endif
, _displayedOpacity(255)
, _realOpacity(255)
@ -2078,6 +2079,7 @@ void Node::setPhysicsBody(PhysicsBody* body)
_physicsBody = body;
_physicsScaleStartX = _scaleX;
_physicsScaleStartY = _scaleY;
_physicsRotationOffset = _rotationZ_X;
_physicsBodyAssociatedWith++;
auto parentNode = this;
@ -2116,7 +2118,7 @@ void Node::updatePhysicsBodyTransform(const Mat4& parentTransform, uint32_t pare
parentTransform.transformPoint(vec3, &ret);
_physicsBody->setPosition(Vec2(ret.x, ret.y));
_physicsBody->setScale(scaleX / _physicsScaleStartX, scaleY / _physicsScaleStartY);
_physicsBody->setRotation(_physicsRotation);
_physicsBody->setRotation(_physicsRotation - _physicsRotationOffset);
}
for (auto node : _children)
@ -2139,7 +2141,7 @@ void Node::updateTransformFromPhysics(const Mat4& parentTransform, uint32_t pare
setPosition(ret.x, ret.y);
}
_physicsRotation = _physicsBody->getRotation();
setRotation(_physicsRotation - _parent->_physicsRotation);
setRotation(_physicsRotation - _parent->_physicsRotation + _physicsRotationOffset);
_physicsWorld->_updateBodyTransform = updateBodyTransform;
}

View File

@ -1842,6 +1842,7 @@ protected:
PhysicsWorld* _physicsWorld; /** The PhysicsWorld associated with the node.*/
int _physicsBodyAssociatedWith; /** The count of PhysicsBody associated with the node and children.*/
float _physicsRotationOffset; /** Record the rotation value when invoke Node::setPhysicsBody.*/
#endif
// opacity controls

View File

@ -817,11 +817,6 @@ void PhysicsWorld::step(float delta)
void PhysicsWorld::update(float delta, bool userCall/* = false*/)
{
if (delta < FLT_EPSILON)
{
return;
}
if(_updateBodyTransform || !_delayAddBodies.empty())
{
_scene->updatePhysicsBodyTransform(_scene->getNodeToParentTransform(), 0, 1.0f, 1.0f);
@ -832,12 +827,17 @@ void PhysicsWorld::update(float delta, bool userCall/* = false*/)
{
updateBodies();
}
if (!_delayAddJoints.empty() || !_delayRemoveJoints.empty())
{
updateJoints();
}
if (delta < FLT_EPSILON)
{
return;
}
if (userCall)
{
cpSpaceStep(_cpSpace, delta);