Sign in to follow this  

Gravity Implementation

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
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
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
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
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

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

If you intended to correct an error in the post then please contact us.

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