Collision Detection

Hi guys,
I am trying to develop a billiards game in Java.
I want to know how to detect ball-2-ball collision so pls help me ?

thank you.

A brute force check every ball against every ball is your best bet. You don’t have many balls and sphere tests are pretty easy. Once you have a collision things get a little more tricky since you need to decide how to handle the fact that the billiard ball have penetrated each other a little bit.

For accurate physics you would predict where and when the next collision will happen. Then move to that time point, calculate the collision dynamics, rinse and repeat. However this is quite complicated mathematically and most folks won’t care about accurate that much.

This article has been on Gamasutra for 11 years.

swept sphere-sphere collision detection.

I implemented it some years ago, and can confirm it works as intended.

Sorry to derail; ‘brute force’ collision detection, is this just checking every item against every other for a collision?

Yes. A nested for loop.

for (int i = 0; i < length; i++) { for (int j = i+1; j < length; j++) {checkCollision(i,j)}};