Collisions between pool balls
Hey guys
I''m making a pool game and the maths for collisons seems to be really hard to get working. My code works for conserving horizontal and vertical momentum, and the angles look good, but it somehow gains total speed.
x1 and y1 are coordinates, a1 is the angle and s1 is the speed. It "works" by finding a line connecting the centres, and assuming there''s no force perpendicular to it and momentum along it is conserved. I''m either doing this the wrong way or I''m missing something obvious.
Code is in Basic, but hopefully it''s fairly obvious what it does.
SUB impact (x1, y1, x2, y2, s1, a1, s2, a2)
''angle of connecting line
theta = (ATN((y2 - y1) / (x2 - x1 + .0000001)))
''angle of velocities to it
alpha = ABS(theta - a1)
beta = ABS(theta - a2) - pi
''components of velocity perpendicular to connecting line
perp1 = s1 * SIN(alpha)
perp2 = s2 * SIN(beta)
''swap components of velocity parallel to connecting line
v2 = s1 * COS(alpha)
v1 = s2 * COS(beta)
dx1 = -perp1 * SIN(theta) + v1 * COS(theta)
dy1 = perp1 * COS(theta) + v1 * SIN(theta)
dx2 = perp2 * SIN(theta) + v2 * COS(theta)
dy2 = perp2 * COS(theta) + v2 * SIN(theta)
''calculate new velocities
s1 = SQR(dx1 ^ 2 + dy1 ^ 2)
s2 = SQR(dx2 ^ 2 + dy2 ^ 2)
a1 = ATN(dy1 / (dx1 + .000001))
a2 = ATN(dy2 / (dx2 + .000001))
END SUB
Thanks in advance
I haven't checked your code but i just have a suggestion...
instead of keeping track of velocity using an angle and magnitude, it will be probably be easier to use a 2D vector to do it... that will make your collisions much easier to handle because you can treat x and y seperatly most of the time, thus avoiding the complications of messing around with angles...
secondly its pretty messy to do this:
theta = (ATN((y2 - y1) / (x2 - x1 + .0000001)))
what if (x2 - x1) == -0.0000001, your still gonna end up with a 0 on the denominator... you should do this instead:
also you should note that arctan of a number will only return a number between -90 and 90 degrees... you need to add in some extra code in to determine what quadrant the angle lies in to get an angle between -180 and 180 degrees...
[edited by - SpaceDude on October 23, 2003 3:59:04 PM]
instead of keeping track of velocity using an angle and magnitude, it will be probably be easier to use a 2D vector to do it... that will make your collisions much easier to handle because you can treat x and y seperatly most of the time, thus avoiding the complications of messing around with angles...
secondly its pretty messy to do this:
theta = (ATN((y2 - y1) / (x2 - x1 + .0000001)))
what if (x2 - x1) == -0.0000001, your still gonna end up with a 0 on the denominator... you should do this instead:
if (x2 - x1) theta = (ATN((y2 - y1) / (x2 - x1)))else theta = PI/2 // (or 90 if your working in degrees)
also you should note that arctan of a number will only return a number between -90 and 90 degrees... you need to add in some extra code in to determine what quadrant the angle lies in to get an angle between -180 and 180 degrees...
[edited by - SpaceDude on October 23, 2003 3:59:04 PM]
There is a very good article on the math/physics of billiards over at www.gamasutra.com:
http://www.gamasutra.com/features/20020118/vandenhuevel_01.htm
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
http://www.gamasutra.com/features/20020118/vandenhuevel_01.htm
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
We had to do this for class once. The simplest solution was to project the velocity/momentum vector along the axes defined by the line between centers and the perpendicular. Then the momentum in the perpendicular remains the same and the momentum in the opposite is reflected, with some absorption of energy.
Tom
Tom
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement