Tpose down fix (#967)

This commit is contained in:
Erimel authored and GitHub committed 2024-03-28 03:17:08 -03:00
1 parent ea28baeef9
commit f391b558c9
1 file changed
+37 -35
@@ -20,6 +20,12 @@ private const val DRIFT_COOLDOWN_MS = 50000L
*/
class TrackerResetsHandler(val tracker: Tracker) {
private val HalfHorizontal = EulerAngles(
EulerOrder.YZX,
0f,
Math.PI.toFloat(),
0f,
).toQuaternion()
private var driftAmount = 0f
private var averagedDriftQuat = Quaternion.IDENTITY
private var rotationSinceReset = Quaternion.IDENTITY
@@ -36,12 +42,7 @@ class TrackerResetsHandler(val tracker: Tracker) {
var lastResetQuaternion: Quaternion? = null
// Manual mounting orientation
var mountingOrientation: Quaternion = EulerAngles(
EulerOrder.YZX,
0f,
Math.PI.toFloat(),
0f,
).toQuaternion()
var mountingOrientation = HalfHorizontal
set(value) {
field = value
// Clear the mounting reset now that it's been set manually
@@ -59,7 +60,7 @@ class TrackerResetsHandler(val tracker: Tracker) {
private var gyroFixNoMounting = Quaternion.IDENTITY
private var attachmentFixNoMounting = Quaternion.IDENTITY
private var yawFixZeroReference = Quaternion.IDENTITY
private var tposeFix = Quaternion.IDENTITY
private var tposeDownFix = Quaternion.IDENTITY
/**
* Reads/loads drift compensation settings from given config
@@ -137,8 +138,9 @@ class TrackerResetsHandler(val tracker: Tracker) {
rot = gyroFix * rot
rot *= attachmentFix
rot = mountRotFix.inv() * (rot * mountRotFix)
rot *= tposeDownFix
rot = yawFix * rot
return rot * tposeFix
return rot
}
/**
@@ -146,7 +148,8 @@ class TrackerResetsHandler(val tracker: Tracker) {
* applying quaternions produced after full reset and yaw reset only
*/
private fun adjustToIdentity(rotation: Quaternion): Quaternion {
var rot = gyroFixNoMounting * rotation
var rot = rotation
rot = gyroFixNoMounting * rot
rot *= attachmentFixNoMounting
rot = yawFixZeroReference * rot
return rot
@@ -172,36 +175,37 @@ class TrackerResetsHandler(val tracker: Tracker) {
* 0). This allows the tracker to be strapped to body at any pitch and roll.
*/
fun resetFull(reference: Quaternion) {
// Adjust for T-Pose
if ((isLeftArmTracker() && armsResetMode == ArmsResetModes.TPOSE_DOWN)) {
tposeFix = EulerAngles(
EulerOrder.YZX,
0f,
0f,
-FastMath.HALF_PI,
).toQuaternion()
// Adjust for T-Pose (down)
tposeDownFix = if ((isLeftArmTracker() && armsResetMode == ArmsResetModes.TPOSE_DOWN)) {
EulerAngles(EulerOrder.YZX, 0f, 0f, -FastMath.HALF_PI).toQuaternion()
} else if ((isRightArmTracker() && armsResetMode == ArmsResetModes.TPOSE_DOWN)) {
tposeFix = EulerAngles(EulerOrder.YZX, 0f, 0f, FastMath.HALF_PI).toQuaternion()
EulerAngles(EulerOrder.YZX, 0f, 0f, FastMath.HALF_PI).toQuaternion()
} else {
tposeFix = Quaternion.IDENTITY
Quaternion.IDENTITY
}
lastResetQuaternion = adjustToReference(tracker.getRawRotation())
val oldRot = adjustToReference(tracker.getRawRotation())
lastResetQuaternion = oldRot
val mountingAdjustedRotation = tracker.getRawRotation() * mountingOrientation
if (tracker.needsMounting) {
gyroFix = fixGyroscope(tposeFix * tracker.getRawRotation() * mountingOrientation)
gyroFix = fixGyroscope(mountingAdjustedRotation * tposeDownFix)
} else {
// Set mounting to the HMD's yaw so that the non-mounting-adjusted
// tracker goes forward.
mountRotFix = getYawQuaternion(reference)
}
attachmentFix = fixAttachment(tracker.getRawRotation() * mountingOrientation)
attachmentFix = fixAttachment(mountingAdjustedRotation)
// Rotate attachmentFix by 180 degrees as a workaround for tpose (down)
if (tposeDownFix != Quaternion.IDENTITY && tracker.needsMounting) {
attachmentFix *= HalfHorizontal
}
makeIdentityAdjustmentQuatsFull()
yawFix = fixYaw(tracker.getRawRotation() * mountingOrientation, reference)
yawFix = fixYaw(mountingAdjustedRotation, reference)
calculateDrift(oldRot)
@@ -218,15 +222,14 @@ class TrackerResetsHandler(val tracker: Tracker) {
* position should be corrected in the source.
*/
fun resetYaw(reference: Quaternion) {
lastResetQuaternion = adjustToReference(tracker.getRawRotation())
val rot: Quaternion = adjustToReference(tracker.getRawRotation())
val oldRot = adjustToReference(tracker.getRawRotation())
lastResetQuaternion = oldRot
yawFix = fixYaw(tracker.getRawRotation() * mountingOrientation, reference)
makeIdentityAdjustmentQuatsYaw()
calculateDrift(rot)
calculateDrift(oldRot)
// Remove the status if yaw reset was performed after the tracker
// was disconnected and connected.
@@ -247,17 +250,16 @@ class TrackerResetsHandler(val tracker: Tracker) {
}
// Get the current calibrated rotation
var buffer = adjustToDrift(tracker.getRawRotation() * mountingOrientation)
buffer *= tposeFix
buffer = gyroFix * buffer
buffer *= attachmentFix
buffer = yawFix * buffer
var rotBuf = adjustToDrift(tracker.getRawRotation() * mountingOrientation)
rotBuf = gyroFix * rotBuf
rotBuf *= attachmentFix
rotBuf = yawFix * rotBuf
// Adjust buffer to reference
buffer = reference.project(Vector3.POS_Y).inv().unit() * buffer
rotBuf = reference.project(Vector3.POS_Y).inv().unit() * rotBuf
// Rotate a vector pointing up by the quat
val rotVector = buffer.sandwich(Vector3.POS_Y)
val rotVector = rotBuf.sandwich(Vector3.POS_Y)
// Calculate the yaw angle using tan
var yawAngle = atan2(rotVector.x, rotVector.z)