简体   繁体   English

如何将向量转换为欧拉角

[英]How to convert a vector to a euler angle

I've scoured the net for almost three hours in an attempt to find the answer to my question. 我已经在网上搜寻了将近三个小时,以期找到问题的答案。 I have read articles and watched many videos that show how to get a vector's angle in degrees or radians. 我已阅读文章并观看了许多视频 ,这些视频显示了如何获取以度或弧度为单位的向量角度。 But that is not exactly what I'm looking for. 但这并不是我想要的。

I suspect I may not know the correct words to describe what I'm trying to do, so I decided to draw it. 我怀疑我可能不知道正确的词来描述我要做什么,所以我决定画出它。 在此处输入图片说明

In essence, I'm trying to write a simple function that takes a 2D vector as input ([3,1] in my example) and returns what I think is called a euler angle. 本质上,我试图编写一个简单的函数,将2D向量作为输入(在我的示例中为[3,1]),然后返回我认为的欧拉角。 In my example the output would be around 290. The vector could have negative coordinates, for instance [-3, -1] would output around 110. 在我的示例中,输出约为290。矢量可能具有负坐标,例如[-3,-1]的输出约为110。

I don't know if this changes anything but negative outputs are also acceptable. 我不知道这是否会改变,但是负输出也是可以接受的。 For instance, instead of 270, -90 would be a corresponding acceptable output. 例如,-270将代替270对应的可接受输出。

Since the angle from x-axis counterclockwise is invert tangent of y/x you just need to have result in degrees, increase it by 270 keeping angles domain in [0,360) like that: 由于与x轴逆时针的角度是y/x反正切,因此只需要以度为单位,就可以将其增加270,从而将角度域保持在[0,360)中,如下所示:

function euler_angle(x,y) { 
    var rad = Math.atan(y/x);   // arcus tangent in radians
    var deg = rad*180/Math.PI;  // converted to degrees
    if (x<0) deg += 180;        // fixed mirrored angle of arctan
    var eul = (270+deg)%360;    // folded to [0,360) domain
    return eul;
}

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

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