How can I know the height to shoot an arrow to get it to hit a certain location?

296 Views Asked by At

I am currently creating a game where archers will have to shoot each other in a 3D space.

I have got the correct X and Z directions to shoot the arrow torward the enemy archer, but how can I know how high to shoot the arrow so that it lands in the correct place?

Every update the arrow will move by it's velocity, and the velocity's y that is given to it will go down by 0.1 every time.

I could potentially check the entire arrow's projectile path and see where it lands, but that is intensive, and I was wondering if there was a mathematical solution.

2

There are 2 best solutions below

2
On BEST ANSWER

Certainly, as long as you are inoring air resistance and arrow-leveling effects from the feathers.

The initial upward velocity is $ v_y = s \sin \theta$ where $s$ is the initial speed of the arrow and $\theta$ is the initial angle of the shot (with respect tot the ground). This velocity will decrease bby the gravitational acceleration, 9.8 meters per second less each second.

The initial horizontal velocity is $v_x = s \cos \theta$. This will stay constant

To hit the target you want to arrive at the correct horizontal distance at just the time the arrow comes to the height of the target.

If the target is at the same height at the shooting point of the arrow, and at some distance $d$, this gives in the end $$ \sin 2\theta = \frac{gd}{s^2}$$.

Even if the heights differ, you just end up with a quadratic equation for $cos^2 \theta$ and you can solve that with the quadratic formula.

Notice that in both cases their are generally two solutions (except if you are shooting the arrow as far as it can go given this initial speed $s$). One has you shooting high and falling on the target; the other, shooting lower and hitting the target in the face.

0
On

The fact that the $y$ velocity decreases by $0.1$ per timestep sets your timestep. Do you want to do that? If you are using metric units, you timestep is $\frac{0.1}{9.8}\approx 0.0102$ seconds. You can compute the time of flight from the $X$ and $Z$ directions-just divide the distance to go by the velocity. Then the arrow will fall $\frac 12gt^2$ in that time. Since your timesteps are $0.0102$ seconds, the arrow falls $\frac 12g(0.0102T)^2$, where $T$ is the number of timesteps until you hit the $X/Z$ position. Aim that much above the true $Y$ and you are good.