12
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ It depends if you want to stick to realistic ballistic curve or is a parabola good enough. \$\endgroup\$
    – AB.
    Apr 9, 2013 at 9:29
  • \$\begingroup\$ Do you want to include wind? Or any other horizontal acceleration? (It makes the math harder, of course) \$\endgroup\$ Apr 9, 2013 at 12:52
  • 1
    \$\begingroup\$ possible duplicate of How to make an arrow land at a specific position in 3D world space \$\endgroup\$
    – House
    Apr 9, 2013 at 13:21
  • \$\begingroup\$ I want to achieve a realistic projectile motion and there is no wind involved. \$\endgroup\$
    – korn3l
    Apr 10, 2013 at 11:09

2 Answers 2

13
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 10, 2013 at 3:26
  • \$\begingroup\$ 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. \$\endgroup\$
    – Azaral
    Apr 10, 2013 at 5:38
  • \$\begingroup\$ @StephenTierney Thank you for the answer. This is what I was looking for. \$\endgroup\$
    – korn3l
    Apr 10, 2013 at 11:09
  • \$\begingroup\$ Found a much simpler solution to this problem, info from en.wikipedia.org/wiki/… \$\endgroup\$ Apr 11, 2013 at 2:18
2
\$\begingroup\$

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

Earlier answer: https://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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .