From 4e5f0736d97475925c559cea9945130299a258fe Mon Sep 17 00:00:00 2001 From: Erimel Date: Mon, 16 Nov 2026 20:07:50 -0500 Subject: [PATCH] Constrain preferred rotations --- .../dev/slimevr/skeleton/bodypart-constraints.kt | 12 ++++++------ .../core/src/main/java/dev/slimevr/skeleton/ik.kt | 13 ++++++++++--- 2 files changed, 16 insertions(+), 9 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 c2a762e67..8a0a118bf 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 @@ -113,7 +113,7 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap = BodyPartMap( 90f * FastMath.DEG_TO_RAD, ), BodyPart.LEFT_LOWER_LEG to LooseHingeConstraint( - -150f * FastMath.DEG_TO_RAD, + -165f * FastMath.DEG_TO_RAD, 6f * FastMath.DEG_TO_RAD, 65f * FastMath.DEG_TO_RAD, ), @@ -130,7 +130,7 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap = BodyPartMap( 90f * FastMath.DEG_TO_RAD, ), BodyPart.RIGHT_LOWER_LEG to LooseHingeConstraint( - -150f * FastMath.DEG_TO_RAD, + -165f * FastMath.DEG_TO_RAD, 6f * FastMath.DEG_TO_RAD, 65f * FastMath.DEG_TO_RAD, ), @@ -219,10 +219,10 @@ val BODY_PART_PREFERRED_ROTATION = BodyPartMap( 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.LEFT_UPPER_LEG to Quaternion.rotationAroundXAxis(35f * FastMath.DEG_TO_RAD), + BodyPart.RIGHT_UPPER_LEG to Quaternion.rotationAroundXAxis(35f * FastMath.DEG_TO_RAD), + BodyPart.LEFT_LOWER_LEG to Quaternion.rotationAroundXAxis(-20f * FastMath.DEG_TO_RAD), + BodyPart.RIGHT_LOWER_LEG to Quaternion.rotationAroundXAxis(-20f * 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), 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 3902d8515..82f3158ce 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/ik.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/ik.kt @@ -32,12 +32,13 @@ fun parentRot( bones[parent]?.rotation } ?: Quaternion.IDENTITY -const val PREFERRED_ROTATION_START_BIAS = 25.0 +const val PREFERRED_ROTATION_START_BIAS = 62.0 const val PREFERRED_ROTATION_START_BIAS_MULTIPLIER = (PREFERRED_ROTATION_START_BIAS * PREFERRED_ROTATION_START_BIAS) - PREFERRED_ROTATION_START_BIAS fun adjustToPreferredRotations( mutableBoneInputs: InputSkeleton, mutableComputedSkeleton: ComputedSkeleton, goals: List, + constraints: BodyPartMap?, ) { val changedBodyParts = mutableSetOf() for (goal in goals) { @@ -48,7 +49,13 @@ fun adjustToPreferredRotations( for (bodyPart in goal.chain) { val preferredRotation = BODY_PART_PREFERRED_ROTATION[bodyPart] ?: continue val bone = mutableBoneInputs[bodyPart] ?: continue - mutableBoneInputs[bodyPart] = bone.copy(rotation = bone.rotation * preferredRotation.pow(t)) + val offsetRotation = bone.rotation * preferredRotation.pow(t) + val newRotation = constraints?.get(bodyPart)?.apply( + parentRot(mutableComputedSkeleton, bodyPart), + offsetRotation, + ) ?: offsetRotation + + mutableBoneInputs[bodyPart] = bone.copy(rotation = newRotation) changedBodyParts.add(bodyPart) } } @@ -200,7 +207,7 @@ fun ccdIk( if (goals.isEmpty()) return true // Set each bone we're gonna do IK on to its preferred rotation - adjustToPreferredRotations(mutableBoneInputs, mutableComputedSkeleton, goals) + adjustToPreferredRotations(mutableBoneInputs, mutableComputedSkeleton, goals, constraints) // IK iterations var allGoalsMet = true