Closed fullptr closed 4 years ago
Thanks for your feedback.
I am not sure see how can I implement something like this more efficiently than your solution. In the physics engine, all the bodies that are not sleeping are updated. I would then on my side have to iterate over all the bodies (except those that were sleeping in the previous frame and still sleeping in the current one) to call the callback which won't be more efficient than if you do it on your side.
The main gain I get here is that the physics engine is already looping through all the rigid bodies as part of DynamicsWorld::update, so this allowed me to hook into that instead of having a second pass over the objects. This also resulted in fewer updates as the change here does not call the callback for sleeping bodies, whereas previously I just had to do the logic for every object.
I guess the main concern here is the performance hit in making an extra virtual function call for every awake body each frame, perhaps I could put it behind a compiler flag so it could be excluded if not needed?
See https://github.com/DanielChappuis/reactphysics3d/pull/147:
In my current project, after updating the physics engine, I need to update my own structures with the positions, orientations and velocities of rigid bodies. My initial approach was just to loop through all of my structures after advancing the physics engine and copying out the data that I needed, which was inefficient.
A nicer approach would be to have a callback available that gets called whenever a RigidBody gets updated. I can then implement this function in my EventListener to get out all the information I need. This approach also benefits from sleeping bodies not calling the callback.