I had a couple of questions on the collision system I was hoping y’all might be able to answer.
When I use a CollisionHandlerEvent object, the method that handles those events gets a CollisionEntry object that describes the collision. Is there anyway to get a time of collision? (ie, in this case I’m getting multiple collisions in one frame, and I’d like to pick which one is the earliest)
Is there any way to run the physics simulation/collision detection on one object for a set period of time? (I’m looking to move the object back to the pos where it first collided, change the velocity based on the collision, and then run the physics/collision system for the remaining bit of the dt)
Getting the time of the “first” collision is not really possible, since in fact there is no such thing as a “first” collision–there is just a set of intersections that were discovered all at the same time. The collision system has no way of knowing which would would have happened “first” if the objects had moved fluidly to their current position from their position in the previous frame.
But you can ask which collision is closest. If you use the CollisionHandlerQueue to report the complete list of collisions, rather than the event system, you can call queue.sortEntries() to sort them in order from closest point of intersection to furthest.
If you want, you can try to estimate the time of the collision based on the distance traveled to the first point of intersection. Then you can reposition your object to the first point, adjust its velocity as you like, and re-run the physics engine, with an appropriately reduced dt value. To do this for just the one object, you’ll need to have a separate physics manager for each object.
But I’m not sure if all this effort is worth it. Clearly, you are trying to get a more physically accurate simulation out of the physics engine than it provides by default. As it is now, the PhysicsCollisionHandler is designed to respond to all of the detected collisions within a given frame in a sensible, if not precisely accurate, way. If you do all this work, you may improve the accuracy in the case of multiple collisions, but only to a point, since there are other parts of the system that are still designed to simplify some of the calculations to save time, at the cost computing of a strictly accurate result.
If you really need accurate physics, you’ll probably be better off integrating with a third-party physics library.