From d58a0c88faefa4ceefc0254b4a5ce4b2c20f5b15 Mon Sep 17 00:00:00 2001 From: Butterscotch! Date: Wed, 28 Oct 2026 21:12:23 -0400 Subject: [PATCH] Fix IK local constraint axes --- .../java/dev/slimevr/skeleton/constraint.kt | 11 +---- .../src/main/java/dev/slimevr/skeleton/ik.kt | 47 +++++++++++-------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/constraint.kt b/server/core/src/main/java/dev/slimevr/skeleton/constraint.kt index fa1ae91c9..eb161866f 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/constraint.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/constraint.kt @@ -92,16 +92,9 @@ data class LooseHingeConstraint( data class AxisConstraint( val axis: Vector3 = Vector3.NEG_X, ) : Constraint { - override fun apply(localRotation: Quaternion): Quaternion = localRotation.project(axis).unit() - - // Optimized to reduce quaternion operations override fun apply( - parentRotation: Quaternion, - childRotation: Quaternion, - ): Quaternion { - val globalAxis = parentRotation.sandwich(axis) - return childRotation.project(globalAxis).unit() - } + localRotation: Quaternion, + ): Quaternion = localRotation.project(axis).unit() } data class LooseAxisConstraint( 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 d4d4dbfd3..1bfdf49dd 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/ik.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/ik.kt @@ -19,6 +19,13 @@ fun chainDistanceFromTarget( return (target - chainTail).len() } +fun parentRot( + bones: ComputedSkeleton, + bodyPart: BodyPart, +) = parentOf(bodyPart)?.let { parent -> + bones[parent]?.rotation +} ?: Quaternion.IDENTITY + // Bones are most likely opposite along the X axis private val oppositeRotation = Quaternion.rotationAroundXAxis(FastMath.PI) private val smallRotation = Quaternion.rotationAroundXAxis(1f * FastMath.DEG_TO_RAD) @@ -47,21 +54,19 @@ fun fromChainToTarget( val offset = Quaternion.fromTo(localChainTail.unit(), localTarget.unit()) - if (localTargetLenSq < localChainTailLenSq && + return if (localTargetLenSq < localChainTailLenSq && FastMath.isApproxZero(offset.angleQ()) ) { - // TODO This is not ideal whatsoever, this should be based on a preferred axis - // and aligned to global space using the bone's parent. + // TODO Should be based off a preferred axis/direction, currently seems to + // "snap" a little when hitting this fallback // If the target is closer than the tail and the bone is parallel to the target, // apply an arbitrary small rotation to promote movement - return smallRotation - } - - return if (FastMath.isApproxEqual(offset.lenSq(), 1f)) { + alignOffset(parentRot(bones, bodyPart).inv(), smallRotation) + } else if (FastMath.isApproxEqual(offset.lenSq(), 1f)) { offset } else { // When the vectors are exactly opposite, arbitrarily choose an axis - oppositeRotation + alignOffset(parentRot(bones, bodyPart).inv(), oppositeRotation) } } @@ -76,6 +81,11 @@ fun computeOffset( rotationWithOffset: Quaternion, ) = rotationWithOffset / rotation +fun alignOffset( + rotation: Quaternion, + offset: Quaternion, +) = rotation.inv() * offset * rotation + fun constrainOffsetWithSkeleton( bodyPart: BodyPart, offset: Quaternion, @@ -83,14 +93,11 @@ fun constrainOffsetWithSkeleton( constraint: Constraint, ): Quaternion { val boneRot = bones[bodyPart]?.rotation ?: return offset - val parentRot = parentOf(bodyPart)?.let { parent -> - bones[parent]?.rotation - } ?: Quaternion.IDENTITY return computeOffset( boneRot, constraint.apply( - parentRot, + parentRot(bones, bodyPart), applyOffset(boneRot, offset), ), ) @@ -125,14 +132,14 @@ fun ccdIkIteration( val bodyPart = chain.first() val offset = fromChainToTarget(bodyPart, mutableComputedSkeleton, chain, target) ?: return - // TODO Constrain the local offset - val localConstrainedOffset = offset -// val localConstrainedOffset = offsetConstraint?.let { -// val parentRot = parentOf(bodyPart)?.let { parent -> -// mutableComputedSkeleton[parent]?.rotation -// } ?: Quaternion.IDENTITY -// it.apply(parentRot, offset) -// } ?: offset + // Constrain the local offset + val localConstrainedOffset = offsetConstraint?.let { + val parentRot = parentRot(mutableComputedSkeleton, bodyPart) + alignOffset( + parentRot.inv(), + it.apply(alignOffset(parentRot, offset)), + ) + } ?: offset // We only need to constrain the bone that we are adjusting val constrainedOffset = constraint?.let {