Merge pull request #12179 from WenhaiLin/v3-37-physics-ap

Node:Fixed the anchor point can only be Vec2::ANCHOR_MIDDLE after setting physics body.
This commit is contained in:
子龙山人 2015-06-08 16:12:56 +08:00
commit 54e23f15e6
2 changed files with 12 additions and 19 deletions

View File

@ -710,14 +710,6 @@ const Vec2& Node::getAnchorPoint() const
void Node::setAnchorPoint(const Vec2& point)
{
#if CC_USE_PHYSICS
if (_physicsBody != nullptr && !point.equals(Vec2::ANCHOR_MIDDLE))
{
CCLOG("Node warning: This node has a physics body, the anchor must be in the middle, you cann't change this to other value.");
return;
}
#endif
if (! point.equals(_anchorPoint))
{
_anchorPoint = point;
@ -2087,14 +2079,6 @@ void Node::setPhysicsBody(PhysicsBody* body)
body->_node = this;
body->retain();
// physics rotation based on body position, but node rotation based on node anthor point
// it cann't support both of them, so I clear the anthor point to default.
if (!getAnchorPoint().equals(Vec2::ANCHOR_MIDDLE))
{
CCLOG("Node warning: setPhysicsBody sets anchor point to Vec2::ANCHOR_MIDDLE.");
setAnchorPoint(Vec2::ANCHOR_MIDDLE);
}
_physicsBody = body;
_physicsScaleStartX = _scaleX;
@ -2133,10 +2117,16 @@ void Node::updatePhysicsBodyTransform(const Mat4& parentTransform, uint32_t pare
if (_physicsBody && ((flags & FLAGS_DIRTY_MASK) || _physicsTransformDirty))
{
_physicsTransformDirty = false;
Vec3 vec3(_position.x, _position.y, 0);
Vec3 vec3(_contentSize.width * 0.5f, _contentSize.height * 0.5f, 0);
Vec3 ret;
parentTransform.transformPoint(vec3, &ret);
_modelViewTransform.transformPoint(vec3, &ret);
_physicsBody->setPosition(Vec2(ret.x, ret.y));
parentTransform.getInversed().transformPoint(&ret);
_offsetX = ret.x - _position.x;
_offsetY = ret.y - _position.y;
_physicsBody->setScale(scaleX / _physicsScaleStartX, scaleY / _physicsScaleStartY);
_physicsBody->setRotation(_physicsRotation - _physicsRotationOffset);
}
@ -2158,7 +2148,7 @@ void Node::updateTransformFromPhysics(const Mat4& parentTransform, uint32_t pare
Vec3 vec3(newPosition.x, newPosition.y, 0);
Vec3 ret;
parentTransform.getInversed().transformPoint(vec3, &ret);
setPosition(ret.x, ret.y);
setPosition(ret.x - _offsetX, ret.y - _offsetY);
}
_physicsRotation = _physicsBody->getRotation();
setRotation(_physicsRotation - _parent->_physicsRotation + _physicsRotationOffset);

View File

@ -1844,6 +1844,9 @@ 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.*/
float _offsetX;
float _offsetY;
#endif
// opacity controls