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
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)
r1
is your ship transformationatanxy
is atan2(y,x) = atan(y/x)
but with sign decomposition so it works on whole < 0,2PI >
intervalatan2,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;
}
One possible solution I found:
(r2 * Quaternion.Inverse(r1)).eulerAngles.Y