Skip to content

Commit

Permalink
feat(headset_velocity): add methods to get velocities for headset
Browse files Browse the repository at this point in the history
The DeviceFinder and SDK classes provide methods to receive the
velocity and angular velocity of controllers. This change adds methods
to all SDKs to retrieve the velocities of the headset. The controller
velocity methods of the VRTK SteamVR SDK have been updated to only
return velocities of controllers, not the headset, because SteamVR uses
indexes for all the tracked objects.
  • Loading branch information
Christopher - Marcel Böddecker committed Jan 19, 2017
1 parent 3c3c147 commit 899e49c
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Assets/VRTK/SDK/Base/SDK_BaseHeadset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public abstract class SDK_BaseHeadset : ScriptableObject
/// <returns>A transform of the object holding the headset camera in the scene.</returns>
public abstract Transform GetHeadsetCamera();

/// <summary>
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
public abstract Vector3 GetHeadsetVelocity();

/// <summary>
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
public abstract Vector3 GetHeadsetAngularVelocity();

/// <summary>
/// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions Assets/VRTK/SDK/Fallback/SDK_FallbackHeadset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ public override Transform GetHeadsetCamera()
return null;
}

/// <summary>
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
public override Vector3 GetHeadsetVelocity()
{
return Vector3.zero;
}

/// <summary>
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
public override Vector3 GetHeadsetAngularVelocity()
{
return Vector3.zero;
}

/// <summary>
/// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions Assets/VRTK/SDK/OculusVR/SDK_OculusVRHeadset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ namespace VRTK
/// </summary>
public class SDK_OculusVRHeadset : SDK_BaseHeadset
{
private Quaternion previousHeadsetRotation;
private Quaternion currentHeadsetRotation;

/// <summary>
/// The ProcessUpdate method enables an SDK to run logic for every Unity Update
/// </summary>
/// <param name="options">A dictionary of generic options that can be used to within the update.</param>
public override void ProcessUpdate(Dictionary<string, object> options)
{
var device = GetHeadset();
previousHeadsetRotation = currentHeadsetRotation;
currentHeadsetRotation = device.transform.rotation;
}

/// <summary>
Expand Down Expand Up @@ -50,6 +56,25 @@ public override Transform GetHeadsetCamera()
return cachedHeadsetCamera;
}

/// <summary>
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
public override Vector3 GetHeadsetVelocity()
{
return OVRManager.isHmdPresent ? OVRPlugin.GetEyeVelocity(OVRPlugin.Eye.Left).ToOVRPose().position : Vector3.zero;
}

/// <summary>
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
public override Vector3 GetHeadsetAngularVelocity()
{
var deltaRotation = currentHeadsetRotation * Quaternion.Inverse(previousHeadsetRotation);
return new Vector3(Mathf.DeltaAngle(0, deltaRotation.eulerAngles.x), Mathf.DeltaAngle(0, deltaRotation.eulerAngles.y), Mathf.DeltaAngle(0, deltaRotation.eulerAngles.z));
}

/// <summary>
/// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
/// </summary>
Expand Down
56 changes: 56 additions & 0 deletions Assets/VRTK/SDK/Simulator/SDK_SimHeadset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,29 @@ namespace VRTK
public class SDK_SimHeadset : SDK_BaseHeadset
{
private Transform camera;
private Vector3 lastPos;
private Vector3 lastRot;
private List<Vector3> posList;
private List<Vector3> rotList;

/// <summary>
/// The ProcessUpdate method enables an SDK to run logic for every Unity Update
/// </summary>
/// <param name="options">A dictionary of generic options that can be used to within the update.</param>
public override void ProcessUpdate(Dictionary<string, object> options)
{
posList.Add((camera.position - lastPos) / Time.deltaTime);
if (posList.Count > 10)
{
posList.RemoveAt(0);
}
rotList.Add((Quaternion.FromToRotation(lastRot, camera.rotation.eulerAngles)).eulerAngles / Time.deltaTime);
if (rotList.Count > 10)
{
rotList.RemoveAt(0);
}
lastPos = camera.position;
lastRot = camera.rotation.eulerAngles;
}

/// <summary>
Expand Down Expand Up @@ -56,6 +72,36 @@ public override Transform GetHeadsetCamera()
return camera;
}

/// <summary>
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
public override Vector3 GetHeadsetVelocity()
{
Vector3 velocity = Vector3.zero;
foreach (Vector3 vel in posList)
{
velocity += vel;
}
velocity /= posList.Count;
return velocity;
}

/// <summary>
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
public override Vector3 GetHeadsetAngularVelocity()
{
Vector3 angularVelocity = Vector3.zero;
foreach (Vector3 vel in rotList)
{
angularVelocity += vel;
}
angularVelocity /= rotList.Count;
return angularVelocity;
}

/// <summary>
/// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
/// </summary>
Expand Down Expand Up @@ -85,6 +131,16 @@ public override void AddHeadsetFade(Transform camera)
{

}

private void Awake()
{
posList = new List<Vector3>();
rotList = new List<Vector3>();

var headset = GetHeadset();
lastPos = headset.position;
lastRot = headset.rotation.eulerAngles;
}
}
#else
public class SDK_SimHeadset : SDK_FallbackHeadset
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRTK/SDK/SteamVR/SDK_SteamVRController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public override SDK_ControllerHapticModifiers GetHapticModifiers()
/// <returns>A Vector3 containing the current velocity of the tracked object.</returns>
public override Vector3 GetVelocityOnIndex(uint index)
{
if (index >= OpenVR.k_unTrackedDeviceIndexInvalid)
if (index <= (uint)SteamVR_TrackedObject.EIndex.Hmd || index >= OpenVR.k_unTrackedDeviceIndexInvalid)
{
return Vector3.zero;
}
Expand All @@ -305,7 +305,7 @@ public override Vector3 GetVelocityOnIndex(uint index)
/// <returns>A Vector3 containing the current angular velocity of the tracked object.</returns>
public override Vector3 GetAngularVelocityOnIndex(uint index)
{
if (index >= OpenVR.k_unTrackedDeviceIndexInvalid)
if (index <= (uint)SteamVR_TrackedObject.EIndex.Hmd || index >= OpenVR.k_unTrackedDeviceIndexInvalid)
{
return Vector3.zero;
}
Expand Down
18 changes: 18 additions & 0 deletions Assets/VRTK/SDK/SteamVR/SDK_SteamVRHeadset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ public override Transform GetHeadsetCamera()
return cachedHeadsetCamera;
}

/// <summary>
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
public override Vector3 GetHeadsetVelocity()
{
return SteamVR_Controller.Input((int)SteamVR_TrackedObject.EIndex.Hmd).velocity;
}

/// <summary>
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
public override Vector3 GetHeadsetAngularVelocity()
{
return SteamVR_Controller.Input((int)SteamVR_TrackedObject.EIndex.Hmd).angularVelocity;
}

/// <summary>
/// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions Assets/VRTK/SDK/VRTK_SDK_Bridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ public static Vector3 GetAngularVelocityOnIndex(uint index)
return GetControllerSDK().GetAngularVelocityOnIndex(index);
}

public static Vector3 GetHeadsetVelocity()
{
return GetHeadsetSDK().GetHeadsetVelocity();
}

public static Vector3 GetHeadsetAngularVelocity()
{
return GetHeadsetSDK().GetHeadsetAngularVelocity();
}

public static Vector2 GetTouchpadAxisOnIndex(uint index)
{
return GetControllerSDK().GetTouchpadAxisOnIndex(index);
Expand Down
18 changes: 18 additions & 0 deletions Assets/VRTK/Scripts/Utilities/VRTK_DeviceFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ public static Vector3 GetControllerAngularVelocity(GameObject givenController)
return VRTK_SDK_Bridge.GetAngularVelocityOnIndex(controllerIndex);
}

/// <summary>
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
public static Vector3 GetHeadsetVelocity()
{
return VRTK_SDK_Bridge.GetHeadsetVelocity();
}

/// <summary>
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
/// </summary>
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
public static Vector3 GetHeadsetAngularVelocity()
{
return VRTK_SDK_Bridge.GetHeadsetAngularVelocity();
}

/// <summary>
/// The HeadsetTransform method is used to retrieve the transform for the VR Headset in the scene. It can be useful to determine the position of the user's head in the game world.
/// </summary>
Expand Down

0 comments on commit 899e49c

Please sign in to comment.