开发者

C# -Trigonometric code explaination (Physics)

开发者 https://www.devze.com 2022-12-08 18:13 出处:网络
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.

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.开发者_JAVA百科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 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.


     |
    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 you want a:

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

for more informations (& prettier graphs): Wikipedia: Polar coordinate system


You can see cos and sin returning the point on a circle.

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

answer2: if you know x and y and you need to know the angle..you need the atan function which returns the angle formed from the sloping side of the triangle where one point is 0,0, the other point is the x,y point and one point is a point which is at the 90 degree angle


Sadly, this is a good question where SO isn't the best format to answer in.

Instead of explaining in text, I think it would be helpful to learn about parametric equations. You can start by searching "circle parametric equation" in Google.

The way that this concept clicked for me was to experiment with different pieces of code until I understood the relation between sin, cos, circles, and angles. Seeing pictures and images help a lot as well. Before then I would read descriptions but could never firmly grasp the explanations.


What you're asking is difficult to explain if you're not familiar with trig.

The line of code in question calculates a unit vector for the direction of the ball, that I presume will be fired from the cannon. The Cos and Sin part of things extract the X and Y components, respectively, of the cannon's angle. So, where the cannon points, that's the direction the ball shoots.

It's a little misleading because the result is most likely only a direction, not an actual velocity. I would assume there's a line below that one that multiplies that vector by a constant, to give the ball its final movement speed.

0

精彩评论

暂无评论...
验证码 换一张
取 消