|Topic was automatically imported from the old Question2Answer platform.
I’m currently making a Pong clone as a learning project, but I have difficulties with the physics engine, more specifically the collision handling.
In some situations I’d like to handle the collision effect manually. My current setup has a StaticBody2D as a paddle and a RigidBody2D in “Character” mode as the ball. The ball has a custom script set with an _integrate_forces method. There I use the state to query the collision, do some math and set a new linear velocity vector for the ball. However, the velocity change is in effect one frame after the physics engine has computed the new position/velocity of the ball it would have after ricocheting from the paddle.
That gives a visible disturbance when the direction the ball takes from the ricochet differs significantly from the one I set manually. See this animation for an example:
You can identify the frame where the collision takes place by the red dot which indicates the location of the collision. In this very frame the ball has already ricocheted in the direction one would expect. By script in this frame I set a velocity which more or less resembles the direction the ball came from.
My question is, can I identify not only the location of the collision but also the time it took place? For example, let us assume the frame before the collision is t0, the frame where the collision is detected and where my scripts reacts to is t1 = t0 + step, then I’d like to know the time t = t0 + delta with delta <= step (and most of the time delta < step will hold). I need this time to calculate the position where the ball should be at the time t1 using the modified velocity.
I could probably infer this time by calculating the difference vector of the ball’s current (=normal ricochet) position and the collision point and dividing it’s length by the ball’s speed, but is there some builtin way to know this delta?