diff --git a/server/android/src/main/java/dev/slimevr/android/ForegroundService.kt b/server/android/src/main/java/dev/slimevr/android/ForegroundService.kt index 91d6b9efb..a78d06bcc 100644 --- a/server/android/src/main/java/dev/slimevr/android/ForegroundService.kt +++ b/server/android/src/main/java/dev/slimevr/android/ForegroundService.kt @@ -36,7 +36,7 @@ import dev.slimevr.provisioning.ProvisioningManager import dev.slimevr.resets.ResetsManager import dev.slimevr.routing.BoneRoutingManager import dev.slimevr.skeleton.Skeleton -import dev.slimevr.solarxr.ServerInfos +import dev.slimevr.solarxr.rpc.ServerInfos import dev.slimevr.tapdetection.TapDetectionManager import dev.slimevr.trackingchecklist.TrackingChecklist import dev.slimevr.udp.UdpServer diff --git a/server/core/src/main/java/dev/slimevr/config/settings.kt b/server/core/src/main/java/dev/slimevr/config/settings.kt index 736e4f95e..86e04ec5c 100644 --- a/server/core/src/main/java/dev/slimevr/config/settings.kt +++ b/server/core/src/main/java/dev/slimevr/config/settings.kt @@ -128,7 +128,7 @@ fun defaultKeybinds(): List = listOf( data class SkeletonTogglesConfig( val floorClip: Boolean = true, val skatingCorrection: Boolean = true, - val toeSnap: Boolean = true, + val toeSnap: Boolean = true, // TODO this was false in main val footPlant: Boolean = true, val mocapMode: Boolean = false, val useTrackerPositions: Boolean = true, 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 55dc5077f..191dcbb3d 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt @@ -86,7 +86,8 @@ class YouSpinMeRightRoundBehaviour(val inputHz: Float = 1f) : SkeletonBehaviour class ComputedSkeletonBehaviour( val hz: Int, - val processors: List = emptyList(), + val inputProcessors: List = emptyList(), + val fkProcessors: List = emptyList(), val targetProcessors: List = emptyList(), ) : SkeletonBehaviour { private val intervalDuration = (1.0 / hz).seconds @@ -104,13 +105,14 @@ class ComputedSkeletonBehaviour( val processTime = measureTime { val targetState = receiver.context.state.value + // TODO find out a cleaner way to pause tracking that doesn't add 1 frame latency (not a priority) val boneInputs = if (targetState.pausedBoneInputs != null) { // Use already-processed paused tracking data except for the head targetState.pausedBoneInputs.mutate { it[BodyPart.HEAD] = targetState.boneInputs[BodyPart.HEAD] } } else { // Run pre-FK processors // TODO: Add a constrain processor (maybe not needed) - val processedInputs = processors.fold(targetState.boneInputs) { state, processor -> processor.process(state) } + val processedInputs = inputProcessors.fold(targetState.boneInputs) { boneInputs, processor -> processor.process(boneInputs, targetState.skeletonHeight) } if (targetState.paused) { // We just paused tracking and this is the last frame before we rely on paused bone inputs receiver.context.dispatch(SkeletonActions.SetPausedBoneInputs(processedInputs)) @@ -118,13 +120,26 @@ class ComputedSkeletonBehaviour( processedInputs } - // Get head position - val rootHead = boneInputs[BodyPart.HEAD] - ?.let { Vector3(it.rawPosition.x, it.rawPosition.y, it.rawPosition.z) } - ?: Vector3(0f, targetState.skeletonHeight, 0f) + // Run initial FK + var fk = buildBones(boneInputs) - // Run FK - val fk = buildBones(boneInputs, rootHead) + // Run FK processors + fkProcessors.fold(boneInputs) { inputs, processor -> + val newInputs = processor.process(inputs, fk, targetState.floorLevel) + if (newInputs != inputs) { + // Inputs changed; re-run FK + // TODO only run FK for what changed `val changedInputs = newInputs.filter { (part, boneInput) -> boneInput != inputs.getValue(part) }` + fk = buildBones(newInputs) + + // For bones with inactive position, update their input's position in state (needed for Localizer) + // TODO figure out if we want nullable position. should this set active? activePosition? idk raahhhh - Erimel + val changedPositionInputs = newInputs.filter { (part, boneInput) -> boneInput.rawPosition != inputs.getValue(part).rawPosition } + for (input in changedPositionInputs) { + receiver.context.dispatch(SkeletonActions.SetBonePosition(input.key, input.value.rawPosition, false)) + } + } + newInputs + } // Run IK processors val ikTargets = targetProcessors.fold(bodyPartMap()) { targets, processor -> processor.process(fk, targets, targetState.floorLevel) } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/foot-plant.kt b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/foot-plant.kt new file mode 100644 index 000000000..1452e00b6 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/foot-plant.kt @@ -0,0 +1,16 @@ +package dev.slimevr.skeleton.fkprocessors + +import dev.slimevr.config.Settings +import dev.slimevr.skeleton.ComputedSkeleton +import dev.slimevr.skeleton.InputSkeleton +import dev.slimevr.skeleton.SkeletonFkProcessor +import solarxr_protocol.datatypes.BodyPart + +class FootPlantFkProcessor(val settings: Settings) : SkeletonFkProcessor { + override fun process(inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float): InputSkeleton { + if (!settings.context.state.value.data.skeletonConfig.toggles.footPlant) return inputSkeleton + + // TODO + return inputSkeleton + } +} 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 new file mode 100644 index 000000000..806614420 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt @@ -0,0 +1,22 @@ +package dev.slimevr.skeleton.fkprocessors + +import dev.slimevr.config.Settings +import dev.slimevr.skeleton.ComputedSkeleton +import dev.slimevr.skeleton.InputSkeleton +import dev.slimevr.skeleton.SkeletonFkProcessor +import dev.slimevr.skeleton.mutate +import io.github.axisangles.ktmath.Vector3 +import solarxr_protocol.datatypes.BodyPart + +class LocalizerFkProcessor(val settings: Settings) : SkeletonFkProcessor { + override fun process(inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float): InputSkeleton { + val headInput = inputSkeleton.getValue(BodyPart.HEAD) + if (!settings.context.state.value.data.skeletonConfig.toggles.mocapMode) { + if (!headInput.isPositionActive) return inputSkeleton.mutate { it[BodyPart.HEAD] = headInput.copy(rawPosition = Vector3.NULL) } + return inputSkeleton + } + + // TODO + return inputSkeleton.mutate { updated -> updated[BodyPart.HEAD] = headInput.copy(rawPosition = headInput.rawPosition.let { Vector3(it.x + 0.001f, it.y, it.z) }) } + } +} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/toe-snap.kt b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/toe-snap.kt new file mode 100644 index 000000000..d302b2279 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/toe-snap.kt @@ -0,0 +1,15 @@ +package dev.slimevr.skeleton.fkprocessors + +import dev.slimevr.config.Settings +import dev.slimevr.skeleton.ComputedSkeleton +import dev.slimevr.skeleton.InputSkeleton +import dev.slimevr.skeleton.SkeletonFkProcessor + +class ToeSnapFkProcessor(val settings: Settings) : SkeletonFkProcessor { + override fun process(inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float): InputSkeleton { + if (!settings.context.state.value.data.skeletonConfig.toggles.toeSnap) return inputSkeleton + + // TODO + return inputSkeleton + } +} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/ik.kt b/server/core/src/main/java/dev/slimevr/skeleton/ik.kt index 9e006517e..0db0c535e 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/ik.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/ik.kt @@ -117,10 +117,7 @@ fun ccdIkIteration( // Mutate the input skeleton rotateChain(boneInputs, chain, constrainedOffset) - // FIXME: This feels weird, we should probably consume the skeleton root position - // independent of the bones - val skeletonRootPos = bones[BodyPart.HEAD]?.headPosition ?: Vector3.NULL - return buildBones(boneInputs, skeletonRootPos) + return buildBones(boneInputs) } data class IKChainGoal( diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-active-link.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-active-link.kt similarity index 72% rename from server/core/src/main/java/dev/slimevr/skeleton/processors/bone-active-link.kt rename to server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-active-link.kt index ddbf2acfa..f5cc5f6ff 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-active-link.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-active-link.kt @@ -1,15 +1,17 @@ -package dev.slimevr.skeleton.processors +package dev.slimevr.skeleton.inputprocessors import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonProcessor +import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.mutate import solarxr_protocol.datatypes.BodyPart +import kotlin.collections.copy +import kotlin.collections.get /** * Handles setting the rotation of an inactive bone with the first active bone in its sources, or keeps * the old rotation if none of them are active. */ -class BoneActiveLinkProcessor : SkeletonProcessor { +class BoneActiveLinkInputProcessor : SkeletonInputProcessor { /** * First element is the BodyPart whose rawBone is not actively receiving data. * @@ -23,13 +25,13 @@ class BoneActiveLinkProcessor : SkeletonProcessor { BodyPart.HIP to arrayOf(BodyPart.WAIST, BodyPart.CHEST, BodyPart.UPPER_CHEST), ) - override fun process(inputSkeleton: InputSkeleton): InputSkeleton = inputSkeleton.mutate { updated -> + override fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton = inputSkeleton.mutate { updated -> for ((bodyPart, sources) in linkedToSources) { val bone = inputSkeleton.getValue(bodyPart) - if (bone.isActive) continue + if (bone.isRotationActive) continue val closestActiveBone = sources.firstNotNullOfOrNull { part -> - inputSkeleton[part]?.takeIf { it.isActive } + inputSkeleton[part]?.takeIf { it.isRotationActive } } ?: continue updated[bodyPart] = bone.copy(rawRotation = closestActiveBone.rawRotation) } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-direct-link.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-direct-link.kt similarity index 79% rename from server/core/src/main/java/dev/slimevr/skeleton/processors/bone-direct-link.kt rename to server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-direct-link.kt index 05febbac8..c7fefeea6 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-direct-link.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-direct-link.kt @@ -1,14 +1,14 @@ -package dev.slimevr.skeleton.processors +package dev.slimevr.skeleton.inputprocessors import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonProcessor +import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.mutate import solarxr_protocol.datatypes.BodyPart /** * Handles setting the rotation of an inactive bone with its source bone. */ -class BoneDirectLinkProcessor : SkeletonProcessor { +class BoneDirectLinkInputProcessor : SkeletonInputProcessor { /** * First element is the linked BodyPart. * @@ -37,10 +37,10 @@ class BoneDirectLinkProcessor : SkeletonProcessor { BodyPart.RIGHT_HAND to BodyPart.RIGHT_LOWER_ARM, ) - override fun process(inputSkeleton: InputSkeleton): InputSkeleton = inputSkeleton.mutate { updated -> + override fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton = inputSkeleton.mutate { updated -> for ((bodyPart, source) in linkedToSource) { val bone = updated.getValue(bodyPart) - if (bone.isActive) continue + if (bone.isRotationActive) continue val sourceBone = updated[source] updated[bodyPart] = bone.copy(rawRotation = sourceBone?.rawRotation ?: bone.rawRotation) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-prediction.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-prediction.kt similarity index 89% rename from server/core/src/main/java/dev/slimevr/skeleton/processors/bone-prediction.kt rename to server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-prediction.kt index 1746b5301..ddbe6f5ba 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-prediction.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-prediction.kt @@ -1,9 +1,9 @@ -package dev.slimevr.skeleton.processors +package dev.slimevr.skeleton.inputprocessors import dev.slimevr.config.Settings import dev.slimevr.skeleton.BodyPartMap import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonProcessor +import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.bodyPartMap import dev.slimevr.skeleton.mapValues import io.github.axisangles.ktmath.Quaternion @@ -14,7 +14,7 @@ import solarxr_protocol.rpc.FilteringType /** * Tries to predict future rotations of bones. */ -class BonePredictionProcessor(val settings: Settings) : SkeletonProcessor { +class BonePredictionInputProcessor(val settings: Settings) : SkeletonInputProcessor { private data class BoneVelocity( val lastRotation: Quaternion, val rotationDelta: Quaternion, @@ -35,7 +35,7 @@ class BonePredictionProcessor(val settings: Settings) : SkeletonProcessor { else -> 1f } - override fun process(inputSkeleton: InputSkeleton): InputSkeleton { + override fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton { val config = settings.context.state.value.data.skeletonConfig.filtering if (config.type != FilteringType.PREDICTION) { // Drop stale velocities so re-enabling doesn't diff against a long outdated pose diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-smoothing.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-smoothing.kt similarity index 88% rename from server/core/src/main/java/dev/slimevr/skeleton/processors/bone-smoothing.kt rename to server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-smoothing.kt index d96a79c12..1f535df68 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-smoothing.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-smoothing.kt @@ -1,9 +1,9 @@ -package dev.slimevr.skeleton.processors +package dev.slimevr.skeleton.inputprocessors import dev.slimevr.config.Settings import dev.slimevr.skeleton.BodyPartMap import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonProcessor +import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.bodyPartMap import dev.slimevr.skeleton.mapValues import dev.slimevr.util.inFloatingSeconds @@ -15,14 +15,14 @@ import solarxr_protocol.rpc.FilteringType /** * Running average of bones to smooth them out. */ -class BoneSmoothingProcessor(val settings: Settings) : SkeletonProcessor { +class BoneSmoothingInputProcessor(val settings: Settings) : SkeletonInputProcessor { private data class SmoothedBone(val rotation: Quaternion, val offset: Vector3) private var smoothed: BodyPartMap = bodyPartMap() private var lastProcessTime = timeSource.markNow() // TODO this isn't linear. Do we want linear smoothing like in main? - override fun process(inputSkeleton: InputSkeleton): InputSkeleton { + override fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton { val config = settings.context.state.value.data.skeletonConfig.filtering if (config.type != FilteringType.SMOOTHING) { // Drop stale poses so re-enabling doesn't blend out of an outdated frame diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt new file mode 100644 index 000000000..466089657 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt @@ -0,0 +1,29 @@ +package dev.slimevr.skeleton.inputprocessors + +import dev.slimevr.skeleton.InputSkeleton +import dev.slimevr.skeleton.SkeletonInputProcessor +import dev.slimevr.skeleton.forEachBone +import dev.slimevr.skeleton.iterateBodyPartHierarchy +import dev.slimevr.skeleton.mutate +import io.github.axisangles.ktmath.Vector3 +import kotlin.collections.set + +/** + * Handles replacing rotations of boneInputs that are not actively receiving data by + * falling back to their parent's yaw. + */ +class BoneYawFallbackInputProcessor : SkeletonInputProcessor { + override fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton = inputSkeleton.mutate { updated -> + inputSkeleton.forEachBone { parentPart, parentBone -> + if (!parentBone.isRotationActive) return@forEachBone // Parent needs to be active + val children = iterateBodyPartHierarchy(parentPart, true) + + for (childPart in children) { + val childBone = inputSkeleton.getValue(childPart.second) + if (childBone.isRotationActive) continue // Child needs to be inactive + + updated[childPart.second] = childBone.copy(rawRotation = parentBone.rawRotation.project(Vector3.POS_Y).unit()) + } + } + } +} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/finger-impute.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/finger-impute.kt similarity index 88% rename from server/core/src/main/java/dev/slimevr/skeleton/processors/finger-impute.kt rename to server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/finger-impute.kt index 798936099..f09a2f6a8 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/finger-impute.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/finger-impute.kt @@ -1,14 +1,14 @@ -package dev.slimevr.skeleton.processors +package dev.slimevr.skeleton.inputprocessors import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonProcessor +import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.mutate import solarxr_protocol.datatypes.BodyPart /** * Handles rotations of inactive finger bones. */ -class FingerImputeProcessor : SkeletonProcessor { +class FingerImputeInputProcessor : SkeletonInputProcessor { /** * First element is the linked BodyPart. * @@ -48,10 +48,10 @@ class FingerImputeProcessor : SkeletonProcessor { ) // TODO : There's more math to do here. Reference the original code. - override fun process(inputSkeleton: InputSkeleton): InputSkeleton = inputSkeleton.mutate { updated -> + override fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton = inputSkeleton.mutate { updated -> for ((bodyPart, source) in fingerToSource) { val bone = updated.getValue(bodyPart) - if (bone.isActive) continue + if (bone.isRotationActive) continue val sourceBone = updated[source] updated[bodyPart] = bone.copy(rawRotation = sourceBone?.rawRotation ?: bone.rawRotation) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-stand.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-stand.kt new file mode 100644 index 000000000..5eeee9cbe --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-stand.kt @@ -0,0 +1,24 @@ +package dev.slimevr.skeleton.inputprocessors + +import dev.slimevr.config.Settings +import dev.slimevr.skeleton.InputSkeleton +import dev.slimevr.skeleton.SkeletonInputProcessor +import dev.slimevr.skeleton.mutate +import io.github.axisangles.ktmath.Vector3 +import solarxr_protocol.datatypes.BodyPart + +/** + * Moves the head input to the skeleton height + */ +class HeadStandInputProcessor(val settings: Settings) : SkeletonInputProcessor { + override fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton { + val headBone = inputSkeleton.getValue(BodyPart.HIP) + + if (headBone.isPositionActive || settings.context.state.value.data.skeletonConfig.toggles.mocapMode) { + return inputSkeleton + } + + // Set the head position to the be standing up at the origin + return inputSkeleton.mutate { it[BodyPart.HEAD] = headBone.copy(rawPosition = Vector3(0f, skeletonHeight, 0f)) } + } +} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/hip-yaw-roll-align.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt similarity index 73% rename from server/core/src/main/java/dev/slimevr/skeleton/processors/hip-yaw-roll-align.kt rename to server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt index 330823988..ce3e1c77d 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/hip-yaw-roll-align.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt @@ -1,8 +1,8 @@ -package dev.slimevr.skeleton.processors +package dev.slimevr.skeleton.inputprocessors import dev.slimevr.config.Settings import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonProcessor +import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.mutate import dev.slimevr.skeleton.resolveAverageRotationFor import io.github.axisangles.ktmath.Quaternion @@ -11,13 +11,15 @@ import solarxr_protocol.datatypes.BodyPart /** * Handles rotating the hip' yaw and roll to match the upper legs' yaw and roll. */ -class HipYawRollAlignProcessor(val settings: Settings) : SkeletonProcessor { +class HipYawRollAlignInputProcessor(val settings: Settings) : SkeletonInputProcessor { val source = arrayOf(BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG) - override fun process(inputSkeleton: InputSkeleton): InputSkeleton = inputSkeleton.mutate { updated -> - val hipBone = updated.getValue(BodyPart.HIP) - if (!hipBone.isActive) { + override fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton { + val hipBone = inputSkeleton.getValue(BodyPart.HIP) + if (hipBone.isRotationActive) return inputSkeleton + + return inputSkeleton.mutate { updated -> val ratio = settings.context.state.value.data.skeletonConfig.ratios.interpolateHipWithUpperLegs val sourceRotation = updated.resolveAverageRotationFor(source) val alignedRotation = alignYawRoll(hipBone.rawRotation, sourceRotation) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/spine-impute.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine-impute.kt similarity index 85% rename from server/core/src/main/java/dev/slimevr/skeleton/processors/spine-impute.kt rename to server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine-impute.kt index cc41cbd42..959acc606 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/spine-impute.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine-impute.kt @@ -1,8 +1,8 @@ -package dev.slimevr.skeleton.processors +package dev.slimevr.skeleton.inputprocessors import dev.slimevr.config.Settings import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonProcessor +import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.mutate import dev.slimevr.skeleton.resolveAverageRotationFor import solarxr_protocol.datatypes.BodyPart @@ -13,7 +13,7 @@ import solarxr_protocol.datatypes.BodyPart * * Similar to FallbackProcessor specifically for the waist and hip. */ -class SpineImputeProcessor(val settings: Settings) : SkeletonProcessor { +class SpineImputeInputProcessor(val settings: Settings) : SkeletonInputProcessor { private enum class SpineSource(val parts: Array) { CHEST(arrayOf(BodyPart.CHEST)), HIP(arrayOf(BodyPart.HIP)), @@ -37,13 +37,13 @@ class SpineImputeProcessor(val settings: Settings) : SkeletonProcessor { else -> error("Invalid missing spine body part $bodyPart") } - override fun process(inputSkeleton: InputSkeleton): InputSkeleton { + override fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton { val ratios = settings.context.state.value.data.skeletonConfig.ratios - val hasChest = inputSkeleton[BodyPart.UPPER_CHEST]?.isActive == true || inputSkeleton[BodyPart.CHEST]?.isActive == true - val hasWaist = inputSkeleton[BodyPart.WAIST]?.isActive == true - val hasHip = inputSkeleton[BodyPart.HIP]?.isActive == true - val hasUpperLegs = inputSkeleton[BodyPart.LEFT_UPPER_LEG]?.isActive == true && inputSkeleton[BodyPart.RIGHT_UPPER_LEG]?.isActive == true + val hasChest = inputSkeleton[BodyPart.UPPER_CHEST]?.isRotationActive == true || inputSkeleton[BodyPart.CHEST]?.isRotationActive == true + val hasWaist = inputSkeleton[BodyPart.WAIST]?.isRotationActive == true + val hasHip = inputSkeleton[BodyPart.HIP]?.isRotationActive == true + val hasUpperLegs = inputSkeleton[BodyPart.LEFT_UPPER_LEG]?.isRotationActive == true && inputSkeleton[BodyPart.RIGHT_UPPER_LEG]?.isRotationActive == true val missingSpineParts = buildList { if (!hasWaist) add(BodyPart.WAIST) if (!hasHip) add(BodyPart.HIP) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/upper-legs-roll-align.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt similarity index 80% rename from server/core/src/main/java/dev/slimevr/skeleton/processors/upper-legs-roll-align.kt rename to server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt index 3aa5535f5..9d6dea2e0 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/upper-legs-roll-align.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt @@ -1,9 +1,8 @@ -package dev.slimevr.skeleton.processors +package dev.slimevr.skeleton.inputprocessors import dev.slimevr.config.Settings import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonProcessor -import dev.slimevr.skeleton.SkeletonState +import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.mutate import io.github.axisangles.ktmath.Quaternion import solarxr_protocol.datatypes.BodyPart @@ -11,7 +10,7 @@ import solarxr_protocol.datatypes.BodyPart /** * Handles rotating the upper legs' roll to match the lower legs' roll. */ -class UpperLegsRollAlignProcessor(val settings: Settings) : SkeletonProcessor { +class UpperLegsRollAlignInputProcessor(val settings: Settings) : SkeletonInputProcessor { /** * First value is the BodyPart to be aligned. * @@ -22,13 +21,14 @@ class UpperLegsRollAlignProcessor(val settings: Settings) : SkeletonProcessor { BodyPart.RIGHT_UPPER_LEG to BodyPart.RIGHT_LOWER_LEG, ) - override fun process(inputSkeleton: InputSkeleton): InputSkeleton { + override fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton { val ratio = settings.context.state.value.data.skeletonConfig.ratios.interpolateUpperLegsTwistWithLowerLegs + if (ratio == 0f) return inputSkeleton return inputSkeleton.mutate { updated -> for (bodyPartToSource in bodyPartToSources) { val bone = inputSkeleton.getValue(bodyPartToSource.first) - if (!bone.isActive) continue + if (!bone.isRotationActive) continue val sourceRotation = inputSkeleton.getValue(bodyPartToSource.second).rawRotation val alignedRotation = alignRoll(bone.rawRotation, sourceRotation) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/skating-correction.kt b/server/core/src/main/java/dev/slimevr/skeleton/legtweaks.kt similarity index 65% rename from server/core/src/main/java/dev/slimevr/skeleton/skating-correction.kt rename to server/core/src/main/java/dev/slimevr/skeleton/legtweaks.kt index 00b14deef..87e2b1367 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/skating-correction.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/legtweaks.kt @@ -1,6 +1,5 @@ package dev.slimevr.skeleton -import dev.slimevr.util.timeSource import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 import solarxr_protocol.datatypes.BodyPart @@ -42,7 +41,7 @@ const val SKATING_VELOCITY_THRESHOLD = 2.4f const val SKATING_ROTATIONAL_VELOCITY_THRESHOLD = 4.5f const val SKATING_ACCELERATION_THRESHOLD = 0.7f -const val FLOOR_CALIBRATION_OFFSET = 0.0025f // TODO: This was added to floorLevel. Do we want to do this? +const val FLOOR_CALIBRATION_OFFSET = 0.0025f const val FLOOR_DISTANCE_THRESHOLD = 0.065f // TODO Where are these numbers from? @@ -153,75 +152,3 @@ fun computeLockState( } else { null } - -// Probably not a SkeletonProcessor, maybe computed processor or smth -class SkatingCorrectionProcessor : SkeletonTargetProcessor { - // Centre of mass - var comState: COMState? = null - - // Do we need to store this or do we just want velocity? We can probably just pull - // the last state - val velocity: BodyPartMap = bodyPartMap() - val lockState: BodyPartMap = bodyPartMap() - - override fun process(fk: ComputedSkeleton, ikTargets: IKTargets, floorLevel: Float): IKTargets { - val curTime = timeSource.markNow() - - // Update center of mass - comState = computeComState( - curTime, - comState, - centerOfMass(fk), - ) - - for (bodyPart in VELOCITY_BODY_PARTS) { - val curBone = fk[bodyPart] ?: continue - - // TODO Pull velocity out into the base skeleton, we need it elsewhere too - val newVel = computeVelocityState( - curTime, - velocity[bodyPart], - curBone, - ) - velocity[bodyPart] = newVel - - // Consider locking BodyPart - val lastState = lockState[bodyPart] - val wasLocked = lastState?.locked ?: false - val isLocked = shouldLock( - newVel, - if (wasLocked) SKATING_LOCK_ENGAGE_PERCENT else 1f, - floorLevel, - ) - - val activeState = computeLockState( - wasLocked, - isLocked, - curBone.tailPosition, - )?.also { - // Track lock state changes - lockState[bodyPart] = it - // Otherwise pull the last state - } ?: lastState ?: continue - - if (activeState.locked) { - ikTargets[bodyPart] = activeState.position - } - } - return ikTargets - } -} - -class FloorClipProcessor( - val bodyParts: Array = arrayOf(BodyPart.LEFT_LOWER_LEG, BodyPart.RIGHT_LOWER_LEG), -) : SkeletonTargetProcessor { - override fun process(fk: ComputedSkeleton, ikTargets: IKTargets, floorLevel: Float): IKTargets { - for (bodyPart in bodyParts) { - // Get existing target or make a new one at the current bone position - val target = ikTargets[bodyPart] ?: fk[bodyPart]?.tailPosition ?: continue - // Snap the target up to the floor if it's under - ikTargets[bodyPart] = Vector3(target.x, target.y.coerceAtLeast(0f), target.z) - } - return ikTargets - } -} 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 51f76985f..417c43e7e 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -1,17 +1,20 @@ package dev.slimevr.skeleton import dev.slimevr.Phase1ContextProvider +import dev.slimevr.config.Settings import dev.slimevr.context.Behaviour import dev.slimevr.context.Context -import dev.slimevr.skeleton.processors.BoneActiveLinkProcessor -import dev.slimevr.skeleton.processors.BoneDirectLinkProcessor -import dev.slimevr.skeleton.processors.BonePredictionProcessor -import dev.slimevr.skeleton.processors.BoneSmoothingProcessor -import dev.slimevr.skeleton.processors.BoneYawFallbackProcessor -import dev.slimevr.skeleton.processors.FingerImputeProcessor -import dev.slimevr.skeleton.processors.HipYawRollAlignProcessor -import dev.slimevr.skeleton.processors.SpineImputeProcessor -import dev.slimevr.skeleton.processors.UpperLegsRollAlignProcessor +import dev.slimevr.skeleton.fkprocessors.FootPlantFkProcessor +import dev.slimevr.skeleton.fkprocessors.ToeSnapFkProcessor +import dev.slimevr.skeleton.inputprocessors.BoneActiveLinkInputProcessor +import dev.slimevr.skeleton.inputprocessors.BoneDirectLinkInputProcessor +import dev.slimevr.skeleton.inputprocessors.BonePredictionInputProcessor +import dev.slimevr.skeleton.inputprocessors.BoneSmoothingInputProcessor +import dev.slimevr.skeleton.inputprocessors.BoneYawFallbackInputProcessor +import dev.slimevr.skeleton.inputprocessors.FingerImputeInputProcessor +import dev.slimevr.skeleton.inputprocessors.HipYawRollAlignInputProcessor +import dev.slimevr.skeleton.inputprocessors.SpineImputeInputProcessor +import dev.slimevr.skeleton.inputprocessors.UpperLegsRollAlignInputProcessor import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 import kotlinx.coroutines.CoroutineScope @@ -25,7 +28,8 @@ data class BoneInput( val offset: Vector3, val rawRotation: Quaternion, val rawPosition: Vector3, - val isActive: Boolean, + val isRotationActive: Boolean, + val isPositionActive: Boolean, ) data class BoneState( @@ -83,7 +87,8 @@ val DEFAULT_SKELETON_STATE: SkeletonState = SkeletonState( bodyPart = bodyPart, offset = tailOffset, rawPosition = Vector3.NULL, - isActive = false, + isRotationActive = false, + isPositionActive = false, ) }, skeletonHeight = DEFAULT_HEIGHT, @@ -92,35 +97,35 @@ val DEFAULT_SKELETON_STATE: SkeletonState = SkeletonState( pausedBoneInputs = null, ) -fun buildBone(bone: BoneInput, parentBone: BoneState?, originPosition: Vector3 = Vector3.NULL): BoneState { - val head = parentBone?.tailPosition ?: originPosition +fun buildBone(bone: BoneInput, parentBone: BoneState?): BoneState { + // Raw position of the bone input is used for the head bone + val headPosition = parentBone?.tailPosition ?: bone.rawPosition return BoneState( bodyPart = bone.bodyPart, offset = bone.offset, - headPosition = head, + headPosition = headPosition, rotation = bone.rawRotation, - tailPosition = head + bone.rawRotation.sandwich(bone.offset), + tailPosition = headPosition + bone.rawRotation.sandwich(bone.offset), parentBone = parentBone, ) } fun buildBones( boneInputs: InputSkeleton, - rootHead: Vector3 = Vector3.NULL, hierarchy: List> = iterateBodyPartHierarchy(), ): ComputedSkeleton { val result = bodyPartMap() hierarchy.forEach { (parentPart, childPart) -> val rawBone = boneInputs[childPart] ?: return@forEach val parentBone = parentPart?.let { result[it] } - result[childPart] = buildBone(rawBone, parentBone, rootHead) + result[childPart] = buildBone(rawBone, parentBone) } return result } sealed interface SkeletonActions { - data class SetBoneRotation(val bodyPart: BodyPart, val rotation: Quaternion) : SkeletonActions - data class SetBonePosition(val bodyPart: BodyPart, val position: Vector3) : SkeletonActions + data class SetBoneRotation(val bodyPart: BodyPart, val rotation: Quaternion, val setActive: Boolean = true) : SkeletonActions + data class SetBonePosition(val bodyPart: BodyPart, val position: Vector3, val setActive: Boolean = true) : SkeletonActions data class DisableBone(val bodyPart: BodyPart) : SkeletonActions data class SetProportions(val lengths: Map) : SkeletonActions data class PauseTracking(val pause: Boolean) : SkeletonActions @@ -130,8 +135,11 @@ sealed interface SkeletonActions { typealias SkeletonContext = Context typealias SkeletonBehaviour = Behaviour -interface SkeletonProcessor { - fun process(inputSkeleton: InputSkeleton): InputSkeleton +interface SkeletonInputProcessor { + fun process(inputSkeleton: InputSkeleton, skeletonHeight: Float): InputSkeleton +} +interface SkeletonFkProcessor { + fun process(inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float): InputSkeleton } typealias IKTargets = BodyPartMap interface SkeletonTargetProcessor { @@ -140,6 +148,7 @@ interface SkeletonTargetProcessor { class Skeleton( val context: SkeletonContext, + val settings: Settings, val computed: MutableSharedFlow, ) { val currentComputed: ComputedSkeleton get() = computed.replayCache.first() @@ -150,22 +159,33 @@ class Skeleton( const val DEFAULT_HZ = 300 fun create(scope: CoroutineScope, ctx: Phase1ContextProvider, hz: Int = DEFAULT_HZ): Skeleton { + val settings = ctx.config.settings val behaviours = listOf( ProportionsBehaviour(ctx.config.userConfig), HeightLogBehaviour(), // YouSpinMeRightRoundBehaviour(inputHz = 50f), ComputedSkeletonBehaviour( hz = hz, - processors = listOf( - BoneYawFallbackProcessor(), - BoneActiveLinkProcessor(), - SpineImputeProcessor(ctx.config.settings), - HipYawRollAlignProcessor(ctx.config.settings), - UpperLegsRollAlignProcessor(ctx.config.settings), - BoneDirectLinkProcessor(), - FingerImputeProcessor(), - BonePredictionProcessor(ctx.config.settings), - BoneSmoothingProcessor(ctx.config.settings), + inputProcessors = listOf( +// HeadStandInputProcessor(settings), TODO this breaks gui preview + BoneYawFallbackInputProcessor(), + BoneActiveLinkInputProcessor(), + SpineImputeInputProcessor(settings), + HipYawRollAlignInputProcessor(settings), + UpperLegsRollAlignInputProcessor(settings), + BoneDirectLinkInputProcessor(), + FingerImputeInputProcessor(), + BonePredictionInputProcessor(settings), + BoneSmoothingInputProcessor(settings), + ), + fkProcessors = listOf( +// LocalizerFkProcessor(settings), TODO + FootPlantFkProcessor(settings), + ToeSnapFkProcessor(settings), + ), + targetProcessors = listOf( +// FloorClipTargetProcessor(settings), +// SkatingCorrectionTargetProcessor(settings), ), ), ) @@ -184,7 +204,7 @@ class Skeleton( ) computed.tryEmit(buildBones(context.state.value.boneInputs)) - return Skeleton(context, computed) + return Skeleton(context, settings, computed) } } } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-yaw-fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-yaw-fallback.kt deleted file mode 100644 index d55aae837..000000000 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-yaw-fallback.kt +++ /dev/null @@ -1,31 +0,0 @@ -package dev.slimevr.skeleton.processors - -import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonProcessor -import dev.slimevr.skeleton.forEachBone -import dev.slimevr.skeleton.iterateBodyPartHierarchy -import dev.slimevr.skeleton.mutate -import io.github.axisangles.ktmath.Vector3 -import kotlin.collections.set - -/** - * Handles replacing rotations of boneInputs that are not actively receiving data by - * falling back to their parent's yaw. - */ -class BoneYawFallbackProcessor : SkeletonProcessor { - override fun process(inputSkeleton: InputSkeleton): InputSkeleton { - return inputSkeleton.mutate { updated -> - inputSkeleton.forEachBone { parentPart, parentBone -> - if (!parentBone.isActive) return@forEachBone // Parent needs to be active - val children = iterateBodyPartHierarchy(parentPart, true) - - for (childPart in children) { - val childBone = inputSkeleton.getValue(childPart.second) - if (childBone.isActive) continue // Child needs to be inactive - - updated[childPart.second] = childBone.copy(rawRotation = parentBone.rawRotation.project(Vector3.POS_Y).unit()) - } - } - } - } -} 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 6ed6696e2..8dd90b138 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt @@ -7,17 +7,17 @@ import solarxr_protocol.datatypes.BodyPart fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when (action) { is SkeletonActions.SetBoneRotation -> { val bone = state.boneInputs[action.bodyPart] ?: return state - state.copy(boneInputs = state.boneInputs.mutate { it[action.bodyPart] = bone.copy(rawRotation = action.rotation, isActive = true) }) + state.copy(boneInputs = state.boneInputs.mutate { it[action.bodyPart] = bone.copy(rawRotation = action.rotation, isRotationActive = action.setActive) }) } is SkeletonActions.SetBonePosition -> { val bone = state.boneInputs[action.bodyPart] ?: return state - state.copy(boneInputs = state.boneInputs.mutate { it[action.bodyPart] = bone.copy(rawPosition = action.position, isActive = true) }) + state.copy(boneInputs = state.boneInputs.mutate { it[action.bodyPart] = bone.copy(rawPosition = action.position, isPositionActive = action.setActive) }) } is SkeletonActions.DisableBone -> { val bone = state.boneInputs[action.bodyPart] ?: return state - state.copy(boneInputs = state.boneInputs.mutate { it[action.bodyPart] = bone.copy(rawRotation = Quaternion.IDENTITY, rawPosition = Vector3.NULL, isActive = false) }) + state.copy(boneInputs = state.boneInputs.mutate { it[action.bodyPart] = bone.copy(rawRotation = Quaternion.IDENTITY, rawPosition = Vector3.NULL, isRotationActive = false, isPositionActive = false) }) } is SkeletonActions.SetProportions -> { diff --git a/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt new file mode 100644 index 000000000..c6d07f131 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt @@ -0,0 +1,26 @@ +package dev.slimevr.skeleton.targetprocessors + +import dev.slimevr.config.Settings +import dev.slimevr.skeleton.ComputedSkeleton +import dev.slimevr.skeleton.IKTargets +import dev.slimevr.skeleton.SkeletonTargetProcessor +import io.github.axisangles.ktmath.Vector3 +import solarxr_protocol.datatypes.BodyPart +import kotlin.collections.set + +class FloorClipTargetProcessor( + val settings: Settings, + val bodyParts: Array = arrayOf(BodyPart.LEFT_LOWER_LEG, BodyPart.RIGHT_LOWER_LEG), +) : SkeletonTargetProcessor { + override fun process(fk: ComputedSkeleton, ikTargets: IKTargets, floorLevel: Float): IKTargets { + if (!settings.context.state.value.data.skeletonConfig.toggles.floorClip) return ikTargets + + for (bodyPart in bodyParts) { + // Get existing target or make a new one at the current bone position + val target = ikTargets[bodyPart] ?: fk[bodyPart]?.tailPosition ?: continue + // Snap the target up to the floor if it's under + ikTargets[bodyPart] = Vector3(target.x, target.y.coerceAtLeast(0f), target.z) + } + return ikTargets + } +} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/skating-correction.kt b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/skating-correction.kt new file mode 100644 index 000000000..16136cad2 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/skating-correction.kt @@ -0,0 +1,82 @@ +package dev.slimevr.skeleton.targetprocessors + +import dev.slimevr.config.Settings +import dev.slimevr.skeleton.BodyPartMap +import dev.slimevr.skeleton.COMState +import dev.slimevr.skeleton.ComputedSkeleton +import dev.slimevr.skeleton.FLOOR_CALIBRATION_OFFSET +import dev.slimevr.skeleton.IKTargets +import dev.slimevr.skeleton.LockState +import dev.slimevr.skeleton.SKATING_LOCK_ENGAGE_PERCENT +import dev.slimevr.skeleton.SkeletonTargetProcessor +import dev.slimevr.skeleton.VELOCITY_BODY_PARTS +import dev.slimevr.skeleton.VelocityState +import dev.slimevr.skeleton.bodyPartMap +import dev.slimevr.skeleton.centerOfMass +import dev.slimevr.skeleton.computeComState +import dev.slimevr.skeleton.computeLockState +import dev.slimevr.skeleton.computeVelocityState +import dev.slimevr.skeleton.shouldLock +import dev.slimevr.util.timeSource + +class SkatingCorrectionTargetProcessor(val settings: Settings) : SkeletonTargetProcessor { + // Centre of mass + var comState: COMState? = null + + // Do we need to store this or do we just want velocity? + // We can probably just pull the last state + val velocity: BodyPartMap = bodyPartMap() + val lockState: BodyPartMap = bodyPartMap() + + override fun process(fk: ComputedSkeleton, ikTargets: IKTargets, floorLevel: Float): IKTargets { + val skeletonConfig = settings.context.state.value.data.skeletonConfig + if (!skeletonConfig.toggles.skatingCorrection) return ikTargets + + val curTime = timeSource.markNow() + + // Update centre of mass + comState = computeComState( + curTime, + comState, + centerOfMass(fk), + ) + + val correctionStrength = skeletonConfig.ratios.skatingCorrectionStrength + + for (bodyPart in VELOCITY_BODY_PARTS) { + val curBone = fk[bodyPart] ?: continue + + // TODO Pull velocity out into the base skeleton, we need it elsewhere too + val newVel = computeVelocityState( + curTime, + velocity[bodyPart], + curBone, + ) + velocity[bodyPart] = newVel + + // Consider locking BodyPart + val lastState = lockState[bodyPart] + val wasLocked = lastState?.locked ?: false + val isLocked = shouldLock( + newVel, + if (wasLocked) SKATING_LOCK_ENGAGE_PERCENT else 1f, + floorLevel + FLOOR_CALIBRATION_OFFSET, + ) + + val activeState = computeLockState( + wasLocked, + isLocked, + curBone.tailPosition, + )?.also { + // Track lock state changes + lockState[bodyPart] = it + // Otherwise pull the last state + } ?: lastState ?: continue + + if (activeState.locked) { + ikTargets[bodyPart] = activeState.position + } + } + return ikTargets + } +} diff --git a/server/core/src/main/java/dev/slimevr/solarxr/module.kt b/server/core/src/main/java/dev/slimevr/solarxr/module.kt index 15de1bd52..c1f716b5f 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/module.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/module.kt @@ -177,7 +177,8 @@ class SolarXRBridge( VrcBehaviour( vrc, appContext.server, - userHeight = { appContext.skeleton.context.state.value.skeletonHeight }) + userHeight = { appContext.skeleton.context.state.value.skeletonHeight }, + ), ) } add(ResetsBehaviour(appContext.config.settings)) diff --git a/server/core/src/main/java/dev/slimevr/solarxr/rpc/firmware.kt b/server/core/src/main/java/dev/slimevr/solarxr/rpc/firmware.kt index 3d56aab8b..6502f7694 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/rpc/firmware.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/rpc/firmware.kt @@ -16,8 +16,7 @@ import solarxr_protocol.rpc.FirmwareUpdateStopQueuesRequest import solarxr_protocol.rpc.OTAFirmwareUpdate import solarxr_protocol.rpc.SerialFirmwareUpdate -class FirmwareBehaviour(private val server: VRServer, private val firmwareManager: FirmwareManager) : - SolarXRBridgeBehaviour { +class FirmwareBehaviour(private val server: VRServer, private val firmwareManager: FirmwareManager) : SolarXRBridgeBehaviour { override fun observe(receiver: SolarXRBridge) { val scope = receiver.context.scope diff --git a/server/core/src/test/java/dev/slimevr/TestServer.kt b/server/core/src/test/java/dev/slimevr/TestServer.kt index 06a4d2dd8..fcc513d2a 100644 --- a/server/core/src/test/java/dev/slimevr/TestServer.kt +++ b/server/core/src/test/java/dev/slimevr/TestServer.kt @@ -119,7 +119,7 @@ fun buildTestSkeleton(scope: CoroutineScope): Skeleton { onBufferOverflow = BufferOverflow.DROP_OLDEST, ) computed.tryEmit(buildBones(context.state.value.boneInputs)) - val skeleton = Skeleton(context, computed) + val skeleton = Skeleton(context, buildTestSettings(scope), computed) skeleton.startObserving() return skeleton } @@ -136,7 +136,7 @@ fun buildTestResetsManager(server: VRServer, settings: Settings, scope: Coroutin behaviours = listOf(ResetsMountingTimeoutBehaviour()), name = "TestResetsManager", ) - val resetsManager = ResetsManager(context, server, settings) + val resetsManager = ResetsManager(context, server, settings, buildTestSkeleton(scope)) resetsManager.startObserving() return resetsManager } diff --git a/server/core/src/test/java/dev/slimevr/bvh/BvhStreamTest.kt b/server/core/src/test/java/dev/slimevr/bvh/BvhStreamTest.kt index 7d5966585..d4e690f83 100644 --- a/server/core/src/test/java/dev/slimevr/bvh/BvhStreamTest.kt +++ b/server/core/src/test/java/dev/slimevr/bvh/BvhStreamTest.kt @@ -3,8 +3,10 @@ package dev.slimevr.bvh import dev.slimevr.config.TextFileHandle import dev.slimevr.skeleton.DEFAULT_SKELETON_STATE import dev.slimevr.skeleton.buildBones +import dev.slimevr.skeleton.mutate import io.github.axisangles.ktmath.Vector3 import kotlinx.coroutines.test.runTest +import solarxr_protocol.datatypes.BodyPart import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -15,8 +17,16 @@ class BvhStreamTest { val file = InMemoryBvhFile() val stream = BvhStream(file) val initialBones = buildBones(DEFAULT_SKELETON_STATE.boneInputs) - val firstFrame = buildBones(DEFAULT_SKELETON_STATE.boneInputs, rootHead = Vector3(1f, 2f, 3f)) - val secondFrame = buildBones(DEFAULT_SKELETON_STATE.boneInputs, rootHead = Vector3(4f, 5f, 6f)) + val firstFrame = buildBones( + DEFAULT_SKELETON_STATE.boneInputs.mutate { + it[BodyPart.HEAD] = it.getValue(BodyPart.HEAD).copy(rawPosition = Vector3(1f, 2f, 3f)) + }, + ) + val secondFrame = buildBones( + DEFAULT_SKELETON_STATE.boneInputs.mutate { + it[BodyPart.HEAD] = it.getValue(BodyPart.HEAD).copy(rawPosition = Vector3(4f, 5f, 6f)) + }, + ) stream.writeHeader(initialBones) stream.writeFrame(firstFrame) @@ -41,7 +51,13 @@ class BvhStreamTest { stream.close() val before = file.content() - stream.writeFrame(buildBones(DEFAULT_SKELETON_STATE.boneInputs, rootHead = Vector3(7f, 8f, 9f))) + stream.writeFrame( + buildBones( + DEFAULT_SKELETON_STATE.boneInputs.mutate { + it[BodyPart.HEAD] = it.getValue(BodyPart.HEAD).copy(rawPosition = Vector3(7f, 8f, 9f)) + }, + ), + ) assertEquals(before, file.content()) } diff --git a/server/core/src/test/java/dev/slimevr/skeleton/IKTest.kt b/server/core/src/test/java/dev/slimevr/skeleton/IKTest.kt index 17bcf1532..ddceb2066 100644 --- a/server/core/src/test/java/dev/slimevr/skeleton/IKTest.kt +++ b/server/core/src/test/java/dev/slimevr/skeleton/IKTest.kt @@ -15,35 +15,40 @@ class IKTest { offset = Vector3.NEG_Y, rawRotation = Quaternion.IDENTITY, rawPosition = Vector3.NULL, - isActive = false, + isRotationActive = false, + isPositionActive = false, ) boneInputs[BodyPart.UPPER_CHEST] = BoneInput( bodyPart = BodyPart.UPPER_CHEST, offset = Vector3.NEG_Y, rawRotation = Quaternion.IDENTITY, rawPosition = Vector3.NULL, - isActive = false, + isRotationActive = false, + isPositionActive = false, ) boneInputs[BodyPart.CHEST] = BoneInput( bodyPart = BodyPart.CHEST, offset = Vector3.NEG_Y, rawRotation = Quaternion.IDENTITY, rawPosition = Vector3.NULL, - isActive = false, + isRotationActive = false, + isPositionActive = false, ) boneInputs[BodyPart.WAIST] = BoneInput( bodyPart = BodyPart.WAIST, offset = Vector3.NEG_Y, rawRotation = Quaternion.IDENTITY, rawPosition = Vector3.NULL, - isActive = false, + isRotationActive = false, + isPositionActive = false, ) boneInputs[BodyPart.HIP] = BoneInput( bodyPart = BodyPart.HIP, offset = Vector3.NEG_Y, rawRotation = Quaternion.IDENTITY, rawPosition = Vector3.NULL, - isActive = false, + isRotationActive = false, + isPositionActive = false, ) val bones = buildBones(boneInputs)