# Gravity

This topic is 4817 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 apply a gravity to an object? I have a constant for the gravity, should i subtract that gravity value (times the framerate) from the Y every frame? or what?

##### Share on other sites
Gravity is acceleration. You'd apply it to the velocity every frame, then apply the velocity to the location.

##### Share on other sites
how would i apply it to the velocity?

I have:

m_Pos {D3DXVECTOR3 position)
m_Velocity
m_Acceleration
m_Gravity

I apply the acceleration every frame, so how would i apply the gravity every frame? plus becuase it's mostlikely a different angle than the "m_Radian"

##### Share on other sites
should i have a separate float called something like m_GravityApply? have that as the gravity velocity, add the m_gravity to the m_GravityApply then add the m_GravityApply to the m_Pos.y? :-D

##### Share on other sites
also, gravity is considered a "force" right? so all other forces i have, i need to have the constant acceleration and have a velocity that is affected by the acceleration and apply that to the Position?

##### Share on other sites
Quote:
 Original post by EvilKnuckles666how would i apply it to the velocity?I have:m_Radian (angle to travel)m_Pos {D3DXVECTOR3 position)m_Velocitym_Accelerationm_GravityI apply the acceleration every frame, so how would i apply the gravity every frame? plus becuase it's mostlikely a different angle than the "m_Radian"

Your velocity vector contains your object's speed and direction information. Your acceleration vector is your object's change in velocity, but since this can be of any force, it's really just a generic 'middle-man' type vector, ie., it holds all of your acceleration information (gravity, air/water resistance, friction).

To be clear, you can apply all of your physics engine's forces on m_Acceleration, then apply that to m_Velocity; although doing this may sound redundant, it can help in visualizing the process (try and draw vector addition on some graph paper - it really helps to see what's going on).

If you're still having troubles, I or somebody else may be able to give you specific examples. Good luck.

##### Share on other sites
In a very simple aproach:

object.velocity(X,Y,Z)=someValueForVelocity
object.position(X,Y,Z)=someValueForPosition
gravity=someValueToGravity

foreach frame
{
deltaTime = Now - LastTime
Lasttime=Now
...
... scene phisics processing, controls balblabla
...
object.velocity.y = object.velocity.y - (gravity*deltaTime)
object.position.x = object.position.x + (object.velocity.x * deltaTime)
object.position.y = object.position.y + (object.velocity.y * deltaTime)
object.position.z = object.position.z + (object.velocity.z * deltaTime)

..... colision detection blablabla

draw the scene
}

This is only a crude ideia... you shell write decent object oriented code, and not what I wrote here. And don't forget to put some attenuation on the gravity (air resistence) if your objects will fall from high.

Hope that helps.

##### Share on other sites
Quote:
 Original post by manuelb object.velocity.y = object.velocity.y - (gravity*deltaTime) object.position.x = object.position.x + (object.velocity.x * deltaTime) object.position.y = object.position.y + (object.velocity.y * deltaTime) object.position.z = object.position.z + (object.velocity.z * deltaTime)

The problem with that code is that the motion of the object changes depending on the frame rate.

For the motion of an object with constant acceleration (e.g. simple gravity), this is frame-rate independent:
    V = V0 + At    P = P0 + V0t + 0.5 * At2    P0 = P    V0 = V

##### Share on other sites
Quote:
Original post by JohnBolton
Quote:
 Original post by manuelb object.velocity.y = object.velocity.y - (gravity*deltaTime) object.position.x = object.position.x + (object.velocity.x * deltaTime) object.position.y = object.position.y + (object.velocity.y * deltaTime) object.position.z = object.position.z + (object.velocity.z * deltaTime)

The problem with that code is that the motion of the object changes depending on the frame rate.

For the motion of an object with constant acceleration (e.g. simple gravity), this is frame-rate independent:
    V = V0 + At    P = P0 + V0t + 0.5 * At2    P0 = P    V0 = V

No, you are wrog. My conde IS frame-rate independent, just becouse acceleration an velocity depends on deltaTime, deltaTime is the the time elapsed in the last frame. This is why acceleratin and velocity are multplyed by deltaTime. I think you misunderstood my pseudo-code.

##### Share on other sites
Just to thow in my two cents:

the ol' "velocity = old velocity + acceleration*time" is called a(an?) Euler Approximation.

Verlet(sp?) gives a better approximation for velocity and position when you have a changing acceleration function (non constant).

I don't know all the details myself, and the implementation isnt as clean, but it's worth a look anyway.

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633755
• Total Posts
3013706
×