# Simulated projectile - calculated initial velocity overshoots the target

• xovangam
In summary: I don't have a simulator here in my brain :)In summary, the conversation discusses the process of creating a simulation of a projectile in a 3D world using known parameters such as the target, angle of launch, and gravity. The equations of motion for the projectile are discussed, as well as the issue of the projectile overshooting the target. The question of whether the size of the time step affects the accuracy of the simulation is also raised. The conversation also delves into the process of calculating the initial velocity and launch direction of the projectile.
xovangam
hi there,

i'm programming a simulation of a projectile that has a known target off in the distance somewhere in my 3D world, i also know the angle at which i will be firing the projectile. i am firing the projectile on flat ground (so the start and end y-coordinates are the same).

i'm using the equation:

d = (v2 / g) * sin(2 * theta)

where:
d = distance to target (known)
g = gravity (known)
theta = angle off the horizontal at which I'm firing the projectile (known)

so i solve for v in order to get an initial velocity with which to fire the projectile.

the velocity that i get out of that causes my simulation to always overshoot the target (i.e. the projectile travels a bit farther than d). each step in the simulation is calculated in each tick like so (pseudo-code):
Code:
     Position.X += Velocity.X * TimeStep;
Position.Y += Velocity.Y * TimeStep;
Position.Z += Velocity.Z * TimeStep - 0.5 * Gravity.Z * TimeStep * TimeStep;
Velocity.Z -= Gravity.Z * TimeStep;
now i assume this occurs because my timesteps lead to an approximation of a trajectory curve, which causes the projectile to travel a longer distance than it otherwise would if the timesteps were infinitely small ?

and if that's true, so I'm currently scaling the initial velocity that i calculate down a bit by a factor of around 20 percent or so to compensate for the fact that i overshoot my target. so out of curiosity, is there some method by which i could calculate a more "exact" scale factor that was relative to the size of the timesteps that I'm taking ? such that if i raise/lower the size of my timestep, i wouldn't have to re-guess at a scale factor ?

TIA -x.

actually, using smaller timestep increments doesn't seem to bring me closer to the target..

is d = (v2 / g) * sin(2 * theta) not what i want to be using here ?

Since this is neither abstract algebra nor linear algebra, I am moving it to "General Mathematics".

The size of your time step shouldn't matter, since the equations of motion you're using are exact. When I do what you say you're doing, I get no overshoot, even if I make my time step big enough for the entire flight of the projectile.

I suspect you've got the wrong velocities. How are you calculating vx, vy, and vz? Are you accounting for the fact that the total horizontal velocity is $v \cos\theta$?

What happens when you remove the term
- 0.5 * Gravity.Z * TimeStep * TimeStep;
?

@pmsrw3: the initial velocity is being calculated as:

Code:
v = sqrt((d * g) / sin (2 * theta))

as i simply solved for v in the initial equation. is that not correct ?

@robphy: sorry, i failed to mention that Z is "up" in my 3D world, so Z is the vertical component, X and Y are horizontal components. does that clarify your question ?

xovangam said:
@pmsrw3: the initial velocity is being calculated as:

Code:
v = sqrt((d * g) / sin (2 * theta))

as i simply solved for v in the initial equation. is that not correct ?
Yes, I understand that. But that's not actually velocity, it's speed. Velocity is a vector with x, y, and z components, Velocity.X, Velocity.Y, and Velocity.Z in your code. How are you calculating those?

ah. so i have an initial launch direction that i start with (i.e. theta is known).

well, more specifically, i have a unit vector that i start with. given that Z is up, i do this to calculate theta (pseudo code):

Code:
up_vector = vector(0,0,1);
Theta = acos(1.0 - (launch_direction dot up_vector));

which should give me the "pitch" angle between flat ground and my launch direction, yes?

and then i do:

Code:
v = sqrt((d * g) / sin (2 * theta));

and then i multiply 'launch_direction' by 'v' to get the initial velocity.

xovangam said:
ah. so i have an initial launch direction that i start with (i.e. theta is known).

well, more specifically, i have a unit vector that i start with. given that Z is up, i do this to calculate theta (pseudo code):

Code:
up_vector = vector(0,0,1);
Theta = acos(1.0 - (launch_direction dot up_vector));

which should give me the "pitch" angle between flat ground and my launch direction, yes?

and then i do:

Code:
v = sqrt((d * g) / sin (2 * theta));

and then i multiply 'launch_direction' by 'v' to get the initial velocity.
I'm going to repeat the question. "Velocity is a vector with x, y, and z components, Velocity.X, Velocity.Y, and Velocity.Z in your code. How are you calculating those?"

xovangam said:
Code:
up_vector = vector(0,0,1);
Theta = acos(1.0 - (launch_direction dot up_vector));
Another question: why "acos(1.0 - (launch_direction dot up_vector))"? Shouldn't it be $\frac{\pi}{2}-\cos^{-1}(\text{launch_direction dot up_vector})$?

i have a target in the world somewhere in front of me. i begin by calculating a displacement vector in the XY plane (Z is up) between the position i am going to fire from and the target:

Code:
fireToTargetXY = (target_pos - fire_pos);
fireToTargetXY.Z = 0.0; // i don't really need to do this because the Z values are equal

the magnitude of this vector is the distance that i need the projectile to travel (d).

Code:
d = magnitude(fireToTargetXY);

i then normalize that vector to give me a direction to the target in the XY plane:

Code:
fireToTargetDirXY = normalize(fireToTargetXY);

basis vectors: X is forward (1,0,0), Y is left/right (0,1,0) and Z is up (0,0,1). so i do:

Code:
launch_dir.X = fireToTargetDirXY dot forward;
launch_dir.Y = sin(acos(launch_dir.X)) * sign(fireToTargetDirXY.Y);
launch_dir.Z = sin(theta); // theta is clamped between 0 and Pi / 2
launch_dir = normalize(launch_dir);

for the Z component i don't actually know theta but instead i am given a raw value between 0 (no pitch) and 1 (straight up) from an outside input source; so that is why i mentioned that i calculate the actual "pitch" angle from the launch_dir vector.

is that more or less along the lines of what you're asking for ?

xovangam said:
is that more or less along the lines of what you're asking for ?
Yes, that helps. It's a whole lot more complicated than it needs to be, but it looks like it will eventually work out right. However, your calculation of theta looks wrong to me. In a previous post you said you calculated it by a weird formula acos(1 - ld.up), which doesn't make any sense to me. In fact, I don't see why you think you have to recalculate theta. You used theta as input to calculate ld in the code you just presented, then you use ld as input to recalculate theta. Why would you do that?

Anyway, I'm convinced that the trouble is not in your simulation, but that there's an error somewhere in that horrendously and unnecessarily complicated calculation you do to get your initial velocity vector. My guess would be that it's in the angle calculation, but it could be almost anywhere.

Last edited:
thanks @pmsrw3. to clarify i don't actually have theta when i calculate the launch direction, as i noted that comes from an external input as a number between 0 (no pitch) and 1 (straight up). so my givens are that and a target pos that is somewhere in front of me in the world.

i will try changing the calculation for theta as you suggest. i agree that looks incorrect.

thanks again! -x.

i suppose i could also just do sin-1(Z) on the input for pitch to get theta now that i think about it..oh well. i agree it's more complex than it should be. in any case i think I'm all good now..

xovangam said:
thanks @pmsrw3. to clarify i don't actually have theta when i calculate the launch direction, as i noted that comes from an external input as a number between 0 (no pitch) and 1 (straight up).
But your calculation of LD contains this line of code:

Code:
launch_dir.Z = sin(theta);

So you must have theta when you calculate launch direction! I understand that you aren't given theta directly, but you have to calculate it before you can calculate LD.

Code:
launch_dir.X = fireToTargetDirXY dot forward;
launch_dir.Y = sin(acos(launch_dir.X)) * sign(fireToTargetDirXY.Y);
launch_dir.Z = sin(theta); // theta is clamped between 0 and Pi / 2
Actually, looking at this again, this code is not correct. It is not going to give an LD vector at angle theta from the horizontal, but at a shallower angle. You construct a vector whose horizontal component is 1 and whose vertical component is sin(theta), which is a vector at angle arctan(sin(theta)).

so my givens are that and a target pos that is somewhere in front of me in the world.
But that's basically theta, isn't it? I mean, all you have to do is multiply it by pi/2.

## 1. What is a simulated projectile?

A simulated projectile is a virtual object that is used to model the motion of a real projectile in a controlled environment. It allows scientists to study and predict the behavior of projectiles without the limitations and risks of real-life experiments.

## 2. What is the initial velocity of a simulated projectile?

The initial velocity of a simulated projectile is the velocity at which it is launched or released from a starting point. It is usually measured in meters per second (m/s) or feet per second (ft/s) and is a crucial factor in determining the trajectory and range of the projectile.

## 3. Why does the calculated initial velocity overshoot the target?

The calculated initial velocity of a simulated projectile may overshoot the target due to various factors such as errors in the initial conditions, imprecise measurements, or limitations of the simulation software. It is important to carefully review and adjust these factors to improve the accuracy of the simulation.

## 4. How can we minimize the overshooting of the target in a simulated projectile?

To minimize the overshooting of the target in a simulated projectile, it is crucial to accurately determine and input the initial conditions such as the angle of launch, initial velocity, and air resistance. Using more precise simulation software and conducting multiple trials can also help to improve the accuracy of the results.

## 5. What are the limitations of using a simulated projectile?

While simulated projectiles offer numerous benefits in terms of safety and cost-effectiveness, they also have limitations. These include the assumption of ideal conditions, such as perfect launch and no external forces, and the inability to fully capture the complexity of real-world scenarios. It is important for scientists to carefully consider these limitations when interpreting the results of a simulated projectile experiment.

• Mechanics
Replies
3
Views
1K
• Introductory Physics Homework Help
Replies
11
Views
364
• Mechanics
Replies
4
Views
1K
• Introductory Physics Homework Help
Replies
5
Views
865
• Introductory Physics Homework Help
Replies
4
Views
1K
• Introductory Physics Homework Help
Replies
1
Views
919
• Introductory Physics Homework Help
Replies
13
Views
2K
• Programming and Computer Science
Replies
7
Views
1K
• Mechanics
Replies
8
Views
2K
• Classical Physics
Replies
25
Views
2K