[physics] add pre/post callback in `update()` (#19118)

* add physics update pre/post callback

* update space

* run before simulation
This commit is contained in:
Arnold 2018-10-16 16:37:51 +08:00 committed by minggo
parent f3ef6b80de
commit b70a6a6def
2 changed files with 33 additions and 5 deletions

View File

@ -872,6 +872,9 @@ void PhysicsWorld::step(float delta)
void PhysicsWorld::update(float delta, bool userCall/* = false*/)
{
if(_preUpdateCallback) _preUpdateCallback(); //fix #11154
if(!_delayAddBodies.empty())
{
updateBodies();
@ -950,6 +953,8 @@ void PhysicsWorld::update(float delta, bool userCall/* = false*/)
// Update physics position, should loop as the same sequence as node tree.
// PhysicsWorld::afterSimulation() will depend on the sequence.
afterSimulation(_scene, sceneToWorldTransform, 0.f);
if(_postUpdateCallback) _postUpdateCallback(); //fix #11154
}
PhysicsWorld* PhysicsWorld::construct(Scene* scene)
@ -1033,6 +1038,16 @@ void PhysicsWorld::afterSimulation(Node *node, const Mat4& parentToWorldTransfor
afterSimulation(child, nodeToWorldTransform, nodeRotation);
}
void PhysicsWorld::setPostUpdateCallback(const std::function<void()> &callback)
{
_postUpdateCallback = callback;
}
void PhysicsWorld::setPreUpdateCallback(const std::function<void()> &callback)
{
_preUpdateCallback = callback;
}
NS_CC_END
#endif // CC_USE_PHYSICS

View File

@ -310,6 +310,16 @@ public:
*/
void setDebugDrawMask(int mask);
/**
* set the callback which invoked before update of each object in physics world.
*/
void setPreUpdateCallback(const std::function<void()> &callback);
/**
* set the callback which invoked after update of each object in physics world.
*/
void setPostUpdateCallback(const std::function<void()> &callback);
/**
* Get the debug draw mask.
*
@ -394,6 +404,9 @@ protected:
std::vector<PhysicsJoint*> _delayAddJoints;
std::vector<PhysicsJoint*> _delayRemoveJoints;
std::function<void()> _preUpdateCallback;
std::function<void()> _postUpdateCallback;
protected:
PhysicsWorld();
virtual ~PhysicsWorld();