Two spheres colliding
June 10, 2007 2:42 PM Subscribe
Help me with the math of two spheres colliding
I'm making a flash game, where I want two circular objects to collide in a half-way realistic fashion, like pool-balls.
As I understand it, when two equal-mass balls collide, assuming a perfectly elastic collision, then the two balls will switch momentum along an axis formed by their relative positions.
So what I have for variables is:
x1 y1 : the position of ball 1
x2 y2 : the position of ball 2
sx1 sy1 : the velocity of ball 1
sx2 sy2 : the velocity of ball 2
I want to be able to calculate what sx1 and sy1 should be after a collision. (And likewise for sx2 and sy2, but it would be the same method for both balls)
So, by subtracting x1 and y1 from x2 and y2, I get
tx ty : the position of ball 2, relative to ball 1
I need to represent sx1 and sx2 with two vectors, one parallel to [tx,ty] and one perpendicular.
sx1 = tx*A + ty*B
sy1 = ty*A - tx*B
A is the parallel and B is the perpendicular.
If I can solve for A and B, then I can replace the A from ball1 with the A from ball2, and vice-versa, and my new values would be:
sx1 = tx*A2 + ty*B1
sy1 = ty*A2 - tx*B1
Is this a good approach?
Plus it's been a few years since I've done serious math, so I don't remember all the tricks to isolate A and B. So far I've got this far:
sx1=tx*A - ty*sy1/tx + ty^2*A/tx
I'm making a flash game, where I want two circular objects to collide in a half-way realistic fashion, like pool-balls.
As I understand it, when two equal-mass balls collide, assuming a perfectly elastic collision, then the two balls will switch momentum along an axis formed by their relative positions.
So what I have for variables is:
x1 y1 : the position of ball 1
x2 y2 : the position of ball 2
sx1 sy1 : the velocity of ball 1
sx2 sy2 : the velocity of ball 2
I want to be able to calculate what sx1 and sy1 should be after a collision. (And likewise for sx2 and sy2, but it would be the same method for both balls)
So, by subtracting x1 and y1 from x2 and y2, I get
tx ty : the position of ball 2, relative to ball 1
I need to represent sx1 and sx2 with two vectors, one parallel to [tx,ty] and one perpendicular.
sx1 = tx*A + ty*B
sy1 = ty*A - tx*B
A is the parallel and B is the perpendicular.
If I can solve for A and B, then I can replace the A from ball1 with the A from ball2, and vice-versa, and my new values would be:
sx1 = tx*A2 + ty*B1
sy1 = ty*A2 - tx*B1
Is this a good approach?
Plus it's been a few years since I've done serious math, so I don't remember all the tricks to isolate A and B. So far I've got this far:
sx1=tx*A - ty*sy1/tx + ty^2*A/tx
As near as I can tell without working through your math, this looks right - you're basing it on conservation of linear momentum.
Real pool-balls have spin which alters their course and their behavior when striking other balls, and they have sliding friction over the tabletop as they start moving, which gradually changes to rolling friction as the ball gets moving and the speed drops. They also do not behave as point masses; because of their radii one ball can hit another 'on-center' (velocity vector is collinear with the center of the target ball, i.e. vector B above is zero), or 'off-center' (i.e., vector B above is not real.) Because of the radii of the ball you have to model how far off-center the target is hit in order to discover the direction of its resultant velocity vector.
If you want these to behave as point-masses in a two-dimensional vacuum, your approach looks good; if you really want them to behave as pool balls, you have to take the above into account as well.
posted by ikkyu2 at 4:48 PM on June 10, 2007
Real pool-balls have spin which alters their course and their behavior when striking other balls, and they have sliding friction over the tabletop as they start moving, which gradually changes to rolling friction as the ball gets moving and the speed drops. They also do not behave as point masses; because of their radii one ball can hit another 'on-center' (velocity vector is collinear with the center of the target ball, i.e. vector B above is zero), or 'off-center' (i.e., vector B above is not real.) Because of the radii of the ball you have to model how far off-center the target is hit in order to discover the direction of its resultant velocity vector.
If you want these to behave as point-masses in a two-dimensional vacuum, your approach looks good; if you really want them to behave as pool balls, you have to take the above into account as well.
posted by ikkyu2 at 4:48 PM on June 10, 2007
Response by poster: thinman: Those spheres seem to be transferring [i]all[/i] of their momentum, X and Y equally, regardless of whether they receive a glancing blow or a dead-on hit. Is this the "before" example that I'm supposed to fix up? I think it gives me an idea for another way I could approach it, though.
ikkyu2: The on-centre/off-centre distinction is what I'm focusing on, for now.
posted by RobotHero at 6:22 PM on June 10, 2007
ikkyu2: The on-centre/off-centre distinction is what I'm focusing on, for now.
posted by RobotHero at 6:22 PM on June 10, 2007
Response by poster: Okay, there was a very simple change necessary to get thinman's example to work, and now it does exactly what I was aiming for.
I'm sure the lesson in the book covers the tiny change.
posted by RobotHero at 6:31 PM on June 10, 2007
I'm sure the lesson in the book covers the tiny change.
posted by RobotHero at 6:31 PM on June 10, 2007
An easier way might be to switch frames of reference to the center of momentum frame. Since the balls are of equal masses, this corresponds to going to the frame in which the sum of the velocities are zero (that is, they are heading directly at one another with the same velocity).
To find this frame, add the x and y-components of the individual ball velocities independently and divide by two. Give your transformation vector, call it dv, these components. Then you have, in the new frame:
v1' = v1-dv
v2' = v2-dv
You can check that v1'+v2'=0, so this is the center of momentum frame (again, for equal mass balls only). In this frame, after the collision, v1'->-v1', v2'->-v2'.
v1f' = -v1' = -v1+dv
v2f' = -v2' = -v2+dv
Now, to go back, you undo the transformation (instead of -dv, it's now +dv):
v1f = v1f'+dv = -v1+2dv
v2f = v2f'+dv= -v2+2dv
You can explicitly check that the condition for a perfectly elastic collision is obeyed:
v1f^2+v2f^2=v1^2+v2^2
Simple, yeah? You never have to worry about the relative positions of the balls.
posted by dsword at 7:33 AM on June 11, 2007
To find this frame, add the x and y-components of the individual ball velocities independently and divide by two. Give your transformation vector, call it dv, these components. Then you have, in the new frame:
v1' = v1-dv
v2' = v2-dv
You can check that v1'+v2'=0, so this is the center of momentum frame (again, for equal mass balls only). In this frame, after the collision, v1'->-v1', v2'->-v2'.
v1f' = -v1' = -v1+dv
v2f' = -v2' = -v2+dv
Now, to go back, you undo the transformation (instead of -dv, it's now +dv):
v1f = v1f'+dv = -v1+2dv
v2f = v2f'+dv= -v2+2dv
You can explicitly check that the condition for a perfectly elastic collision is obeyed:
v1f^2+v2f^2=v1^2+v2^2
Simple, yeah? You never have to worry about the relative positions of the balls.
posted by dsword at 7:33 AM on June 11, 2007
Sorry, "same velocity" should read, "same speed." Not to be pedantic or anything.
posted by dsword at 7:34 AM on June 11, 2007
posted by dsword at 7:34 AM on June 11, 2007
Sorry again. I should read more closely before posting.
What I said above works for point-like balls. If you're giving them some size, then they can still have glancing hits in the cms frame. Taking that into account isn't too difficult, it just requires a couple more steps. I'll try to post a follow-up in a bit. In any case, the above reduces the problem to two balls with the same speed glancing off each other, which is much simpler.
posted by dsword at 8:04 AM on June 11, 2007
What I said above works for point-like balls. If you're giving them some size, then they can still have glancing hits in the cms frame. Taking that into account isn't too difficult, it just requires a couple more steps. I'll try to post a follow-up in a bit. In any case, the above reduces the problem to two balls with the same speed glancing off each other, which is much simpler.
posted by dsword at 8:04 AM on June 11, 2007
Best answer: Ok, now to follow up to take size into account. This will hopefully actually answer your original question, which I again apologize for not reading carefully.
Transform to the CMS as before. Let v' be the velocity of either ball in this frame, and r1, r2 be their two-component coordinate vectors. Also, define a displacement vector r=r2-r1, where r2 and r1 are the locations of the respective balls. From here on, if I follow a vector with x, it means to take the x-component, e.g. rx, and likewise for y. Note that the magnitude of r should just be the diameter of a ball, D.
Now consider the cosine and sine of the angle r makes with the x-axis. These are given by:
ct = rx/D
st = ry/D
Transform v' in the following way to form a new vector w:
v'x->ct*v'x+st*v'y = wx
v'y->-st*v'y+ct*v'x = wy
In this way, wx is the component of velocity parallel to the vector connecting the balls, and wy is the component perpendicular. Since we're still in the CMS, just flip the sign of wx to take into account the collision. Then, transform back to the unrotated CMS.
wx->ct*wx-st*wy = w'x
wy->st*wx+ct*wy = w'y
This gives the new velocity of the ball in the CMS. The other ball's velocity is -w'. So, finally, boost back to the original frame of reference as before, by adding dv to each vector.
In summary:
1) Go to the CMS to simplify calculations.
2) The components parallel and perpendicular to the displacement of the balls are given by sines and cosines, which are simple in this case.
3) Switch the parallel component.
4) Boost back.
Part 2 gives the answer to your specific question, and you may be able to get away with only looking at this (and not boosting to the CMS). The whole recipe as described above, however, is easily extended to take into account balls of different masses and sizes.
One more note--a common problem with these things can involve the sign of the angle that you're using, so watch out for it getting you into trouble. Hope this helps.
posted by dsword at 9:26 AM on June 11, 2007
Transform to the CMS as before. Let v' be the velocity of either ball in this frame, and r1, r2 be their two-component coordinate vectors. Also, define a displacement vector r=r2-r1, where r2 and r1 are the locations of the respective balls. From here on, if I follow a vector with x, it means to take the x-component, e.g. rx, and likewise for y. Note that the magnitude of r should just be the diameter of a ball, D.
Now consider the cosine and sine of the angle r makes with the x-axis. These are given by:
ct = rx/D
st = ry/D
Transform v' in the following way to form a new vector w:
v'x->ct*v'x+st*v'y = wx
v'y->-st*v'y+ct*v'x = wy
In this way, wx is the component of velocity parallel to the vector connecting the balls, and wy is the component perpendicular. Since we're still in the CMS, just flip the sign of wx to take into account the collision. Then, transform back to the unrotated CMS.
wx->ct*wx-st*wy = w'x
wy->st*wx+ct*wy = w'y
This gives the new velocity of the ball in the CMS. The other ball's velocity is -w'. So, finally, boost back to the original frame of reference as before, by adding dv to each vector.
In summary:
1) Go to the CMS to simplify calculations.
2) The components parallel and perpendicular to the displacement of the balls are given by sines and cosines, which are simple in this case.
3) Switch the parallel component.
4) Boost back.
Part 2 gives the answer to your specific question, and you may be able to get away with only looking at this (and not boosting to the CMS). The whole recipe as described above, however, is easily extended to take into account balls of different masses and sizes.
One more note--a common problem with these things can involve the sign of the angle that you're using, so watch out for it getting you into trouble. Hope this helps.
posted by dsword at 9:26 AM on June 11, 2007
This thread is closed to new comments.
You can download the example files for the book here. Specifically, you want file ch19_13.fla. That file demonstrates collisions between same-mass objects.
Good luck.
posted by thinman at 3:16 PM on June 10, 2007