Skip to content

Commit 899e49c

Browse files
author
Christopher - Marcel Böddecker
committed
feat(headset_velocity): add methods to get velocities for headset
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.
1 parent 3c3c147 commit 899e49c

File tree

8 files changed

+159
-2
lines changed

8 files changed

+159
-2
lines changed

Assets/VRTK/SDK/Base/SDK_BaseHeadset.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public abstract class SDK_BaseHeadset : ScriptableObject
3333
/// <returns>A transform of the object holding the headset camera in the scene.</returns>
3434
public abstract Transform GetHeadsetCamera();
3535

36+
/// <summary>
37+
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
38+
/// </summary>
39+
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
40+
public abstract Vector3 GetHeadsetVelocity();
41+
42+
/// <summary>
43+
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
44+
/// </summary>
45+
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
46+
public abstract Vector3 GetHeadsetAngularVelocity();
47+
3648
/// <summary>
3749
/// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
3850
/// </summary>

Assets/VRTK/SDK/Fallback/SDK_FallbackHeadset.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ public override Transform GetHeadsetCamera()
3838
return null;
3939
}
4040

41+
/// <summary>
42+
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
43+
/// </summary>
44+
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
45+
public override Vector3 GetHeadsetVelocity()
46+
{
47+
return Vector3.zero;
48+
}
49+
50+
/// <summary>
51+
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
52+
/// </summary>
53+
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
54+
public override Vector3 GetHeadsetAngularVelocity()
55+
{
56+
return Vector3.zero;
57+
}
58+
4159
/// <summary>
4260
/// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
4361
/// </summary>

Assets/VRTK/SDK/OculusVR/SDK_OculusVRHeadset.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ namespace VRTK
1010
/// </summary>
1111
public class SDK_OculusVRHeadset : SDK_BaseHeadset
1212
{
13+
private Quaternion previousHeadsetRotation;
14+
private Quaternion currentHeadsetRotation;
15+
1316
/// <summary>
1417
/// The ProcessUpdate method enables an SDK to run logic for every Unity Update
1518
/// </summary>
1619
/// <param name="options">A dictionary of generic options that can be used to within the update.</param>
1720
public override void ProcessUpdate(Dictionary<string, object> options)
1821
{
22+
var device = GetHeadset();
23+
previousHeadsetRotation = currentHeadsetRotation;
24+
currentHeadsetRotation = device.transform.rotation;
1925
}
2026

2127
/// <summary>
@@ -50,6 +56,25 @@ public override Transform GetHeadsetCamera()
5056
return cachedHeadsetCamera;
5157
}
5258

59+
/// <summary>
60+
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
61+
/// </summary>
62+
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
63+
public override Vector3 GetHeadsetVelocity()
64+
{
65+
return OVRManager.isHmdPresent ? OVRPlugin.GetEyeVelocity(OVRPlugin.Eye.Left).ToOVRPose().position : Vector3.zero;
66+
}
67+
68+
/// <summary>
69+
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
70+
/// </summary>
71+
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
72+
public override Vector3 GetHeadsetAngularVelocity()
73+
{
74+
var deltaRotation = currentHeadsetRotation * Quaternion.Inverse(previousHeadsetRotation);
75+
return new Vector3(Mathf.DeltaAngle(0, deltaRotation.eulerAngles.x), Mathf.DeltaAngle(0, deltaRotation.eulerAngles.y), Mathf.DeltaAngle(0, deltaRotation.eulerAngles.z));
76+
}
77+
5378
/// <summary>
5479
/// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
5580
/// </summary>

Assets/VRTK/SDK/Simulator/SDK_SimHeadset.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,29 @@ namespace VRTK
1111
public class SDK_SimHeadset : SDK_BaseHeadset
1212
{
1313
private Transform camera;
14+
private Vector3 lastPos;
15+
private Vector3 lastRot;
16+
private List<Vector3> posList;
17+
private List<Vector3> rotList;
1418

1519
/// <summary>
1620
/// The ProcessUpdate method enables an SDK to run logic for every Unity Update
1721
/// </summary>
1822
/// <param name="options">A dictionary of generic options that can be used to within the update.</param>
1923
public override void ProcessUpdate(Dictionary<string, object> options)
2024
{
25+
posList.Add((camera.position - lastPos) / Time.deltaTime);
26+
if (posList.Count > 10)
27+
{
28+
posList.RemoveAt(0);
29+
}
30+
rotList.Add((Quaternion.FromToRotation(lastRot, camera.rotation.eulerAngles)).eulerAngles / Time.deltaTime);
31+
if (rotList.Count > 10)
32+
{
33+
rotList.RemoveAt(0);
34+
}
35+
lastPos = camera.position;
36+
lastRot = camera.rotation.eulerAngles;
2137
}
2238

2339
/// <summary>
@@ -56,6 +72,36 @@ public override Transform GetHeadsetCamera()
5672
return camera;
5773
}
5874

75+
/// <summary>
76+
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
77+
/// </summary>
78+
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
79+
public override Vector3 GetHeadsetVelocity()
80+
{
81+
Vector3 velocity = Vector3.zero;
82+
foreach (Vector3 vel in posList)
83+
{
84+
velocity += vel;
85+
}
86+
velocity /= posList.Count;
87+
return velocity;
88+
}
89+
90+
/// <summary>
91+
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
92+
/// </summary>
93+
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
94+
public override Vector3 GetHeadsetAngularVelocity()
95+
{
96+
Vector3 angularVelocity = Vector3.zero;
97+
foreach (Vector3 vel in rotList)
98+
{
99+
angularVelocity += vel;
100+
}
101+
angularVelocity /= rotList.Count;
102+
return angularVelocity;
103+
}
104+
59105
/// <summary>
60106
/// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
61107
/// </summary>
@@ -85,6 +131,16 @@ public override void AddHeadsetFade(Transform camera)
85131
{
86132

87133
}
134+
135+
private void Awake()
136+
{
137+
posList = new List<Vector3>();
138+
rotList = new List<Vector3>();
139+
140+
var headset = GetHeadset();
141+
lastPos = headset.position;
142+
lastRot = headset.rotation.eulerAngles;
143+
}
88144
}
89145
#else
90146
public class SDK_SimHeadset : SDK_FallbackHeadset

Assets/VRTK/SDK/SteamVR/SDK_SteamVRController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public override SDK_ControllerHapticModifiers GetHapticModifiers()
290290
/// <returns>A Vector3 containing the current velocity of the tracked object.</returns>
291291
public override Vector3 GetVelocityOnIndex(uint index)
292292
{
293-
if (index >= OpenVR.k_unTrackedDeviceIndexInvalid)
293+
if (index <= (uint)SteamVR_TrackedObject.EIndex.Hmd || index >= OpenVR.k_unTrackedDeviceIndexInvalid)
294294
{
295295
return Vector3.zero;
296296
}
@@ -305,7 +305,7 @@ public override Vector3 GetVelocityOnIndex(uint index)
305305
/// <returns>A Vector3 containing the current angular velocity of the tracked object.</returns>
306306
public override Vector3 GetAngularVelocityOnIndex(uint index)
307307
{
308-
if (index >= OpenVR.k_unTrackedDeviceIndexInvalid)
308+
if (index <= (uint)SteamVR_TrackedObject.EIndex.Hmd || index >= OpenVR.k_unTrackedDeviceIndexInvalid)
309309
{
310310
return Vector3.zero;
311311
}

Assets/VRTK/SDK/SteamVR/SDK_SteamVRHeadset.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ public override Transform GetHeadsetCamera()
5858
return cachedHeadsetCamera;
5959
}
6060

61+
/// <summary>
62+
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
63+
/// </summary>
64+
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
65+
public override Vector3 GetHeadsetVelocity()
66+
{
67+
return SteamVR_Controller.Input((int)SteamVR_TrackedObject.EIndex.Hmd).velocity;
68+
}
69+
70+
/// <summary>
71+
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
72+
/// </summary>
73+
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
74+
public override Vector3 GetHeadsetAngularVelocity()
75+
{
76+
return SteamVR_Controller.Input((int)SteamVR_TrackedObject.EIndex.Hmd).angularVelocity;
77+
}
78+
6179
/// <summary>
6280
/// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
6381
/// </summary>

Assets/VRTK/SDK/VRTK_SDK_Bridge.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ public static Vector3 GetAngularVelocityOnIndex(uint index)
120120
return GetControllerSDK().GetAngularVelocityOnIndex(index);
121121
}
122122

123+
public static Vector3 GetHeadsetVelocity()
124+
{
125+
return GetHeadsetSDK().GetHeadsetVelocity();
126+
}
127+
128+
public static Vector3 GetHeadsetAngularVelocity()
129+
{
130+
return GetHeadsetSDK().GetHeadsetAngularVelocity();
131+
}
132+
123133
public static Vector2 GetTouchpadAxisOnIndex(uint index)
124134
{
125135
return GetControllerSDK().GetTouchpadAxisOnIndex(index);

Assets/VRTK/Scripts/Utilities/VRTK_DeviceFinder.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,24 @@ public static Vector3 GetControllerAngularVelocity(GameObject givenController)
251251
return VRTK_SDK_Bridge.GetAngularVelocityOnIndex(controllerIndex);
252252
}
253253

254+
/// <summary>
255+
/// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
256+
/// </summary>
257+
/// <returns>A Vector3 containing the current velocity of the headset.</returns>
258+
public static Vector3 GetHeadsetVelocity()
259+
{
260+
return VRTK_SDK_Bridge.GetHeadsetVelocity();
261+
}
262+
263+
/// <summary>
264+
/// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
265+
/// </summary>
266+
/// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
267+
public static Vector3 GetHeadsetAngularVelocity()
268+
{
269+
return VRTK_SDK_Bridge.GetHeadsetAngularVelocity();
270+
}
271+
254272
/// <summary>
255273
/// 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.
256274
/// </summary>

0 commit comments

Comments
 (0)