Created
January 29, 2013 02:55
-
-
Save benloong/4661336 to your computer and use it in GitHub Desktop.
Angle theta required to hit coordinate (x,y) based on http://en.wikipedia.org/wiki/Trajectory_of_a_projectile#Angle_required_to_hit_coordinate_.28x.2Cy.29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
class MathHelper { | |
float static CalcAngleOfElevation (Vector3 from, Vector3 to, float v, float g) { | |
float x = (to - from).z; | |
float y = (to - from).y; | |
float v2 = v * v; | |
float v4 = v2 * v2; | |
float fac = v4 - g*(g*x*x + 2*y*v2); | |
if(fac < 0) { | |
return Mathf.Infinity; | |
} | |
float theta = -Mathf.Atan((v2-Mathf.Sqrt(fac))/(g*x)) * Mathf.Rad2Deg; | |
while(theta < 0) theta += 360f; | |
return theta; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment