# Help with Billiards Program

## Recommended Posts

Hey guys, I'm creating a billiards program and need some help with some ideas. I'm using an update method that I'm passing several different methods. One of these is a ShootBall() method, and I'm having smoe issues with it. Here is some of what I have so far

double Physics::CheckForCollision(Vector2 a, Vector2 b)
{
double distance;

for(unsigned int i =0; i<14; i++)
{
for(unsigned int j=0; j<16; j++)
{
//get the position of the first ball and second
a.x = gMyGameWorld->GetSpherePositionX(i);
if(a.x < -50)
a.x = -46.5;
if(a.x > 50)
a.x = 46.5;

a.y = gMyGameWorld->GetSpherePositionY(i);
if(a.y < -90)
a.y = -86.5;
else if(a.y >90)
a.y = 86.5;

b.x = gMyGameWorld->GetSpherePositionX(j+1);
if(b.x < -50)
b.x = -46.5;
else if(b.x >50)
b.x = 46.5;

b.y = gMyGameWorld->GetSpherePositionY(j+1);
if(b.y < 90)
b.y = -86.5;
else if(b.y >90)
b.y = 86.5;

distance= sqrt( (b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y));
//distance = sqrt (pow( (b.x - a.x) ) + (pow( (b.y - a.y) ) ) );
//distance= sqrt( (double) (b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y));

}//close j for loop

}//close i for loop
return distance;

}//end method

//will ultimately reverse the angle
//angle of incidence/reflection (maybe???)
double Physics::reverse( double angle )
{
angle += pi;
if ( angle > 2 * pi )
angle -= 2 * pi;
return angle;
}

void ShootBall()
{
double force =.2;
double acceleration;
double mass = .2;
double moveX=0, moveY=0;

acceleration = force/mass;
//I think I'm having issues calculating the moving part, as well as getting
//the ball to stop with correct physics.
//	moveX = GameWorld::GetSpherePositionX(15) + .2*t + (.5*.25*(t*t));
//	moveY = GameWorld::GetSpherePositionY(15) + .2*t + (.5*-9.8*(t*t));

}

static float t =0;
static double distance = 150.0;
static Vector2 c;
static Vector2 d;
static Physics check;

static double force=0;
static double acceleration=0;
static double fricCoEff =0;
static double Fnew =0;
static double Fnorm =0;

t = t + time_elapsed;

//	force = mass * acceleration;

//rigid body collision detection
//X = Xo + Vo*T + 1/2 A*T (squared)
//Dx =Xo + Vox*T + .5Ax *T(squared)
//Dy =Yo + Voy*T + .5Ay *T(squared)

check.CheckForCollision(c,d);

}


The part I'm having issues with is actually animating the balls, and getting them to stop with the collision detection. I've tried using several different methods, but can't seem to figure it out. I want the table to have friction, as well as appear somewhat realistic with the collision detection. Can someone help me with this? Thanks for looking!

##### Share on other sites
I think your code would really benefit from some substantial refactoring, because as it stands it's difficult for me to follow what each part of it is supposed to be doing. Specifically, you appear to be using C++, so why haven't you encoded a ball as its own class? Rather than having this painful GetSpherePositionX/GetSpherePositionY construct you can just GetSphere() and then access a Position property on it.

Also, what's with comments like "angle of incidence/reflection (maybe???)" Do you not know what your own code is supposed to do? Decide what the function is supposed to do and specify that as the comment; if the function does anything else then it's a bug, but at least you can build stuff on top of that function without the uncertainty about what's going on percolating upwards.

I honestly don't see much point in trying to approach the simulation aspects you've mentioned until the structure of the code is sufficiently untangled to allow them to be implemented clearly.

##### Share on other sites
Thanks for looking SuperPig!

The angle of incidence/reflection commented out is something that I will need in the future. I have it commented out right now, as there is no use for it.

The main thing I was looking for was the next step I should take, and how to go about programming the physics of the balls. I have the table set up right now, and all of the ball objects (spheres) created, but don't know how to animate the balls using forces, velocities and acceleration. I want it to look somewhat realistic, but was hoping I could get some advice on where to go next.

I haven't created a ball class yet, what all should go into the class? Also, what is a good site to find the physics equations that I am looking for?

##### Share on other sites
Well, consider the properties of a ball:

class Ball{  Vector2 position;  double  radius;  double  mass;  Vector2 velocity;}

From the sound of it you've already got a 'sphere' class that covers some of this - certainly you shouldn't duplicate work here, consider inheritance, composition, or replacing one of the classes with the other entirely.

For the most accurate collision detection, you should use swept collisions. Your physics update step will look something like this:

1. Calculate the volume swept through by each ball assuming that it just moves in a straight line - no collisions. (This will be a 'capsule' shape). Apply things like table friction here.

2. Test the volumes against each other and against the edges of the table. If none of them intersect, you can just move the balls to their new positions and end the update now.

3. For all the intersections you find between the capsules, check whether the balls actually /did/ collide, and if so, the precise time at which the collision occurred. (Just because two capsules intersect doesn't mean the balls actually collided - they might have just missed).

4. Move all the balls to the positions they would be at that the time of the earliest collision. (This means your two collided balls should now be touching).

5. Update the velocities of the two balls involved in the collision.

6. Run the whole process again, but only from the time of collision to the end of the update step, rather than from the beginning of the update step.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627743
• Total Posts
2978892

• 10
• 10
• 21
• 14
• 14