Search code examples
game-physicsphysics

Finding the resulting X and Y velocities of a ball after collision with a line


I am trying to build a physics engine for a game (and I would prefer to avoid using a physics engine such as box2d) and I've come across an issue with resolving collisions between balls and angled lines (detecting them is fine). Effectively I'm looking for a formula to find the resulting X and Y velocities of a ball after a collision with an angled line while disregarding any rotation / angular momentum.

Here's a link to a visual that represents my issue


Solution

  • If we express the before and after velocities in terms of vectors, you can use a vector reflection formula:

    v2 = v1 − 2n(v1 ⋅ n)

    v1 = before reflection vector, v2 = after reflection vector, n = normal line of wall (must be normalized to unit vector), v1 ⋅ n is the dot product of v1 and n