Skip to content

Commit cb3c49f

Browse files
committed
fix(simulator): fix angular and velocity calculations
Fix the calculation for angular velocity, now decently correct. Tweak calculation of velocity to use fewer samples.
1 parent b5759f1 commit cb3c49f

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

Assets/VRTK/SDK/Simulator/SDK_ControllerSim.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
public class SDK_ControllerSim : MonoBehaviour
77
{
88
private Vector3 lastPos;
9-
private Vector3 lastRot;
9+
private Quaternion lastRot;
1010
private List<Vector3> posList;
1111
private List<Vector3> rotList;
1212
private bool selected;
13+
private float magnitude;
14+
private Vector3 axis;
1315

1416
public bool Selected
1517
{
@@ -50,23 +52,25 @@ private void Awake()
5052
posList = new List<Vector3>();
5153
rotList = new List<Vector3>();
5254
lastPos = transform.position;
53-
lastRot = transform.rotation.eulerAngles;
55+
lastRot = transform.rotation;
5456
}
5557

5658
private void Update()
5759
{
5860
posList.Add((transform.position - lastPos) / Time.deltaTime);
59-
if (posList.Count > 10)
61+
if (posList.Count > 4)
6062
{
6163
posList.RemoveAt(0);
6264
}
63-
rotList.Add((Quaternion.FromToRotation(lastRot, transform.rotation.eulerAngles)).eulerAngles / Time.deltaTime);
64-
if (rotList.Count > 10)
65+
Quaternion deltaRotation = transform.rotation * Quaternion.Inverse (lastRot);
66+
deltaRotation.ToAngleAxis(out magnitude, out axis);
67+
rotList.Add((axis * magnitude));
68+
if (rotList.Count > 4)
6569
{
6670
rotList.RemoveAt(0);
6771
}
6872
lastPos = transform.position;
69-
lastRot = transform.rotation.eulerAngles;
73+
lastRot = transform.rotation;
7074
}
7175
}
7276
}

Assets/VRTK/SDK/Simulator/SDK_SimHeadset.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ public class SDK_SimHeadset : SDK_BaseHeadset
1212
{
1313
private Transform camera;
1414
private Vector3 lastPos;
15-
private Vector3 lastRot;
15+
private Quaternion lastRot;
1616
private List<Vector3> posList;
1717
private List<Vector3> rotList;
18+
private float magnitude;
19+
private Vector3 axis;
1820

1921
/// <summary>
2022
/// The ProcessUpdate method enables an SDK to run logic for every Unity Update
@@ -23,17 +25,19 @@ public class SDK_SimHeadset : SDK_BaseHeadset
2325
public override void ProcessUpdate(Dictionary<string, object> options)
2426
{
2527
posList.Add((camera.position - lastPos) / Time.deltaTime);
26-
if (posList.Count > 10)
28+
if (posList.Count > 4)
2729
{
2830
posList.RemoveAt(0);
2931
}
30-
rotList.Add((Quaternion.FromToRotation(lastRot, camera.rotation.eulerAngles)).eulerAngles / Time.deltaTime);
31-
if (rotList.Count > 10)
32+
Quaternion deltaRotation = camera.rotation * Quaternion.Inverse (lastRot);
33+
deltaRotation.ToAngleAxis(out magnitude, out axis);
34+
rotList.Add((axis * magnitude));
35+
if (rotList.Count > 4)
3236
{
3337
rotList.RemoveAt(0);
3438
}
3539
lastPos = camera.position;
36-
lastRot = camera.rotation.eulerAngles;
40+
lastRot = camera.rotation;
3741
}
3842

3943
/// <summary>
@@ -143,7 +147,7 @@ private void Awake()
143147
if (headset != null)
144148
{
145149
lastPos = headset.position;
146-
lastRot = headset.rotation.eulerAngles;
150+
lastRot = headset.rotation;
147151
}
148152
}
149153
}

0 commit comments

Comments
 (0)