Guest User

Untitled

a guest
Dec 1st, 2018
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. /// <summary>
  2. /// Mirrors a heading rotation (z-forward) across a plane
  3. /// </summary>
  4. /// <param name="origHeading">the rotation to mirror</param>
  5. /// <param name="planeNormal">the normal of the plane across which to mirror the rotation</param>
  6. /// <returns>the mirrored rotation</returns>
  7. static public Quaternion MirrorHeading(Quaternion origHeading, Vector3 planeNormal)
  8. {
  9. Vector3 origHeadingVec = origHeading * Vector3.forward;
  10. Vector3 origUpVec = origHeading * Vector3.up;
  11.  
  12. if (Vector3.Dot(origHeadingVec, planeNormal) < 0.0f) { planeNormal = -planeNormal; }
  13. Vector3 newHeadingVec = MirrorVector(origHeadingVec, planeNormal);
  14. Vector3 newUpVec = MirrorVector(origUpVec, planeNormal);
  15.  
  16. return Quaternion.LookRotation(newHeadingVec, newUpVec);
  17. }
  18. #endregion
  19.  
  20. #region Vector Math
  21.  
  22. static public Vector3 MirrorVector(Vector3 vector, Vector3 normal)
  23. {
  24. Vector3 flipVec = normal * Vector3.Dot(vector, normal); // (project the vector onto the normal, then...)
  25. return vector - (2f * flipVec); // (subtracting 1x gets you v on the plan, subtracting 2x gets you the mirror)
  26. }
Advertisement
Add Comment
Please, Sign In to add comment