0

I'm writing a game where I want the player to shoot projectiles that will hit a target point in 3D, the game designers should be able to tweak arc height and gravity only, speed and angle should be derived from this.

So far I've worked out the math to calculate the speed given a fixed angle:

$$ v_{0} = \sqrt{\frac{g \; x^2}{2 \; \cos^2θ \; (x \; \tanθ - y)}} $$

Where:

$$v_{0} = {\rm speed}$$ $$x = {\rm horizontal \; distance}$$ $$y = {\rm vertical \; distance}$$ $$g = {\rm gravity}$$ $$θ = {\rm angle}$$

This works fine as the speed will be adjusted for any valid angle. However, it does not control the height of the arc, which can sometimes look silly for long distances from the target.

The equation of the max height of the arc I'm using to try to find the angle is:

$$ h = y_{0} + \frac{(v_{0} \; \sinθ)^2}{2g} $$

I've tried substituting the $v_{0}$ variable with the first equation, but this has proven to be much harder to solve than the algebra at my skill level. And I'm not really sure if this is even the right approach, so any guidance would be much appreciated!

HeroZhang001
  • 2,201
Belfer4
  • 103

1 Answers1

1

Let's assume that the projectile must rise from height $y_0$ above the reference plane to height $y_\max$ and then descend to height $y_0 + y.$ That is, $y_\max > y_0$ and $y_\max > y_0 + y.$ You can set $y_\max$ however you want as long as it satisfies both of those inequalities.

This implies you'll always be firing at some upward angle, even at targets far below, and the projectile will always hit the targets at a downward angle, even if the target is far above you. But it means we can divide the trajectory into an upward portion that ends with zero vertical component of velocity and a downward portion that begins with zero vertical component of velocity.

We start by computing how long it takes to reach height $y_\max$, starting from height $y_0,$ if the vertical component of velocity at $y_\max$ must be zero. This is the same as the time to fall from height $y_\max$ to height $y_0$ starting from rest. In time $t_1,$ starting from rest, supposing that $g$ is a positive number, a body will fall a distance $d = \frac12 gt_1^2.$ We need that distance to be $y_\max - y_0,$ so we need to ensure that $$ \frac12 gt_1^2 = y_\max - y_0. $$ Solving for $t_1,$ $$ t_1 = \sqrt{\frac{2(y_\max - y_0)}{g}}. $$

The initial vertical component of velocity $v_{0y}$ that is needed in order to reach height $y_\max$ from height $y_0$ is the same as the final vertical velocity falling from $y_\max$ to $y_0$, which is $gt_1$ (the speed gained during the time it takes to fall that distance); that is,

$$ v_{0y} = gt_1 = \sqrt{2g(y_\max - y_0)}. $$

The time to fall from height $y_\max$ to height $y_0 + y$ is similarly $$ t_2 = \sqrt{\frac{2(y_\max - y_0 - y)}{g}}. $$ (Just use $y_\max - (y_0 + y)$ instead of $y_\max - y_0$ in the formula we used for $t_1.$)

So we now know $t_1,$ which is the time to travel from the shooter to the top of the arc, and $t_2,$ which is the time to travel from the top of the arc to the target.

Therefore in order to hit a target which is at horizontal distance $x$ as well as vertical distance $y$ from the shooter, the projectile must be able to travel $x$ distance horizontally in time $t_1 + t_2,$ that is, the horizontal component of velocity must be $$ v_{0x} = \frac{x}{t_1 + t_2}. $$

The initial speed is therefore $$ v_0 = \sqrt{v_{0x}^2 + v_{0y}^2} $$ and (assuming $x$ is positive) the initial angle is $$ \theta = \arctan\left(\frac{v_{0y}}{v_{0x}}\right). $$

David K
  • 98,388
  • A more typical projectile motion calculation would be to assume a given initial speed $v_0$ (large enough to reach the most distant targets you want to be able to hit) and compute the angle $\theta$ at which to shoot the projectile. (There will usually be two possible solutions for $\theta$; you can take the lower one to avoid an unnecessarily high arc.) But that is not what this question asked for. – David K Feb 17 '23 at 04:26
  • Yes, this is what I wanted, basically, the projectile is a net so it makes sense that the angle would be down before hitting the target. I had to add an extra step tho, which is that the desired height should be: height + max(source.y, target.y). This way the arc height is always above both positions. – Belfer4 Feb 17 '23 at 16:50
  • I want to make this the answer to the question but I'm still unclear on how you reached those equations, if you could post the original formula that would be great! – Belfer4 Feb 17 '23 at 16:50