From 5fec3962ee864254fd73863ec396c2cbda417373 Mon Sep 17 00:00:00 2001 From: Erimel Date: Wed, 16 Sep 2026 16:10:06 -0400 Subject: [PATCH] Simplify position input logic --- .../main/java/dev/slimevr/resets/module.kt | 2 +- .../java/dev/slimevr/skeleton/behaviours.kt | 26 ++++--------------- .../skeleton/fkprocessors/localizer.kt | 11 +++++--- .../inputprocessors/head-position-fallback.kt | 2 +- .../main/java/dev/slimevr/skeleton/module.kt | 9 +++---- .../main/java/dev/slimevr/skeleton/reducer.kt | 20 +++++++------- 6 files changed, 26 insertions(+), 44 deletions(-) diff --git a/server/core/src/main/java/dev/slimevr/resets/module.kt b/server/core/src/main/java/dev/slimevr/resets/module.kt index fc46f95ab..3a5eb5f5f 100644 --- a/server/core/src/main/java/dev/slimevr/resets/module.kt +++ b/server/core/src/main/java/dev/slimevr/resets/module.kt @@ -90,7 +90,7 @@ class ResetsManager(val context: ResetsContext, val server: VRServer, val settin if (resetType == ResetType.FULL) { // Tell the skeleton to set the floor level and try resetting the head position (for mocap mode) - skeleton.context.dispatchAll(listOf(SkeletonActions.ResetHeadPosition, SkeletonActions.ComputeFloorLevel)) + skeleton.context.dispatch(SkeletonActions.ComputeFloorLevel) } // Reset the skeleton processors that store temporary data (smoothing, localizer, skating correction, etc.) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt index e5e329569..208913274 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt @@ -55,17 +55,6 @@ class HeightLogBehaviour : SkeletonBehaviour { } } -/** - * Handles resetting the head position whenever mocap mode gets disabled - */ -class LocalizerResetBehaviour(val settings: Settings) : SkeletonBehaviour { - override fun observe(receiver: Skeleton) { - settings.context.state.distinctUntilChangedBy { it.data.skeletonConfig.toggles.mocapMode }.onEach { - if (!it.data.skeletonConfig.toggles.mocapMode) receiver.context.dispatch(SkeletonActions.ResetHeadPosition) - }.launchIn(receiver.context.scope) - } -} - class YouSpinMeRightRoundBehaviour(val inputHz: Float = 1f) : SkeletonBehaviour { override fun observe(receiver: Skeleton) { receiver.context.scope.launch { @@ -228,11 +217,10 @@ class ComputedSkeletonBehaviour( val targetState = receiver.context.state.value val boneInputs = if (targetState.pausedProcessedBoneInputs != null) { - // TODO improve pause tracking code - // and also possibly head default position (HeadPositionFallbackProcessor) + // TODO improve pause tracking code, maybe using a processor // Use already-processed paused tracking data except for the head val headBone = targetState.boneInputs[BodyPart.HEAD] - targetState.pausedProcessedBoneInputs.mutateCopy { it[BodyPart.HEAD] = headBone?.copy(position = if (headBone.isPositionActive) headBone.position else it[BodyPart.HEAD]?.position) } + targetState.pausedProcessedBoneInputs.mutateCopy { it[BodyPart.HEAD] = headBone?.copy(position = if (headBone.isPositionActive) headBone.position else it[BodyPart.HEAD]!!.position) } } else { // Run pre-FK processors // TODO: Add a constrain processor (maybe not needed) @@ -262,13 +250,9 @@ class ComputedSkeletonBehaviour( fkChangedParts.clear() boneInputs.forEachBone { bodyPart, boneInput -> val previous = beforeFk[bodyPart] - if (boneInput == previous) return@forEachBone - fkChangedParts.add(bodyPart) - beforeFk[bodyPart] = boneInput - - // For changed bones with inactive positions, update their input's position in state (needed for Localizer) - if (!boneInput.isPositionActive && boneInput.position != previous?.position) { - receiver.context.dispatch(SkeletonActions.SetBonePosition(bodyPart, boneInput.position, false)) + if (boneInput != previous) { + fkChangedParts.add(bodyPart) + beforeFk[bodyPart] = boneInput } } // Inputs changed; re-run FK diff --git a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt index 9e4bd3282..f891a85a2 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt @@ -208,6 +208,7 @@ object COMLocalizer { class LocalizerFkProcessor(val settings: Settings) : SkeletonFkProcessor, ResettableSkeletonProcessor { + private var lastHeadPosition: Vector3? = null private var plantedFoot = FootLocalizer.PlantedFoot.NONE private var targetFoot = Vector3.ZERO @@ -275,7 +276,7 @@ class LocalizerFkProcessor(val settings: Settings) : // Return horizontal foot travel and vertical COM travel Vector3(footTravel.x, comTravel.y, footTravel.z) - Vector3.ZERO // TODO +// Vector3.ZERO // TODO } FollowSource.COM -> { @@ -284,7 +285,7 @@ class LocalizerFkProcessor(val settings: Settings) : // Return COM travel COMLocalizer.computeCOMTravel(targetCOM, targetCOM) - Vector3.ZERO // TODO +// Vector3.ZERO // TODO } FollowSource.HIP -> { @@ -293,16 +294,18 @@ class LocalizerFkProcessor(val settings: Settings) : // Return the sitting travel HipLocalizer.computeSittingTravel(currentHip, targetHip) - Vector3.ZERO // TODO +// Vector3.ZERO // TODO } } - val newHeadPosition = (headInput.position ?: Vector3.ZERO) + travel + val newHeadPosition = (lastHeadPosition ?: headInput.position) + travel mutableInputSkeleton[BodyPart.HEAD] = headInput.copy(position = newHeadPosition) + lastHeadPosition = newHeadPosition } override fun reset(resetType: ResetType) { if (resetType == ResetType.FULL) { + lastHeadPosition = null plantedFoot = FootLocalizer.PlantedFoot.NONE targetFoot = Vector3.ZERO sittingTime = Duration.ZERO diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-position-fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-position-fallback.kt index 6d342f851..3aaf281d0 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-position-fallback.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-position-fallback.kt @@ -12,7 +12,7 @@ import solarxr_protocol.datatypes.BodyPart class HeadPositionFallbackProcessor(val settings: Settings) : SkeletonInputProcessor { override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { val headBone = mutableInputSkeleton[BodyPart.HEAD] ?: return - if (headBone.position != null) return + if (headBone.isPositionActive) return // Set the head position to the be standing up at the origin mutableInputSkeleton[BodyPart.HEAD] = headBone.copy(position = Vector3(0f, skeletonHeight, 0f)) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/module.kt b/server/core/src/main/java/dev/slimevr/skeleton/module.kt index 095119417..e93bc5944 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -46,7 +46,7 @@ data class BoneInput( val offset: Vector3, val rotation: Quaternion, val acceleration: Vector3, - val position: Vector3?, + val position: Vector3, val isRotationActive: Boolean, val isAccelerationActive: Boolean, val isPositionActive: Boolean, @@ -100,7 +100,7 @@ val DEFAULT_BONE_INPUT = BoneInput( offset = Vector3.ZERO, rotation = Quaternion.IDENTITY, acceleration = Vector3.ZERO, - position = null, + position = Vector3.ZERO, isRotationActive = false, isAccelerationActive = false, isPositionActive = false, @@ -125,8 +125,7 @@ val DEFAULT_SKELETON_STATE = run { fun buildBone(bone: BoneInput, parentBone: BoneState?, velocity: Velocity = ZERO_VELOCITY): BoneState { // Raw position of the bone input is used for BodyPart.HEAD since it has no parent - val headPosition = parentBone?.let { it.tailPosition + it.rotation.sandwich(bone.headOffset) } - ?: bone.position ?: Vector3.ZERO + val headPosition = parentBone?.let { it.tailPosition + it.rotation.sandwich(bone.headOffset) } ?: bone.position return BoneState( parentBone = parentBone, bodyPart = bone.bodyPart, @@ -166,7 +165,6 @@ sealed interface SkeletonActions { data class SetProportions(val lengths: Map) : SkeletonActions data class PauseTracking(val pause: Boolean) : SkeletonActions data class SetPausedBoneInputs(val pausedBoneInputs: InputSkeleton) : SkeletonActions - data object ResetHeadPosition : SkeletonActions data object ComputeFloorLevel : SkeletonActions } @@ -213,7 +211,6 @@ class Skeleton( val behaviours = listOf( ProportionsBehaviour(ctx.config.userConfig), HeightLogBehaviour(), - LocalizerResetBehaviour(settings), // YouSpinMeRightRoundBehaviour(inputHz = 50f), ComputedSkeletonBehaviour( hz = hz, diff --git a/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt b/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt index f0a825bf7..fbc6935c2 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt @@ -17,7 +17,7 @@ fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when is SkeletonActions.SetBonePosition -> { val bone = state.boneInputs[action.bodyPart] ?: return state - state.copy(boneInputs = state.boneInputs.mutateCopy { it[action.bodyPart] = bone.copy(position = action.position, isPositionActive = action.setActive) }) + state.copy(boneInputs = state.boneInputs.mutateCopy { it[action.bodyPart] = bone.copy(position = action.position ?: Vector3.ZERO, isPositionActive = action.setActive) }) } is SkeletonActions.DisableBone -> { @@ -27,7 +27,7 @@ fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when it[action.bodyPart] = bone.copy( rotation = Quaternion.IDENTITY, acceleration = Vector3.ZERO, - position = null, + position = Vector3.ZERO, isRotationActive = false, isAccelerationActive = false, isPositionActive = false, @@ -51,16 +51,14 @@ fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when is SkeletonActions.SetPausedBoneInputs -> state.copy(pausedProcessedBoneInputs = action.pausedBoneInputs) - is SkeletonActions.ResetHeadPosition -> { - val boneInputs = state.boneInputs - val headBone = boneInputs[BodyPart.HEAD] ?: return state - if (headBone.isPositionActive) return state - state.copy(boneInputs = boneInputs.mutateCopy { it[BodyPart.HEAD] = headBone.copy(position = null) }) - } - is SkeletonActions.ComputeFloorLevel -> { val skeletonHeight = state.skeletonHeight - val headHeight = state.boneInputs[BodyPart.HEAD]?.position?.y ?: skeletonHeight - state.copy(floorLevel = headHeight - skeletonHeight) + val headBone = state.boneInputs[BodyPart.HEAD] + if (headBone != null && headBone.isPositionActive) { + val headHeight = headBone.position.y + state.copy(floorLevel = headHeight - skeletonHeight) + } else { + state.copy(floorLevel = 0f) + } } }