Skip to content

Commit 3e05056

Browse files
committed
fix(Utilities): use local space for velocity estimation samples
The Velocity Estimator was using world space to determine the samples for velocity and angular velocity, which would cause problems if the Velocity Estimator was attached to a GameObject that's parent was rotated. It could be seen in the Unity SDK by simply rotating the CameraRig around the Y axis and then the throwing mechanism would throw things in the wrong direction. The solution is to simply use local space for position and rotation as then the actual object the Velocity Estimator is attached to will be used for the relative position and rotation.
1 parent 9a959f0 commit 3e05056

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Assets/VRTK/Scripts/Utilities/VRTK_VelocityEstimator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ protected virtual IEnumerator EstimateVelocity()
137137
{
138138
currentSampleCount = 0;
139139

140-
Vector3 previousPosition = transform.position;
141-
Quaternion previousRotation = transform.rotation;
140+
Vector3 previousPosition = transform.localPosition;
141+
Quaternion previousRotation = transform.localRotation;
142142
while (true)
143143
{
144144
yield return new WaitForEndOfFrame();
@@ -149,8 +149,8 @@ protected virtual IEnumerator EstimateVelocity()
149149
int w = currentSampleCount % angularVelocitySamples.Length;
150150
currentSampleCount++;
151151

152-
velocitySamples[v] = velocityFactor * (transform.position - previousPosition);
153-
Quaternion deltaRotation = transform.rotation * Quaternion.Inverse(previousRotation);
152+
velocitySamples[v] = velocityFactor * (transform.localPosition - previousPosition);
153+
Quaternion deltaRotation = transform.localRotation * Quaternion.Inverse(previousRotation);
154154

155155
float theta = 2.0f * Mathf.Acos(Mathf.Clamp(deltaRotation.w, -1.0f, 1.0f));
156156
if (theta > Mathf.PI)
@@ -166,8 +166,8 @@ protected virtual IEnumerator EstimateVelocity()
166166

167167
angularVelocitySamples[w] = angularVelocity;
168168

169-
previousPosition = transform.position;
170-
previousRotation = transform.rotation;
169+
previousPosition = transform.localPosition;
170+
previousRotation = transform.localRotation;
171171
}
172172
}
173173
}

0 commit comments

Comments
 (0)