From b61adcfdbbee97ac73e8199dfe05f2db776b5e62 Mon Sep 17 00:00:00 2001 From: Wenhai Lin Date: Wed, 14 Jan 2015 17:01:04 +0800 Subject: [PATCH] Fixed can't get correct position of PhysicsBody after add PhysicsBody to PhysicsWorld in the current frame. --- cocos/physics/CCPhysicsBody.cpp | 17 ++++++++++++++--- cocos/physics/CCPhysicsBody.h | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cocos/physics/CCPhysicsBody.cpp b/cocos/physics/CCPhysicsBody.cpp index 4d622b116a..23e65bc5d6 100644 --- a/cocos/physics/CCPhysicsBody.cpp +++ b/cocos/physics/CCPhysicsBody.cpp @@ -72,6 +72,7 @@ PhysicsBody::PhysicsBody() , _recordedPosition(Vec2::ZERO) , _recordedRotation(0.0f) , _recordedAngle(0.0) +, _positionInitDirty(true) { } @@ -334,6 +335,7 @@ void PhysicsBody::setGravityEnable(bool enable) void PhysicsBody::setPosition(const Vec2& position) { + _positionInitDirty = false; _recordedPosition = position; cpBodySetPos(_cpBody, PhysicsHelper::point2cpv(position + _positionOffset)); } @@ -355,9 +357,18 @@ void PhysicsBody::setScale(float scaleX, float scaleY) const Vec2& PhysicsBody::getPosition() { - _latestPosition.x = _cpBody->p.x - _positionOffset.x; - _latestPosition.y = _cpBody->p.y - _positionOffset.y; - + if (_positionInitDirty) { + if (_node) { + if (_node->getParent()) { + _latestPosition = _node->getParent()->convertToWorldSpace(_node->getPosition()); + } else { + _latestPosition = _node->getPosition(); + } + } + } else { + _latestPosition.x = _cpBody->p.x - _positionOffset.x; + _latestPosition.y = _cpBody->p.y - _positionOffset.y; + } return _latestPosition; } diff --git a/cocos/physics/CCPhysicsBody.h b/cocos/physics/CCPhysicsBody.h index 249770c5d8..91cfc57d53 100644 --- a/cocos/physics/CCPhysicsBody.h +++ b/cocos/physics/CCPhysicsBody.h @@ -338,6 +338,7 @@ protected: float _angularDamping; int _tag; + bool _positionInitDirty; Vec2 _recordedPosition; Vec2 _latestPosition; Vec2 _positionOffset;