Advertisement

Physics behind Billiard.....

Started by December 03, 2002 07:13 AM
7 comments, last by glnefugio 22 years, 2 months ago
Hi, Do you have an idea on how to calculate the velocity and the angle on wich to colliding balls leave after it? I would like to use this for a billiard game. Everything is nearly finished but that. I searched the internet but haven''t found something usefull. Any idea or formular? thanks PS: Don''t know if it was asked before, first time in this forum
Here''s the code I use in my game (a 2D shooter):


  vect2_t d;v2_dir(&ship1->position, &ship2->position, &d);vect2_t dv = ship1->velocity;v2_sub(&dv, &ship2->velocity);// calculating the impulse as a projection of// ship velocities on the line going through their centres// At least this is what I think I''m doingfloat k = v2_dot(&d, &dv);// projecting it backvect2_t r = d;v2_mul(&r, k);// Adding the impulse (mass = 1, so forget it) to the ship velocitiesv2_sub(&ship1->velocity, &r);v2_add(&ship2->velocity, &r);  


Ships are defined as


  struct ship_t{	vect2_t position;	vect2_t velocity;// and so on};  


where vect2_t is a 2D vector


  struct vect2_t{	float x;	float y;};  


and vector operations used are


  // ugly, but simplevoid v2_add(vect2_t *v1, vect2_t *v2){	v1->x += v2->x;	v1->y += v2->y;}void v2_sub(vect2_t *v1, vect2_t *v2){	v1->x -= v2->x;	v1->y -= v2->y;}void v2_mul(vect2_t *v, float a){	v->x *= a;	v->y *= a;}// make unit vector showing the direction from v1 to v2void v2_dir(vect2_t *v1, vect2_t *v2, vect2_t *d){	float dx = v2->x - v1->x;	float dy = v2->y - v1->y;	float l = sqrt(dx * dx + dy * dy);	d->x = dx / l;	d->y = dy / l;}// dot productfloat v2_dot(vect2_t *v1, vect2_t *v2){	return v1->x * v2->x + v1->y * v2->y;}  


It all will work only if the masses of both objects are equal.
Advertisement
You could use Baraff's paper (use Google), but it covers a lot more than what you want...

Do you know about linear momentum? If you don't, you should Google some info before tackling this problem. The (simplified) equation for collision between spheres is

j = -(1 + coef) * DotProduct(sphere2.speed - sphere1.speed, Normal) / Length(Normal) / (1 / sphere1.mass + 1 / sphere2.mass)

where
coef is the restitution coefficient (user-defined), between 0 and 1
Normal is the vector defined as sphere2.center - sphere1.center

then,

sphere1.speed += j / sphere1.mass * Normal / Length(Normal)
sphere2.speed -= j / sphere2.mass * Normal / Length(Normal)

Very roughly speaking.

Cédric

[edited by - cedricl on December 3, 2002 2:59:36 PM]
And here''s the equations I used in my code:


Initial data:

v1x, v1y - x and y components of first ball velocity
v2x, v2y - x and y components of second ball velocity

x1, y1 - coordinates of first ball
x2, y2 - coordinates of second ball

m1 - mass of first ball
m2 - mass of second ball

Calculations:

dx = x2 - x1
dy = y2 - y1

rx = dx / (sqrt(dx * dx + dy * dy))
ry = dy / (sqrt(dx * dx + dy * dy))

k = (rx * (v2x - v1x) + ry * (v2y - v1y)) / (m1 + m2)

Updating velocities:

v1x += rx * m2 * k;
v1y += ry * m2 * k;
v2x -= rx * m1 * k;
v2y -= ry * m1 * k;


Actually it appears to be equivalent to equations posted by cedricl, so you can choose either (there''s no restitution coefficient in mine).
Slight correction - in think there should be

k = 2 * (rx * (v2x - v1x) + ry * (v2y - v1y)) / (m1 + m2)

instead of

k = (rx * (v2x - v1x) + ry * (v2y - v1y)) / (m1 + m2)
I''m surprised you didn''t find any useful information in your search, but anyway, have a look at Pool Hall Lessons:
Fast, Accurate Collision Detection between Circles or Spheres
over on gamasutra.

Hope that helps,

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Advertisement
How much do the balls get faster/slower as before?
collisions are elastic... no speed is lost... granted ball a hits ball b and stops...

If barbie is so popular, why do you have to buy her friends?
If barbie is so popular, why do you have to buy her friends?
quote:
Original post by llama_beast
collisions are elastic... no speed is lost... granted ball a hits ball b and stops...


I think he means how much does each one increase/decrease by. Because in elastic collisions, momentum and energy in the system does not change, but the speed, momentum and energy(kinetic) of each ball does change.

This topic is closed to new replies.

Advertisement