Tuesday 15 February 2011

c# - Hit point with projectile -



c# - Hit point with projectile -

i trying calculate right angle cannon needs @ nail target point. code below sets angle 90 degrees. using c# , unity. suggestions doing wrong?

double x = -(source.x - position.x); double y = (source.y - position.y); double v = 500; //m/s double g = physics2d.gravity.y; double sqrt = (v * v * v * v) - (g * (g * (x * x) + 2 * y * (v * v))); sqrt = math.sqrt(sqrt); double angleinradians = math.atan2(((v * v) + sqrt), (g * x)); double degrees = angleinradians * mathf.rad2deg; //this 90 vector3 angle = new vector3(0,180,(float)degrees); _cannon.transform.localeulerangles = angle;

according mathf.atan2 definition:

if y positive , x 0, θ = π/2.

x sec parameter, g * x results in 0. basic math tells can if or of operands equal 0.

so, in essence, either:

-(source.x - position.x) or physics2d.gravity.y

is equal 0. i'd guess gravity's y value culprit here.

c# unity3d trigonometry

No comments:

Post a Comment