Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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?

share|improve this question
    
Just to check to begin with, you aren't mixing up coordinate systems, are you? Is positive $y$ either always upwards or always downwards? –  SiliconCelery Aug 6 '12 at 20:29
    
Positive y is always upwards. The origin of the map is in the upper left, so I'm always dealing with negative y values. In this case, the target is lower than the origin of the shot so we have a negative elevation. –  GorillaOne Aug 6 '12 at 20:34
    
What are a, b and c? –  SiliconCelery Aug 6 '12 at 20:35
    
a,b, and c are the different parts of the Quadratic Equation. en.wikipedia.org/wiki/Quadratic_equation –  GorillaOne Aug 6 '12 at 20:41

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks Javier, this works great. Thank you for pointing out the error. –  GorillaOne Aug 6 '12 at 22:22
    
@Javier I'm getting a coefficient of $\Delta x$ for the $\tan{\theta}$ term. What am I doing wrong? –  ladaghini Aug 6 '12 at 23:11
    
@ladaghini: Nothing, my mistake. –  Javier Badia Aug 6 '12 at 23:16

Without looking at the details of your question, I have one suggestion: be careful of applying mathematically theoretical equations to computer programming. In particular, you must be very, very, very careful of round-off errors. I have learned this the hard way. When you try to plot graphics using decimal values there is almost always a round-off error because pixels are referenced with integer coordinates.

I need to log off soon, so I will add more details here later if needed.

share|improve this answer
    
The only time I convert from double to float is during the atan calculation. $angle1 = (float)atan(calculationresults);$ –  GorillaOne Aug 6 '12 at 20:36
    
It looks like you found the problem. My comments here were directed at converting a floating-point value (whether single- or double-precision) to an integer pixel coordinate. Of course, these details depend on what graphics platform you are using and if you are doing these calculations manually or if your graphics library handles them. –  Code-Guru Aug 6 '12 at 23:14

The position equations you use should be $y=y_o+v_yt+\frac{1}{2}gt^2$ and $x=x_o+v_xt$. Initial angle would come from $atan(\frac{y_f-y_o}{x_f-x_o})$, and you could use that to calculate your $x$ and $y$ velocities using cosine and sine like you were. From there, you would hold your $x$ velocity constant and your $y$ velocity changes as $v_y=v_{yo}+at$, but your motion equation should do this for you.

share|improve this answer
    
I don't know time, just $v_i$, the origin location, and target location. If I knew time, this would be much simpler, but it will vary from shot to shot. –  GorillaOne Aug 6 '12 at 20:42
    
@GorillaOne: If you know those, you can find out the time from $x=x_0+v_x t$ –  Javier Badia Aug 6 '12 at 20:52
    
Unless I'm missing something here, I can't find out $t$ unless i know $v_x$, and I can't get $v_x$ until I get the $\theta$, which is what I'm using the quadratic equation for - to solve for $\theta$ without time. Yes? To be clear, I don't know $v_x$ or $v_y$, I just know $v$. –  GorillaOne Aug 6 '12 at 21:04
    
@GorillaOne: Yes, sorry, I messed that up. I'll write an answer if I figure it out. –  Javier Badia Aug 6 '12 at 22:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.