Sign in to follow this  
rgirard413

Gravity Implementation

Recommended Posts

rgirard413    133
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 this post


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


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

Share this post


Link to post
Share on other sites
hplus0603    11347
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 * gravity
force += other_inputs
acceleration = force / mass
velocity = velocity + acceleration * time
position = position + velocity


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

Share this post


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

Share this post


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


Link to post
Share on other sites
Mattman    336
Quote:
Original post by hplus0603

force = mass * gravity
force += other_inputs
acceleration = force / mass
velocity = velocity + acceleration * time
position = 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 * time
position += velocity


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

Share this post


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

Share this post


Link to post
Share on other sites

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