# Trajectory with gravity

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

## Recommended Posts

I have learned how to do simple trajectory, however, it does no take into account of gravity. Currently I calculate it like this m_vVelocity = D3DXVECTOR3(0.1f, 0.0f, 0.0f); m_vSpeed = D3DXVECTOR3(0.5f, 0.5f, 0.0f); m_fAngle = 60.0f; // Calculate the x and y scales (this will give the x/y equivalent of the angle) float fScaleX = cos(DegreesToRadians(m_fAngle)); float fScaleY = sin(DegreesToRadians(m_fAngle)); m_vVelocity.x = (m_vSpeed.x * fScaleX); // Speed times fScaleX is the x velocity m_vVelocity.y = (m_vSpeed.y * fScaleY); // Speed times fScaleY is the y velocity (IN THE GAME LOOP) object->GetTransform()->_41 += (m_vVelocity.x * dElapsedTime); object->GetTransform()->_42 += (m_vVelocity.y * dElapsedTime); That in its self is rather basic, however, I would like to add gravity to it. How could I do this properly? I normally just have "m_vVelocity.y -= (m_fGravity * dElapsedTime)" but in this situation it is not valid since velocity is getting remade every time. Am I perhaps making the velocity for the trajectory wrong? Right now it looks as if this is only in 2D, however, I am trying to get it to work in 3D in the future.

##### Share on other sites
You seem to have answered your own question already. As with the velocity being remade every frame, that is something you need to remove to add the effect of acceleration from gravity. But besides that you seem to know how to add the effect of gravity.

Try moving the velocity setting code into an area called "LaunchTrajectory" or something rather than the update.

##### Share on other sites
Well to me it would make logical sense to do something like this

m_vVelocity.x = (m_vSpeed.x * fScaleX);
m_vVelocity.y = (m_vSpeed.y * fScaleY);

m_vVelocity.y -= (m_fGravity * dElapsedTime);

m_vRenderObjects[1]->GetTransform()->_41 += (m_vVelocity.x * dElapsedTime);
m_vRenderObjects[1]->GetTransform()->_42 += (m_vVelocity.y * dElapsedTime);

Where the velocity just has gravity taken out for it right away, and that is how I did it before I posted this question. However, all this will do is cause the angle at which it is acceleration on the y to be lower, not actually force it down over time.

I figured I need a dedicated velocity, that gets fScale applied to it.

##### Share on other sites
If you're resetting the position each frame rather than iterating through the movement you are looking for is:

Position = StartPosition + StartVelocity*time + .5 * Acceleration * time * time

##### Share on other sites
Quote:
 Original post by AlrecenkIf you're resetting the position each frame rather than iterating through the movement you are looking for is:Position = StartPosition + StartVelocity*time + .5 * Acceleration * time * time

I often see physics for games displayed with a * time* time on it, however, I do not wish to have it this way. I want to be able to launch the object more than once, so its global time will not matter. I know they I could simply reset the time for the per object when I wish to project it again, but is there no other way?

##### Share on other sites
The other way is to not reset the velocity every frame. So you'd have two methods:
One that gets called at launch and one that gets called every frame.
Luanch(){
position = initial position
velocity = initial velocity
}
Update(){
velocity = velocity + acceleration * (time since last frame)
position = position + velocity * (time since last frame)
}

##### Share on other sites
Quote:
 Original post by AlrecenkThe other way is to not reset the velocity every frame. So you'd have two methods:One that gets called at launch and one that gets called every frame.Luanch(){position = initial positionvelocity = initial velocity}Update(){velocity = velocity + acceleration * (time since last frame)position = position + velocity * (time since last frame)}

I have tried a similar method, and I have implement basically the same method in different physics simulations. The problem that I came into is that my velocity is generated by

float fScaleX = cos(DegreesToRadians(m_fAngle));m_vVelocity.x = (m_vSpeed.x * fScaleX);

rather than the traditional "velocity += acceleration * elapsedTime"

##### Share on other sites
That's the initial velocity.

##### Share on other sites
If you haven't already, I'd definitely recommend reading up on Newton's laws of motion - that's the backbone of what you are trying to accomplish.

##### Share on other sites
Quote:
 Original post by AlrecenkThat's the initial velocity.

Ah, after some testing it turns out that my speed was just not that high enough of a number and it was causing gravity to crash my ball into the ground before it had a chance to launch, how foolish of me.

Thanks for the help again =)

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002063
×