Search Unity

Rotating a Vector3's coordinates based on a Quaternion???

Discussion in 'Scripting' started by EnsurdFrndship, Aug 26, 2014.

  1. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    Hello,
    How would I rotate a Vector3's position based on a Vector3 pivot point using a Quaternion or Euler rotation from the pivotal point? I don't want to have to create an object and attach a child and destroy the object in order to do this, and would just like the new Vector3 coordinates returned from a function. I think the Unity developers should invent some functions called...

    Vector3.RotateFromPivot(point : Vector3, pivot : Vector3, rotation : Quaternion) : Vector3

    Vector3.RotateFromPivot(point : Vector3, pivot : Vector3, eulerRotation : Vector3) : Vector3

    and

    Vector2.RotateFromPivot(point : Vector2, pivot : Vector2, angle : float) : Vector2

    In the next version of Unity.
    Thank you,
    Michael S. Lowe
     
    Last edited: Aug 26, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Polymorphik likes this.
  3. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    That returns a quaternion angle. What I am looking for is a new Vector3 position that got rotated from an old Vector3 position based on a pivotal point where the pivot got rotated through either a quaternion or a Euler rotation, and to do this without having to attach any children to any empty objects while using transform rotations and then detaching the child and destroying the parent in order to figure out my new Vector3 position.

    It would make things a whole lot easier if Unity had some built-in functions (like the 3 functions above) that would quickly calculate a new Vector3 position without having to use transforms.
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I'm better in C#, though I'm sure you can convert this to UnityScript easy. *NOT TESTED* (I hope I'm right about this!)
    Code (csharp):
    1. public static Vector3 RotateFromPivot(Vector3 point, Vector3 pivot, Quaternion rotation)
    2. {
    3.   return pivot + (rotation * (point - pivot));
    4. }
    5.  
    6. public static Vector3 RotateFromPivot(Vector3 point, Vector3 pivot, Vector3 eulerRotation)
    7. {
    8.   return RotateFromPivot(point, pivot, Quaternion.Euler(eulerRotation);
    9. }
    10.  
    11. public static Vector2 RotateFromPivot(Vector2 point, Vector2 pivot, float angle)
    12. {
    13.   return RotateFromPivot(point, pivot, angle * Vector3.forward);
    14. }
    Here's that first function expanded out into the steps I took to obtain it.
    Code (csharp):
    1. public static Vector3 RotateFromPivot(Vector3 point, Vector3 pivot, Quaternion rotation)
    2. {
    3.   // Direction vector from pivot to point
    4.   Vector3 directionVector = point - pivot;
    5.   Vector3 rotatedDirectionVector = rotation * directionVector;
    6.   Vector3 rotatedPoint = pivot + rotatedDirectionVector;
    7.   return rotatedPoint;
    8. }
     
    Last edited: Aug 27, 2014
  5. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    Thank you. It works.
     
    GarthSmith likes this.