Skip to main content
gamedev.net
🔒 Locked

Gravity Implementation

Started by rgirard413 7 replies 950+ views
Original Post
rgirard413
rgirard413
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
http://www.thedizzle.com
Jan-Lieuwe
Jan-Lieuwe
velocity.y += gravity * delta_time;
position += velocity * delta_time;

So basicaly it leads to:

position.y += gravity * delta_time * delta_time;
Stani R
Stani R
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.

hplus0603
hplus0603
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.
enum Bool { True, False, FileNotFound };
JohnBolton
JohnBolton
Quote:
Original post by Jan-Lieuwe
velocity.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; 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
dawidjoubert
dawidjoubert
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?
----------------------------http://djoubert.co.uk
Mattman
Mattman
Quote:
Original post by hplus0603
force = 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.
Raghar
Raghar
Quote:
Original post by JohnBolton
Quote:
Original post by Jan-Lieuwe
velocity.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...

Topic Locked

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

Sign in to reply to this topic.