dean_harding 138 Report post Posted April 24, 2001 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. 0 Share this post Link to post Share on other sites

dean_harding 138 Report post Posted April 25, 2001 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. 0 Share this post Link to post Share on other sites

Null and Void 1088 Report post Posted April 25, 2001 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/ 0 Share this post Link to post Share on other sites

dean_harding 138 Report post Posted April 25, 2001 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. 0 Share this post Link to post Share on other sites

Mikey 122 Report post Posted April 25, 2001 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 destinationt = v0/d, where d is the horizontal distance.You can then uses = v_h0*t + 0.5*g*t*tto calculate your initial horizontal velocity, v_h0. Since s is zero in the horizontal plane, you havev_h0 = -0.5*g*t(be careful about the sign of gravity, I''m assuming it''s negative).Mike. 0 Share this post Link to post Share on other sites

Guest Anonymous Poster Report post Posted April 25, 2001 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 destinationt = v0/d, where d is the horizontal distance.You can then uses = v_h0*t + 0.5*g*t*tto calculate your initial horizontal velocity, v_h0. Since s is zero in the horizontal plane, you havev_h0 = -0.5*g*t(be careful about the sign of gravity, I''m assuming it''s negative).Mike. 0 Share this post Link to post Share on other sites