Fixed #4755: Fix PhysicsSprite transform not updated issue

This commit is contained in:
pandamicro 2014-04-23 17:11:55 +08:00
parent 55911e1e78
commit 1966d1f50e
2 changed files with 37 additions and 22 deletions

View File

@ -328,61 +328,57 @@ void PhysicsSprite::setRotation(float fRotation)
}
// returns the transform matrix according the Chipmunk Body values
const kmMat4& PhysicsSprite::getNodeToParentTransform() const
void PhysicsSprite::syncPhysicsTransform() const
{
// Although scale is not used by physics engines, it is calculated just in case
// the sprite is animated (scaled up/down) using actions.
// For more info see: http://www.cocos2d-iphone.org/forum/topic/68990
#if CC_ENABLE_CHIPMUNK_INTEGRATION
cpVect rot = (_ignoreBodyRotation ? cpvforangle(-CC_DEGREES_TO_RADIANS(_rotationX)) : _CPBody->rot);
float x = _CPBody->p.x + rot.x * -_anchorPointInPoints.x * _scaleX - rot.y * -_anchorPointInPoints.y * _scaleY;
float y = _CPBody->p.y + rot.y * -_anchorPointInPoints.x * _scaleX + rot.x * -_anchorPointInPoints.y * _scaleY;
if (_ignoreAnchorPointForPosition)
{
x += _anchorPointInPoints.x;
y += _anchorPointInPoints.y;
}
kmScalar mat[] = { (kmScalar)rot.x * _scaleX, (kmScalar)rot.y * _scaleX, 0, 0,
(kmScalar)-rot.y * _scaleY, (kmScalar)rot.x * _scaleY, 0, 0,
0, 0, 1, 0,
x, y, 0, 1};
(kmScalar)-rot.y * _scaleY, (kmScalar)rot.x * _scaleY, 0, 0,
0, 0, 1, 0,
x, y, 0, 1};
kmMat4Fill(&_transform, mat);
return _transform;
#elif CC_ENABLE_BOX2D_INTEGRATION
b2Vec2 pos = _pB2Body->GetPosition();
float x = pos.x * _PTMRatio;
float y = pos.y * _PTMRatio;
if (_ignoreAnchorPointForPosition)
{
x += _anchorPointInPoints.x;
y += _anchorPointInPoints.y;
}
// Make matrix
float radians = _pB2Body->GetAngle();
float c = cosf(radians);
float s = sinf(radians);
if (!_anchorPointInPoints.equals(Point::ZERO))
{
x += ((c * -_anchorPointInPoints.x * _scaleX) + (-s * -_anchorPointInPoints.y * _scaleY));
y += ((s * -_anchorPointInPoints.x * _scaleX) + (c * -_anchorPointInPoints.y * _scaleY));
}
// Rot, Translate Matrix
kmScalar mat[] = { (kmScalar)c * _scaleX, (kmScalar)s * _scaleX, 0, 0,
@ -392,9 +388,25 @@ const kmMat4& PhysicsSprite::getNodeToParentTransform() const
kmMat4Fill(&_transform, mat);
return _transform;
#endif
}
// returns the transform matrix according the Chipmunk Body values
const kmMat4& PhysicsSprite::getNodeToParentTransform() const
{
syncPhysicsTransform();
return _transform;
}
void PhysicsSprite::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated)
{
if (isDirty())
{
syncPhysicsTransform();
}
Sprite::draw(renderer, _transform, transformUpdated);
}
NS_CC_EXT_END

View File

@ -112,7 +112,10 @@ public:
virtual void setPosition(const Point &position) override;
virtual float getRotation() const override;
virtual void setRotation(float fRotation) override;
virtual void syncPhysicsTransform() const;
virtual const kmMat4& getNodeToParentTransform() const override;
virtual void draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) override;
protected:
const Point& getPosFromPhysics() const;