From 080e2debe8c804bdc5ece92fecc39df8b2d146a2 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 8 Jun 2015 13:43:51 +0800 Subject: [PATCH 1/2] Fix transform issue of PhysicsSprite itself and its children --- extensions/physics-nodes/CCPhysicsSprite.cpp | 20 +++++++++----------- extensions/physics-nodes/CCPhysicsSprite.h | 6 +++--- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/extensions/physics-nodes/CCPhysicsSprite.cpp b/extensions/physics-nodes/CCPhysicsSprite.cpp index 620b0d2dff..97323a864b 100644 --- a/extensions/physics-nodes/CCPhysicsSprite.cpp +++ b/extensions/physics-nodes/CCPhysicsSprite.cpp @@ -393,22 +393,20 @@ void PhysicsSprite::syncPhysicsTransform() const #endif } -// returns the transform matrix according the Chipmunk Body values -const Mat4& PhysicsSprite::getNodeToParentTransform() const +void PhysicsSprite::onEnter() { - syncPhysicsTransform(); - - return _transform; + Node::onEnter(); + scheduleUpdate(); } -void PhysicsSprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) +void PhysicsSprite::update(float delta) { - if (isDirty()) - { - syncPhysicsTransform(); - } + Node::update(delta); + syncPhysicsTransform(); - Sprite::draw(renderer, _transform, flags); + _transformDirty = false; + _transformUpdated = true; + setDirtyRecursively(true); } NS_CC_EXT_END diff --git a/extensions/physics-nodes/CCPhysicsSprite.h b/extensions/physics-nodes/CCPhysicsSprite.h index 6dd88562f9..0f6c6fda9e 100644 --- a/extensions/physics-nodes/CCPhysicsSprite.h +++ b/extensions/physics-nodes/CCPhysicsSprite.h @@ -108,6 +108,7 @@ public: float getPTMRatio() const; void setPTMRatio(float fPTMRatio); + virtual void syncPhysicsTransform() const; // overrides virtual const Vec2& getPosition() const override; @@ -117,10 +118,9 @@ 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 update(float delta) override; protected: const Vec2& getPosFromPhysics() const; From 81f77b00108a32093334265d7d8e89fd6bfe4afa Mon Sep 17 00:00:00 2001 From: pandamicro Date: Sat, 1 Aug 2015 16:50:16 +0800 Subject: [PATCH 2/2] Improve sync transform logic using custom listener --- extensions/physics-nodes/CCPhysicsSprite.cpp | 19 ++++++++++++++++--- extensions/physics-nodes/CCPhysicsSprite.h | 7 ++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/extensions/physics-nodes/CCPhysicsSprite.cpp b/extensions/physics-nodes/CCPhysicsSprite.cpp index 97323a864b..86080ad7aa 100644 --- a/extensions/physics-nodes/CCPhysicsSprite.cpp +++ b/extensions/physics-nodes/CCPhysicsSprite.cpp @@ -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() @@ -396,12 +399,22 @@ void PhysicsSprite::syncPhysicsTransform() const void PhysicsSprite::onEnter() { Node::onEnter(); - scheduleUpdate(); + _syncTransform = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_AFTER_UPDATE, std::bind(&PhysicsSprite::afterUpdate, this, std::placeholders::_1)); + _syncTransform->retain(); } -void PhysicsSprite::update(float delta) +void PhysicsSprite::onExit() +{ + if (_syncTransform != nullptr) + { + Director::getInstance()->getEventDispatcher()->removeEventListener(_syncTransform); + _syncTransform->release(); + } + Node::onExit(); +} + +void PhysicsSprite::afterUpdate(EventCustom *event) { - Node::update(delta); syncPhysicsTransform(); _transformDirty = false; diff --git a/extensions/physics-nodes/CCPhysicsSprite.h b/extensions/physics-nodes/CCPhysicsSprite.h index 0f6c6fda9e..ae17b824a0 100644 --- a/extensions/physics-nodes/CCPhysicsSprite.h +++ b/extensions/physics-nodes/CCPhysicsSprite.h @@ -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) @@ -120,10 +121,11 @@ public: virtual void setRotation(float fRotation) override; virtual void onEnter() override; - virtual void update(float delta) 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