Sign in to follow this  
BloodLust666

Gravity

Recommended Posts

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
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
Quote:
Original post by manuelb
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.

The important thing to note about your code is that it is an approximation. It is called Euler Integration (as Numsgil pointed out). One of the effects of the approximation is that the amount of error depends on the frame rate. The error comes from computing a continuous function in discrete steps. The lower the frame rate, the bigger the step, and thus the more the error.

Share this post


Link to post
Share on other sites

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