3D 相对角度和计算



>我有一个旋转 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 差异?

我找到的一个可能的解决方案:

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

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

如上一个问题所述,欧拉角不是最适合您的目的,只会让您搞砸事情,但无论如何,这又如何:

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是雷达

[编辑1] 从链接的问题中阅读您的编辑后,终于清楚您想要什么

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在哪里
  • 雷达变换与背景图像无关
  • atanxy atan2(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;
        }

最新更新