From dce4717510b5be463649fa1bf03959f9f422112e Mon Sep 17 00:00:00 2001 From: Erimel Date: Fri, 4 Sep 2026 20:03:40 -0400 Subject: [PATCH] Progress on Mocap Mode! ^-^ --- .../dev/slimevr/skeleton/centre-of-mass.kt | 56 ++++ .../skeleton/fkprocessors/localizer.kt | 265 +++++++++++++----- .../main/java/dev/slimevr/skeleton/module.kt | 1 + .../targetprocessors/skating-correction.kt | 59 +--- 4 files changed, 255 insertions(+), 126 deletions(-) create mode 100644 server/core/src/main/java/dev/slimevr/skeleton/centre-of-mass.kt diff --git a/server/core/src/main/java/dev/slimevr/skeleton/centre-of-mass.kt b/server/core/src/main/java/dev/slimevr/skeleton/centre-of-mass.kt new file mode 100644 index 000000000..895f11e17 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/centre-of-mass.kt @@ -0,0 +1,56 @@ +package dev.slimevr.skeleton + +import dev.slimevr.util.inFloatingSeconds +import io.github.axisangles.ktmath.Vector3 +import solarxr_protocol.datatypes.BodyPart +import kotlin.time.ComparableTimeMark + +// TODO Where are these numbers from? +val BODY_PART_MASSES = mapOf( + BodyPart.HEAD to 0.0827f, + BodyPart.LEFT_UPPER_ARM to 0.0263f, + BodyPart.RIGHT_UPPER_ARM to 0.0263f, + BodyPart.LEFT_LOWER_ARM to 0.0224f, + BodyPart.RIGHT_LOWER_ARM to 0.0224f, + BodyPart.CHEST to 0.1870f, + BodyPart.WAIST to 0.1320f, + BodyPart.HIP to 0.1530f, + BodyPart.LEFT_UPPER_LEG to 0.1122f, + BodyPart.RIGHT_UPPER_LEG to 0.1122f, + BodyPart.LEFT_LOWER_LEG to 0.0620f, + BodyPart.RIGHT_LOWER_LEG to 0.0620f, +) + +data class COMState( + val time: ComparableTimeMark, + val position: Vector3, + val velocity: Vector3, + val acceleration: Vector3, +) + +fun centreOfMass( + bones: ComputedSkeleton, +): Vector3 = BODY_PART_MASSES.entries.fold(Vector3.ZERO) { acc: Vector3, massEntry -> + val bone = bones[massEntry.key] ?: return@fold acc + val boneCenter = (bone.headPosition + bone.tailPosition) / 2f + return@fold acc + (boneCenter * massEntry.value) +} + +fun computeComState(time: ComparableTimeMark, last: COMState?, com: Vector3): COMState = if (last != null) { + val deltaTime = (time - last.time).inFloatingSeconds + val comVelocity = (com - last.position) / deltaTime + val comAcceleration = (comVelocity - last.velocity) / deltaTime + COMState( + time, + com, + comVelocity, + comAcceleration, + ) +} else { + COMState( + time, + com, + Vector3.ZERO, + Vector3.ZERO, + ) +} 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 0e6aceaa5..7db9854bd 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 @@ -5,89 +5,35 @@ import dev.slimevr.skeleton.BoneState import dev.slimevr.skeleton.ComputedSkeleton import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonFkProcessor +import dev.slimevr.skeleton.centreOfMass +import dev.slimevr.util.timeSource import io.github.axisangles.ktmath.Vector3 import solarxr_protocol.datatypes.BodyPart - -enum class MovementStates { - LEFT_LOCKED, - RIGHT_LOCKED, - NONE_LOCKED, - FOLLOW_FOOT, - FOLLOW_COM, - FOLLOW_SITTING, -} +import kotlin.time.Duration +import kotlin.time.Duration.Companion.seconds private const val WARMUP_FRAMES = 100 // ~0.1 seconds private const val MAX_FOOT_PERCENTAGE = 50.0f private const val MAX_ACCEL_UP = 2.0f private const val SITTING_KNEE_THRESHOLD = 1.1f -private const val SITTING_EARLY = 1000f private const val VELOCITY_SAMPLE_RATE: Long = 100000000 // 10ms private const val CONSTANT_ACCELERATION: Float = 2.0f +private val SITTING_THRESHOLD = 1.2.seconds -fun getPlantedFoot(fk: ComputedSkeleton): MovementStates { - val leftFoot = fk[BodyPart.LEFT_FOOT] ?: return MovementStates.NONE_LOCKED - val rightFoot = fk[BodyPart.RIGHT_FOOT] ?: return MovementStates.NONE_LOCKED - - return if (leftFoot.headPosition.y < rightFoot.headPosition.y) { - MovementStates.LEFT_LOCKED - } else { - MovementStates.RIGHT_LOCKED - } +enum class FollowSource { + FOOT, + COM, + HIP, } -// check if the foot that is planted is actually planted -private fun getWorldReference(fk: ComputedSkeleton): MovementStates = if (isUserSitting(fk)) { - // User is sitting down - MovementStates.FOLLOW_SITTING -} else if (!isFootOnGround(fk)) { - // if the foot is not on the ground, use the COM - MovementStates.FOLLOW_COM -} else { - MovementStates.FOLLOW_FOOT -} - -// Gets travel from a planted foot -private fun getPlantedFootTravel(fk: ComputedSkeleton): Vector3 { - // Which foot is planted - val plantedFoot = getPlantedFoot(fk) - val footBone = when (plantedFoot) { - MovementStates.LEFT_LOCKED -> fk[BodyPart.LEFT_FOOT] - MovementStates.RIGHT_LOCKED -> fk[BodyPart.RIGHT_FOOT] - else -> null - } - if (footBone == null) return Vector3.ZERO - - // Get foot travel - val footPosition: Vector3 = footBone.headPosition - return footPosition - Vector3.ZERO -} - -// Gets the sitting travel (emulates hip lock) -private fun computeSittingTravel(fk: ComputedSkeleton): Vector3 { - val hip = fk[BodyPart.HIP]?.headPosition ?: return Vector3.ZERO - return Vector3.ZERO -} - -// Returns true if either foot's position is below 0 -fun isFootOnGround(fk: ComputedSkeleton): Boolean { - val leftFoot = fk[BodyPart.LEFT_FOOT] ?: return false - val rightFoot = fk[BodyPart.RIGHT_FOOT] ?: return false - return leftFoot.headPosition.y <= 0f || rightFoot.headPosition.y <= 0f -} - -// returns the bone closest to or furthest inside the ground -private fun getLowestBone(fk: ComputedSkeleton): BoneState = fk.minBy { it.value.headPosition.y }.value - -// returns true if the user is likely sitting -// (assumes the floor is flat at 0.0) +// Returns true if the user is likely sitting private fun isUserSitting(fk: ComputedSkeleton): Boolean { // based on the waist to knee vector decide if the user is sitting or // standing (ie, if the user is sitting the vector will be pointing off // to the side for both feet) var leftKnee: Vector3 = fk[BodyPart.LEFT_UPPER_LEG]?.tailPosition ?: return false var rightKnee: Vector3 = fk[BodyPart.RIGHT_UPPER_LEG]?.tailPosition ?: return false - val hip: Vector3 = fk[BodyPart.HIP]?.headPosition ?: return false + val hip: Vector3 = fk[BodyPart.HIP]?.tailPosition ?: return false leftKnee = hip - leftKnee rightKnee = hip - rightKnee @@ -98,24 +44,199 @@ private fun isUserSitting(fk: ComputedSkeleton): Boolean { return sittingLeft && sittingRight } +// Returns true if either foot's position is below 0 +fun isFootOnGround(fk: ComputedSkeleton): Boolean { + val leftFoot = fk[BodyPart.LEFT_FOOT] ?: return false + val rightFoot = fk[BodyPart.RIGHT_FOOT] ?: return false + return leftFoot.tailPosition.y <= 0f || rightFoot.tailPosition.y <= 0f +} + +private fun getSourceToFollow(fk: ComputedSkeleton): FollowSource = if (isUserSitting(fk)) { + // The user is sitting down + FollowSource.HIP +} else if (isFootOnGround(fk)) { + // One of the user's foot is on the ground + FollowSource.FOOT +} else { + // The user is neither sitting nor has a foot on the ground. Use Center Of Mass. + FollowSource.COM +} + +// returns the bone closest to or furthest inside the ground +fun getLowestBone(fk: ComputedSkeleton): BoneState = fk.minBy { it.value.tailPosition.y }.value + +object FootLocalizer { + enum class PlantedFoot { + LEFT, + RIGHT, + NONE, + } + + fun getPlantedFoot(fk: ComputedSkeleton): PlantedFoot { + val leftFoot = fk[BodyPart.LEFT_FOOT] ?: return PlantedFoot.NONE + val rightFoot = fk[BodyPart.RIGHT_FOOT] ?: return PlantedFoot.NONE + + // TODO + return if (leftFoot.tailPosition.y < rightFoot.tailPosition.y) { + PlantedFoot.LEFT + } else { + PlantedFoot.RIGHT + } + } + + fun getCurrentFootPosition(fk: ComputedSkeleton, plantedFoot: PlantedFoot) = when (plantedFoot) { + PlantedFoot.LEFT -> fk[BodyPart.LEFT_FOOT] + PlantedFoot.RIGHT -> fk[BodyPart.RIGHT_FOOT] + else -> null + }?.tailPosition ?: Vector3.ZERO + + fun computeFootTravel(currentFoot: Vector3, targetFoot: Vector3) = currentFoot - targetFoot +} + +object HipLocalizer { + fun getAdjustedTargetHip(fk: ComputedSkeleton, targetHip: Vector3): Vector3 { + val lowestBone = getLowestBone(fk) + if (lowestBone.tailPosition.y < 0f) { + return Vector3(targetHip.x, targetHip.y - lowestBone.tailPosition.y, targetHip.z) + } + return targetHip + } + + fun computeSittingTravel(fk: ComputedSkeleton, hip: Vector3, targetHip: Vector3) = hip - targetHip +} + +object COMLocalizer { + // get the velocity of the COM +// private fun getCOMVelocity(): Vector3 { +// val comY = comVelocity.y +// +// var buf = bufCur +// val timeStart: Long = buf.timeOfFrame +// var timeEnd = timeStart - VELOCITY_SAMPLE_RATE +// val comPosStart: Vector3 = buf.centerOfMass +// +// // get the buffer that occurred VELOCITY_SAMPLE_RATE ago in time +// while (buf.timeOfFrame > timeEnd && buf.parent != null) { +// buf = buf.parent!! +// } +// +// val comPosEnd: Vector3 = buf.centerOfMass +// timeEnd = buf.timeOfFrame +// +// // calculate the velocity +// comVelocity = (comPosEnd - comPosStart) / ((timeEnd - timeStart) / LegTweaksBuffer.NS_CONVERT) +// +// // if the feet have been the reference for a short amount of time nullify any upwards acceleration to prevent flying away +// if (footFrames < WARMUP_FRAMES) { +// comAccel = Vector3( +// comAccel.x, +// FastMath.clamp(comAccel.y, -9999.0f, 0.0f), +// comAccel.z, +// ) +// } +// +// // constantly pull the skeleton down a little to account for acceleration +// // inaccuracy +// val gravity = comAccel.y - CONSTANT_ACCELERATION +// +// // add the acceleration of gravity +// comVelocity = Vector3( +// comVelocity.x, +// comY + (gravity / bufCur.getTimeDelta()), +// comVelocity.z, +// ) +// +// return comVelocity +// } +// +// private fun getTargetCOM() { +// // if not in COM tracking mode, just use the current COM +// var targetCOM = Vector3.ZERO +// var currentCOM = Vector3.ZERO +// +// if (worldReference == MovementStates.FOLLOW_FOOT || worldReference == MovementStates.FOLLOW_SITTING) { +// targetCOM = centreOfMass(fk) +// } else { +// currentCOM = targetCOM +// } +// +// targetCOM += (comVelocity / bufCur.getTimeDelta()) +// +// val lowTracker = getLowestTracker() +// +// // update the target COM and velocity to reflect this new distance +// if (lowTracker != null) { +// if (lowTracker.position.y < uncorrectedFloor) { +// targetCOM = Vector3(targetCOM.x, targetCOM.y + (uncorrectedFloor - lowTracker.position.y), targetCOM.z) +// comVelocity = Vector3(comVelocity.x, 0.0f, comVelocity.z) +// } +// } +// } + + fun computeCOMTravel(currentCOM: Vector3, targetCOM: Vector3) = currentCOM - targetCOM +} + class LocalizerFkProcessor(val settings: Settings) : SkeletonFkProcessor { + private var plantedFoot = FootLocalizer.PlantedFoot.LEFT + private var targetFoot = Vector3.ZERO + + private var sittingTime = Duration.ZERO + private var targetHip = Vector3.ZERO + + private var targetCOM = Vector3.ZERO + + private var lastProcessTime = timeSource.markNow() + override fun process(mutableInputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float) { val headInput = mutableInputSkeleton[BodyPart.HEAD] ?: return if (headInput.isPositionActive || !settings.context.state.value.data.skeletonConfig.toggles.mocapMode) { return } - val travel = when (getWorldReference(fk)) { - MovementStates.FOLLOW_FOOT -> getPlantedFootTravel(fk) + val now = timeSource.markNow() + val deltaTime = now - lastProcessTime + lastProcessTime = now -// MovementStates.FOLLOW_COM -> getCOMTravel(fk) TODO + val currentCOM = centreOfMass(fk) + val currentHip = fk[BodyPart.HIP]?.tailPosition ?: Vector3.ZERO - MovementStates.FOLLOW_SITTING -> computeSittingTravel(fk) - - else -> Vector3.ZERO + // Only follow the hip if the user's been sitting for enough time, else follow the foot + val followSource = getSourceToFollow(fk).let { + if (it == FollowSource.HIP) { + sittingTime += deltaTime + if (sittingTime < SITTING_THRESHOLD) { + FollowSource.FOOT + } else { + FollowSource.HIP + } + } else { + sittingTime = Duration.ZERO + it + } } - val newHeadPosition = (headInput.position ?: Vector3.ZERO) - travel + val travel = when (followSource) { + FollowSource.FOOT -> { + targetHip = currentHip + plantedFoot = FootLocalizer.getPlantedFoot(fk) + val currentFoot = FootLocalizer.getCurrentFootPosition(fk, plantedFoot) + targetFoot = FootLocalizer.computeFootTravel(currentFoot, targetFoot) + val comTravel = COMLocalizer.computeCOMTravel(currentCOM, targetCOM) + Vector3(targetFoot.x, comTravel.y, targetFoot.z) + } + + FollowSource.COM -> { + targetHip = currentHip + COMLocalizer.computeCOMTravel(currentCOM, targetCOM) + } + + FollowSource.HIP -> { + targetHip = HipLocalizer.getAdjustedTargetHip(fk, targetHip) + HipLocalizer.computeSittingTravel(fk, currentHip, targetHip) + } + } + + val newHeadPosition = (headInput.position ?: Vector3.ZERO) + travel mutableInputSkeleton[BodyPart.HEAD] = headInput.copy(position = newHeadPosition) } } 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 f037c5cef..7b9e01551 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -6,6 +6,7 @@ import dev.slimevr.context.Behaviour import dev.slimevr.context.Context import dev.slimevr.skeleton.computedprocessors.VelocityComputedProcessor import dev.slimevr.skeleton.fkprocessors.FootPlantFkProcessor +import dev.slimevr.skeleton.fkprocessors.LocalizerFkProcessor import dev.slimevr.skeleton.fkprocessors.ToeSnapFkProcessor import dev.slimevr.skeleton.inputprocessors.BoneActiveLinkInputProcessor import dev.slimevr.skeleton.inputprocessors.BoneDirectLinkInputProcessor 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 index ef8c844a1..5c8cdd346 100644 --- 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 @@ -2,23 +2,17 @@ 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.IKTargets import dev.slimevr.skeleton.SkeletonTargetProcessor import dev.slimevr.skeleton.Velocity import dev.slimevr.skeleton.bodyPartMap -import dev.slimevr.util.inFloatingSeconds +import dev.slimevr.skeleton.centreOfMass +import dev.slimevr.skeleton.computeComState import dev.slimevr.util.timeSource import io.github.axisangles.ktmath.Vector3 import solarxr_protocol.datatypes.BodyPart -import kotlin.time.ComparableTimeMark - -data class COMState( - val time: ComparableTimeMark, - val position: Vector3, - val velocity: Vector3, - val acceleration: Vector3, -) data class LockState( val locked: Boolean, @@ -41,22 +35,6 @@ const val SKATING_ACCELERATION_THRESHOLD = 0.49f const val FLOOR_CALIBRATION_OFFSET = 0.0025f const val FLOOR_DISTANCE_THRESHOLD = 0.065f -// TODO Where are these numbers from? -val BODY_PART_MASSES = mapOf( - BodyPart.HEAD to 0.0827f, - BodyPart.LEFT_UPPER_ARM to 0.0263f, - BodyPart.RIGHT_UPPER_ARM to 0.0263f, - BodyPart.LEFT_LOWER_ARM to 0.0224f, - BodyPart.RIGHT_LOWER_ARM to 0.0224f, - BodyPart.CHEST to 0.1870f, - BodyPart.WAIST to 0.1320f, - BodyPart.HIP to 0.1530f, - BodyPart.LEFT_UPPER_LEG to 0.1122f, - BodyPart.RIGHT_UPPER_LEG to 0.1122f, - BodyPart.LEFT_LOWER_LEG to 0.0620f, - BodyPart.RIGHT_LOWER_LEG to 0.0620f, -) - // TODO Implement feet accel/velocity sensitivity calculation // TODO check if the first line (with SKATING_DISTANCE_THRESHOLD) is correct fun shouldLock( @@ -72,34 +50,6 @@ fun shouldLock( (position.y - floorLevel <= FLOOR_DISTANCE_THRESHOLD * thresholdMultiplier) && (acceleration.lenSq() <= SKATING_ACCELERATION_THRESHOLD * thresholdMultiplier) -// TODO Use this to calculate feet pressure -fun centerOfMass( - bones: ComputedSkeleton, -): Vector3 = BODY_PART_MASSES.entries.fold(Vector3.ZERO) { acc: Vector3, massEntry -> - val bone = bones[massEntry.key] ?: return@fold acc - val boneCenter = (bone.headPosition + bone.tailPosition) / 2f - return@fold acc + (boneCenter * massEntry.value) -} - -fun computeComState(time: ComparableTimeMark, last: COMState?, com: Vector3): COMState = if (last != null) { - val deltaTime = (time - last.time).inFloatingSeconds - val comVelocity = (com - last.position) / deltaTime - val comAcceleration = (comVelocity - last.velocity) / deltaTime - COMState( - time, - com, - comVelocity, - comAcceleration, - ) -} else { - COMState( - time, - com, - Vector3.ZERO, - Vector3.ZERO, - ) -} - fun computeLockState( wasLocked: Boolean, isLocked: Boolean, @@ -133,10 +83,11 @@ class SkatingCorrectionTargetProcessor(val settings: Settings) : SkeletonTargetP val curTime = timeSource.markNow() // Update centre of mass + // TODO Use this to calculate feet pressure comState = computeComState( curTime, comState, - centerOfMass(fk), + centreOfMass(fk), ) val correctionStrength = skeletonConfig.ratios.skatingCorrectionStrength // TODO