- Home /
Mapping from External to Unity World Coordinates
I get 3D coordinates from an external device. Can someone tell me, how I can construct a matrix or use existing Unity functions (preferably) to map 3D points from the external coordinate system to Unity's 3D world coordinate system?
The device's coordinate system is left handed like Unity's and oriented the same way (i.e. the device's x-axis is parallel to Unity's x-axis and so on), so there would be no rotation necessary. The origins are in different places and the scales of the coordinate systems are different, however.
Does anyone know, how I can construct a mapping if I only have a set of corresponding pairs of points (my guess is I would need three pairs like "point a
in the device's coordinate system, corresponds to point a*
in Unity's world coordinate system").
Cheers
Answer by Bunny83 · Jan 20, 2014 at 12:56 PM
Just create a Matrix4x4 with your desired values:
Vector3 position = new Vector3(XX,YY,ZZ);
Vector3 scale = Vector3.one * scale;
// or
//Vector3 scale = new Vector3(scaleX,scaleY,scaleZ);
Matrix4x4 mat = Matrix4x4.TRS(position, Quaternion.identity, scale);
edit
If you don't have any rotation you can also manually scale / transform the points:
Vector3 Map(Vector3 aPos)
{
aPos *= scale;
aPos += position;
return aPos;
}
2. edit
As final alternative you can simply create an empty gameobject, move it where your "new" origin of your external position should be and change the scale ot the object. All childs can be placed at the incoming external coordinates (localPosition) and to read the unity world position just read ".position".
With this approach you get the transformation matrix for free since the Transform component calculates it for you. You can also use TransformPoint and InverseTransformPoint to convert a position from one space to the other.
Thanks a bunch! How would I get the scale factor and the position/translation vector? Would the following be correct?
Calculate the scale as
Vector3.Distance (a*, b*) / Vector3.Distance (a, b)
(witha
andb
being the original points anda*
andb*
being the corresponding points in the target system).Scale
a
with the calculated factor to geta'
. Usea* - a'
as the position/translation vector.
Tried it and it worked (with the scale and translation calculated as in the above comment)! Thanks a lot!