问题
I have a 3D object with rotation r1 in a quaternion form. I rotate it with local euler angles:
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
Now I have rotation r2. How can I retrieve the local Y rotation sum 15-5+0=10 if I don\'t know what angles have been applied? It may be impossible to get exactly that value (10) but you\'ve got my idea. May be I can just get Y diff in the local r2 space?
回答1:
One possible solution I found:
(r2 * Quaternion.Inverse(r1)).eulerAngles.Y
回答2:
I am still convinced that transform matrices will be much better approach for you
As mentioned in previous question Euler angles are not the best for your purpose and only mess thing up for you but anyway what about this:
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)
I assume r1
is ship and r2
is radar
[Edit1] after read of your edit from linked question is finally clear what you want
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)
- where
r1
is your ship transformation - radar transformation is irelevant to background image
atanxy
isatan2(y,x) = atan(y/x)
but with sign decomposition so it works on whole< 0,2PI >
interval
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;
}
来源:https://stackoverflow.com/questions/21681992/3d-relative-angle-sum-calculation