C # - explanation of trigonometric code (physics)

This code snippet was taken from a game built using XNA. I would like to get some explanation of how this works in terms of trigger and physics.

ball.velocity = new Vector2 ((float) Math.cos (cannon.rotation), (float) Math.sin (cannon.rotation));

ball.rotation - The rotation of the sprite in what I should be thinking, radians.

Why they can only use the angle in radians to find the x position is the same thing to find the y position of the direction where the hypotenuse points.

Why did I ask about this. I would like to get an idea of ​​how this framework performs calculations for a trigger. I am trying to get a sprite to rotate in the direction where the mouse is, i.e.: x and y are known, I just need the angle.

So there are two questions here. explaining this code above and pointing the sprite towards a known point.

Update:

I found out that the point a that the object is at is not (0,0) because xna uses the inverse coordinate system. So now the variables I have are:

point of object. mouse points.

+2


source to share


5 answers


Each corner corresponds to a point on the unit circle (a unit circle is a single circle centered at the origin with a radius of one, i.e. a unit circle is a set of points that satisfy the condition x^2 + y^2 = 1

). The correspondence is as follows: given the angle theta

, the theta

point corresponds (cos theta, sin theta)

. Why (cos theta, sin theta)

does he live on a single circle? For every beloved personality

cos^2 theta + sin^2 theta = 1.

      

That is, with x = cos theta

and the y = sin theta

point (x, y)

satisfies x^2 + y^2 = 1

, so it (x, y)

is on the unit circle.



To reverse this, given a point on the unit circle, you can find the angle using the inverse tangent , which will handle those messy details for us atan2

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

, this is the function arctan

I talked about above and Math.Atan2

is the function atan2

I talked about above.

+10


source


     |
    y.-----* P
     |    /|
     |   / |
     | r/  |
     | / a |
     |/)___.__
    O          x

    we have:

    a = angle in radians
    O: origin
    P: known point
    r: distince between O & P

    to calculate x, y:

         x = r*cos(a)
         y = r*sin(a)

(in your example : r = 1, a = cannon.rotation)

      

Now if you have x, y and want:



if x!= 0  a = atan(y/x)
otherwise a = sign(y)*Pi/2

      

for more information (and prettier graphs): Wikipedia: Polar coordinate system

+4


source


You can see that cos and sin return point per circle.

In this regard, see the middle of the canon as the center of the circle. Then, given the angle (canon angle), you can get the position on the circle it points to with sin and cos. If you think of the gun being centered at the 0.0 position, then that value is also the direction the bullet should move.

answer2: if you know x and y and you need to know the angle. You need an atan function that returns the angle formed by the oblique side of a triangle where one point is 0,0 and the other point is the x, y point and one point is the point that is at 90 degrees

+2


source


Unfortunately this is a good question, where SO is not the best format to answer.

Instead of an explanation in the text, I think it would be useful to learn about parametric equations. You can start by searching for "parametric equation of the circle" on Google.

The way this concept pushed for me was to experiment with different pieces of code until I figured out the relationship between sin, cos, circles and angles. Seeing pictures and images also helps. I used to read descriptions but could never firmly understand the explanation.

+2


source


What you are asking is difficult to explain if you are not familiar with the trigger.

The line of code in question computes a unit vector for the direction of the ball that I believe will be fired from the cannon. The cosmic and sinful parts of things extract the X and Y components respectively from the corner of the cannon. So where the cannon indicates that the ball is shooting.

This is a little misleading because the result is most likely only direction and not actual speed. I would suggest that below is the line that multiplies this vector by a constant to give the ball its final speed.

0


source







All Articles