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 "CCPhysicsSprite.h"
|
||||||
|
#include "base/CCDirector.h"
|
||||||
|
#include "base/CCEventDispatcher.h"
|
||||||
|
|
||||||
#if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION)
|
#if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION)
|
||||||
|
|
||||||
|
@ -41,6 +43,7 @@ PhysicsSprite::PhysicsSprite()
|
||||||
, _CPBody(nullptr)
|
, _CPBody(nullptr)
|
||||||
, _pB2Body(nullptr)
|
, _pB2Body(nullptr)
|
||||||
, _PTMRatio(0.0f)
|
, _PTMRatio(0.0f)
|
||||||
|
, _syncTransform(nullptr)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
PhysicsSprite* PhysicsSprite::create()
|
PhysicsSprite* PhysicsSprite::create()
|
||||||
|
@ -393,22 +396,30 @@ void PhysicsSprite::syncPhysicsTransform() const
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the transform matrix according the Chipmunk Body values
|
void PhysicsSprite::onEnter()
|
||||||
const Mat4& PhysicsSprite::getNodeToParentTransform() const
|
{
|
||||||
|
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();
|
syncPhysicsTransform();
|
||||||
|
|
||||||
return _transform;
|
_transformDirty = false;
|
||||||
}
|
_transformUpdated = true;
|
||||||
|
setDirtyRecursively(true);
|
||||||
void PhysicsSprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
|
||||||
{
|
|
||||||
if (isDirty())
|
|
||||||
{
|
|
||||||
syncPhysicsTransform();
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprite::draw(renderer, _transform, flags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_CC_EXT_END
|
NS_CC_EXT_END
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "2d/CCSprite.h"
|
#include "2d/CCSprite.h"
|
||||||
#include "extensions/ExtensionMacros.h"
|
#include "extensions/ExtensionMacros.h"
|
||||||
#include "extensions/ExtensionExport.h"
|
#include "extensions/ExtensionExport.h"
|
||||||
|
#include "base/CCEventListenerCustom.h"
|
||||||
|
|
||||||
#if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION)
|
#if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION)
|
||||||
|
|
||||||
|
@ -108,6 +109,7 @@ public:
|
||||||
|
|
||||||
float getPTMRatio() const;
|
float getPTMRatio() const;
|
||||||
void setPTMRatio(float fPTMRatio);
|
void setPTMRatio(float fPTMRatio);
|
||||||
|
virtual void syncPhysicsTransform() const;
|
||||||
|
|
||||||
// overrides
|
// overrides
|
||||||
virtual const Vec2& getPosition() const override;
|
virtual const Vec2& getPosition() const override;
|
||||||
|
@ -117,13 +119,13 @@ public:
|
||||||
virtual void setPosition(const Vec2 &position) override;
|
virtual void setPosition(const Vec2 &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 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:
|
protected:
|
||||||
const Vec2& getPosFromPhysics() const;
|
const Vec2& getPosFromPhysics() const;
|
||||||
|
void afterUpdate(EventCustom *event);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool _ignoreBodyRotation;
|
bool _ignoreBodyRotation;
|
||||||
|
@ -134,6 +136,9 @@ protected:
|
||||||
// box2d specific
|
// box2d specific
|
||||||
b2Body *_pB2Body;
|
b2Body *_pB2Body;
|
||||||
float _PTMRatio;
|
float _PTMRatio;
|
||||||
|
|
||||||
|
// Event for update synchronise physic transform
|
||||||
|
cocos2d::EventListenerCustom* _syncTransform;
|
||||||
};
|
};
|
||||||
|
|
||||||
NS_CC_EXT_END
|
NS_CC_EXT_END
|
||||||
|
|
Loading…
Reference in New Issue