When you develop AR/MR applications in Unity you will always arrive at a point where you have to transform coordinates of some right-handed coordinate system into Unity’s left-handed pendant or vice-versa. One would think that there is plenty of documentation and example code that would help you with that. But, no, that is not the case at all. I browsed through quite some code recently and it appears to me that people often found a working transformation by trial-and-error instead of just digging into the docs and doing the math.
This article provides an overview of the different coordinate systems that can be involved and how to transform between them.
Let’s start with the definition of each coordinate system…
What does it mean to transform between any two of these coordinate systems? Well, it basically means that you have to flip certain components to get from one to the other.
Let’s look at an example: Assuming we would like to transform coordinates from OpenCV to Unity, the OpenCV vector (x, y, z) would become (x, -y, z) in Unity. What does this mean in terms of code? Let’s assume we have an OpenCV method findTrackedObject that returns a transformation in OpenCV’s coordinate system. The code below transforms this matrix into Unity’s coordinate system by negating the y-component of each column:
Matrix4x4 transform = OpenCV.findTrackedObject(...);transform.M21 *= -1;
transform.M22 *= -1;
transform.M23 *= -1;
transform.M24 *= -1;
The table below summarizes all combinations of transformations between any two of the coordinate systems we have looked at in this blog post.