Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Rotation from Vectors

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

  1. En_Bee

    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

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,212
    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

    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 o_O.

    Thank you for the help with this!
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,212
    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. Matrix4x4 matrix = new Matrix4x4();
    2. matrix.SetColumn(0, rightVector);
    3. matrix.SetColumn(1, upVector);
    4. matrix.SetColumn(2, forwardVector);
    5. matrix.SetColumn(3, new Vector4(0, 0, 0, 1));
    6. Quaternion rotation = matrix.rotation;
    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

    En_Bee

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

    Edy

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