From b87b1f3e428160cb5528c6c1cac4e9a12e8afea5 Mon Sep 17 00:00:00 2001 From: Erimel Date: Wed, 11 Nov 2026 18:47:55 -0500 Subject: [PATCH] Improve arm IK and allow leg positional trackers --- .../slimevr/skeleton/bodypart-constraints.kt | 48 ++++++++++++------- .../slimevr/skeleton/bodypart-ik-chains.kt | 13 +---- .../src/main/java/dev/slimevr/skeleton/ik.kt | 13 +++-- .../slimevr/skeleton/inputprocessors/arms.kt | 22 +++++---- .../skeleton/inputprocessors/constraint.kt | 3 +- .../targetprocessors/positional-tracker.kt | 34 ++++++++----- 6 files changed, 78 insertions(+), 55 deletions(-) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt index 99a3f53d8..ee8bcc64a 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt @@ -4,6 +4,7 @@ import com.jme3.math.FastMath import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 import solarxr_protocol.datatypes.BodyPart +import javax.swing.text.StyledEditorKit private val METACARPAL_THUMB_CONSTRAINT = LooseHingeConstraint( -80f * FastMath.DEG_TO_RAD, @@ -191,10 +192,12 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap = BodyPartMap( fun constrainSkeleton( bones: InputSkeleton, constraints: BodyPartMap, + onlyActive: Boolean, ): InputSkeleton { // Apply constraints top-down for ((parentBodyPart, bodyPart) in iterateBodyPartHierarchy()) { val bone = bones[bodyPart] ?: continue + if (onlyActive && !bone.isRotationActive) continue val constraint = constraints[bodyPart] ?: continue val parentRotation = bones[parentBodyPart]?.rotation ?: Quaternion.IDENTITY bones[bodyPart] = bone.copy( @@ -212,15 +215,20 @@ fun constrainSkeleton( */ val BODY_PART_PREFERRED_ROTATION = BodyPartMap( mapOf( - BodyPart.LEFT_UPPER_ARM to Quaternion.rotationAroundXAxis(-30f * FastMath.DEG_TO_RAD), - BodyPart.RIGHT_UPPER_ARM to Quaternion.rotationAroundXAxis(-30f * FastMath.DEG_TO_RAD), - BodyPart.LEFT_LOWER_ARM to Quaternion.rotationAroundXAxis(90f * FastMath.DEG_TO_RAD), - BodyPart.RIGHT_LOWER_ARM to Quaternion.rotationAroundXAxis(90f * FastMath.DEG_TO_RAD), + BodyPart.LEFT_UPPER_ARM to Quaternion.fromRotationVector(-45f * FastMath.DEG_TO_RAD, 90f * FastMath.DEG_TO_RAD, -100f * FastMath.DEG_TO_RAD), + BodyPart.RIGHT_UPPER_ARM to Quaternion.fromRotationVector(-45f * FastMath.DEG_TO_RAD, -90f * FastMath.DEG_TO_RAD, 100f * FastMath.DEG_TO_RAD), + BodyPart.LEFT_LOWER_ARM to Quaternion.fromRotationVector(0f, 70f * FastMath.DEG_TO_RAD, 0f), + BodyPart.RIGHT_LOWER_ARM to Quaternion.fromRotationVector(0f, -70f * FastMath.DEG_TO_RAD, 0f), BodyPart.LEFT_UPPER_LEG to Quaternion.rotationAroundXAxis(115f * FastMath.DEG_TO_RAD), BodyPart.RIGHT_UPPER_LEG to Quaternion.rotationAroundXAxis(115f * FastMath.DEG_TO_RAD), BodyPart.LEFT_LOWER_LEG to Quaternion.rotationAroundXAxis(-48f * FastMath.DEG_TO_RAD), BodyPart.RIGHT_LOWER_LEG to Quaternion.rotationAroundXAxis(-48f * FastMath.DEG_TO_RAD), + +// BodyPart.UPPER_CHEST to Quaternion.rotationAroundXAxis(-10f * FastMath.DEG_TO_RAD), +// BodyPart.LOWER_CHEST to Quaternion.rotationAroundXAxis(-5f * FastMath.DEG_TO_RAD), +// BodyPart.UPPER_WAIST to Quaternion.rotationAroundXAxis(7.5f * FastMath.DEG_TO_RAD), +// BodyPart.LOWER_WAIST to Quaternion.rotationAroundXAxis(7.5f * FastMath.DEG_TO_RAD), ), ) @@ -232,8 +240,8 @@ val BODY_PART_OFFSET_CONSTRAINT_MAP: BodyPartMap = BodyPartMap( // TODO Figure out arms BodyPart.LEFT_UPPER_ARM to TwistSwingConstraint( 5f * FastMath.DEG_TO_RAD, - 30f * FastMath.DEG_TO_RAD, - Vector3.POS_X, + 10f * FastMath.DEG_TO_RAD, + Vector3.POS_Z, ), BodyPart.LEFT_LOWER_ARM to LooseAxisConstraint( 1f * FastMath.DEG_TO_RAD, @@ -241,24 +249,32 @@ val BODY_PART_OFFSET_CONSTRAINT_MAP: BodyPartMap = BodyPartMap( BodyPart.RIGHT_UPPER_ARM to TwistSwingConstraint( 5f * FastMath.DEG_TO_RAD, - 30f * FastMath.DEG_TO_RAD, - Vector3.POS_X, + 10f * FastMath.DEG_TO_RAD, + Vector3.NEG_Z, ), BodyPart.RIGHT_LOWER_ARM to LooseAxisConstraint( 1f * FastMath.DEG_TO_RAD, ), - BodyPart.UPPER_CHEST to LooseAxisConstraint( - 15f * FastMath.DEG_TO_RAD, + BodyPart.UPPER_CHEST to TwistSwingConstraint( + 4f * FastMath.DEG_TO_RAD, + 2f * FastMath.DEG_TO_RAD, + Vector3.POS_X, ), - BodyPart.LOWER_CHEST to LooseAxisConstraint( - 10f * FastMath.DEG_TO_RAD, + BodyPart.LOWER_CHEST to TwistSwingConstraint( + 3f * FastMath.DEG_TO_RAD, + 0.5f * FastMath.DEG_TO_RAD, + Vector3.POS_X, ), - BodyPart.UPPER_WAIST to LooseAxisConstraint( - 10f * FastMath.DEG_TO_RAD, + BodyPart.UPPER_WAIST to TwistSwingConstraint( + 3f * FastMath.DEG_TO_RAD, + 2f * FastMath.DEG_TO_RAD, + Vector3.POS_X, ), - BodyPart.LOWER_WAIST to LooseAxisConstraint( - 10f * FastMath.DEG_TO_RAD, + BodyPart.LOWER_WAIST to TwistSwingConstraint( + 2f * FastMath.DEG_TO_RAD, + 0.5f * FastMath.DEG_TO_RAD, + Vector3.POS_X, ), BodyPart.LEFT_UPPER_LEG to LooseAxisConstraint( diff --git a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-ik-chains.kt b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-ik-chains.kt index 32f0be4e4..70de2aba7 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-ik-chains.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-ik-chains.kt @@ -17,12 +17,11 @@ val BODY_PART_IK_CHAIN_MAP: BodyPartMap = BodyPartMap( BodyPart.RIGHT_UPPER_ARM, BodyPart.RIGHT_LOWER_ARM, ), - BodyPart.HIP to listOf( + BodyPart.LOWER_WAIST to listOf( BodyPart.UPPER_CHEST, BodyPart.LOWER_CHEST, BodyPart.UPPER_WAIST, BodyPart.LOWER_WAIST, - BodyPart.HIP, ), BodyPart.LEFT_LOWER_LEG to listOf( BodyPart.LEFT_UPPER_LEG, @@ -32,15 +31,5 @@ val BODY_PART_IK_CHAIN_MAP: BodyPartMap = BodyPartMap( BodyPart.RIGHT_UPPER_LEG, BodyPart.RIGHT_LOWER_LEG, ), - BodyPart.LEFT_FOOT to listOf( - BodyPart.LEFT_UPPER_LEG, - BodyPart.LEFT_LOWER_LEG, - BodyPart.LEFT_FOOT, - ), - BodyPart.RIGHT_FOOT to listOf( - BodyPart.RIGHT_UPPER_LEG, - BodyPart.RIGHT_LOWER_LEG, - BodyPart.RIGHT_FOOT, - ), ), ) 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 9898f4920..2f8dec0c8 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/ik.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/ik.kt @@ -41,10 +41,15 @@ fun adjustToPreferredRotations( ) { val changedBodyParts = mutableSetOf() for (goal in goals) { - val distanceToTarget = chainDistanceFromTarget(mutableComputedSkeleton, goal.chain, goal.target).toDouble() - val chainLength = chainOffsetsLength(mutableComputedSkeleton, goal.chain) - // TODO what if the target is outwards, out of reach? Shouldn't really happen with legs, but for arms yes - val distanceRatio = (distanceToTarget / chainLength).coerceAtMost(1.0) + val distanceFromTarget = chainDistanceFromTarget(mutableComputedSkeleton, goal.chain, goal.target).toDouble() + val maxChainLength = chainOffsetsLength(mutableComputedSkeleton, goal.chain) + val chainStartToTarget = (goal.target - requireBone(mutableComputedSkeleton, goal.chain.first()).headPosition).len() + val distanceRatio = if (chainStartToTarget < maxChainLength) { + (distanceFromTarget / maxChainLength).coerceAtMost(1.0) + } else { + // Unreachable + continue + } val t = (log(PREFERRED_ROTATION_START_BIAS + (distanceRatio * PREFERRED_ROTATION_START_BIAS_MULTIPLIER), PREFERRED_ROTATION_START_BIAS) - 1).toFloat() for (bodyPart in goal.chain) { val preferredRotation = BODY_PART_PREFERRED_ROTATION[bodyPart] ?: continue diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/arms.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/arms.kt index fd824b630..4b0d65426 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/arms.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/arms.kt @@ -27,26 +27,26 @@ val ARM_TO_SOURCE = arrayOf( ), BodyPart.LEFT_UPPER_ARM to arrayOf( - ArmSource(BodyPart.LEFT_LOWER_ARM, true, 0.5f), - ArmSource(BodyPart.LEFT_SHOULDER, true, 0.5f), - ArmSource(BodyPart.UPPER_CHEST, false, 0.75f), - ArmSource(BodyPart.LEFT_HAND), + ArmSource(BodyPart.LEFT_LOWER_ARM, true), + ArmSource(BodyPart.LEFT_HAND, true), + ArmSource(BodyPart.LEFT_SHOULDER, true, 0.4f), + ArmSource(BodyPart.UPPER_CHEST, false), ), BodyPart.RIGHT_UPPER_ARM to arrayOf( - ArmSource(BodyPart.RIGHT_LOWER_ARM, true, 0.5f), - ArmSource(BodyPart.RIGHT_SHOULDER, true, 0.5f), - ArmSource(BodyPart.UPPER_CHEST, false, 0.75f), - ArmSource(BodyPart.RIGHT_HAND), + ArmSource(BodyPart.RIGHT_LOWER_ARM, true), + ArmSource(BodyPart.RIGHT_HAND, true), + ArmSource(BodyPart.RIGHT_SHOULDER, true, 0.4f), + ArmSource(BodyPart.UPPER_CHEST, false), ), BodyPart.LEFT_LOWER_ARM to arrayOf( - ArmSource(BodyPart.LEFT_HAND, true, 0.8f), + ArmSource(BodyPart.LEFT_HAND, true), ArmSource(BodyPart.LEFT_UPPER_ARM, true), ArmSource(BodyPart.LEFT_SHOULDER, true, 0.2f), ArmSource(BodyPart.UPPER_CHEST, false), ), BodyPart.RIGHT_LOWER_ARM to arrayOf( - ArmSource(BodyPart.RIGHT_HAND, true, 0.8f), + ArmSource(BodyPart.RIGHT_HAND, true), ArmSource(BodyPart.RIGHT_UPPER_ARM, true), ArmSource(BodyPart.RIGHT_SHOULDER, true, 0.2f), ArmSource(BodyPart.UPPER_CHEST, false), @@ -69,6 +69,8 @@ val ARM_TO_SOURCE = arrayOf( /** * Handles imputing missing arm bones from other arm bones and the upper chest. * + * It's useful to set a good starting rotation for arms before IK. + * * This processor must run after the spine processor. */ class ArmsInputProcessor : SkeletonInputProcessor { diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/constraint.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/constraint.kt index 3092e14b3..a09b3fb8c 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/constraint.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/constraint.kt @@ -8,7 +8,6 @@ import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.constrainSkeleton -// TODO don't run constraints for inactive bones, or inactive bone groups. class ConstraintInputProcessor( val settings: Settings, val constraints: BodyPartMap = BODY_PART_CONSTRAINT_MAP, @@ -17,6 +16,6 @@ class ConstraintInputProcessor( val skeletonConfig = settings.context.state.value.data.skeletonConfig if (!skeletonConfig.toggles.enforceConstraints) return - constrainSkeleton(mutableInputSkeleton, constraints) + constrainSkeleton(mutableInputSkeleton, constraints, true) } } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/positional-tracker.kt b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/positional-tracker.kt index 67bf29ad3..79e8bec53 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/positional-tracker.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/positional-tracker.kt @@ -1,6 +1,7 @@ package dev.slimevr.skeleton.targetprocessors import dev.slimevr.config.Settings +import dev.slimevr.skeleton.BodyPartMap import dev.slimevr.skeleton.ComputedSkeleton import dev.slimevr.skeleton.IKTargets import dev.slimevr.skeleton.InputSkeleton @@ -12,6 +13,24 @@ import kotlin.collections.set val POSITIONAL_IK_TARGETS = arrayOf( BodyPart.LEFT_HAND to BodyPart.LEFT_LOWER_ARM, BodyPart.RIGHT_HAND to BodyPart.RIGHT_LOWER_ARM, + +// BodyPart.HIP to BodyPart.LOWER_WAIST, + + BodyPart.LEFT_UPPER_LEG to BodyPart.LEFT_UPPER_LEG, + BodyPart.RIGHT_UPPER_LEG to BodyPart.RIGHT_UPPER_LEG, + + BodyPart.LEFT_LOWER_LEG to BodyPart.LEFT_LOWER_LEG, + BodyPart.RIGHT_LOWER_LEG to BodyPart.RIGHT_LOWER_LEG, + + BodyPart.LEFT_FOOT to BodyPart.LEFT_LOWER_LEG, + BodyPart.RIGHT_FOOT to BodyPart.RIGHT_LOWER_LEG, +) + +val requiredInactive = BodyPartMap( + mapOf( + BodyPart.LEFT_HAND to arrayOf(BodyPart.LEFT_SHOULDER, BodyPart.LEFT_UPPER_ARM, BodyPart.LEFT_LOWER_ARM), + BodyPart.RIGHT_HAND to arrayOf(BodyPart.RIGHT_SHOULDER, BodyPart.RIGHT_UPPER_ARM, BodyPart.RIGHT_LOWER_ARM), + ), ) // Target IK to positional trackers @@ -19,20 +38,13 @@ class PositionalTargetProcessor( val settings: Settings, val bodyParts: Array> = POSITIONAL_IK_TARGETS, ) : SkeletonTargetProcessor { - val requiredInactive = arrayOf( - BodyPart.LEFT_LOWER_ARM, - BodyPart.LEFT_UPPER_ARM, - BodyPart.RIGHT_LOWER_ARM, - BodyPart.RIGHT_UPPER_ARM, - ) - override fun process(mutableIkTargets: IKTargets, inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float) { if (!settings.context.state.value.data.skeletonConfig.toggles.useTrackerPositions) return - // TODO Use active rotations as IK constraints instead and fill in the blanks - // Disable if you have arm trackers - if (requiredInactive.any { inputSkeleton[it]?.isRotationActive == true }) return - for ((bodyPart, target) in bodyParts) { + // TODO Use active rotations as IK constraints instead and fill in the blanks + // Disable if you have arm trackers + if (requiredInactive[bodyPart]?.any { inputSkeleton[it]?.isRotationActive == true } == true) return + mutableIkTargets[target] = inputSkeleton[bodyPart]?.position ?: continue } }