Ball bounce physics
In this article I am discussing a possible aproach on how to implement a collision detection and bounce beaviour of a 2D circular moving object on other static objects.
You will also find interactive examples and a link to the source code.
The problem
Imagine you are writing a simulation or a game that involves some kind of ball physics, like Pinball or Pachinko.
I'm assuming that the simulation involves a cycle of update of the ball position depending on velocity and position of the previous state (like a Verlet integration).
So you have a ball object that on a certain moment t_n is located in A and on t_(n+1) is in B.
What may happen is that at a certain point the next state of the ball is going to collide with another object (like a peg for example).
In this case we will consider a collision with a static ball (i.e. that won't move) centered in C.
At this point we might want to find a new not colliding state for our moving ball by detecting the collision and calculating a bounce behaviour.
This means we want to find the new position for the ball and new direction away from the object that caused the collision.
So how do we do it?
The simplified problem
First of all. How do we find out if the next state is colliding?
That's pretty simple. We can just see of the distance between the center of our ball in the next state is colliding with the static object.
If the distance is less than the sum of the radii then it's colliding, otherwise it is not.
Now that we have discovered a collision how do we implement a bounce effect?
That might sound complicated having two circular objects. You may say that the moving ball should stop at the point it touches the other ball, then bounce back. But how do we find where it should stop? Let's try a simpler aproach.
We might notice that the collision detection is the same that we would obtain by calculating the moving effect of a single point colliding with a ball of radius equal to the sum of the radii of the two balls.
NOTE
By using this collision detection algorythm we will not detect a collision in case the line segment AB (trajectory of our ball) intersects the the colliding object and B is not in a colliding position. We are just going to ignore this case, but you might use a different aproach if you want to avoid this problem by calculating the interseciton point between the segment and the ball (that might be more demanding in terms of total computations).
Likely the point of collision is where the segment AB intersects the ball cenetered in C with radius equal to the sum of the radii.
Concolusion
At this point we just need to calculate the reflection of the segment P1B on the tangent line of the ball on P1.
That means that we need to find the symmetric point of B on the tangent line.
And so we found B1.
We can also calculate the new direction for the ball. That is B1 - P1.
Source code and more
Source code of the examples can be found here 👉 github.com/crcarlo/ball-bounce-physics.
For any questions or suggestions you can open an issue here 👉 github.com/crcarlo/ball-bounce-physics/issues