Hello internet
I’m building a game with balls and walls and paddles (Pong, but crazy). I have coded custom bounce-physics for my paddles and walls. I apply these in OnCollisionEnter2D, by rolling back the ball to the point of contact, then solving the bounce into a new velocity, then move the ball the same length it had moved since the collision, along the new velocity vector. And it all works fine…
…until the ball hits two walls/paddles in the same physics step. OnCollisionEnter(2D) is properly called for both collisions (yay), but I don’t have much info about what happened. I have the circumstances for each collision, like the collision normal, the separation/distance and contact point(s), but I don’t have data for each object, like position and velocity/direction before and after the collision, or how far into the physics step the collision occurred.
Even if I did, I cannot simply roll back the object to the collision point, and do some custom bounce-code for each collision (which is my current solution), because when I change the behaviour/result of the first collision/OnCollisionEnter(2D), then the data for the second collision/OnCollisionEnter(2D) call is made invalid (because the entire physics step has already been carried out). An even then, with only a single collision, my change to the velocity and moving the object, might put the ball inside a wall which it should have collided with.
Is it at all possible to implement a callback function in the middle of the PhysX/Box2D physics step, so I can change e.g. the velocity of an object during the physics step? Can we have an OnCollisionEnterPreSolve2D or something? Or maybe a custom Physics Material 2D, where I can put in some code to solve the collision? Or a better solution from someone wiser than I.
It has only been asked about a few times, but [consistently over the years]( We need a way to detect and resolve collision hits manually page-2).
Exposing some of the constants and functions was a great step, but if we’re stuck with Physic Material and Physics Material 2D for customizing bounce physics, the latter of which only has 2 settings, we’re never going to have fun Pong/Arkanoid games…unless people start coding custom physics engines…and we don’t want that.