Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
7  
If you want to practice Java coding, wouldn't it make sense for you to implement it yourself? –  DeadMG Jan 29 '11 at 21:19
2  
I'm with DeadMG. If you want to solve it yourself, though, you are probably going to need some knowledge of vectors. Luckily, someone posted a great walkthrough on linear mathematics elsewhere on this site just the other day. –  doppelgreener Jan 30 '11 at 4:54
2  
I don't see how (doing it yourself) implementing a physics algorithm will really help you practice Java. –  The Communist Duck Jan 30 '11 at 15:33
    
maybe this site can help you archive.ncsa.illinois.edu/Classes/MATH198/townsend/… –  user13479 Feb 14 '12 at 18:10

3 Answers 3

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

share|improve this answer
1  
Which of these are Java physics engines though? –  Ricket Jan 30 '11 at 16:34
    
There are Java ports of, or at least bindings for Box2D, Chipmunk, Bullet, and ODE –  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.

share|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
    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: http://freespace.virgin.net/hugo.elias/models/m_snokr.htm, but I remember that the impulse multiplication was incorrect there.

share|improve this answer
    
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? –  Poldie Jul 20 at 12:50
    
@Poldie If the dot is negative, the balls are already moving away from each other. No need to handle collision in that case. –  msell Jul 20 at 15:22
    
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). –  Poldie Jul 20 at 18:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.