IK cleanup, test, docs & quat parent space fix

This commit is contained in:
Butterscotch! committed 2026-10-28 19:53:36 -04:00
1 parent 546101defa
commit c911a29d25
7 files changed
+281 -65

No files matched your search

@@ -1,6 +1,9 @@
package dev.slimevr.skeleton
import com.jme3.math.FastMath
import dev.slimevr.quaternionAssertEquals
import io.github.axisangles.ktmath.EulerOrder
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import kotlin.test.Test
@@ -49,13 +52,95 @@ class IKTest {
target,
)
val ikOut = ccdIk(boneInputs, bones, listOf(goal), BODY_PART_CONSTRAINT_MAP, 0.01f, 100)
assert(ikOut.goalsReached.all { it.value }) {
val boneRots = ikOut.bones.values.joinToString {
val goalsMet = ccdIk(
boneInputs,
bones,
listOf(goal),
BODY_PART_OFFSET_CONSTRAINT_MAP,
BODY_PART_CONSTRAINT_MAP,
0.01f,
100,
)
assert(goalsMet) {
val boneRots = bones.values.joinToString {
"${it.bodyPart}: ${it.rotation.toEulerAngles(EulerOrder.YZX)}"
}
val targetDist = chainDistanceFromTarget(ikOut.bones, goal.chain, goal.target)
val targetDist = chainDistanceFromTarget(bones, goal.chain, goal.target)
"Failed to reach target:\nDistance from target: $targetDist\nBone rotations: $boneRots"
}
}
@Test
fun testRotateChain() {
val boneInputs: InputSkeleton = bodyPartMap()
boneInputs[BodyPart.NECK] = DEFAULT_BONE_INPUT.copy(
bodyPart = BodyPart.NECK,
offset = Vector3.NEG_Y,
rotation = Quaternion.rotationAroundXAxis(FastMath.QUARTER_PI),
)
boneInputs[BodyPart.UPPER_CHEST] = DEFAULT_BONE_INPUT.copy(
bodyPart = BodyPart.UPPER_CHEST,
offset = Vector3.NEG_Y,
rotation = Quaternion.rotationAroundYAxis(FastMath.QUARTER_PI),
)
boneInputs[BodyPart.LOWER_CHEST] = DEFAULT_BONE_INPUT.copy(
bodyPart = BodyPart.LOWER_CHEST,
offset = Vector3.NEG_Y,
rotation = Quaternion.rotationAroundZAxis(FastMath.QUARTER_PI),
)
boneInputs[BodyPart.UPPER_WAIST] = DEFAULT_BONE_INPUT.copy(
bodyPart = BodyPart.UPPER_WAIST,
offset = Vector3.NEG_Y,
rotation = Quaternion.rotationAroundXAxis(FastMath.HALF_PI),
)
boneInputs[BodyPart.LOWER_WAIST] = DEFAULT_BONE_INPUT.copy(
bodyPart = BodyPart.LOWER_WAIST,
offset = Vector3.NEG_Y,
rotation = Quaternion.rotationAroundYAxis(FastMath.HALF_PI),
)
boneInputs[BodyPart.HIP] = DEFAULT_BONE_INPUT.copy(
bodyPart = BodyPart.HIP,
offset = Vector3.NEG_Y,
rotation = Quaternion.rotationAroundZAxis(FastMath.HALF_PI),
)
val rotatedBoneInputs = boneInputs.clone()
val chain = listOf(
BodyPart.NECK,
BodyPart.UPPER_CHEST,
BodyPart.LOWER_CHEST,
BodyPart.UPPER_WAIST,
BodyPart.LOWER_WAIST,
BodyPart.HIP,
)
rotateChain(
rotatedBoneInputs,
chain,
Quaternion.rotationAroundYAxis(FastMath.QUARTER_PI),
)
assert(boneInputs != rotatedBoneInputs) {
"Bones were not rotated"
}
fun getLocalRotation(
bones: InputSkeleton,
bone: BodyPart,
parent: BodyPart,
) = bones[bone]!!.rotation.localAbout(bones[parent]!!.rotation)
for (i in 1 until chain.size) {
val bone = chain[i]
val parent = chain[i - 1]
val originalLocal = getLocalRotation(boneInputs, bone, parent)
val newLocal = getLocalRotation(rotatedBoneInputs, bone, parent)
quaternionAssertEquals(
originalLocal,
newLocal,
message = "Local rotations are not matching after chain rotation",
)
}
}
}