Skip to main content
GameDev.net gamedev.net
🔒 Locked

Bouncing a ball

Started by daniel_i_l Aug 7, 2006 at 1:09 PM 1 replies 850+ views
Original Post
daniel_i_l
daniel_i_l
When a ball bounces into something like the floor the component of the speed normal to the plane (for example the floor) gets reversed. I'm trying to program a physics simulation were there are, among other things, balls bouncing off walls. Now I know that I could simply reverse the speed component as I explained above to make the ball bounce butI find this as somewhat of a "hack", what I want to know is how it "really" happens - in terms of forces and stuff. The ball changes it's momentum which means that there is some force that pushes the ball from the wall over a certain time - impulse. How do I calculate this force (by perimiters such as the rigedness of the wall, speed of the ball...) and how do you know when to apply it and when to stop? One of the main reasons that I want to do it this way, is that I want the entire simulation to work by different forces being applied to the ball and the ball moving accordingly, and not every scenario having it;s own special rules.(that's not how it works in nature either) Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
grhodes_at_work
grhodes_at_work
A good starting point for bouncing physics will be one of Jeff Lander's articles over @ Gamasutra. It provides formulas and has nice illustrations. The article, as I recall (forgot my password so couldn't log in today to double check), uses the impulse-momentum formulas, which approximate the impulse as occuring instantaneously, e.g., when the collision occurs you immediately compute the velocity after the collision rather than apply a force over time.

Physics on the Back of a Cocktail Napkin (requires free registration)

There are pros and cons to using impulse-momentum for collision response. A pro is that it is stable, and (as long as your collision detection system is adequate) unlimited in terms of time step/frame rate. A con is that is can be exceedingly difficult to deal with multiple simultaneous collisions in a strictly correct way, e.g., when one ball is simultaneously impacting a wall and being hit by 2 or 3 or 7 other balls. You won't find closed-form formulas for anything other than 2 balls hitting each other or one ball hitting one wall.

Another approach, which handles multiple simultaneous collisions better, is the penalty force method, which applies a penalty force any time an object interpenetrates another object, causing the objects to separate. This is what you have described, e.g., apply a force over a few frames until the objects separate. The force would need to be varied each frame based on interpenetration depth. This works for any number of simultaneous collisions. The pro here is that it can be simpler to code, and in theory is more scalable. The con here is that it can be exceedingly difficult to make a penalty-method code stable. The robustness of such an approach can be improved somewhat by reducing time step, which can lead to various frame rate issues. For a general game, you'd want to use what is known as "implicit time integration" to maximize stability and reduce sensitivity to time step size/frame rate. Also, coding an implicit integrator can be nontrivial, particularly for someone who is just learning about numerical simulation.

Getting integration to work well can be so frustrating for someone just starting to code physics.....that I do suggest starting with impulse-momentum. And limit the number of balls to something that only rarely has each object colliding with no more than 1 other object at any given time. If you have 100 balls, you can still have up to 100 simultaneous collisions, if each ball only collides with a wall, or up to 50 simultaneous collisions, if each ball only collides with one other ball. And when you do have a ball that collides with more than 1 other thing, you can just process the collisions in order and decide to ignore the occasional artifact. Or, play with more advanced ideas...

You could also, initially, include some simple friction model into your response, e.g., slightly reduce the magnitude (not direction) of the tangential component of velocity when a ball hits the wall. Lots of neat things to play with!

Long term, as you get more and more sophisticated, and deal with more and more complex object shapes, you may want to consider moving to a penalty method approach.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
daniel_i_l
daniel_i_l
Thanks, actually, the real reason that I wanted to do the bouncing with forces is because of the fact that in my simulation I have string = a lot of springs connected with masses. Now, I want a ball to be able to collide correctly with a string, for example, if both of the ends of the string are ancored (on the x axis) and the ball falls onto the middle of the string, I want the string to stretch a little and then "throw" the ball back up. to do this I thought that I'd model each spring in the string as a small "wall" and then to a wall-ball collision. In order to do this I'd have to take into account the forces on the ends of the spring(wall) and the forces on the ball, then I'd have to move the spring and the ball accordingly. Whats the best way to do that?
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.