microsoft / MixedRealityToolkit-Unity

This repository is for the legacy Mixed Reality Toolkit (MRTK) v2. For the latest version of the MRTK please visit https://github.com/MixedRealityToolkit/MixedRealityToolkit-Unity
https://aka.ms/mrtkdocs
MIT License
6k stars 2.12k forks source link

Issue porting code from Academy to latest HoloToolkit. #565

Closed alamboley closed 7 years ago

alamboley commented 7 years ago

Hello there,

I played with Hololens lately, it's really nice :) I watched the tutorials from the Academy, however since HoloToolkit evolved I've some troubles to port old examples.

This code, from the academy, is just making a rotation on a object. How could I do the same with the latest HololensToolkit version?

using Academy.HoloToolkit.Unity;
using UnityEngine;

public class GestureAction : MonoBehaviour
{
    [Tooltip("Rotation max speed controls amount of rotation.")]
    public float RotationSensitivity = 10.0f;

    private Vector3 manipulationPreviousPosition;

    private float rotationFactor;

    void Update()
    {
        PerformRotation();
    }

    private void PerformRotation()
    {
        if (GestureManager.Instance.IsNavigating &&
            (!ExpandModel.Instance.IsModelExpanded ||
            (ExpandModel.Instance.IsModelExpanded && HandsManager.Instance.FocusedGameObject == gameObject)))
        {

            rotationFactor = GestureManager.Instance.NavigationPosition.x * RotationSensitivity;

            transform.Rotate(new Vector3(0, -1 * rotationFactor, 0));
        }
    }

    void PerformManipulationStart(Vector3 position)
    {
        manipulationPreviousPosition = position;
    }

    void PerformManipulationUpdate(Vector3 position)
    {
        if (GestureManager.Instance.IsManipulating)
        {

            Vector3 moveVector = Vector3.zero;
            moveVector = position - manipulationPreviousPosition;

            manipulationPreviousPosition = position;

            transform.position += moveVector;
        }
    }
}

Thanks for the help.

thebanjomatic commented 7 years ago

@alamboley I'd encourage you to look at the documentation for the Input module, but this should be pretty straightforward to convert.https://github.com/Microsoft/HoloToolkit-Unity/blob/master/Assets/HoloToolkit/Input/README.md

The rotation specific portion of the script you posted is using the "Navigation" gesture, so you need to implement the "INavigationHandler" interface. You can do something simple like this where you update the rotation factor inside of OnNavigationUpdated and zero it out in OnNavigationCompleted and OnNavigationCanceled. You can then use this rotation factor in the Update function to perform the rotation. I'm not sure what frequency the navigation events come in, which is why I'm still rotating in the update function instead of per event.

Note, I haven't tested the following code, its mainly for illustration purposes.

public class RotateOnNavigate : MonoBehaviour, INavigationHandler
{
    [Tooltip("Rotation max speed controls amount of rotation.")]
    public float RotationSensitivity = 10.0f;

    private Vector3 rotationFactor = Vector3.zero;

    void INavigationHandler.OnNavigationStarted(NavigationEventData eventData)
    {
    }

    void INavigationHandler.OnNavigationUpdated(NavigationEventData eventData)
    {
        rotationFactor.y = -eventData.CumulativeDelta.x * RotationSensitivity;
    }

    void INavigationHandler.OnNavigationCanceled(NavigationEventData eventData)
    {
        rotationFactor = Vector3.zero;
    }

    void INavigationHandler.OnNavigationCompleted(NavigationEventData eventData)
    {
        rotationFactor = Vector3.zero;
    }

    private void Update()
    {
        transform.Rotate(rotationFactor);
    }
}
alamboley commented 7 years ago

That's perfect, many thanks!