mirror of https://github.com/axmolengine/axmol.git
Merge pull request #12239 from pandamicro/PhysicsSpriteFix
Fix transform issue of PhysicsSprite itself and its children
This commit is contained in:
commit
5f9469ac7b
|
@ -21,6 +21,8 @@
|
|||
*/
|
||||
|
||||
#include "CCPhysicsSprite.h"
|
||||
#include "base/CCDirector.h"
|
||||
#include "base/CCEventDispatcher.h"
|
||||
|
||||
#if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION)
|
||||
|
||||
|
@ -41,6 +43,7 @@ PhysicsSprite::PhysicsSprite()
|
|||
, _CPBody(nullptr)
|
||||
, _pB2Body(nullptr)
|
||||
, _PTMRatio(0.0f)
|
||||
, _syncTransform(nullptr)
|
||||
{}
|
||||
|
||||
PhysicsSprite* PhysicsSprite::create()
|
||||
|
@ -393,22 +396,30 @@ void PhysicsSprite::syncPhysicsTransform() const
|
|||
#endif
|
||||
}
|
||||
|
||||
// returns the transform matrix according the Chipmunk Body values
|
||||
const Mat4& PhysicsSprite::getNodeToParentTransform() const
|
||||
void PhysicsSprite::onEnter()
|
||||
{
|
||||
Node::onEnter();
|
||||
_syncTransform = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_AFTER_UPDATE, std::bind(&PhysicsSprite::afterUpdate, this, std::placeholders::_1));
|
||||
_syncTransform->retain();
|
||||
}
|
||||
|
||||
void PhysicsSprite::onExit()
|
||||
{
|
||||
if (_syncTransform != nullptr)
|
||||
{
|
||||
Director::getInstance()->getEventDispatcher()->removeEventListener(_syncTransform);
|
||||
_syncTransform->release();
|
||||
}
|
||||
Node::onExit();
|
||||
}
|
||||
|
||||
void PhysicsSprite::afterUpdate(EventCustom *event)
|
||||
{
|
||||
syncPhysicsTransform();
|
||||
|
||||
return _transform;
|
||||
}
|
||||
|
||||
void PhysicsSprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
||||
{
|
||||
if (isDirty())
|
||||
{
|
||||
syncPhysicsTransform();
|
||||
}
|
||||
|
||||
Sprite::draw(renderer, _transform, flags);
|
||||
_transformDirty = false;
|
||||
_transformUpdated = true;
|
||||
setDirtyRecursively(true);
|
||||
}
|
||||
|
||||
NS_CC_EXT_END
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include "2d/CCSprite.h"
|
||||
#include "extensions/ExtensionMacros.h"
|
||||
#include "extensions/ExtensionExport.h"
|
||||
#include "base/CCEventListenerCustom.h"
|
||||
|
||||
#if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION)
|
||||
|
||||
|
@ -108,6 +109,7 @@ public:
|
|||
|
||||
float getPTMRatio() const;
|
||||
void setPTMRatio(float fPTMRatio);
|
||||
virtual void syncPhysicsTransform() const;
|
||||
|
||||
// overrides
|
||||
virtual const Vec2& getPosition() const override;
|
||||
|
@ -117,13 +119,13 @@ public:
|
|||
virtual void setPosition(const Vec2 &position) override;
|
||||
virtual float getRotation() const override;
|
||||
virtual void setRotation(float fRotation) override;
|
||||
virtual void syncPhysicsTransform() const;
|
||||
virtual const Mat4& getNodeToParentTransform() const override;
|
||||
|
||||
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
|
||||
virtual void onEnter() override;
|
||||
virtual void onExit() override;
|
||||
|
||||
protected:
|
||||
const Vec2& getPosFromPhysics() const;
|
||||
void afterUpdate(EventCustom *event);
|
||||
|
||||
protected:
|
||||
bool _ignoreBodyRotation;
|
||||
|
@ -134,6 +136,9 @@ protected:
|
|||
// box2d specific
|
||||
b2Body *_pB2Body;
|
||||
float _PTMRatio;
|
||||
|
||||
// Event for update synchronise physic transform
|
||||
cocos2d::EventListenerCustom* _syncTransform;
|
||||
};
|
||||
|
||||
NS_CC_EXT_END
|
||||
|
|
Loading…
Reference in New Issue