3D相对角度和计算

问题描述 投票:3回答:2

我有一个以四元数形式旋转r1的3D对象。我用当地的欧拉角旋转它:

transform.Rotate(new Vector3(0f, 15f, 0f), relativeTo: Space.Self); // right
transform.Rotate(new Vector3(-10f, -5f, 0f), relativeTo: Space.Self); // left up
transform.Rotate(new Vector3(0f, 0f, 90f), relativeTo: Space.Self); // 90 clockwise

现在我有旋转r2。如果我不知道应用了哪些角度,如何检索局部Y旋转和15-5 + 0 = 10?可能无法获得完全相同的价值(10),但你已经有了我的想法。也许我可以在当地的r2空间获得Y diff?

c# math unity3d euler-angles
2个回答
6
投票

我发现一个可能的解决方案

        (r2 * Quaternion.Inverse(r1)).eulerAngles.Y

0
投票

我仍然相信变换矩阵对你来说会更好

正如前面提到的问题所提到的,欧拉角度并不是最适合您的目的,只会让您感到困惑,但无论如何:

P0=(0,0,0)
P1=(1,0,0) // or (0,0,1) y=0 !!!
A0=r2_localtoglobal(P0)
A1=r2_localtoglobal(P1)
B0=r2r1_localtoglobal(P0)
B1=r2r1_localtoglobal(P1)
A=A1-A0 // local r2 X axis direction in GCS (without r1)
B=B1-B0 // local r2r1 X axis direction in GCS (with r1)
angle=-acos((A.B)/(|A|.|B|)) // angle between A,B (but inverted because you wanted local angle)

我认为r1是船,r2是雷达

从链接问题中读取您的编辑后,[Edit1]终于清楚了您想要的内容

P0=(0,0,0)
P1=(1,0,0) // or (0,0,1) y=0 !!!
A0=r1_globaltolocal(P0)
A1=r1_globaltolocal(P1)
A=A1-A0   
angle=atanxy(A.x,A.z)
  • 其中r1是你的船改造
  • 雷达变换与背景图像无关
  • atanxyatan2(y,x) = atan(y/x),但有符号分解所以它适用于整个< 0,2PI >间隔

ATAN2,atanxy:

const double pi=M_PI;
const double pi2=2.0*M_PI;

double atanxy(double x,double y) // atan2 return < 0 , 2.0*M_PI >
        {
        int sx,sy;
        double a;
        const double _zero=1.0e-30;
        sx=0; if (x<-_zero) sx=-1; if (x>+_zero) sx=+1;
        sy=0; if (y<-_zero) sy=-1; if (y>+_zero) sy=+1;
        if ((sy==0)&&(sx==0)) return 0;
        if ((sx==0)&&(sy> 0)) return 0.5*pi;
        if ((sx==0)&&(sy< 0)) return 1.5*pi;
        if ((sy==0)&&(sx> 0)) return 0;
        if ((sy==0)&&(sx< 0)) return pi;
        a=y/x; if (a<0) a=-a;
        a=atan(a);
        if ((x>0)&&(y>0)) a=a;
        if ((x<0)&&(y>0)) a=pi-a;
        if ((x<0)&&(y<0)) a=pi+a;
        if ((x>0)&&(y<0)) a=pi2-a;
        return a;
        }
© www.soinside.com 2019 - 2024. All rights reserved.