# Rotation from Vectors

Discussion in 'Physics' started by En_Bee, May 15, 2018.

1. ### En_Bee

Joined:
Apr 26, 2018
Posts:
3
I'm sure this has probably been asked before but I've been searching and cannot find an answer. I am trying to update the rotation of game objects based on x, y, and z vectors.

I exported an assembly from other software to separate STL files because it did not offer any other compatible format. Then, I imported the STL files into Blender and exported an FBX file. In doing this, I basically lost the assembly relationship information. I created an XML file during the export with the assembly information and am using it to post process the assembly in Unity. The data looks like:

``````<part name="my part" parent="parent part" id="53" parentId="187">
<origin>5.43007,-1.29000,-4.59284</origin>
<x>-0.00000,-1.00000,0.00000</x>
<y>-0.63608,0.00000,-0.77162</y>
<z>0.77162,-0.00000,-0.63608</z>
</part>``````

Updating the position was pretty straightforward. The vectors in the x, y, and z values are what I was able to extract from the CAD software to determine the rotation of the part in the assembly. Any idea how I can turn these into a Quaternion to rotate my parts? Basically, point (0, 0, 0) to each of the specified points are the axis vector for each axis specified.

2. ### Edy

Joined:
Jun 3, 2010
Posts:
1,143
If my memory serves me correctly, you should use those vectors as columns of a rotation matrix. Then you could extract a quaternion out of it with Matrix4x4.quaternion.

Depending on the rotation convention, you may need to change the vector order and/or switch the direction of some of them.

3. ### En_Bee

Joined:
Apr 26, 2018
Posts:
3
Is this what you're thinking?
``Quaternion rotation = Matrix4x4.LookAt(Vector3.zero, xvector, yvector).rotation;``

It looks like it may be working but I'm not 100% sure because it doesn't line up the way the original model.I think the source model isn't consistent and different parts and sub-assemblies may beusing different scales .

Thank you for the help with this!

4. ### Edy

Joined:
Jun 3, 2010
Posts:
1,143
No, I'm thinking on creating a Matrix4x4, populate its columns with the vectors via Matrix4x4.SetColumn, then get the rotation out of it.

Oh! The example code for Matrix4x4.SetColumn shows exactly how to build the matrix out of the vectors:
https://docs.unity3d.com/ScriptReference/Matrix4x4.SetColumn.html

It should be something like this:
Code (CSharp):
1. matrix.SetColumn(0, rightVector);
2. matrix.SetColumn(1, upVector);
3. matrix.SetColumn(2, forwardVector);
4. matrix.SetColumn(3, new Vector4(0, 0, 0, 1));
You should only figure out how your vectors (xvector, yvector, zvector) translate to direction vectors in Unity (forward +Z, up +Y, right +X). For example, some coordinate systems assume "up" to be Z instead of Y, or "right" to be -X instead of +X.

En_Bee likes this.
5. ### En_Bee

Joined:
Apr 26, 2018
Posts:
3
That worked. Thank you for the help with this. You rock!

Edy likes this.

Joined:
Jun 3, 2010
Posts:
1,143
My pleasure!