Gravity

Started by
9 comments, last by JohnBolton 18 years, 7 months ago
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?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement
Gravity is acceleration. You'd apply it to the velocity every frame, then apply the velocity to the location.
how would i apply it to the velocity?

I have:

m_Radian (angle to travel)
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"
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
how would i apply it to the velocity?

I have:

m_Radian (angle to travel)
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"

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.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
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.
--------------------------Rory Gallagher and Chico Science music !!!!!!!---------------------------____||~~~~|/TT
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 

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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.
--------------------------Rory Gallagher and Chico Science music !!!!!!!---------------------------____||~~~~|/TT
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.
[size=2]Darwinbots - [size=2]Artificial life simulation

This topic is closed to new replies.

Advertisement