#### Archived

This topic is now archived and is closed to further replies.

# Initial trajectory for a mortar-like weapon

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

## Recommended Posts

I''m currently writing a 3D RTS game, and I have a mortar unit, but I''m having a few problems with the maths involved in calculating the initial trajectory for the weapon, so that it will land where I am aiming it. Say the unit is at position pos, and I want the mortar to hit position dest. My current code is:
  CVector dir = dest - pos; dir.z = 0.0f; // ignore height difference for now float d = dir.length(); dir = dir.normalize(); float s = 0.0f; // height difference between the two points float v0 = 35.0f; // v0 = velocity of projectile float g = 9.0f; // gravity float t = d / v0; // time to hit target float y = (-s - 0.5f * g * t * t) / t; float x2 = (v0 * v0) - (y * y); // x ^ 2 if( x2 < 0.0f ) { // out of range! } else { float x = sqrtf( x2 ); traj = CVector( dir.x * x, dir.y * x, y ); } 
and traj points in the direction that shells are fired. However, there''s a few problems. This seems to get answers that are close, but sometimes they land too far away, sometimes too close. I want it to land right on dest.

##### Share on other sites
No one has any ideas?

One other thing of note is that if there''s another way to describe the trajectory than with my current method of just adding a gravity vector to the velocity each frame (for example, I was thinking of maybe making it follow part of a circle or ellipse) then that''s cool. I just need to get it to look at least somewhat parabolic - it doesn''t need to be perfectly realistic.

##### Share on other sites
Gravity (on earth) is 9.8 meters/(sec^2). Here''s an equation to find change in distance over time:

Distance = Initial_Velocity*Time + 0.5*(Acceleration*Time^2)

Time is in seconds as long as your acceleration (gravity in this case) is in units of seconds (it is). You''d probably use -9.8 to make the object slow down and fall instead of flying away. For the horizontal direction, it isn''t affected over time unless there''s a wind or something.

Remember that this is how you get the velocity for the vertical direction:
           .           |\           |_\           |  \           |   \           |    \   <-- Velocity of launch sin(AoS)  |     \    *      |      \   VoL     |       \           |        \           |         \           |_         \           |_|_______/_\ <-- Angle of shot

http://druidgames.cjb.net/

##### Share on other sites
Thanks, Null and Void, it actually turned out that I was just being silly. I did get my maths right (I had fiddled with my code trying to get the best result, so it''s not the same maths as I came up with originally).

My problem was that I had three variables, vx, vy and t, but I could only think of two equations, namely vx = d / t and vy = (1/2 * g * t * t - s) / t (which is the one you gave, re-arranged to make vy the subject). But then I remembered that v^2 = vx^2 + vy^2, and since both vx^2 and vy^2 have a t in them, I can solve that for t. So, my final code (if anyone is interested) is:

  float t = sqrtf( 2 * d / g ); float y = 0.5f * g * t; // still ignoring s, the height difference for now float x = d / t; CVector traj = CVector( dir.x * x, dir.y * x, y );

where d, g, and dir are defined as in the original post.

##### Share on other sites
The important thing to remember for projectiles is that the hrizontal and vertical components are independent, so you specify equations of motion for each.

In this case, you are specifying a final position, and an initial velocity in the horizontal plane, v0. From this you correctly calculate the time to reach the destination

t = v0/d, where d is the horizontal distance.

You can then use

s = v_h0*t + 0.5*g*t*t

to calculate your initial horizontal velocity, v_h0. Since s is zero in the horizontal plane, you have

v_h0 = -0.5*g*t

(be careful about the sign of gravity, I''m assuming it''s negative).

Mike.

##### Share on other sites
The important thing to remember for projectiles is that the hrizontal and vertical components are independent, so you specify equations of motion for each.

In this case, you are specifying a final position, and an initial velocity in the horizontal plane, v0. From this you correctly calculate the time to reach the destination

t = v0/d, where d is the horizontal distance.

You can then use

s = v_h0*t + 0.5*g*t*t

to calculate your initial horizontal velocity, v_h0. Since s is zero in the horizontal plane, you have

v_h0 = -0.5*g*t

(be careful about the sign of gravity, I''m assuming it''s negative).

Mike.

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

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002031
×