[MATH] Bouncing balls

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

[MATH] Bouncing balls

Post by driesguldolf »

In my game there are a lot of balls bouncing against each other

I've done some research and I've found out that if you want a ball (with an incoming angle = i) to bounce off against a wall (with angle = d) then the outgoing angle of that ball is:
o = 2d - i (the result can be outside the [0°;360°[ range)

But I'm still looking for formulas to bounce two balls (also keep the speed in mind)
This is definately harder then I tought... :(

Any ideas?
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

Wall wouldn't be movable, so its not the same kind of collision. Between to balls bouncing off each other that would be an Elastic Collision. Assuming all balls have the same mass you would only figure out how much energy they impart on each other, which is still a some what similar equation. The wiki link actually has what you need I believe.
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

If bouncing off orthogonal walls, you shouldn't need to calculate the angles. If it's a horizontal wall, invert the velocity's y component, if it's a vertical wall invert the velocity's x component.
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

benryves wrote:If bouncing off orthogonal walls, you shouldn't need to calculate the angles. If it's a horizontal wall, invert the velocity's y component, if it's a vertical wall invert the velocity's x component.
It could work, but I don't represent the speed and angle by a vector:
Angles are used to advance the position, the speed is represented by how many times per second this calculation happens
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

So that would make it Polar Coordinates, which you can make into a vector.

the angle is the angle (lol) and the funny number that indicates speed would be the radius http://en.wikipedia.org/wiki/Polar_coordinate_system
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

Yes but I think that I prefer:

Code: Select all

;IN
; a  -> angle of the wall
; c  -> incoming angle of object
;OUT
; a  -> outgoing angle of object
bouncewall:
   rlca
   sub c
   jp p, {+}
   add a, 64
+  and 63
   ret
This works and is still easy to comprehend 8)
Note: a full circle contains 64 angles
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

driesguldolf wrote:It could work, but I don't represent the speed and angle by a vector:
Angles are used to advance the position, the speed is represented by how many times per second this calculation happens
@King Harold: Ah I see what you mean now (we didn't spend much time on polar coordinates, last time we came across it is during last year I think)
It basically are polar coordinates, but with a difference in the radius part
Instead of doing x=r*cos(theta) and y=r*sin(theta) (this would need 8.8fixedpoint math multiplication) I update the coordinates in different time intervals, wich (I tought) gives me much better results then the multiplication
Altough now I think of it... It might be easier with a radius... I'll check it out
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

It's basically the same thing, but your number is the inverse ( 1/ x) of the radius I think? That would be rather complicated.. It might be easier to add a 8.8 fixed to the location at unvarying intervals (atleast that's how I always do it and it's very easy..)
Anyway, you can do it any way you like of course, but I think normal coordinates might be a bit easier to use.. You would just have to add a Vector2 that is the X and t speeds to a Vector2 that is the position, and bouncing on the walls would just be negating a part of the speed vector.
Although the way you do it doubtlessly works as well..
coelurus
Calc Wizard
Posts: 585
Joined: Sun 19 Dec, 2004 9:02 pm
Location: Sweden
Contact:

Post by coelurus »

Having two balls bounce off of each other is tricky, and if you plan on really implementing it, you _should_ switch to vectors or you could die.
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

They just bounce off an imaginary wall between them right?
Like this?
Image
just mirrored around the normal of the collision line?
Or am I terribly wrong?

edit: I know I suck at drawing..
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

King Harold wrote:They just bounce off an imaginary wall between them right?
Like this?
Image
just mirrored around the normal of the collision line?
Or am I terribly wrong?

edit: I know I suck at drawing..
Its not a bounce off an imaginary wall, its a transfer of energy.
For example

Code: Select all

  |
  |
  v
  o o<---
One ball never collides with this imaginary wall, the wall collides with it. In this case the ball on the left would end up going down left while the one on the right would lose its energy.


In other words you'll need vectors really, but don't go throwing away the polar math. You'll still need it.
Image
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

@Jim e and King Harold: That is exactly what I found BUT... This is only true of both balls go both in the direction of the wall... And sadly these collisions aren't the only ones
This can also happen:

Image - Image - Image

.0 Both balls have the same speed
.1 This one hits the imag wall and bounces of that wall
.2 This one DOES NOT hit the wall and I believe it is calculated by adding 2 vectors .1 and .2
.3 The imag wall (calculated by some inverted tangens and stuff...)
.4 This ball simply bounces of the wall, use the formula stated in the first post
.5 Because both balls have the same speed simply find the average of the 2 angles
Is this correct? And are this all the possible cases? I assume that at least one object has to go in the direction of the wall (2 objects flying away from each other can't possibly hit can they?)

But on a second tought... Can this happen if all the balls go at the same speed? It definately can if they have variable speeds
Wich brings another problem... In the site stated in Jim e's post (wiki) says that in a one dimensional area they just exchange speeds (they have the same mass), too bad this isn't true in 2 dimensionals. And I can't understand the part where they explain how the speed changes in these cases
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

driesguldolf wrote:@Jim e and King Harold: That is exactly what I found BUT... This is only true of both balls go both in the direction of the wall... And sadly these collisions aren't the only ones
What did I say?
in the site stated in Jim e's post (wiki) says that in a one dimensional area they just exchange speeds (they have the same mass), too bad this isn't true in 2 dimensionals.
Funny thing about 2d is that its just two 1 dimensions. Just apply the x and y vectors separate from each other.

Here's an example source. think you need the allegro.dll to run the exe.
http://jim.revsoft.org/collide.zip

close enough for what your doing.
Image
coelurus
Calc Wizard
Posts: 585
Joined: Sun 19 Dec, 2004 9:02 pm
Location: Sweden
Contact:

Post by coelurus »

The safest way to tackle this problem by now is for you to read up on elastic collisions (which is pretty easy) and then scribble down all the relationships you need (which is not hard but a bit painful).

Elastic collisions are very easy in 1D. Just make use of the conservation laws and solve for the object velocities, cake peace. Try this first and get it right, have a couple of pixels or something in a row and shoot another pixel on them.

The 2D problem becomes very simple if you apply exactly the same mathematics described above. Of course, you'll have to do a little extra before and after to setup the algorithm, but that's just some vector decomposition. The idea is you create a virtual wall between the two balls, but don't ever think of it as a physical wall! (I should call it a reference frame.) Now, project the linear momentum vectors of the balls onto this wall and onto the vector between the ball centers. Holey moley, you now got a 1D problem in the direction of the centroid-vector! Do the collision along this vector as with your pixels which you should have working, then recreate the lin. mom. vectors for the two balls by adding up the "new" lin. moms. from the collision with the vectors projected onto the "virtual wall".

That's it.
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Why are we pretending they are Perfect Elastic collisions? Such a collision does not exist.. Forexample, as soon as you can hear something bounce it isn't perfectly-elastical anymore, you lost some sound
So maybe give those balls a little fraction to compensate? (something like decreasing the speed every frame - towards zero so watch out with 2's complement)
And no matter how you call it (energy transfer or otherwise) you'll need that virtual wall which is the tangent at the shared point of the balls (yes they share a point - the point is smaller than a pixel though)
.2 This one DOES NOT hit the wall and I believe it is calculated by adding 2 vectors .1 and .2
Yes it does, otherwise the balls wouldn't hit eachother at all.
Post Reply