[Math] Projectile Motion

physicstrigonometry

Hello Stack Exchangers!

I'm developing a video game. One feature requires an archer be able to target an enemy and shoot an arrow at it. I've looked around and found plenty of guides on how to do this with the quadratic formula with the appropriate variables and am receiving what I believe to be the correct output, but the arrow trajectory isn't what it should be (it doesn't hit the enemy). Here is what I have (with a sample of values taken from the program).

Origin of the shot (608,-352);
Target of the shot (1280, -705);
x = 672;
y = -353;
g = -275 (this is what I'm using for gravity);
v = 1000; (The velocity the arrow is shot at);

setting up the variables:
a = $.5g(x/v)^2 = -62.0928$
b = $x = 672$
c = $.5g(x/v)^2+y = -415.0928$

$ angle 1 = atan(\cfrac{-b + \sqrt {b^2 – 4ac}}{2a}) = 0.5817$

$ angle 2 = atan(\cfrac{-b – \sqrt {b^2 – 4ac}}{2a}) = 1.4727$
Then in order to get the individual X/Y velocities from that angle (which I believe is in radians):

For Angle 1
$v_x = v cos \theta = 835.50$
$v_y = v sin \theta = 549.48$

For Angle 2
$v_x = v cos \theta = 97.90$
$v_y = v sin \theta = 995.19$

Unfortunately, the arrow doesn't arc through the target location. The second angle (from the – part of the quadratic equation) doesn't fare any better.

Any help out there?

Best Answer

The basic equations for a projectile without air resistance are (assuming initial time is set to $0$):

$$y = y_0 + v_0 \sin \theta t + \frac12gt^2$$ $$x = x_0 + v_0 \cos \theta t$$

From the second one we get $t = \frac{x-x_0}{v_0 \cos \theta}$. Plug that in in the first one, do some manipulation and we get the following:

$$ \Delta y = \tan \theta \Delta x + \frac{g (\Delta x)^2}{2 v_0^2}+\frac{g (\Delta x)^2}{2 v_0^2}\tan^2 \theta $$

What I call $\Delta x$ and $\Delta y$ are what you call $x$ and $y$, that is, the difference between target and origin of the shot. From here you can get $\theta$. It seems to me that you simply have a sign error in $c$: It should be $c = \frac{g (\Delta x)^2}{2 v_0^2} - \Delta y$, as seen from the above equation. Let me know if this works.

Related Question