简体   繁体   English

这是使用点积查找两个向量之间的角度的正确方法吗? C ++ SFML

[英]Is this the correct method of using dot product to find the angle between two vectors? C++ SFML

So in another question I was told that you could use a simplified formula of the dot product to find the angle between two vectors: 因此,在另一个问题中,我被告知您可以使用点积的简化公式来找到两个向量之间的角度:

angle = atan2(mouseY, mouseX) - atan2(yPos, xPos); //xPos, yPos is position of player

Except, from what I understood it simply takes points as vectors. 除了,据我了解,它只是将点作为向量。 Which is why the mouse and player position is plugged in the parameters. 这就是将鼠标和播放器位置插入参数的原因。 However when I move the mouse around the player in a 360 degree radius, the angle and therefore rotation of the player is a value from around -0.3... to -1.4... Is this not the correct method? 但是,当我以360度半径在播放器上移动鼠标时,播放器的角度和因此的旋转角度是-0.3 ...到-1.4 ...之间的值?这不是正确的方法吗? Am I supposed to be representing vectors not as X, Y positions but as something else? 我应该不是将向量表示为X,Y位置而是其他吗?

There's also another formula I found which also doesn't seem to work for me: 我发现还有另一个公式似乎对我不起作用:

angle = atan2(mouseY - yPos, mouseX - xPos);

The first method is correct, the second is wrong. 第一种方法是正确的,第二种是错误的。 The function atan2(x,y) computes the angle in radians of a vector pointing from (0,0) to (x,y) with respect to the x-axis. 函数atan2(x,y)计算相对于x轴从(0,0)(x,y)的向量的弧度角。 It is thus correct to calculate two angles (in radians) the vectors have wih respect to the x-axis and then subtract these from each other to obtain the angles between the two vectors. 因此,正确的做法是计算向量相对于x轴的两个角度(以弧度为单位),然后将它们彼此相减以获得两个向量之间的角度。

The result of the function atan2 is a value in the half-open interval (-pi,pi] . Expressed in degrees, this corresponds to (-180°,180°) . 0° thereby denotes a vector pointing right (along the x-axis), 90° a vector pointing up, 180° a vector pointing left and -90° a vector pointing down. 函数atan2的结果是半开间隔(-pi,pi]的值,以度表示,对应于(-180°,180°) 。0°表示指向右的矢量(沿x轴),90°指向上方的矢量,180°指向左侧的矢量,-90°指向下方的矢量。

You can transform the result in radians to angles via the formula 您可以通过以下公式将结果以弧度形式转换为角度

atan(x,y)*180/pi

So, if you want to transform your resulting values into angles, just multiply them by 180/pi . 因此,如果要将结果值转换为角度,只需将它们乘以180/pi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM