Ball Bouncing

Started by
9 comments, last by ProblemBaby 19 years, 5 months ago
Hi Iam trying to make some balls bounce in 2D. My code goes something like this: if the balls intersect: Normal = -atan2(y, x); // y and x is simply the distance between the balls ball1.dir = ball1.dir - (Normal - ball1.dir) * 2; ball2.dir = ball2.dir - (Normal - ball2.dir) * 2; My questions is: This works if they collide when they move towards each other, but not (for example) if they are moving in same direction but the other ball is faster how can I get rid of that. Does this kind of code works for many balls what about three collide at the same time and such stuff. Some information would be nice! Thanks in Advance
Problems every where
Advertisement
I forgot one thing:
when they collide the velocity will be the same as the fastest ball, they will never slow down or taking care of the Momentum (Hope you got what I mean cause I dont know how it is spelled)
Problems every where
[google] "elastic collision"
CLICKY
This links only tells me the stuff I dont want to know.
Iam interested in a way to find out the resulting angles no mass or such stuff.

And also I need an idea of how to do that when there are more then two balls.
Problems every where
Well, that's the thing. If you want to use momentum properly, you need the mass of the objects. You could, however, just use the same mass for every object and then the numbers would cancel out and leave you working without mass. But then you have the issue that every object will appear to have the same mass (larger objects will be effected by collisions as much as smaller ones). If you're writing a pool game, then that's fine (except that the cue ball is either lighter or heavier than the rest); and it's fine for most anything else. Just be aware of that. Better to do this on paper first so you get the massless formulae in the code. Consider it an exercise for the reader :)

As for more than two, you just have to apply the momentum formulae on a collision by collision basis. If you have the scenario of one ball hitting two at exactly the same time, I believe you impart the same momentum on both (though you might have to halve it... not sure).

-Auron, who's been avoiding physics for way too long
Well this is a formula for the velocity right?
v1f = v1i * (m1 - m2) / (m1 + m2) + v2i * (2 * m2) / (m1 + m2)
v2f = v1i * (2 * m1) / (m1 + m2) + v2i * (m2 - m1) / (m2 + m1)

what about the direction angle?
Problems every where
the velocity has direction and angle in it! Remember vectors have magnitude AND direction. The 'v's in that equation are vectors like (0.6, 2.3) for 2D or (0.6, 2.3, 1.8) for 3D (the formula works find for both). So you can use the vector's direction to work out the angles. If you have no idea what I'm talking about go look up some 3D maths tutorials!

What are you actually after? Because if you have 2 objects moving and you want them to bounce then those formula will do exactly what your after. If you want to find the angle after a collision for some reason then you'll need to convert the final velocities back to angles which with a bit of simple trig shouldn't be too hard!
Well iam not that good at such math stuff
one more question how is this turned into code (I thought it was but I must been wrong)

v1f = v1i * (m1 - m2) / (m1 + m2) + v2i * (2 * m2) / (m1 + m2)
v2f = v1i * (2 * m1) / (m1 + m2) + v2i * (m2 - m1) / (m2 + m1)

if I instead have:

m1, v1i, d1i
m1, v1f, d1f

m2, v2i, d2i
m2, v2f, d2f

// v = velocity
// d = direction

and since iam not going to use different masses I can make it a bit simplier to but I think i can do that myself.
Problems every where
A velocity as you're expected to use it in those equations is represented as a vector: (vx, vy) for 2D collision. That is, x and y components.

If you have a magnitude and direction, then you'll have to convert back and forth. start here (I have no affiliation with the institutation; it was first link on Google looking for "vectors direction", and it looked like decent material) and read through the subsequent pages (links at bottom of page). Once you've got that part down, you can go look up the documentation for that atan2 function again, and see if you can't figure it out.

This topic is closed to new replies.

Advertisement