Pong – Ball and Wall Collision
This collision algorithm “assume” that the wall is infinite, and is defined by a point q and a normal n. The ball is defined by a point p and a radius r.
In this time step the ball is moving along the ds vector. The point of impact is at the “time” x along that vector. So in order to find the x value, we will first find the d vector, which is defined from the center of the ball p to the point qn.
Then we will find the green and the purple vector. The green vector is defined from the center of the ball p to the “time of impact” xds. And the purple vector is defined from the center of the ball p to the point qn. Both the vectors are in the same direction as the normal of the wall.
x, by putting the green vector and the purple vector equals each other into an equation.
x <ds, n> n = <d, n> n
- If x is in (0, 1], this would be the “time of impact”.
- If x < 0, then the ball is moving away from the wall.
- If x > 1, then not a collision within this time step.
- If the <ds,n> is equals approximately to zero, then the ball is moving parallel with the wall.