I am looking for algorithm to calculate direction and speed of balls in a pool game. I am sure there has to be some type of open source code for this since pool games are some of the oldest computer games I can remember.

I mean, when one ball hits another, I need a algorithm to calculate direction of both of them. It will depend of exact angle of where they hit each other and on speed.

I want to practice Java coding, so I am looking for java code or package that has this type of code.


Whilst basic sphere-sphere collision detection/response is quite simple, doing it accurately enough for a good pool simulation would be more tricky, as you'd have to deal with spin.

Are you aware of the existence of physics engines? Some popular examples are these (and they can do a lot more than just pool ball collisions). Probably a good choice for making a pool game, but not so much for learning Java...

In 2D

Box2D: http://www.box2d.org

Chipmunk: http://code.google.com/p/chipmunk-physics/

In 3D

Bullet: http://bulletphysics.org/

ODE: http://www.ode.org

If you were making a big-budget commercial game:

Havok: http://www.havok.com

| improve this answer | |
  • 1
    \$\begingroup\$ Which of these are Java physics engines though? \$\endgroup\$ – Ricket Jan 30 '11 at 16:34
  • \$\begingroup\$ There are Java ports of, or at least bindings for Box2D, Chipmunk, Bullet, and ODE \$\endgroup\$ – bluescrn Jan 30 '11 at 20:47

You may be interested in the article "Pool Hall Lessons: Fast, Accurate Collision Detection Between Circles or Spheres" if you choose to go the "roll your own" route. It's not Java-specific, but does discuss some of the algorithms involved for a simple simulation.

| improve this answer | |

For a simple pool game where spin is not modeled the algorithm is quite simple.

  1. To check if a collision happens, check if the distance between the balls is smaller than the sum of their radius.
  2. Calculate the normal of the impact
  3. Calculate the impact force based on the speed difference, normal, impact coefficient and masses
  4. 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.

| improve this answer | |
  • \$\begingroup\$ What about if dot is less than zero? I've been investigating this pseudocode (and the one you linked too, but the other one ends up with my trying to take the square of a negative number - perhaps that's the problem you identified with it). Surely you want to produce a result with every set of input positions and velocities? \$\endgroup\$ – user5196 Jul 20 '14 at 12:50
  • \$\begingroup\$ @Poldie If the dot is negative, the balls are already moving away from each other. No need to handle collision in that case. \$\endgroup\$ – msell Jul 20 '14 at 15:22
  • \$\begingroup\$ I've just knocked up my version of your code here: ideone.com/DhsAoW and I'm getting -0.707 for a positions of 110,90 and 100,100, and velocities of 0,2 and 0,-3. This is a more or less head-on collision. (Assume the initial radius-based collision detection check has already occurred). \$\endgroup\$ – user5196 Jul 20 '14 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy