Search code examples
collision-detectionpygame

Billiards game collision


I've choice to write a billiard game for class. All runs ok but I would like to improve realism to ball collisions.

For each ball I have a pygame rectangle, that means, ball center, ball diameter and position in game. And also a velocity vector (u_x, u_y).

At this time I check collision with colliderect, but I can change it measuring center balls distances.

I have read about Conservation of momentum equations but this not have ball positions in consideration. Is not the same two ball frontal impact than a lateral impact.

Someone can help to me in formula for new ball velocities after collision considering ball position at the impact moment. Thanks a lot!


Solution

  • The module euclid will help. It contains a vector class, that has normal(), dot(), etc... http://partiallydisassembled.net/euclid.html ( You can copy the module into your project folder, without needing to install. )

    For the formulas, check : https://gamedev.stackexchange.com/a/7901/6776 , or http://archive.ncsa.illinois.edu/Classes/MATH198/townsend/math.html#COLLISION

    Note: the more complicated your physics become, the more you need to use a physics engine, such as [enter link description here][pymunk] or ODE. But, it is a good learning experience.