Improved constraints and reduced feet sinking

This commit is contained in:
Erimel committed 2026-11-10 19:41:48 -05:00
1 parent 1d8a441d01
commit 7c5d3219c3
8 files changed
+183 -125

No files matched your search

@@ -120,7 +120,7 @@ data class SkeletonRatiosConfig(
val predictionStrength: Float = 0.30f,
val spineCurvature: Float = 0.75f,
val imputeSpineFromUpperToLower: Float = DEFAULT_SPINE_UPPER_LOWER,
val interpolateUpperLegsTwistWithLowerLegs: Float = 0.85f,
val interpolateUpperLegsTwistWithLowerLegs: Float = 0.8f,
val skatingCorrectionStrength: Float = 0.5f,
)
@@ -5,17 +5,39 @@ import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
private val FINGER_CONSTRAINT = LooseAxisConstraint(
25f * FastMath.DEG_TO_RAD,
private val METACARPAL_THUMB_CONSTRAINT = LooseHingeConstraint(
-80f * FastMath.DEG_TO_RAD,
100f * FastMath.DEG_TO_RAD,
55f * FastMath.DEG_TO_RAD,
Vector3.NEG_X,
)
private val THUMB_CONSTRAINT = LooseHingeConstraint(
-5f * FastMath.DEG_TO_RAD,
100f * FastMath.DEG_TO_RAD,
5f * FastMath.DEG_TO_RAD,
Vector3.NEG_X,
)
private val LEFT_PROXIMAL_FINGER_CONSTRAINT = LooseHingeConstraint(
-65f * FastMath.DEG_TO_RAD,
110f * FastMath.DEG_TO_RAD,
30f * FastMath.DEG_TO_RAD,
Vector3.POS_Z,
)
private val THUMB_CONSTRAINT = LooseAxisConstraint(
35f * FastMath.DEG_TO_RAD,
Vector3.POS_X,
private val LEFT_FINGER_CONSTRAINT = LooseHingeConstraint(
-5f * FastMath.DEG_TO_RAD,
110f * FastMath.DEG_TO_RAD,
0f,
Vector3.POS_Z,
)
private val TOE_CONSTRAINT = LooseAxisConstraint(
25f * FastMath.DEG_TO_RAD,
Vector3.POS_X,
private val RIGHT_PROXIMAL_FINGER_CONSTRAINT = LEFT_PROXIMAL_FINGER_CONSTRAINT.copy(hingeAxis = Vector3.NEG_Z)
private val RIGHT_FINGER_CONSTRAINT = LEFT_FINGER_CONSTRAINT.copy(hingeAxis = Vector3.NEG_Z)
private val TOE_CONSTRAINT = LooseHingeConstraint(
-70f * FastMath.DEG_TO_RAD,
90f * FastMath.DEG_TO_RAD,
30f * FastMath.DEG_TO_RAD,
Vector3.NEG_X,
)
val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
@@ -23,15 +45,18 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
// Left arm
BodyPart.LEFT_SHOULDER to TwistSwingConstraint(
105f * FastMath.DEG_TO_RAD,
50f * FastMath.DEG_TO_RAD,
70f * FastMath.DEG_TO_RAD,
),
BodyPart.LEFT_UPPER_ARM to TwistConstraint(
45f * FastMath.DEG_TO_RAD,
BodyPart.LEFT_UPPER_ARM to FreeHingeConstraint(
-30f * FastMath.DEG_TO_RAD,
130f * FastMath.DEG_TO_RAD,
Vector3.POS_Y,
),
BodyPart.LEFT_LOWER_ARM to LooseHingeConstraint(
-170f * FastMath.DEG_TO_RAD,
-160f * FastMath.DEG_TO_RAD,
0f * FastMath.DEG_TO_RAD,
55f * FastMath.DEG_TO_RAD,
Vector3.NEG_Z,
),
BodyPart.LEFT_HAND to TwistSwingConstraint(
70f * FastMath.DEG_TO_RAD,
@@ -41,15 +66,18 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
// Right arm
BodyPart.RIGHT_SHOULDER to TwistSwingConstraint(
105f * FastMath.DEG_TO_RAD,
50f * FastMath.DEG_TO_RAD,
70f * FastMath.DEG_TO_RAD,
),
BodyPart.RIGHT_UPPER_ARM to TwistConstraint(
45f * FastMath.DEG_TO_RAD,
BodyPart.RIGHT_UPPER_ARM to FreeHingeConstraint(
-30f * FastMath.DEG_TO_RAD,
130f * FastMath.DEG_TO_RAD,
Vector3.NEG_Y,
),
BodyPart.RIGHT_LOWER_ARM to LooseHingeConstraint(
-170f * FastMath.DEG_TO_RAD,
-160f * FastMath.DEG_TO_RAD,
0f * FastMath.DEG_TO_RAD,
55f * FastMath.DEG_TO_RAD,
Vector3.POS_Z,
),
BodyPart.RIGHT_HAND to TwistSwingConstraint(
70f * FastMath.DEG_TO_RAD,
@@ -59,92 +87,92 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
// Spine
BodyPart.UPPER_CHEST to TwistSwingConstraint(
95f * FastMath.DEG_TO_RAD,
115f * FastMath.DEG_TO_RAD,
105f * FastMath.DEG_TO_RAD,
),
BodyPart.LOWER_CHEST to TwistSwingConstraint(
30f * FastMath.DEG_TO_RAD,
40f * FastMath.DEG_TO_RAD,
90f * FastMath.DEG_TO_RAD,
),
BodyPart.UPPER_WAIST to TwistSwingConstraint(
40f * FastMath.DEG_TO_RAD,
90f * FastMath.DEG_TO_RAD,
35f * FastMath.DEG_TO_RAD,
60f * FastMath.DEG_TO_RAD,
),
BodyPart.LOWER_WAIST to TwistSwingConstraint(
40f * FastMath.DEG_TO_RAD,
90f * FastMath.DEG_TO_RAD,
35f * FastMath.DEG_TO_RAD,
55f * FastMath.DEG_TO_RAD,
),
BodyPart.HIP to TwistSwingConstraint(
60f * FastMath.DEG_TO_RAD,
100f * FastMath.DEG_TO_RAD,
50f * FastMath.DEG_TO_RAD,
80f * FastMath.DEG_TO_RAD,
),
// Left leg
BodyPart.LEFT_UPPER_LEG to LooseHingeConstraint(
-50f * FastMath.DEG_TO_RAD,
170f * FastMath.DEG_TO_RAD,
85f * FastMath.DEG_TO_RAD,
hingeAxis = Vector3.POS_X,
165f * FastMath.DEG_TO_RAD,
90f * FastMath.DEG_TO_RAD,
),
BodyPart.LEFT_LOWER_LEG to LooseHingeConstraint(
-6f * FastMath.DEG_TO_RAD,
170f * FastMath.DEG_TO_RAD,
-150f * FastMath.DEG_TO_RAD,
6f * FastMath.DEG_TO_RAD,
50f * FastMath.DEG_TO_RAD,
),
BodyPart.LEFT_FOOT to TwistSwingConstraint(
35f * FastMath.DEG_TO_RAD,
BodyPart.LEFT_FOOT to LooseHingeConstraint(
-45f * FastMath.DEG_TO_RAD,
20f * FastMath.DEG_TO_RAD,
55f * FastMath.DEG_TO_RAD,
),
// Right leg
BodyPart.RIGHT_UPPER_LEG to LooseHingeConstraint(
-50f * FastMath.DEG_TO_RAD,
170f * FastMath.DEG_TO_RAD,
85f * FastMath.DEG_TO_RAD,
hingeAxis = Vector3.POS_X,
165f * FastMath.DEG_TO_RAD,
90f * FastMath.DEG_TO_RAD,
),
BodyPart.RIGHT_LOWER_LEG to LooseHingeConstraint(
-6f * FastMath.DEG_TO_RAD,
170f * FastMath.DEG_TO_RAD,
-150f * FastMath.DEG_TO_RAD,
6f * FastMath.DEG_TO_RAD,
50f * FastMath.DEG_TO_RAD,
),
BodyPart.RIGHT_FOOT to TwistSwingConstraint(
35f * FastMath.DEG_TO_RAD,
BodyPart.RIGHT_FOOT to LooseHingeConstraint(
-45f * FastMath.DEG_TO_RAD,
20f * FastMath.DEG_TO_RAD,
55f * FastMath.DEG_TO_RAD,
),
// Left fingers
BodyPart.LEFT_THUMB_METACARPAL to THUMB_CONSTRAINT,
BodyPart.LEFT_THUMB_METACARPAL to METACARPAL_THUMB_CONSTRAINT,
BodyPart.LEFT_THUMB_PROXIMAL to THUMB_CONSTRAINT,
BodyPart.LEFT_THUMB_DISTAL to THUMB_CONSTRAINT,
BodyPart.LEFT_INDEX_PROXIMAL to FINGER_CONSTRAINT,
BodyPart.LEFT_INDEX_INTERMEDIATE to FINGER_CONSTRAINT,
BodyPart.LEFT_INDEX_DISTAL to FINGER_CONSTRAINT,
BodyPart.LEFT_MIDDLE_PROXIMAL to FINGER_CONSTRAINT,
BodyPart.LEFT_MIDDLE_INTERMEDIATE to FINGER_CONSTRAINT,
BodyPart.LEFT_MIDDLE_DISTAL to FINGER_CONSTRAINT,
BodyPart.LEFT_RING_PROXIMAL to FINGER_CONSTRAINT,
BodyPart.LEFT_RING_INTERMEDIATE to FINGER_CONSTRAINT,
BodyPart.LEFT_RING_DISTAL to FINGER_CONSTRAINT,
BodyPart.LEFT_LITTLE_PROXIMAL to FINGER_CONSTRAINT,
BodyPart.LEFT_LITTLE_INTERMEDIATE to FINGER_CONSTRAINT,
BodyPart.LEFT_LITTLE_DISTAL to FINGER_CONSTRAINT,
BodyPart.LEFT_INDEX_PROXIMAL to LEFT_PROXIMAL_FINGER_CONSTRAINT,
BodyPart.LEFT_INDEX_INTERMEDIATE to LEFT_FINGER_CONSTRAINT,
BodyPart.LEFT_INDEX_DISTAL to LEFT_FINGER_CONSTRAINT,
BodyPart.LEFT_MIDDLE_PROXIMAL to LEFT_PROXIMAL_FINGER_CONSTRAINT,
BodyPart.LEFT_MIDDLE_INTERMEDIATE to LEFT_FINGER_CONSTRAINT,
BodyPart.LEFT_MIDDLE_DISTAL to LEFT_FINGER_CONSTRAINT,
BodyPart.LEFT_RING_PROXIMAL to LEFT_PROXIMAL_FINGER_CONSTRAINT,
BodyPart.LEFT_RING_INTERMEDIATE to LEFT_FINGER_CONSTRAINT,
BodyPart.LEFT_RING_DISTAL to LEFT_FINGER_CONSTRAINT,
BodyPart.LEFT_LITTLE_PROXIMAL to LEFT_PROXIMAL_FINGER_CONSTRAINT,
BodyPart.LEFT_LITTLE_INTERMEDIATE to LEFT_FINGER_CONSTRAINT,
BodyPart.LEFT_LITTLE_DISTAL to LEFT_FINGER_CONSTRAINT,
// Right fingers
BodyPart.RIGHT_THUMB_METACARPAL to THUMB_CONSTRAINT,
BodyPart.RIGHT_THUMB_METACARPAL to METACARPAL_THUMB_CONSTRAINT,
BodyPart.RIGHT_THUMB_PROXIMAL to THUMB_CONSTRAINT,
BodyPart.RIGHT_THUMB_DISTAL to THUMB_CONSTRAINT,
BodyPart.RIGHT_INDEX_PROXIMAL to FINGER_CONSTRAINT,
BodyPart.RIGHT_INDEX_INTERMEDIATE to FINGER_CONSTRAINT,
BodyPart.RIGHT_INDEX_DISTAL to FINGER_CONSTRAINT,
BodyPart.RIGHT_MIDDLE_PROXIMAL to FINGER_CONSTRAINT,
BodyPart.RIGHT_MIDDLE_INTERMEDIATE to FINGER_CONSTRAINT,
BodyPart.RIGHT_MIDDLE_DISTAL to FINGER_CONSTRAINT,
BodyPart.RIGHT_RING_PROXIMAL to FINGER_CONSTRAINT,
BodyPart.RIGHT_RING_INTERMEDIATE to FINGER_CONSTRAINT,
BodyPart.RIGHT_RING_DISTAL to FINGER_CONSTRAINT,
BodyPart.RIGHT_LITTLE_PROXIMAL to FINGER_CONSTRAINT,
BodyPart.RIGHT_LITTLE_INTERMEDIATE to FINGER_CONSTRAINT,
BodyPart.RIGHT_LITTLE_DISTAL to FINGER_CONSTRAINT,
BodyPart.RIGHT_INDEX_PROXIMAL to RIGHT_PROXIMAL_FINGER_CONSTRAINT,
BodyPart.RIGHT_INDEX_INTERMEDIATE to RIGHT_FINGER_CONSTRAINT,
BodyPart.RIGHT_INDEX_DISTAL to RIGHT_FINGER_CONSTRAINT,
BodyPart.RIGHT_MIDDLE_PROXIMAL to RIGHT_PROXIMAL_FINGER_CONSTRAINT,
BodyPart.RIGHT_MIDDLE_INTERMEDIATE to RIGHT_FINGER_CONSTRAINT,
BodyPart.RIGHT_MIDDLE_DISTAL to RIGHT_FINGER_CONSTRAINT,
BodyPart.RIGHT_RING_PROXIMAL to RIGHT_PROXIMAL_FINGER_CONSTRAINT,
BodyPart.RIGHT_RING_INTERMEDIATE to RIGHT_FINGER_CONSTRAINT,
BodyPart.RIGHT_RING_DISTAL to RIGHT_FINGER_CONSTRAINT,
BodyPart.RIGHT_LITTLE_PROXIMAL to RIGHT_PROXIMAL_FINGER_CONSTRAINT,
BodyPart.RIGHT_LITTLE_INTERMEDIATE to RIGHT_FINGER_CONSTRAINT,
BodyPart.RIGHT_LITTLE_DISTAL to RIGHT_FINGER_CONSTRAINT,
// Toes
BodyPart.LEFT_BIG_TOE to TOE_CONSTRAINT,
@@ -180,19 +208,19 @@ fun constrainSkeleton(
}
/**
* Used for the initial rotation before doing IK.
* Used for the initial rotation before doing IK to promote movement and preferred axis.
*/
val BODY_PART_PREFERRED_ROTATION = BodyPartMap(
mapOf(
BodyPart.LEFT_UPPER_LEG to Quaternion.rotationAroundXAxis(90f * FastMath.DEG_TO_RAD),
BodyPart.RIGHT_UPPER_LEG to Quaternion.rotationAroundXAxis(90f * FastMath.DEG_TO_RAD),
BodyPart.LEFT_LOWER_LEG to Quaternion.rotationAroundXAxis(-45f * FastMath.DEG_TO_RAD),
BodyPart.RIGHT_LOWER_LEG to Quaternion.rotationAroundXAxis(-45f * FastMath.DEG_TO_RAD),
// TODO arms and others
// BodyPart.LEFT_UPPER_ARM to Quaternion.rotationAroundXAxis(-45f * FastMath.DEG_TO_RAD),
// BodyPart.RIGHT_UPPER_ARM to Quaternion.rotationAroundXAxis(-45f * 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.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_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),
),
)
@@ -205,7 +233,7 @@ val BODY_PART_OFFSET_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
BodyPart.LEFT_UPPER_ARM to TwistSwingConstraint(
5f * FastMath.DEG_TO_RAD,
30f * FastMath.DEG_TO_RAD,
Vector3.NEG_X,
Vector3.POS_X,
),
BodyPart.LEFT_LOWER_ARM to LooseAxisConstraint(
1f * FastMath.DEG_TO_RAD,
@@ -214,7 +242,7 @@ val BODY_PART_OFFSET_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
BodyPart.RIGHT_UPPER_ARM to TwistSwingConstraint(
5f * FastMath.DEG_TO_RAD,
30f * FastMath.DEG_TO_RAD,
Vector3.NEG_X,
Vector3.POS_X,
),
BodyPart.RIGHT_LOWER_ARM to LooseAxisConstraint(
1f * FastMath.DEG_TO_RAD,
@@ -99,7 +99,7 @@ inline fun BodyPart.findFirstParent(predicate: (BodyPart) -> Boolean): BodyPart?
}
val headPartSet = setOf(BodyPart.HEAD)
fun highestBodyParts(bodyParts: Set<BodyPart>): Set<BodyPart> {
fun highestBodyParts(bodyParts: Collection<BodyPart>): Set<BodyPart> {
if (BodyPart.HEAD in bodyParts) return headPartSet
// Construct boolean array once
@@ -1,9 +1,11 @@
package dev.slimevr.skeleton
import com.jme3.math.FastMath
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import kotlin.math.abs
import kotlin.math.min
import kotlin.math.cos
import kotlin.math.floor
import kotlin.math.sin
import kotlin.math.sqrt
import kotlin.math.withSign
@@ -41,7 +43,7 @@ data class TwistSwingConstraint(
* Swing range in radians.
*/
val swing: Float,
val twistAxis: Vector3 = Vector3.NEG_Y,
val twistAxis: Vector3 = Vector3.POS_Y,
) : Constraint {
override fun apply(localRotation: Quaternion): Quaternion {
var (swingQ, twistQ) = decomposeToSwingTwist(localRotation, twistAxis)
@@ -56,7 +58,7 @@ data class TwistConstraint(
* Twist range in radians.
*/
val twist: Float = 0f,
val twistAxis: Vector3 = Vector3.NEG_Y,
val twistAxis: Vector3 = Vector3.POS_Y,
) : Constraint {
override fun apply(localRotation: Quaternion): Quaternion {
var (swingQ, twistQ) = decomposeToSwingTwist(localRotation, twistAxis)
@@ -74,7 +76,7 @@ data class HingeConstraint(
* Maximum rotation in radians.
*/
val max: Float,
val hingeAxis: Vector3 = Vector3.NEG_X,
val hingeAxis: Vector3 = Vector3.POS_X,
) : Constraint {
override fun apply(localRotation: Quaternion): Quaternion {
val (_, hingeAxisRot) = decomposeToSwingTwist(localRotation, hingeAxis)
@@ -95,7 +97,7 @@ data class LooseHingeConstraint(
* Maximum non-hinge axis rotation in radians.
*/
val allowedDeviation: Float,
val hingeAxis: Vector3 = Vector3.NEG_X,
val hingeAxis: Vector3 = Vector3.POS_X,
) : Constraint {
override fun apply(localRotation: Quaternion): Quaternion {
val (nonHingeRot, hingeAxisRot) = decomposeToSwingTwist(localRotation, hingeAxis)
@@ -104,8 +106,25 @@ data class LooseHingeConstraint(
}
}
data class FreeHingeConstraint(
/**
* Minimum rotation in radians.
*/
val min: Float = 0f,
/**
* Maximum rotation in radians.
*/
val max: Float,
val hingeAxis: Vector3 = Vector3.POS_X,
) : Constraint {
override fun apply(localRotation: Quaternion): Quaternion {
val (nonHingeRot, hingeAxisRot) = decomposeToSwingTwist(localRotation, hingeAxis)
return nonHingeRot * constrainOnAxis(hingeAxisRot, min, max, hingeAxis)
}
}
data class AxisConstraint(
val axis: Vector3 = Vector3.NEG_X,
val axis: Vector3 = Vector3.POS_X,
) : Constraint {
override fun apply(
localRotation: Quaternion,
@@ -117,7 +136,7 @@ data class LooseAxisConstraint(
* Maximum non-axis rotation in radians.
*/
val allowedDeviation: Float,
val axis: Vector3 = Vector3.NEG_X,
val axis: Vector3 = Vector3.POS_X,
) : Constraint {
override fun apply(localRotation: Quaternion): Quaternion {
val (nonAxisRot, axisRot) = decomposeToSwingTwist(localRotation, axis)
@@ -158,36 +177,27 @@ fun constrainAngle(rotation: Quaternion, angle: Float): Quaternion {
}
}
// Wraps an angle to -pi to pi
private fun wrapAngle(angle: Float) = angle - FastMath.TWO_PI * floor((angle + FastMath.PI) / FastMath.TWO_PI)
fun constrainOnAxis(
rotation: Quaternion,
minAngle: Float,
maxAngle: Float,
axis: Vector3,
): Quaternion {
val magnitudeMin = sin(minAngle * 0.5f)
val magnitudeMax = sin(maxAngle * 0.5f)
val magnitudeSqrMin = (magnitudeMin * magnitudeMin).withSign(minAngle)
val magnitudeSqrMax = (magnitudeMax * magnitudeMax).withSign(maxAngle)
val vector = rotation.xyz
val rotMagnitudeSqr = vector.lenSq().withSign(vector.dot(axis) * rotation.w)
return if (rotMagnitudeSqr !in magnitudeSqrMin..magnitudeSqrMax) {
val distToMin = min(
abs(rotMagnitudeSqr - magnitudeSqrMin),
abs(rotMagnitudeSqr + magnitudeSqrMin),
)
val distToMax = min(
abs(rotMagnitudeSqr - magnitudeSqrMax),
abs(rotMagnitudeSqr + magnitudeSqrMax),
)
val magnitude = if (distToMin < distToMax) magnitudeMin else magnitudeMax
val magnitudeSqr =
abs(if (distToMin < distToMax) magnitudeSqrMin else magnitudeSqrMax)
val angle = rotation.angleAboutR(axis)
return if (angle !in minAngle..maxAngle) {
val distToMin = abs(wrapAngle(angle - minAngle))
val distToMax = abs(wrapAngle(angle - maxAngle))
val magnitude = if (distToMin < distToMax) minAngle else maxAngle
// Could do Quaternion.fromRotationVector(axis * magnitude) but this is more optimized
// and constraints run several times per frame because of IK.
val halfMagnitude = magnitude * 0.5f
Quaternion(
sqrt(1f - magnitudeSqr),
vector.unit() * -magnitude,
cos(halfMagnitude),
axis * sin(halfMagnitude),
)
} else {
rotation
@@ -5,6 +5,7 @@ import dev.slimevr.util.subListOfLast
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import kotlin.math.log
fun requireBone(bones: ComputedSkeleton, bodyPart: BodyPart) = requireNotNull(bones[bodyPart]) {
"The computed skeleton is missing \"${bodyPart}\" from the IK chain."
@@ -19,6 +20,11 @@ fun chainDistanceFromTarget(
return (target - chainTail).len()
}
fun chainOffsetsLength(
bones: ComputedSkeleton,
chain: IKChain,
): Double = chain.sumOf { requireBone(bones, it).offset.len().toDouble() }
fun parentRot(
bones: ComputedSkeleton,
bodyPart: BodyPart,
@@ -26,6 +32,30 @@ fun parentRot(
bones[parent]?.rotation
} ?: Quaternion.IDENTITY
const val PREFERRED_ROTATION_START_BIAS = 25.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<IKChainGoal>,
) {
val changedBodyParts = mutableSetOf<BodyPart>()
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 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
val bone = mutableBoneInputs[bodyPart] ?: continue
mutableBoneInputs[bodyPart] = bone.copy(rotation = bone.rotation * preferredRotation.pow(t))
changedBodyParts.add(bodyPart)
}
}
if (changedBodyParts.isNotEmpty()) buildBones(mutableComputedSkeleton, mutableBoneInputs, changedBodyParts)
}
// Bones are most likely opposite along the X axis
private val oppositeRotation = Quaternion.rotationAroundXAxis(FastMath.PI)
fun fromChainToTarget(
@@ -146,7 +176,7 @@ fun ccdIkIteration(
// Only build bones for inputs that were changed
// If performance is bad, we can combine buildBones calls for non-overlapping IK chains
buildBones(mutableComputedSkeleton, mutableBoneInputs, chain.toSet())
buildBones(mutableComputedSkeleton, mutableBoneInputs, chain)
}
typealias IKChain = List<BodyPart>
@@ -170,22 +200,10 @@ fun ccdIk(
// No goal leaves every bone as the FK pass built it, so nothing below needs to run
if (goals.isEmpty()) return true
// Set each bone in each chain of each goal to its preferred rotation
val changedBodyParts = mutableSetOf<BodyPart>()
for (goal in goals) {
val distanceToTarget = chainDistanceFromTarget(mutableComputedSkeleton, goal.chain, goal.target)
val chainLength = goal.chain.sumOf { requireBone(mutableComputedSkeleton, it).offset.len().toDouble() }.toFloat()
val t = (distanceToTarget / chainLength).coerceAtMost(1f)
for (bodyPart in goal.chain) {
val bone = mutableBoneInputs[bodyPart] ?: continue
val preferredRotation = BODY_PART_PREFERRED_ROTATION[bodyPart] ?: continue
val rotationChange = preferredRotation.pow(t)
mutableBoneInputs[bodyPart] = bone.copy(rotation = bone.rotation * rotationChange)
changedBodyParts.add(bodyPart)
}
}
if (changedBodyParts.isNotEmpty()) buildBones(mutableComputedSkeleton, mutableBoneInputs, changedBodyParts)
// Set each bone we're gonna do IK on to its preferred rotation
adjustToPreferredRotations(mutableBoneInputs, mutableComputedSkeleton, goals)
// IK iterations
var allGoalsMet = true
for (i in 0 until maxIterations) {
allGoalsMet = true
@@ -8,6 +8,7 @@ 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<Constraint> = BODY_PART_CONSTRAINT_MAP,
@@ -8,6 +8,7 @@ import dev.slimevr.util.side
import io.github.axisangles.ktmath.Quaternion
import solarxr_protocol.datatypes.BodyPart
// TODO only use this for forward/curling. Everything else it should just copy
private const val INTERMEDIATE_FROM_PROXIMAL = 2.12f
private const val DISTAL_FROM_PROXIMAL = 3.03f
@@ -168,7 +168,7 @@ fun buildBone(bone: BoneInput, parentBone: BoneState?, velocity: Velocity = ZERO
/**
* Runs FK from boneInputs, mutating the passed bones.
*/
fun buildBones(mutableBones: BodyPartMap<BoneState>, boneInputs: InputSkeleton, changedParts: Set<BodyPart> = headPartSet) {
fun buildBones(mutableBones: BodyPartMap<BoneState>, boneInputs: InputSkeleton, changedParts: Collection<BodyPart> = headPartSet) {
for (bodyPart in highestBodyParts(changedParts)) {
for ((parentPart, childPart) in iterateBodyPartHierarchy(bodyPart, false)) {
val rawBone = boneInputs[childPart] ?: continue
@@ -279,12 +279,12 @@ class Skeleton(
FallbackInputProcessor(),
SpineInputProcessor(settings),
NeckInputProcessor(),
UpperLegsRollAlignInputProcessor(settings),
DirectLinkInputProcessor(),
ArmsInputProcessor(),
FingersInputProcessor(),
ToesInputProcessor(),
ConstraintInputProcessor(settings),
UpperLegsRollAlignInputProcessor(settings),
),
// Run on the result of FK and persist
fkComputedProcessors = listOf(