Fixed Scale9Sprite can't run move/scale/rotation action in physics scene

This commit is contained in:
Wenhai Lin 2015-01-27 16:13:45 +08:00
parent 81198583f1
commit f2335fb3bc
5 changed files with 65 additions and 10 deletions

View File

@ -126,6 +126,7 @@ Node::Node(void)
, _physicsScaleStartY(1.0f) , _physicsScaleStartY(1.0f)
, _physicsRotation(0.0f) , _physicsRotation(0.0f)
, _physicsTransformDirty(true) , _physicsTransformDirty(true)
, _updateTransformFromPhysics(true)
#endif #endif
, _displayedOpacity(255) , _displayedOpacity(255)
, _realOpacity(255) , _realOpacity(255)
@ -330,6 +331,11 @@ void Node::setRotation(float rotation)
_rotationZ_X = _rotationZ_Y = rotation; _rotationZ_X = _rotationZ_Y = rotation;
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS
if (_physicsBody && _physicsBody->getWorld()) {
_physicsBody->getWorld()->_updateBodyTransform = true;
}
#endif
updateRotationQuat(); updateRotationQuat();
} }
@ -466,6 +472,11 @@ void Node::setScale(float scale)
_scaleX = _scaleY = _scaleZ = scale; _scaleX = _scaleY = _scaleZ = scale;
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS
if (_physicsBody && _physicsBody->getWorld()) {
_physicsBody->getWorld()->_updateBodyTransform = true;
}
#endif
} }
/// scaleX getter /// scaleX getter
@ -483,6 +494,11 @@ void Node::setScale(float scaleX,float scaleY)
_scaleX = scaleX; _scaleX = scaleX;
_scaleY = scaleY; _scaleY = scaleY;
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS
if (_physicsBody && _physicsBody->getWorld()) {
_physicsBody->getWorld()->_updateBodyTransform = true;
}
#endif
} }
/// scaleX setter /// scaleX setter
@ -493,6 +509,11 @@ void Node::setScaleX(float scaleX)
_scaleX = scaleX; _scaleX = scaleX;
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS
if (_physicsBody && _physicsBody->getWorld()) {
_physicsBody->getWorld()->_updateBodyTransform = true;
}
#endif
} }
/// scaleY getter /// scaleY getter
@ -532,6 +553,11 @@ void Node::setScaleY(float scaleY)
_scaleY = scaleY; _scaleY = scaleY;
_transformUpdated = _transformDirty = _inverseDirty = true; _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; _transformUpdated = _transformDirty = _inverseDirty = true;
_usingNormalizedPosition = false; _usingNormalizedPosition = false;
#if CC_USE_PHYSICS
if (_physicsBody && _physicsBody->getWorld()) {
_physicsBody->getWorld()->_updateBodyTransform = true;
}
#endif
} }
void Node::setPosition3D(const Vec3& position) void Node::setPosition3D(const Vec3& position)
@ -631,6 +662,11 @@ void Node::setNormalizedPosition(const Vec2& position)
_usingNormalizedPosition = true; _usingNormalizedPosition = true;
_normalizedPositionDirty = true; _normalizedPositionDirty = true;
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS
if (_physicsBody && _physicsBody->getWorld()) {
_physicsBody->getWorld()->_updateBodyTransform = true;
}
#endif
} }
ssize_t Node::getChildrenCount() const ssize_t Node::getChildrenCount() const
@ -1270,9 +1306,16 @@ uint32_t Node::processParentFlags(const Mat4& parentTransform, uint32_t parentFl
if(flags & FLAGS_DIRTY_MASK) if(flags & FLAGS_DIRTY_MASK)
_modelViewTransform = this->transform(parentTransform); _modelViewTransform = this->transform(parentTransform);
#if CC_USE_PHYSICS
if (_updateTransformFromPhysics) {
_transformUpdated = false;
_contentSizeDirty = false;
}
#else
_transformUpdated = false; _transformUpdated = false;
_contentSizeDirty = false; _contentSizeDirty = false;
#endif
return flags; 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; _updateTransformFromPhysics = false;
auto flags = processParentFlags(parentTransform, parentFlags); auto flags = processParentFlags(parentTransform, parentFlags);
@ -2046,7 +2089,7 @@ void Node::updatePhysicsBodyTransform(Scene* scene, const Mat4& parentTransform,
for (auto node : _children) for (auto node : _children)
{ {
node->updatePhysicsBodyTransform(scene, _modelViewTransform, flags, scaleX, scaleY); node->updatePhysicsBodyTransform(_modelViewTransform, flags, scaleX, scaleY);
} }
} }

View File

@ -1543,7 +1543,7 @@ public:
void updateTransformFromPhysics(const Mat4& parentTransform, uint32_t parentFlags); 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 #endif
// overrides // overrides

View File

@ -300,6 +300,12 @@ void Director::drawScene()
_runningScene->render(_renderer); _runningScene->render(_renderer);
_eventDispatcher->dispatchEvent(_eventAfterVisit); _eventDispatcher->dispatchEvent(_eventAfterVisit);
#if CC_USE_PHYSICS
if(physicsWorld)
{
physicsWorld->_updateBodyTransform = false;
}
#endif
} }
// draw the notifications node // draw the notifications node

View File

@ -334,7 +334,7 @@ void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Vec2& point1,
{ {
if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) 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(); updateBodies();
} }
RayCastCallbackInfo info = { this, func, point1, point2, data }; RayCastCallbackInfo info = { this, func, point1, point2, data };
@ -358,7 +358,7 @@ void PhysicsWorld::queryRect(PhysicsQueryRectCallbackFunc func, const Rect& rect
{ {
if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) 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(); updateBodies();
} }
RectQueryCallbackInfo info = {this, func, data}; RectQueryCallbackInfo info = {this, func, data};
@ -381,7 +381,7 @@ void PhysicsWorld::queryPoint(PhysicsQueryPointCallbackFunc func, const Vec2& po
{ {
if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) 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(); updateBodies();
} }
PointQueryCallbackInfo info = {this, func, data}; PointQueryCallbackInfo info = {this, func, data};
@ -823,9 +823,13 @@ void PhysicsWorld::update(float delta, bool userCall/* = false*/)
return; return;
} }
_scene->updatePhysicsBodyTransform(_scene, _scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); if(_updateBodyTransform || !_delayAddBodies.empty())
{
if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) _scene->updatePhysicsBodyTransform(_scene->getNodeToParentTransform(), 0, 1.0f, 1.0f);
updateBodies();
_updateBodyTransform = false;
}
else if (!_delayRemoveBodies.empty())
{ {
updateBodies(); updateBodies();
} }
@ -880,6 +884,7 @@ PhysicsWorld::PhysicsWorld()
, _autoStep(true) , _autoStep(true)
, _debugDraw(nullptr) , _debugDraw(nullptr)
, _debugDrawMask(DEBUGDRAW_NONE) , _debugDrawMask(DEBUGDRAW_NONE)
, _updateBodyTransform(false)
{ {
} }

View File

@ -204,6 +204,7 @@ protected:
int _substeps; int _substeps;
cpSpace* _cpSpace; cpSpace* _cpSpace;
bool _updateBodyTransform;
Vector<PhysicsBody*> _bodies; Vector<PhysicsBody*> _bodies;
std::list<PhysicsJoint*> _joints; std::list<PhysicsJoint*> _joints;
Scene* _scene; Scene* _scene;