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

Constant checking and syncing

Started by Winegums Jan 16, 2007 at 12:46 AM 3 replies 500+ views
Original Post
Winegums
Winegums
Not so much a mathmatical problem, but something I'm struggling with. I'm trying to impliment a bouncing ball into my program. It is created at the origin and there is a plane 3 units below it. with a coefficient of restitution equal to 1.0f and gravity being the only force acting on the ball (other than its own velocity/acceleration), i thought the ball should be perpetually hopping on the spot (if this is not the case then please speak up!). however my ball bounces off to infinity unless i fiddle the numbers around (makeing coef of restitution something like 0.005 and gravity 0.0098). I think my problem is how often i update the physics on the ball. Is there a good resource to learn how to regulate the application of forces onto objects? Currently I have a function in the render loop that checks if 10 miliseconds have passed since the last execution, and if they have it updates the objects acceleration with gravity (a fraction thereof). Perhaps a better system would be to find the time passed since the last time the function ran, and treat that as a factor of how much gravity to add to acceleration and how much acceleration to add to velocity? ie if the program lags and it takes 1.2 seconds to get back to the physics part, add 1.2 * g to acceleration, and 1.2*a to velocity?
Vorpy
Vorpy
1. Decouple your physics from rendering and use constant timesteps for your physics. Your physics involves calculating accelerations, updating velocities and positions, and handling collisions. Your rendering involves drawing the position of all the objects.

2. Gravity does not increase acceleration; it causes acceleration. In the absence of other forces, the ball's acceleration should be equal to the acceleration caused by gravity.
pTymN
pTymN
The ball will always either tend towards infinity or no movement, because of integration errors (Euler or even RK4 integration) and because of floating point errors. You can use a parametized version of the ball's motion, but that is not going to allow the ball to react to other objects with real collision response.
 
Merlz
Merlz
Ignore the gravity on the step the ball impacts with the ground :D

I had this problem a while ago (albeit with a lot more balls) and I found that the easiest way around it is to simply ignore gravity on impact (in otherwords, assume half the timestep passes before the ball bounces, so you get g/2 accel towards the ground and g/2 decel moving away from the ground). Constant timesteps will also help a lot, but they won't cancel the problem - leave it for long enough and it'll become unstable. Of course, the best solution is to figure out exactly *when* in the timestep the ball hits the ground and add gravity accordingly... RK4 would help too
JohnBolton
JohnBolton
Assuming that g never changes, here are the formulas you need:
    <big>s = s<sub>0</sub> + v<sub>0</sub>t + ½gt<sup>2</sup></big>    <big>v = v<sub>0</sub> + gt</big>    s<sub>0</sub> is the starting position    v<sub>0</sub> is the starting velocity    t is the time since the start    g is the acceleration due to gravity    s is the new position    v is the new velocity 
The starting point can be any point in the past as long as s0 and v0 haven't changed. For example if the starting point is the previous collision, then when the ball collides, you set the new s0, recompute v0, and reset t. If the starting point is the last frame, then you set s0 and v0 to the values of s and v from the last frame and t is the time since the last frame (recomputing the values whenever there is a collision).

Also, note that if this is 3D, then s, s0, v, v0, and g can all be vectors. If this is 2D, then s and s0 are heights above the plane.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

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

Sign in to reply to this topic.