I'm writing a pool simulation program and have an awful problem with my ball collisions (elastic collisions).

The theory (in terms of vectors) is that when two balls collide you take the line between their centers and the velocity components which are perpendicular to this line are unaffected, while the parallel ones are calculated from the formula:

[tex]

v '_{1} = \frac{2m _{2}v _{2}-v _{1}(m _{2} -m _{1}) }{m _{1}+m _{2} }

[/tex]

[tex]

v '_{2} = \frac{2m _{1}v _{1}-v _{2}(m _{1} -m _{2}) }{m _{1}+m _{2} }

[/tex]

What I do is when I find that the balls are in touch I find a vector which is the line joining the centers. I then find the polar angle (in radians).

I then premultiply the velocity vectors by the (counter-clockwise) rotation matrix:

1st row(cos(angle) sin(angle))

2nd row(-sin(angle) cos(angle))

Then I take the x components (which are now the components parallel to the line joining centers?) And apply the formulas given above. Now I have new x components in the new basis and the old y components in the new basis.

So I rotate everything back and I should get the right velocity components?

The simulation looks ok, it even works fine for a head-on collision. But When I print out the balls velocities I find that the momentum wasn't conserved. Eg. balls of same mass, one ball is initially stationary, other going at 80pix/s:

After collision I find that the velocity magnitudes are 77 and 19

That's way too much.

Can anyone find the error? I'm pretty sure my code if fine, especially because I also calculated it on the calculator and got a meaningless result.