Advertisement
Why7090

ProjectileAim

Oct 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ProjectileAim
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.WriteLine(AimAngle(10, 1, 5));
  10.             Console.WriteLine(AimAngle(10, 5, 1));
  11.             Console.WriteLine(AimAngle(1000, 100000, 2));
  12.  
  13.             Console.Write("Press any key to exit : ");
  14.             Console.ReadKey();
  15.         }
  16.  
  17.         /// <summary>
  18.         /// Aim at a relative position
  19.         /// </summary>
  20.         /// <param name="x">Target ground distance</param>
  21.         /// <param name="y">Target relative altitude</param>
  22.         /// <param name="v">Projectile initial velocity magnitude</param>
  23.         /// <param name="highArc">Higher arc (larger than 45 degrees)</param>
  24.         /// <param name="g">Gravity acceleration</param>
  25.         /// <param name="returnDegrees">Convert result from radians to degrees</param>
  26.         /// <returns>Elevation angle</returns>
  27.         static double AimAngle(double v, double x, double y, bool highArc = false, bool returnDegrees = true, double g = 9.807)
  28.         {
  29.             //LaTeX: \arctan{\left(\frac{v^2\pm\sqrt{v^4-g(gx^2+2yv^2)}}{gx}\right)}
  30.             return Math.Atan2(v * v + (highArc ? 1 : -1) * Math.Sqrt(Math.Pow(v, 4) - g * (g * x * x + 2 * y * v * v)), (g * x)) * (returnDegrees ? (180 / Math.PI) : 1);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement