Merge pull request #6359 from pandamicro/NewPhysicsSpriteFix

Fixed #4755: Fix PhysicsSprite transform not updated issue
This commit is contained in:
James Chen 2014-04-25 10:36:46 +08:00
commit 45cc122074
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 void PhysicsSprite::syncPhysicsTransform() const
const kmMat4& PhysicsSprite::getNodeToParentTransform() const
{ {
// Although scale is not used by physics engines, it is calculated just in case // Although scale is not used by physics engines, it is calculated just in case
// the sprite is animated (scaled up/down) using actions. // the sprite is animated (scaled up/down) using actions.
// For more info see: http://www.cocos2d-iphone.org/forum/topic/68990 // For more info see: http://www.cocos2d-iphone.org/forum/topic/68990
#if CC_ENABLE_CHIPMUNK_INTEGRATION #if CC_ENABLE_CHIPMUNK_INTEGRATION
cpVect rot = (_ignoreBodyRotation ? cpvforangle(-CC_DEGREES_TO_RADIANS(_rotationX)) : _CPBody->rot); 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 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; float y = _CPBody->p.y + rot.y * -_anchorPointInPoints.x * _scaleX + rot.x * -_anchorPointInPoints.y * _scaleY;
if (_ignoreAnchorPointForPosition) if (_ignoreAnchorPointForPosition)
{ {
x += _anchorPointInPoints.x; x += _anchorPointInPoints.x;
y += _anchorPointInPoints.y; y += _anchorPointInPoints.y;
} }
kmScalar mat[] = { (kmScalar)rot.x * _scaleX, (kmScalar)rot.y * _scaleX, 0, 0, kmScalar mat[] = { (kmScalar)rot.x * _scaleX, (kmScalar)rot.y * _scaleX, 0, 0,
(kmScalar)-rot.y * _scaleY, (kmScalar)rot.x * _scaleY, 0, 0, (kmScalar)-rot.y * _scaleY, (kmScalar)rot.x * _scaleY, 0, 0,
0, 0, 1, 0, 0, 0, 1, 0,
x, y, 0, 1}; x, y, 0, 1};
kmMat4Fill(&_transform, mat); kmMat4Fill(&_transform, mat);
return _transform;
#elif CC_ENABLE_BOX2D_INTEGRATION #elif CC_ENABLE_BOX2D_INTEGRATION
b2Vec2 pos = _pB2Body->GetPosition(); b2Vec2 pos = _pB2Body->GetPosition();
float x = pos.x * _PTMRatio; float x = pos.x * _PTMRatio;
float y = pos.y * _PTMRatio; float y = pos.y * _PTMRatio;
if (_ignoreAnchorPointForPosition) if (_ignoreAnchorPointForPosition)
{ {
x += _anchorPointInPoints.x; x += _anchorPointInPoints.x;
y += _anchorPointInPoints.y; y += _anchorPointInPoints.y;
} }
// Make matrix // Make matrix
float radians = _pB2Body->GetAngle(); float radians = _pB2Body->GetAngle();
float c = cosf(radians); float c = cosf(radians);
float s = sinf(radians); float s = sinf(radians);
if (!_anchorPointInPoints.equals(Point::ZERO)) if (!_anchorPointInPoints.equals(Point::ZERO))
{ {
x += ((c * -_anchorPointInPoints.x * _scaleX) + (-s * -_anchorPointInPoints.y * _scaleY)); x += ((c * -_anchorPointInPoints.x * _scaleX) + (-s * -_anchorPointInPoints.y * _scaleY));
y += ((s * -_anchorPointInPoints.x * _scaleX) + (c * -_anchorPointInPoints.y * _scaleY)); y += ((s * -_anchorPointInPoints.x * _scaleX) + (c * -_anchorPointInPoints.y * _scaleY));
} }
// Rot, Translate Matrix // Rot, Translate Matrix
kmScalar mat[] = { (kmScalar)c * _scaleX, (kmScalar)s * _scaleX, 0, 0, kmScalar mat[] = { (kmScalar)c * _scaleX, (kmScalar)s * _scaleX, 0, 0,
@ -392,9 +388,25 @@ const kmMat4& PhysicsSprite::getNodeToParentTransform() const
kmMat4Fill(&_transform, mat); kmMat4Fill(&_transform, mat);
return _transform;
#endif #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 NS_CC_EXT_END

View File

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