Simplify position input logic

This commit is contained in:
Erimel committed 2026-09-16 16:10:06 -04:00
1 parent 3b51900a00
commit 5fec3962ee
6 files changed
+26 -44

No files matched your search

@@ -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.)
@@ -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
@@ -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
@@ -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))
@@ -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<SkeletonBone, Float>) : 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,
@@ -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)
}
}
}