Quaternion from world space to app space.

Hello.

When I say world space, I mean real world space. I have the rotation of my android device in the form of a unit quaternion in real world space. I want to mimic the rotations in my unity app on the PC.

Up until now I have come up with a solution to point the device at the screen and press a button - then I get the initial rotation of the mesh (0, 0, 0), I get the rotation of the real world at the time, find the difference between them with the formula

Quaternion relative = Quaternion.Inverse(worldQuaternion) * initialQuaternion

then apply the difference to every world quaternion I receive and set it to my object that’s mimicking the whole thing.

MimickingObject.transform.rotation = differenceBetweenInitialAndWorldAtTimeOfPressingButton * everyWorldQuaternionFromDevice;

This corrects it only a certain case. I have a few images to illustrate.

Here is the unity coordinate system.

This is my real world axis looked at from my PC screen (This is how I orientate my device and its also the initial model rotation - parallel to the screen, with the top pointing at the sky. In this case when I click the button I get the arbitrary rotation that I get from my device, get the difference between it and Quaternion.Euler(0, 0, 0), correct the device quaternion with this difference and I get proper mimicking.

HOWEVER, when I initially orientate the device like this It’s like my axis get scrambled. Rotating around device Y rotates around app X and so on.

Any help will be greatly appreciated, because I can’t seem to be able to ask the right questions and find an answer.

It sounds like you’re saying the coordinate space of the ‘real world’ (the rotation as it comes from the phone) is in an x-forward, z-up, y-left system. As opposed to unity’s z-forward, y-up, x-right system.

What you should do is convert the quat, you should be able to do this on a piece-wise basis.

var quat = *origin quat, your real-world rotation*;
var convertedQuat = new Quaternion(-quat.y, quat.z, quat.x, quat.w);

If that doesn’t work, try converting to axis-angle, transpose the axis, and then create the new quat.

1 Like