C# -Trigonometric code explaination (Physics)
c#, physics, trigonometry, xna
Solution
Every angle corresponds to a point on the unit circle (the unit circle is the unique circle centered at the origin with radius one; that is, the unit circle is the set of points satisfying `x^2 + y^2 = 1`). The correspondence is the following: given an angle `theta`, `theta` corresponds to the point `(cos theta, sin theta)`. Why does `(cos theta, sin theta)` live on the unit circle? Because of everyone's favorite identity
cos^2 theta + sin^2 theta = 1.
That is with `x = cos theta` and `y = sin theta`, the point `(x, y)` satisfies `x^2 + y^2 = 1` so that `(x, y)` is on the unit circle.
To reverse this, given a point on the unit circle you can find the angle by using the inverse tangent (perhaps known to you as `arctan` or `atan` and sometimes `tan``-1`). Precisely, given `(x, y)` on the unit circle you can find the angle corresponding to `(x, y)` by computing `theta = arctan(y / x)`.
Of course, there are some messy details here. The function `arctan` can't tell the difference between the inputs `(x, y)` and `(-x, -y)` because `y / x` and `(-y / -x)` have the same sign. Further, `arctan` can't handle inputs where `x = 0`. So we typically handle these by defining the function `atan2` that will handle these messy details for us
atan2(y, x) = arctan(y / x) if x > 0
= pi + arctan(y / x) if y >= 0, x < 0
= -pi + arctan(y / x) if y < 0, x < 0
= pi / 2 if y > 0, x = 0
= -pi / 2 if y < 0, x = 0
= NaN if y = 0, x = 0
In C#, `Math.Atan` is the function `arctan` that I have referred to above, and `Math.Atan2` is the function `atan2` that I have referred to above.
Problem
This piece of code has been taken from a game built with XNA framework. I'd like some explanation of how it works in terms of trig and physics. ball.velocity = new Vector2((float)Math.Cos(cannon.rotation), (float)Math.Sin(cannon.rotation)); ball.rotation is the rotation of a sprite in what i should think, radians. Why is it that they can use the angle in radians only to find the x position then the same thing to find the y position of a direction of where the hypotenuse is pointing. Reason why I asked this. I would like to get a feel of how this frameworks does calculations for trig. I am trying to get a sprite to turn in the direction of where the mouse is, that is: x and y is known, i just need the angle. So there are 2 questions here. explaining that code above and pointing a sprite in the direction of a known point. Update: I found out that the point a which the object is at is not (0,0) because xna uses inverse coordinate system. So now the variables I have are these: point of object. point of mouse.