Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ProjectileAim
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine(AimAngle(10, 1, 5));
- Console.WriteLine(AimAngle(10, 5, 1));
- Console.WriteLine(AimAngle(1000, 100000, 2));
- Console.Write("Press any key to exit : ");
- Console.ReadKey();
- }
- /// <summary>
- /// Aim at a relative position
- /// </summary>
- /// <param name="x">Target ground distance</param>
- /// <param name="y">Target relative altitude</param>
- /// <param name="v">Projectile initial velocity magnitude</param>
- /// <param name="highArc">Higher arc (larger than 45 degrees)</param>
- /// <param name="g">Gravity acceleration</param>
- /// <param name="returnDegrees">Convert result from radians to degrees</param>
- /// <returns>Elevation angle</returns>
- static double AimAngle(double v, double x, double y, bool highArc = false, bool returnDegrees = true, double g = 9.807)
- {
- //LaTeX: \arctan{\left(\frac{v^2\pm\sqrt{v^4-g(gx^2+2yv^2)}}{gx}\right)}
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement