3

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?

Vlad
  • 3,001
  • 1
  • 22
  • 52
  • So you applied r1 via a quaternion and you want to apply a second transform in conjunction with the r1 called r2 which I assume is a quaternion? If that is the case you don't need to get r1, you just apply r2 to the result of r1. They are simply compounded. so `r3 = r2.r1` then `T1 = r1.T0` and `T2 = r2.T1` then `T2 = r2.r1.T0` which is `T2 = r3.T0` – Felix Castor Feb 10 '14 at 16:55
  • @FelixCastor, r1 is the starting rotation. r2 is the rotation after applying all the transformations. "you want to apply a second transform in conjunction with the r1" - no, I want to find the local (for r2) Y angle difference between r1 and r2. – Vlad Feb 10 '14 at 17:25
  • see edit1 in my answer – Spektre Feb 11 '14 at 09:09

2 Answers2

6

One possible solution I found:

        (r2 * Quaternion.Inverse(r1)).eulerAngles.Y
Vlad
  • 3,001
  • 1
  • 22
  • 52
0

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 is atan2(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;
        }
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • 1
    As you see I used a unity3d tag so I don't expect a math-complicated answer. P0, P1 - what is it? I don't understand how you use that localtoglobal. And than, are you subtracting rotations in the "A=A1-A0"? Are you passing x and z components of the two quaternions to the atan function? You provided the atan methods' source code while I can just use the System.Math.Atan/Atan2 but you didn't provide details on a lot more complicated things. – Vlad Feb 12 '14 at 12:17
  • Sorry I am not Unity User, P0,P1,A0,A1,B0,B1,A,B are 3D points/vectors I taught it is obvious enough) and local to global means you have to convert between coordinate systems (use your unity euler angle math functions for that). This answer is far from complicated and if you do not grasp it you should take a look for basic vector math in 3D space. Without proper knowledge you soon hit a wall in your programming (do not expect there is math function for any occasion inside some library you always will need to code something yourself) – Spektre Feb 12 '14 at 12:33
  • Idea is to compare rotated and unrotated axis and get their angle between them – Spektre Feb 12 '14 at 12:35
  • This raw math is admirable, but unfortunately just has absolutely no connection to using Unity. (It would be like offering the math of a collison, when someone wants to know "how to bounce objects Unity!", Heh. All you do is invert the rotation, it's trivial - rotations are "built in" to Unity (of course - it's a game engine, if you couldn't move things around it wouldn't exist!) – Fattie Feb 04 '16 at 01:41