Gravity Implementation

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

Recommended Posts

I just had a quick question about implementing gravity. How do i add a value(say 9.8) every second^2? i understand every second, but i dont quite get every second squared. thanks for any insight

Share on other sites
velocity.y += gravity * delta_time;
position += velocity * delta_time;

position.y += gravity * delta_time * delta_time;

Share on other sites
Every second squared (aka: acceleration) just means that you're changing the velocity vector instead of the position (roughly). On the implementation side, you'll probably have to think about what to do with the velocity when the object hits the ground though. I suppose in a game you'd only be applying gravity when the object is off the ground. (Also: 9.81 is fine if your game is taking place on the surface of the earth, the value is different in other locales - it varies with masses of the two objects attracting each other and the distance between them, iirc)

You might want to grab a physics book (or website) and look up the meaning of force, acceleration, and gravity.

Share on other sites
Maybe it is more intuitive to think about the units as (m/s)/s, the change in velocity per second.

Share on other sites
Another option is to model it using forces. The force is constant over time, so if your integrator applies forces (rather than directly sets velocity), the expression is straightforward.

Expressed as a forward Euler (simplest possible) integrator:

force = mass * gravityforce += other_inputsacceleration = force / massvelocity = velocity + acceleration * timeposition = position + velocity

You can model collisions, user control, force fields, and other things with the "other_inputs" term.

Share on other sites
Quote:
 Original post by Jan-Lieuwevelocity.y += gravity * delta_time;position += velocity * delta_time;So basicaly it leads to:position.y += gravity * delta_time * delta_time;

It is important to note that the above code is only an approximation. The actual computation is this:
    velocity = previous_velocity + gravity * delta_time;    position = previous_position + previous_velocity * delta_time + 0.5 * gravity * delta_time * delta_time;

Share on other sites
What Jan means is that the equation for the distance is

s = ut + (at^2)/2

Where
u = Initial Velocity
a = Acceleration
t = time (or in this case deltaTime)

but JAN arent you suppose to add gravity afterward like this
position = previous_position + previous_velocity * delta_time + 0.5 * gravity * delta_time * delta_time;

velocity = previous_velocity + gravity * delta_time;

Because otherwise you apply gravity twice?

Share on other sites
Quote:
 Original post by hplus0603force = mass * gravityforce += other_inputsacceleration = force / massvelocity = velocity + acceleration * timeposition = position + velocity

Since acceleration due to gravity is "constant" (9.82 m/s^2) at "sea level", this can be reduced to the following:

velocity += 9.82 * timeposition += velocity

To calculate time, just determine how much time has passed between each frame in seconds.

Share on other sites
Quote:
Original post by JohnBolton
Quote:
 Original post by Jan-Lieuwevelocity.y += gravity * delta_time;position += velocity * delta_time;So basicaly it leads to:position.y += gravity * delta_time * delta_time;

It is important to note that the above code is only an approximation. The actual computation is this:
    velocity = previous_velocity + gravity * delta_time;    position = previous_position + previous_velocity * delta_time + 0.5 * gravity * delta_time * delta_time;

don't forget that a = Mg*k/d^2
thus acceleration could change. This is especially poetical with 3+ objects...