ViveSoftware / ViveInputUtility-Unity

A toolkit that helps developing/prototyping VR apps.
http://u3d.as/uF7
Other
353 stars 82 forks source link

How to read Velocity and Angular Velocity from devices #19

Closed wirelessdreamer closed 6 years ago

wirelessdreamer commented 6 years ago

I didn't see anything in the examples about how to read velocity or angular velocity of devices.

wirelessdreamer commented 6 years ago

I've tried using SteamVR_TrackedObject trackedObj; trackedObj = gameObject.GetComponent(); viveRole = ViveRoleProperty.New(gameObject.transform.parent.GetComponent().viveRole); trackedObj.SetDeviceIndex((int)viveRole.GetDeviceIndex());

and then pulling velocity off of the tracked object, but the index's don't appear to match up.

dariol commented 6 years ago

Hi wirelessdreamer,

Basically VIU replaces the need to use SteamVR_TrackedObject. See the ViveCameraRig or ViveRig prefabs for reference. Also take a look at the guide pdf for details. Basically VivePoseTracker replaces SteamVR_TrackedObject using a role instead of an index. A benefit of using roles allows one to persist devices so that bindings can be re-established across starts easily.

wirelessdreamer commented 6 years ago

I don't see a replacement method to get velocity in VIU, the only reason I tried the above was trying to find ways to get angular and rotational velocity. what is the method to get it with VUI?

lawwong commented 6 years ago

In short cut:

using UnityEngine;
using HTC.UnityPlugin.Vive;

public class GetDeviceVelocity : MonoBehaviour
{
    public ViveRoleProperty role = ViveRoleProperty.New(HandRole.RightHand);
    public Vector3 velocity;
    public Vector3 angularVelocity;

    private void Update()
    {
        velocity = VivePose.GetVelocity(role);
        angularVelocity = VivePose.GetAngularVelocity(role);
    }
}

If you need more detail, you can get them from the device state, which returned from VRModule.GetCurrentDeviceState under HTC.UnityPlugin.VRModuleManagement. We will add VRModule in the document later.

using UnityEngine;
using HTC.UnityPlugin.Vive;
using HTC.UnityPlugin.VRModuleManagement;

public class PrintDeviceState : MonoBehaviour
{
    public ViveRoleProperty role = ViveRoleProperty.New(HandRole.RightHand);

    private void Update()
    {
        IVRModuleDeviceState deviceState = VRModule.GetCurrentDeviceState(role.GetDeviceIndex());
        Debug.Log("isConnected=" + deviceState.isConnected);
        Debug.Log("velocity=" + deviceState.velocity);
        Debug.Log("angularVelocity=" + deviceState.angularVelocity);
        Debug.Log("serialNumber=" + deviceState.serialNumber);
        Debug.Log("modelNumber=" + deviceState.modelNumber);
        Debug.Log("deviceClass=" + deviceState.deviceClass);
    }
}
wirelessdreamer commented 6 years ago

Thank you, thats what I was looking for.