Fixed can't get correct position of PhysicsBody after add PhysicsBody to PhysicsWorld in the current frame.

This commit is contained in:
Wenhai Lin 2015-01-14 17:01:04 +08:00
parent 8a67b9b368
commit b61adcfdbb
2 changed files with 15 additions and 3 deletions

View File

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

View File

@ -338,6 +338,7 @@ protected:
float _angularDamping;
int _tag;
bool _positionInitDirty;
Vec2 _recordedPosition;
Vec2 _latestPosition;
Vec2 _positionOffset;