// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Physics { /// /// Implements common logic for rotating holograms using a handlebar metaphor. /// each frame, object_rotation_delta = rotation_delta(current_hands_vector, previous_hands_vector) /// where hands_vector is the vector between two hand/controller positions. /// /// Usage: /// When a manipulation starts, call Setup. /// Call Update any time to update the move logic and get a new rotation for the object. /// internal class TwoHandRotateLogic { private Vector3 startHandlebar; private Quaternion startRotation; /// /// Setup the rotation logic. /// /// Array with positions of down pointers public void Setup(Vector3[] handsPressedArray, Transform t) { startHandlebar = GetHandlebarDirection(handsPressedArray); startRotation = t.rotation; } /// /// Update the rotation based on input. /// /// Array with positions of down pointers, order should be the same as handsPressedArray provided in Setup /// Desired rotation public Quaternion Update(Vector3[] handsPressedArray, Quaternion currentRotation) { var handlebarDirection = GetHandlebarDirection(handsPressedArray); return Quaternion.FromToRotation(startHandlebar, handlebarDirection) * startRotation; } private static Vector3 GetHandlebarDirection(Vector3[] handsPressedArray) { Debug.Assert(handsPressedArray.Length > 1); return handsPressedArray[1] - handsPressedArray[0]; } } }