Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

Gravity

This topic is 4634 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

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


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

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!