Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am making a 2d game in which units fire arrows at each other. I know the shooter's and the target's position and the initial velocity of the projectile. I want to know the angle the projectile should have in order to land on the target. The target could be at a different height than the shooter.

To sum up I know v0, R and g and I need to find the angle (or the height ?).

I read http://en.wikipedia.org/wiki/Projectile_motion ,but I can't find something related to what I need.

share|improve this question
    
It depends if you want to stick to realistic ballistic curve or is a parabola good enough. –  Aron Boguta Apr 9 '13 at 9:29
    
Do you want to include wind? Or any other horizontal acceleration? (It makes the math harder, of course) –  Seth Battin Apr 9 '13 at 12:52
1  
possible duplicate of How to make an arrow land at a specific position in 3D world space –  Byte56 Apr 9 '13 at 13:21
    
I want to achieve a realistic projectile motion and there is no wind involved. –  korn3l Apr 10 '13 at 11:09

2 Answers 2

up vote 8 down vote accepted

The formula to find the angle is

Formula

where v is initial launch speed, g is the gravity constant, x and y are the target's distance and height.

The two roots of this equation give you two possible angles. If the results are imaginary then your initial velocity is not great enough to reach the target (if you want to calculate the angle of reach read this). It's up to you which angle is selected. It would make sense to choose the most direct path i.e. the smaller angle.

You can see a GIF of this equation below with different target values and a constant launch velocity.

Formula graphed as animated GIF

Resources from this wikipedia article

share|improve this answer
    
Note that in most cases there are two valid solutions. Assuming no drag or the like max range is attained when the projectile is fired at a 45 degree angle. Going higher OR lower will lower the range--thus unless you need every bit of oomph from your gun there will be both a higher and a lower solution. –  Loren Pechtel Apr 10 '13 at 3:26
    
You would probably take the angle that has the shortest flight time, which is usually smaller angle (probably always but I'm allowing myself the possibility of being wrong gracefully heh). It's faster to shoot at the ground in front of your feet by aiming down than by aiming up at a really steep angle. –  Azaral Apr 10 '13 at 5:38
    
@StephenTierney Thank you for the answer. This is what I was looking for. –  korn3l Apr 10 '13 at 11:09
    
Found a much simpler solution to this problem, info from en.wikipedia.org/wiki/… –  Stephen Tierney Apr 11 '13 at 2:18

Earlier this year I created a simple top down shooter. I used the following method:

Earlier answer: http://stackoverflow.com/questions/15364852/move-sprite-diagonally/15365570#15365570

public static class Helper_Direction
{

    // Rotates one object to face another object (or position)
    public static double FaceObject(Vector2 position, Vector2 target)
    {
        return (Math.Atan2(position.Y - target.Y, position.X - target.X) * (180 / Math.PI));
    }

    // Creates a Vector2 to use when moving object from position to a target, with a given speed
    public static Vector2 MoveTowards(Vector2 position, Vector2 target, float speed)
    {
        double direction = (float)(Math.Atan2(target.Y - position.Y, target.X - position.X) * 180 / Math.PI);

        Vector2 move = new Vector2(0, 0);

        move.X = (float)Math.Cos(direction * Math.PI/180) * speed;
        move.Y = (float)Math.Sin(direction * Math.PI / 180) * speed;

        return move;
    }
}

It calculates a trajectory between two positions.

share|improve this answer

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.