C#-三角函数代码解释(物理)

发布于 2024-08-07 05:52:17 字数 530 浏览 7 评论 0原文

这段代码取自一个使用 XNA 框架构建的游戏。我想要一些关于它在三角学和物理学方面如何工作的解释。

ball.velocity = 新 Vector2((float)Math.Cos(cannon.rotation), (float)Math.Sin(cannon.rotation));

ball.rotation 是精灵以我认为的弧度旋转。

为什么他们只能使用以弧度为单位的角度来找到 x 位置,然后用同样的方法来找到斜边指向的方向的 y 位置。

我问这个的原因。我想了解一下这个框架如何进行三角计算。我试图让精灵朝鼠标所在的方向转动,即:x 和 y 已知,我只需要角度。

所以这里有2个问题。解释上面的代码并将精灵指向已知点的方向。

更新:

我发现对象所在的点 a 不是 (0,0),因为 xna 使用逆坐标系。所以现在我拥有的变量是:

对象点。 鼠标点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诗酒趁年少 2024-08-14 05:52:17

每个角度对应于单位圆上的一个点(单位圆是以原点为圆心、半径为一的唯一圆;也就是说,单位圆是满足 x^2 + y^2 = 1 的点的集合)。对应关系如下:给定一个角度thetatheta对应于点(cos theta, sin theta)。为什么(cos theta, sin theta) 位于单位圆上?由于大家最喜欢的恒等式

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

x = cos thetay = sin theta,点 (x, y) 满足 x^2 + y^2 = 1 使得 (x, y) 位于单位圆上。

要反转这个,给定单位圆上的一点,您可以使用 反正切 找到角度(您可能将其称为 arctanatan,有时称为 tan-1) 。准确地说,给定单位圆上的 (x, y),您可以通过计算 theta = arctan(y / x) 找到与 (x, y) 对应的角度)

当然,这里有一些混乱的细节。函数 arctan 无法区分输入 (x, y)(-x, -y) 之间的差异,因为 y / x(-y / -x) 具有相同的符号。此外,arctan 无法处理 x = 0 的输入。因此,我们通常通过定义将处理这些的函数 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

在 C# 中,Math.Atan是我上面提到的函数 arctan,以及 Math.Atan2 是我上面提到的函数 atan2

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.

素食主义者 2024-08-14 05:52:17
     |
    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)

现在,如果您有 x,y 并且您想要:

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

了解更多信息(和更漂亮的图表): 维基百科:极坐标系

     |
    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

笔芯 2024-08-14 05:52:17

您可以看到 cos 和 sin 返回圆上的点。

在这方面,将佳能的中间视为圆的中心。然后给定一个角度(佳能的角度),您可以通过 sin 和 cos 获得它所指向的圆上的位置。
如果您认为大炮以 0,0 位置为中心,那么该值也是子弹应行进的方向。

答案2:如果你知道x和y并且你需要知道角度..你需要atan函数,它返回从三角形的斜边形成的角度,其中一个点是0,0,另一个点是x,y点和一个点是 90 度角的点

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

梦巷 2024-08-14 05:52:17

遗憾的是,这是一个很好的问题,但 SO 并不是最佳的回答格式。

我认为学习参数方程会很有帮助,而不是用文本解释。你可以先在谷歌中搜索“圆参数方程”。

这个概念对我来说很重要,就是尝试不同的代码,直到我理解 sin、cos、圆和角度之间的关系。查看图片和图像也有很大帮助。在此之前,我会阅读说明,但永远无法牢牢理解其中的解释。

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.

¢蛋碎的人ぎ生 2024-08-14 05:52:17

如果您不熟悉三角函数,您所问的问题很难解释。

有问题的代码行计算球方向的单位向量,我认为球将从大炮中发射。事物的 Cos 和 Sin 部分分别提取大炮角度的 X 和 Y 分量。所以,大炮指向的地方,就是球射出的方向。

这有点误导,因为结果很可能只是一个方向,而不是实际速度。我假设该线下方有一条线将该向量乘以一个常数,从而得到球的最终移动速度。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文