From f2335fb3bcfdfeadada8fa494bb96826973dd15c Mon Sep 17 00:00:00 2001 From: Wenhai Lin Date: Tue, 27 Jan 2015 16:13:45 +0800 Subject: [PATCH] Fixed Scale9Sprite can't run move/scale/rotation action in physics scene --- cocos/2d/CCNode.cpp | 49 ++++++++++++++++++++++++++++++-- cocos/2d/CCNode.h | 2 +- cocos/base/CCDirector.cpp | 6 ++++ cocos/physics/CCPhysicsWorld.cpp | 17 +++++++---- cocos/physics/CCPhysicsWorld.h | 1 + 5 files changed, 65 insertions(+), 10 deletions(-) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 1eb1bb57bd..26acd89da3 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -126,6 +126,7 @@ Node::Node(void) , _physicsScaleStartY(1.0f) , _physicsRotation(0.0f) , _physicsTransformDirty(true) +, _updateTransformFromPhysics(true) #endif , _displayedOpacity(255) , _realOpacity(255) @@ -330,6 +331,11 @@ void Node::setRotation(float rotation) _rotationZ_X = _rotationZ_Y = rotation; _transformUpdated = _transformDirty = _inverseDirty = true; +#if CC_USE_PHYSICS + if (_physicsBody && _physicsBody->getWorld()) { + _physicsBody->getWorld()->_updateBodyTransform = true; + } +#endif updateRotationQuat(); } @@ -466,6 +472,11 @@ void Node::setScale(float scale) _scaleX = _scaleY = _scaleZ = scale; _transformUpdated = _transformDirty = _inverseDirty = true; +#if CC_USE_PHYSICS + if (_physicsBody && _physicsBody->getWorld()) { + _physicsBody->getWorld()->_updateBodyTransform = true; + } +#endif } /// scaleX getter @@ -483,6 +494,11 @@ void Node::setScale(float scaleX,float scaleY) _scaleX = scaleX; _scaleY = scaleY; _transformUpdated = _transformDirty = _inverseDirty = true; +#if CC_USE_PHYSICS + if (_physicsBody && _physicsBody->getWorld()) { + _physicsBody->getWorld()->_updateBodyTransform = true; + } +#endif } /// scaleX setter @@ -493,6 +509,11 @@ void Node::setScaleX(float scaleX) _scaleX = scaleX; _transformUpdated = _transformDirty = _inverseDirty = true; +#if CC_USE_PHYSICS + if (_physicsBody && _physicsBody->getWorld()) { + _physicsBody->getWorld()->_updateBodyTransform = true; + } +#endif } /// scaleY getter @@ -532,6 +553,11 @@ void Node::setScaleY(float scaleY) _scaleY = scaleY; _transformUpdated = _transformDirty = _inverseDirty = true; +#if CC_USE_PHYSICS + if (_physicsBody && _physicsBody->getWorld()) { + _physicsBody->getWorld()->_updateBodyTransform = true; + } +#endif } @@ -563,6 +589,11 @@ void Node::setPosition(float x, float y) _transformUpdated = _transformDirty = _inverseDirty = true; _usingNormalizedPosition = false; +#if CC_USE_PHYSICS + if (_physicsBody && _physicsBody->getWorld()) { + _physicsBody->getWorld()->_updateBodyTransform = true; + } +#endif } void Node::setPosition3D(const Vec3& position) @@ -631,6 +662,11 @@ void Node::setNormalizedPosition(const Vec2& position) _usingNormalizedPosition = true; _normalizedPositionDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true; +#if CC_USE_PHYSICS + if (_physicsBody && _physicsBody->getWorld()) { + _physicsBody->getWorld()->_updateBodyTransform = true; + } +#endif } ssize_t Node::getChildrenCount() const @@ -1270,9 +1306,16 @@ uint32_t Node::processParentFlags(const Mat4& parentTransform, uint32_t parentFl if(flags & FLAGS_DIRTY_MASK) _modelViewTransform = this->transform(parentTransform); - + +#if CC_USE_PHYSICS + if (_updateTransformFromPhysics) { + _transformUpdated = false; + _contentSizeDirty = false; + } +#else _transformUpdated = false; _contentSizeDirty = false; +#endif return flags; } @@ -2021,7 +2064,7 @@ void Node::setPhysicsBody(PhysicsBody* body) } } -void Node::updatePhysicsBodyTransform(Scene* scene, const Mat4& parentTransform, uint32_t parentFlags, float parentScaleX, float parentScaleY) +void Node::updatePhysicsBodyTransform(const Mat4& parentTransform, uint32_t parentFlags, float parentScaleX, float parentScaleY) { _updateTransformFromPhysics = false; auto flags = processParentFlags(parentTransform, parentFlags); @@ -2046,7 +2089,7 @@ void Node::updatePhysicsBodyTransform(Scene* scene, const Mat4& parentTransform, for (auto node : _children) { - node->updatePhysicsBodyTransform(scene, _modelViewTransform, flags, scaleX, scaleY); + node->updatePhysicsBodyTransform(_modelViewTransform, flags, scaleX, scaleY); } } diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 89f8d8da82..1533a4e698 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -1543,7 +1543,7 @@ public: void updateTransformFromPhysics(const Mat4& parentTransform, uint32_t parentFlags); - virtual void updatePhysicsBodyTransform(Scene* scene, const Mat4& parentTransform, uint32_t parentFlags, float parentScaleX, float parentScaleY); + virtual void updatePhysicsBodyTransform(const Mat4& parentTransform, uint32_t parentFlags, float parentScaleX, float parentScaleY); #endif // overrides diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 82fcfd12dc..8f39cfaac1 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -300,6 +300,12 @@ void Director::drawScene() _runningScene->render(_renderer); _eventDispatcher->dispatchEvent(_eventAfterVisit); +#if CC_USE_PHYSICS + if(physicsWorld) + { + physicsWorld->_updateBodyTransform = false; + } +#endif } // draw the notifications node diff --git a/cocos/physics/CCPhysicsWorld.cpp b/cocos/physics/CCPhysicsWorld.cpp index 1989ecd16a..33d3380160 100644 --- a/cocos/physics/CCPhysicsWorld.cpp +++ b/cocos/physics/CCPhysicsWorld.cpp @@ -334,7 +334,7 @@ void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Vec2& point1, { if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) { - _scene->updatePhysicsBodyTransform(_scene, _scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); + _scene->updatePhysicsBodyTransform(_scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); updateBodies(); } RayCastCallbackInfo info = { this, func, point1, point2, data }; @@ -358,7 +358,7 @@ void PhysicsWorld::queryRect(PhysicsQueryRectCallbackFunc func, const Rect& rect { if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) { - _scene->updatePhysicsBodyTransform(_scene, _scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); + _scene->updatePhysicsBodyTransform(_scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); updateBodies(); } RectQueryCallbackInfo info = {this, func, data}; @@ -381,7 +381,7 @@ void PhysicsWorld::queryPoint(PhysicsQueryPointCallbackFunc func, const Vec2& po { if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) { - _scene->updatePhysicsBodyTransform(_scene, _scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); + _scene->updatePhysicsBodyTransform(_scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); updateBodies(); } PointQueryCallbackInfo info = {this, func, data}; @@ -823,9 +823,13 @@ void PhysicsWorld::update(float delta, bool userCall/* = false*/) return; } - _scene->updatePhysicsBodyTransform(_scene, _scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); - - if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) + if(_updateBodyTransform || !_delayAddBodies.empty()) + { + _scene->updatePhysicsBodyTransform(_scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); + updateBodies(); + _updateBodyTransform = false; + } + else if (!_delayRemoveBodies.empty()) { updateBodies(); } @@ -880,6 +884,7 @@ PhysicsWorld::PhysicsWorld() , _autoStep(true) , _debugDraw(nullptr) , _debugDrawMask(DEBUGDRAW_NONE) +, _updateBodyTransform(false) { } diff --git a/cocos/physics/CCPhysicsWorld.h b/cocos/physics/CCPhysicsWorld.h index 08224937c2..6f714b55ab 100644 --- a/cocos/physics/CCPhysicsWorld.h +++ b/cocos/physics/CCPhysicsWorld.h @@ -204,6 +204,7 @@ protected: int _substeps; cpSpace* _cpSpace; + bool _updateBodyTransform; Vector _bodies; std::list _joints; Scene* _scene;