From 06a54e106ed2091bdd0fc532fad64a4e73810758 Mon Sep 17 00:00:00 2001 From: Erimel Date: Fri, 27 Nov 2026 18:38:25 +0100 Subject: [PATCH] clamp stay aligned deltatime closes https://github.com/SlimeVR/SlimeVR-Server/issues/1971 --- .../dev/slimevr/tracker/behaviours/stay-aligned.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/server/core/src/main/java/dev/slimevr/tracker/behaviours/stay-aligned.kt b/server/core/src/main/java/dev/slimevr/tracker/behaviours/stay-aligned.kt index 6def7b603..3ae250238 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/behaviours/stay-aligned.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/behaviours/stay-aligned.kt @@ -22,6 +22,10 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import solarxr_protocol.datatypes.MagnetometerStatus +import kotlin.time.Duration.Companion.seconds + +// When a tracker comes back from sleep, prevents a disproportionate correction +private val MAX_DELTA_TIME = 0.5.seconds class TrackerStayAlignedBehaviour( private val settings: Settings, @@ -72,20 +76,17 @@ class TrackerStayAlignedBehaviour( .flatMapLatest { (enabled, imuType, magStatus) -> if (magStatus == MagnetometerStatus.ENABLED || imuType == null || !enabled) return@flatMapLatest emptyFlow() - // Ignore every other emission for performance (50FPS instead of 100FPS when at 100TPS) - var index = 0 var lastRotationTime = timeSource.markNow() receiver.context.state .distinctUntilChangedBy { it.rawRotation } - .filter { index++ % 2 == 0 } .onEach { state -> val yawCorrectionPerSec = IMU_TO_YAW_CORRECTION.getOrDefault(imuType, YAW_CORRECTION_DEFAULT) if (yawCorrectionPerSec == Angle.ZERO) return@onEach - val lastFrameTimeSeconds = lastRotationTime.elapsedNow().inFloatingSeconds + val deltaTime = lastRotationTime.elapsedNow().coerceAtMost(MAX_DELTA_TIME).inFloatingSeconds lastRotationTime = timeSource.markNow() - val applyYawCorrection = yawCorrectionPerSec * lastFrameTimeSeconds + val applyYawCorrection = yawCorrectionPerSec * deltaTime val yawCorrectionResult = TrackerYawCorrection.computeYawCorrection( state,