#### Archived

This topic is now archived and is closed to further replies.

# Gravity effects

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

## Recommended Posts

How would i go about getting a gravity effect within a 3D world? for example i fall off a cliff, and i''d like to accelerate at 9.8m/s or something like that, how would i implement that into the program?

##### Share on other sites
The way I do it is to define a variable that represents the object''s movement increment. This value is added to the object''s position every frame. The increment is like it''s speed. Then, you can accelerate the object by varying amounts by adding/subtracting from the increments every frame.
Here''s some BASIC:

''Object''s position
private Pos as single

''Object''s increment
private Inc as single

''''''''Game loop''''''''''

''Increase increment (Gravity @ 9.8 units per sec)
inc = inc + 9.8

''Move object according to increment
pos = pos + inc

''''''''''''''''''''''''''''''''''''

You could do this for whatever axis is your up/down in your 3d world for gravity.

You pros probably have a better way to do it, but that''s what I do...

##### Share on other sites
You should probably already know that gravity only affects your vertical axis.

Given the 4 kinematic equations:

1. vf = vi + a * t
2. d = (vi + vf)/2 * t
3. d = vi * t + (1/2) * a * t^2
4. vf^2 = vi^2 + 2 * a * d

where d = df - di (treat distance as your object''s position)

If I wanted to drop an object (from rest) from above, I could use the third
equation let the initial velocity be zero:

d = (1/2) * a * t^2

If I was using DirectX, I would treat these as vectors like:

D3DVECTOR gravity = D3DVECTOR( 0.0f, -9.8f, 0.0f );
D3DVECTOR position = D3DVECTOR( 100.0f, 100f, 100.0f );

You can easily add, subtract, multiply, etc. vectors in DirectX. You don''t
have to individually access their members. The equation would look exactly
the same:

position = 0.5f * gravity * frame_time * frame_time;

• 15
• 13
• 35
• 39