For a simple pool game where spin is not modeled the algorithm is quite simple.
- To check if a collision happens, check if the distance between the balls is smaller than the sum of their radius.
- Calculate the normal of the impact
- Calculate the impact force based on the speed difference, normal, impact coefficient and masses
- Apply the impact force to both balls
In pseudo code this becomes:
vector difference = ball2.position - ball1.position
float distance = sqrt(difference)
if (distance < ball1.radius + ball2.radius) {
vector normal = difference / distance
//vector velocityDelta = ball2.velocity - ball1.velocity
vector velocityDelta = ball1.velocity - ball2.velocity
float dot = dotProduct(velocityDelta, normal)
if (dot > 0) {
float coefficient = 0.5
float impulseStrength = (1 + coefficient) * dot * (1 / ball1.mass + 1 / ball2.mass)
vector impulse = impulseStrength * normal
ball1.velocity -= impulse / ball1.mass
ball2.velocity += impulse / ball2.mass
}
}
You can omit the mass from the algorithm if all balls have same mass and also assume constant radius for all balls for a pool game, but the code will be more useful for you without those simplifications.
The code is based on this tutorial, but I remember that the impulse multiplication was incorrect there.