Make position nullable again

This commit is contained in:
Erimel committed 2026-09-17 18:20:54 -04:00
1 parent 18ef835373
commit 081f576643
6 files changed
+41 -20

No files matched your search

@@ -88,9 +88,9 @@ class ResetsManager(val context: ResetsContext, val server: VRServer, val settin
// Reset trackers
executeTrackerResets(resetType, bodyParts, settings.context.state.value.data.resetsConfig)
// Reset skeleton floor level on full resets
if (resetType == ResetType.FULL) {
skeleton.context.dispatch(SkeletonActions.ResetFloorLevel)
// Tell the skeleton to set the floor level and try resetting the head position (for mocap mode)
skeleton.context.dispatchAll(listOf(SkeletonActions.ResetHeadPosition, SkeletonActions.ResetFloorLevel))
}
// Update state and config
@@ -1,5 +1,6 @@
package dev.slimevr.skeleton
import dev.slimevr.config.Settings
import dev.slimevr.config.UserConfig
import dev.slimevr.logging.AppLogger
import dev.slimevr.util.MonotonicValueTimeMark
@@ -54,6 +55,17 @@ 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 {
@@ -231,7 +243,7 @@ class ComputedSkeletonBehaviour(
// 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
val processedInputs = runInputProcessors(inputProcessors, targetState.boneInputs, targetState.skeletonHeight)
@@ -260,9 +272,13 @@ class ComputedSkeletonBehaviour(
fkChangedParts.clear()
boneInputs.forEachBone { bodyPart, boneInput ->
val previous = beforeFk[bodyPart]
if (boneInput != previous) {
fkChangedParts.add(bodyPart)
beforeFk[bodyPart] = boneInput
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))
}
}
// Inputs changed; re-run FK
@@ -317,7 +317,7 @@ class LocalizerFkProcessor(val settings: Settings) :
targetFoot = currentFoot
// Update head position from travel
val newHeadPosition = headInput.position + travel
val newHeadPosition = (headInput.position ?: Vector3.ZERO) + travel
mutableInputSkeleton[BodyPart.HEAD] = headInput.copy(position = newHeadPosition)
}
@@ -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.isPositionActive) return
if (headBone.position != null) return
// Set the head position to the be standing up at the origin
mutableInputSkeleton[BodyPart.HEAD] = headBone.copy(position = Vector3(0f, skeletonHeight, 0f))
@@ -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,
@@ -101,7 +101,7 @@ val DEFAULT_BONE_INPUT = BoneInput(
offset = Vector3.ZERO,
rotation = Quaternion.IDENTITY,
acceleration = Vector3.ZERO,
position = Vector3.ZERO,
position = null,
isRotationActive = false,
isAccelerationActive = false,
isPositionActive = false,
@@ -126,7 +126,8 @@ 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
val headPosition = parentBone?.let { it.tailPosition + it.rotation.sandwich(bone.headOffset) }
?: bone.position ?: Vector3.ZERO
return BoneState(
parentBone = parentBone,
bodyPart = bone.bodyPart,
@@ -166,6 +167,7 @@ sealed interface SkeletonActions {
data class SetProportions(val lengths: Map<SkeletonBone, Float>) : SkeletonActions
data class PauseTracking(val pause: Boolean) : SkeletonActions
data class SetPausedBoneInputs(val pausedBoneInputs: InputSkeleton) : SkeletonActions
data object ResetHeadPosition : SkeletonActions
data object ResetFloorLevel : SkeletonActions
data class RequestProcessorReset(val resetType: ResetType) : SkeletonActions
data class ProcessorResetsApplied(val count: Int) : SkeletonActions
@@ -208,6 +210,7 @@ class Skeleton(
val behaviours = listOf(
ProportionsBehaviour(ctx.config.userConfig),
HeightLogBehaviour(),
LocalizerResetBehaviour(settings),
// YouSpinMeRightRoundBehaviour(inputHz = 50f),
ComputedSkeletonBehaviour(
hz = hz,
@@ -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 ?: Vector3.ZERO, isPositionActive = action.setActive) })
state.copy(boneInputs = state.boneInputs.mutateCopy { it[action.bodyPart] = bone.copy(position = action.position, 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 = Vector3.ZERO,
position = null,
isRotationActive = false,
isAccelerationActive = false,
isPositionActive = false,
@@ -51,15 +51,17 @@ 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.ResetFloorLevel -> {
val skeletonHeight = state.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)
}
val headHeight = state.boneInputs[BodyPart.HEAD]?.position?.y ?: skeletonHeight
state.copy(floorLevel = headHeight - skeletonHeight)
}
is SkeletonActions.RequestProcessorReset -> state.copy(processorResets = state.processorResets + action.resetType)