Archive: Bouncey Bouncey


20th July 2003 03:18 UTC

Bouncey Bouncey
AVS collisions ;). Just for fun.

If the balls start inside of eachother, weird behaviour will occur. Just restart and try again...
It can theoretically be extended up to N balls, though it requires a lot of copy-pasting, because it simply handles all the possible one-on-one collisions. For n balls, there are:

     n!     
(n-2)! * 2!

possible one-on-one collisions.


20th July 2003 03:28 UTC

Good, you didn't beat me to 3D collisions.:p

I like how yours have gravity - that's nice. I still like my 2D collisions more though.:p

Bug: Circles aren't complete - there's a little hole in the bottom right of each circle. Probably because you used one SSC to draw all 3 balls.

I'm going to post mine. Time for the faceoff.:p


20th July 2003 03:44 UTC

Gravity is just adding a constant vector to the speeds every tick. In any case, 3D is exactly the same, except more typing ;). The vectorial formula is:

P1,P2 - Positions
V1,V2 - Speeds before collision
V1',V2' - Speeds after collision

ΔV = (P2-P1).(V2-V1) * (P2-P1)
          |(P2-P1)|²

(. = dot product, * = scalar*vector)

V1' = V1 + ΔV
V2' = V2 - ΔV

Oh and as far as the circles go, it's AVS' buggy drawing routines actually. If you set green=1 or something, you'll notice that the circles are 'connected' at their rightmost points, not at the -45° location.


20th July 2003 03:48 UTC

/me goes off to work on 3D collisions again.


20th July 2003 03:50 UTC

I hate it when people reply before I'm doing editing my post for the umpteenth time... I'd use PHP to write the 3D collisions ;).


20th July 2003 04:18 UTC

well since I've only just finished Algebra II its ok that i don't understand this, right?
and keep up the good work! looks awesome


20th July 2003 05:59 UTC

That's a surprisingly small amount of code you've got there. Good job!


20th July 2003 06:56 UTC

remids me of the jezzball game:)


20th July 2003 09:12 UTC

GOD FREAKING DAMMIT! I worked that all out a long time ago and never got around to programming it...damn you all :D


20th July 2003 09:50 UTC

Hmmm I just realized this... how the heck do you like your collisions more? I worked mine out using conservation of impulse and energy, and unless you used other methods, they should be identical.


20th July 2003 10:33 UTC

okay, just wondering if i have this all correct anyway: the speed of object A colliding with exactly identical object B is switched to object B with respect to the axis of incedent, and vice versa; i.e.:
a ---> <- b
<- a b --->
but only if they are exactly the same...i never worked it out exactly for differing masses.
(and i'm only talking about head on collisions there; assumedly nothing happens on the opposite axis (in relation to the axis of incedent))


20th July 2003 16:09 UTC

Atero, that's common thought, but in actual fact, both forces would cancel out, and both would stop. All momentum is conserved. Thus is Ball A was +5 velocity, and Ball B was -5 velocity (Heading towards each other) when they collide, the total momentum is +5-5=0, thus they stop.

--- BUT ---

The ENERGY has to go somewhere, it doesn't get canceled out. If the balls can't get rid of the energy, it carries along through the balls to the other side, colliding and creating a new force towards the air. However, if the balls cannot withstand this force, they will break and the force will be exerted in the chunks they fly sideways from the collision.


20th July 2003 17:02 UTC

Zevensoft, or they'll just heat up.


20th July 2003 17:53 UTC

but seriously, ever heard of something called elasticity?


20th July 2003 21:23 UTC

but seriously, ever heard of something called the edit button?


20th July 2003 22:28 UTC

Conservation of momentum tells you the ratio between the change in velocity of the two balls (opposite for equal masses) while conservation of energy tells you the size of the change in velocity.


21st July 2003 01:29 UTC

Hope this helps anyone wanting to know how collisions are done. The picture attached is to help with the explanation of the n and t axes(they're red).

-----------------------------------------------------------

That's true for a perfectly elastic head on collision, but most collisions are not going to be head on.

To find the final velocities with non head-on collisions:
1. Find the normal and tangential axes. The normal axis lies along the line connecting the centers of both balls. The tangential axis is perpendicular to the normal axis(duh) and the two axes intersect at the point of collision. The normal line that we find is merely the two positions of the balls subtracted from eachother(we'll call those B and A).

N = B - A = <xb0 - xa0 , yb0 - ya0>

2. Normalize the normal axis. This means to make it a vector with length of 1. All you have to do is divide the vector by it's magnitude.

n = N/|N| = <NX , NY> / sqrt(NX² + NY²)

The tangential axis is just the normal axis rotated 90 degrees, which is real easy to do - you just switch the x and y components.

t = <ny , nx>

3. Find the components of the velocities:
The normal component of one of the velocities is just the dot product of that velocity and the normal.

vain = vai . n = <vaix , vaiy> . <nx , ny> = vaix * nx + vaiy * ny

The other velocities are found in the same manner.

vait = vai . t = <vaix , vaiy> . <tx , ty> = vaix * tx + vaiy * ty
vbin = vbi . n = <vbix , vbiy> . <nx , ny> = vbix * nx + vbiy * ny
vbit = vbi . t = <vbix , vbiy> . <tx , ty> = vbix * tx + vbiy * ty

4. Find the final velocities:
Like I said above, the tangential components don't change during the collision.

vaft = vait
vbft = vbit

The normal components do - you just have to consider the energy and momentum like any head on collision.

vafn = (mb * vbin * (e + 1) + vain * (ma - e * mb)) / (ma + mb)
vbfn = (ma * vain * (e + 1) - vbin * (ma - e * mb)) / (ma + mb)

Now to explain the above: ma and mb are the masses of the balls - if both masses are the same then things clear up nicely. E is the coefficient of restitution - kinda like the elasticity of the collision. A perfectly elastic collision(which is probably what you want - otherwise the balls would eventually stop moving) has a coefficient of 1, which also helps optimize the code.

5. Convert back to the x and y coordinate system:
That's done like this:

vafx = vafn * nx + vaft * tx
vafy = vafn * ny + vaft * ty
vbfx = vbfn * nx + vbft * tx
vbfy = vbfn * ny + vbft * ty

And there you have your final velocities. Note: A LOT of optimizations can be done(tangential components, same masses, coeff., etc), but this gives the complete picture.

List of variables and what they are:
nx and ny - x and y components of the normal vector
tx and ty - x and y components of the tangential vector
vaix and vaiy - x and y components of the initial velocity of ball A
vbix and vbiy - x and y components of the initial velocity of ball B
vain and vait - n and t components of the initial velocity of ball A
vbin and vbit - n and t components of the initial velocity of ball B
vafn and vaft - n and t components of the final velocity of ball A
vbfn and vbft - n and t components of the final velocity of ball B
vafx and vafy - x and y components of the final velocity of ball A
vbfx and vbfy - x and y components of the final velocity of ball B
ma and mb - masses of balls A and B
e - coefficient of restitution


21st July 2003 03:21 UTC

Thanks anubis! I think that is a best explanation of collisions I have gotten anywere else before! (I even understand it now!)


21st July 2003 03:23 UTC

No problem. I forgot to mention - About 90% of the credit goes to Andre LaMothe - the author of the book Tricks of the Windows Game Programming Gurus. That book has some great stuff in it.


21st July 2003 17:53 UTC

I just used conservation of energy and momentum...

v1,v2 = speeds before collision
v1',v2' = speeds after collision

Any force between the balls (in an ideally hard collision) will lie along the axes between the ball's centres, so all you need to calculate are the two (signed) lengths of the vectors that describe the change between v1 and v1', and v2 and v2':

k1, k2.

Conservation of momentum:

m1*v1 + m2*v2 = m1*v1' + m2*v2'
<=>
m1*v1 + m2*v2 = m1(v1 + k1*u) + m2(v2 + k2*u)
where 'u' is a vector connecting the two balls.
<=> 0 = m1*k1*u + m2*k2*u
<=> -k2*m2 = k1*m1

(for equal masses, k1 = -k2 = k)

At this point I assumed the masses were equal, but you can do the same for different masses:

Conservation of (kinetic) energy:
m*(v1^2)/2 + m*(v2^2)/2 = m*(v1'^2)/2 + m*(v2'^2)/2
<=> (divide by m and multiply by 2)
v1^2 + v2^2 = v1'^2 + v2'^2
<=>
v1^2 + v2^2 = (v1 + k*u)^2 + (v2 - k*u)^2

Eventually you will get a quadratic equation in 'k' without a constant term. This means k=0 is a solution (no-change = situation before collision = of course also valid). By dividing by k, you get a linear equation which can be rewritten into the vector-formula above.

If you want non-perfect collisions, I think you could incorporate a loss of kinetic energy that has dissipated into heat into the second formula, but I'm not sure.


22nd July 2003 01:48 UTC

My method goes through that - I didn't show that part. The main thing about my explanation was how to apply that to only the normal components of the velocities.


22nd July 2003 14:05 UTC

Hmmm yeah but doesn't this mean that all the transformations in your code are useless? I just write out the general momentum/energy equations for the entire velocities (not just normal)... the requirement that only normal components change is built-in.

E.g. I don't have a normalization, it's done later on (but without the square root).

Also, a slighty mistake in your explanation (though it's probably just a slip, otherwise your balls would freak out I think):

The tangential axis is just the normal axis rotated 90 degrees, which is real easy to do - you just switch the x and y components.
t = <ny , nx>
This is not right ;). The correct rule is: switch the x and y components and change the sign of one of them.

22nd July 2003 17:24 UTC

Whoops, yeah I didn't realize that. I would change it, but I don't think that I can edit it any more.


23rd July 2003 07:37 UTC

no u cant:rolleyes: it can only be done within 3 hrs of posting.(unless you are a mod)