Sign in to follow this  

Ball Bouncing

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Yeah I got that!
Iam now using velocity in terms of xv, yv

But I still doesnt understand if they collide

xfa = xia * (1 - 1) / (1 + 1) + xib * (2 * 1) / (1 + 1);
yfa = yia * (1 - 1) / (1 + 1) + yib * (2 * 1) / (1 + 1);

xfb = xia * (2 * 1) / (1 + 1) + xib * (1 - 1) / (1 + 1);
yfb = yia * (2 * 1) / (1 + 1) + yib * (1 - 1) / (1 + 1);

Are this the new velocities?
I changed m to 1 cause they have same mass
well it doesnt work so I have probably missed something

Share this post


Link to post
Share on other sites

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this