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.
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 angletheta,thetacorresponds to the point(cos theta, sin theta). Why does(cos theta, sin theta)live on the unit circle? Because of everyone’s favorite identityThat is with
x = cos thetaandy = sin theta, the point(x, y)satisfiesx^2 + y^2 = 1so 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
arctanoratanand sometimestan-1). Precisely, given(x, y)on the unit circle you can find the angle corresponding to(x, y)by computingtheta = arctan(y / x).Of course, there are some messy details here. The function
arctancan’t tell the difference between the inputs(x, y)and(-x, -y)becausey / xand(-y / -x)have the same sign. Further,arctancan’t handle inputs wherex = 0. So we typically handle these by defining the functionatan2that will handle these messy details for usIn C#,
Math.Atanis the functionarctanthat I have referred to above, andMath.Atan2is the functionatan2that I have referred to above.