Add digit constraints + tune and clean up

This commit is contained in:
Erimel committed 2026-09-17 13:45:02 -04:00
1 parent 737d6f5e2a
commit 3c707268a7
2 files changed
+85 -36

No files matched your search

@@ -4,10 +4,18 @@ import com.jme3.math.FastMath
import io.github.axisangles.ktmath.Quaternion
import solarxr_protocol.datatypes.BodyPart
// For fingers and toes
// TODO: should constrain splay separately from swing.
private val DIGIT_CONSTRAINT = TwistSwingConstraint(
10f * FastMath.DEG_TO_RAD,
115f * FastMath.DEG_TO_RAD,
)
// TODO Actually figure out good values for these constraints, maybe a source would be
// good?
val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
mapOf(
// Left arm
BodyPart.LEFT_SHOULDER to TwistSwingConstraint(
105f * FastMath.DEG_TO_RAD,
45f * FastMath.DEG_TO_RAD,
@@ -26,6 +34,7 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
120f * FastMath.DEG_TO_RAD,
),
// Right arm
BodyPart.RIGHT_SHOULDER to TwistSwingConstraint(
105f * FastMath.DEG_TO_RAD,
45f * FastMath.DEG_TO_RAD,
@@ -44,27 +53,29 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
120f * FastMath.DEG_TO_RAD,
),
// Spine
BodyPart.UPPER_CHEST to TwistSwingConstraint(
90f * FastMath.DEG_TO_RAD,
95f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
),
BodyPart.LOWER_CHEST to TwistSwingConstraint(
60f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
40f * FastMath.DEG_TO_RAD,
110f * FastMath.DEG_TO_RAD,
),
BodyPart.UPPER_WAIST to TwistSwingConstraint(
60f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
40f * FastMath.DEG_TO_RAD,
110f * FastMath.DEG_TO_RAD,
),
BodyPart.LOWER_WAIST to TwistSwingConstraint(
60f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
40f * FastMath.DEG_TO_RAD,
110f * FastMath.DEG_TO_RAD,
),
BodyPart.HIP to TwistSwingConstraint(
60f * FastMath.DEG_TO_RAD,
65f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
),
// Left leg
BodyPart.LEFT_UPPER_LEG to TwistSwingConstraint(
120f * FastMath.DEG_TO_RAD,
170f * FastMath.DEG_TO_RAD,
@@ -80,6 +91,7 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
60f * FastMath.DEG_TO_RAD,
),
// Right leg
BodyPart.RIGHT_UPPER_LEG to TwistSwingConstraint(
120f * FastMath.DEG_TO_RAD,
170f * FastMath.DEG_TO_RAD,
@@ -94,6 +106,54 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
90f * FastMath.DEG_TO_RAD,
60f * FastMath.DEG_TO_RAD,
),
// Left fingers
BodyPart.LEFT_THUMB_METACARPAL to DIGIT_CONSTRAINT,
BodyPart.LEFT_THUMB_PROXIMAL to DIGIT_CONSTRAINT,
BodyPart.LEFT_THUMB_DISTAL to DIGIT_CONSTRAINT,
BodyPart.LEFT_INDEX_PROXIMAL to DIGIT_CONSTRAINT,
BodyPart.LEFT_INDEX_INTERMEDIATE to DIGIT_CONSTRAINT,
BodyPart.LEFT_INDEX_DISTAL to DIGIT_CONSTRAINT,
BodyPart.LEFT_MIDDLE_PROXIMAL to DIGIT_CONSTRAINT,
BodyPart.LEFT_MIDDLE_INTERMEDIATE to DIGIT_CONSTRAINT,
BodyPart.LEFT_MIDDLE_DISTAL to DIGIT_CONSTRAINT,
BodyPart.LEFT_RING_PROXIMAL to DIGIT_CONSTRAINT,
BodyPart.LEFT_RING_INTERMEDIATE to DIGIT_CONSTRAINT,
BodyPart.LEFT_RING_DISTAL to DIGIT_CONSTRAINT,
BodyPart.LEFT_LITTLE_PROXIMAL to DIGIT_CONSTRAINT,
BodyPart.LEFT_LITTLE_INTERMEDIATE to DIGIT_CONSTRAINT,
BodyPart.LEFT_LITTLE_DISTAL to DIGIT_CONSTRAINT,
// Right fingers
BodyPart.RIGHT_THUMB_METACARPAL to DIGIT_CONSTRAINT,
BodyPart.RIGHT_THUMB_PROXIMAL to DIGIT_CONSTRAINT,
BodyPart.RIGHT_THUMB_DISTAL to DIGIT_CONSTRAINT,
BodyPart.RIGHT_INDEX_PROXIMAL to DIGIT_CONSTRAINT,
BodyPart.RIGHT_INDEX_INTERMEDIATE to DIGIT_CONSTRAINT,
BodyPart.RIGHT_INDEX_DISTAL to DIGIT_CONSTRAINT,
BodyPart.RIGHT_MIDDLE_PROXIMAL to DIGIT_CONSTRAINT,
BodyPart.RIGHT_MIDDLE_INTERMEDIATE to DIGIT_CONSTRAINT,
BodyPart.RIGHT_MIDDLE_DISTAL to DIGIT_CONSTRAINT,
BodyPart.RIGHT_RING_PROXIMAL to DIGIT_CONSTRAINT,
BodyPart.RIGHT_RING_INTERMEDIATE to DIGIT_CONSTRAINT,
BodyPart.RIGHT_RING_DISTAL to DIGIT_CONSTRAINT,
BodyPart.RIGHT_LITTLE_PROXIMAL to DIGIT_CONSTRAINT,
BodyPart.RIGHT_LITTLE_INTERMEDIATE to DIGIT_CONSTRAINT,
BodyPart.RIGHT_LITTLE_DISTAL to DIGIT_CONSTRAINT,
// Left toes
BodyPart.LEFT_BIG_TOE to DIGIT_CONSTRAINT,
BodyPart.LEFT_INDEX_TOE to DIGIT_CONSTRAINT,
BodyPart.LEFT_MIDDLE_TOE to DIGIT_CONSTRAINT,
BodyPart.LEFT_RING_TOE to DIGIT_CONSTRAINT,
BodyPart.LEFT_LITTLE_TOE to DIGIT_CONSTRAINT,
// Right toes
BodyPart.RIGHT_BIG_TOE to DIGIT_CONSTRAINT,
BodyPart.RIGHT_INDEX_TOE to DIGIT_CONSTRAINT,
BodyPart.RIGHT_MIDDLE_TOE to DIGIT_CONSTRAINT,
BodyPart.RIGHT_RING_TOE to DIGIT_CONSTRAINT,
BodyPart.RIGHT_LITTLE_TOE to DIGIT_CONSTRAINT,
),
)
@@ -104,7 +164,7 @@ fun constrainOffset(
offset: Quaternion,
): Quaternion {
// TODO: Ensure the quaternion multiplication order is correct here
return constraint.apply(parent, offset * bone) * bone.inv()
return constraint.apply(parent, offset * bone) / bone
}
fun constrainOffsetWithSkeleton(
@@ -32,26 +32,20 @@ interface Constraint {
): Quaternion = applyLocalOperation(parentRotation, childRotation, ::apply)
}
class CompleteConstraint : Constraint {
override fun apply(localRotation: Quaternion): Quaternion = localRotation
}
data class TwistSwingConstraint(
/**
* Twist range in radians.
*/
val twist: Float = 0.0f,
val twist: Float = 0f,
/**
* Swing range in radians.
*/
val swing: Float = 0.0f,
val allowedDeviation: Float = 0f,
val maxDeviationFromTracker: Float = 15f,
val swing: Float = 0f,
) : Constraint {
override fun apply(localRotation: Quaternion): Quaternion {
var (swingQ, twistQ) = decomposeToSwingTwist(localRotation, Vector3.NEG_Y)
swingQ = constrain(swingQ, swing)
twistQ = constrain(twistQ, twist)
swingQ = constrainAngle(swingQ, swing)
twistQ = constrainAngle(twistQ, twist)
return swingQ * twistQ
}
}
@@ -60,11 +54,11 @@ data class HingeConstraint(
/**
* Minimum rotation in radians.
*/
val min: Float = 0.0f,
val min: Float = 0f,
/**
* Maximum rotation in radians.
*/
val max: Float = 0.0f,
val max: Float = 0f,
val maxDeviationFromTracker: Float = 15f,
val hingeAxis: Vector3 = Vector3.NEG_X,
) : Constraint {
@@ -78,18 +72,18 @@ data class LooseHingeConstraint(
/**
* Minimum rotation in radians.
*/
val min: Float = 0.0f,
val min: Float = 0f,
/**
* Maximum rotation in radians.
*/
val max: Float = 0.0f,
val max: Float = 0f,
val allowedDeviation: Float = 0f,
val maxDeviationFromTracker: Float = 15f,
val hingeAxis: Vector3 = Vector3.NEG_X,
) : Constraint {
override fun apply(localRotation: Quaternion): Quaternion {
val (nonHingeRot, hingeAxisRot) = decomposeToSwingTwist(localRotation, hingeAxis)
return constrain(nonHingeRot, allowedDeviation) *
return constrainAngle(nonHingeRot, allowedDeviation) *
constrainOnAxis(hingeAxisRot, min, max, hingeAxis)
}
}
@@ -98,17 +92,12 @@ fun decomposeToSwingTwist(
rotation: Quaternion,
twistAxis: Vector3,
): Pair<Quaternion, Quaternion> {
// TODO simplify this?
val projection = rotation.project(twistAxis).unit()
val twist = Quaternion(
sqrt(1.0f - projection.xyz.lenSq()).withSign(rotation.w),
projection.xyz,
)
val swing = (rotation / twist)
val twist = rotation.project(twistAxis).unit()
val swing = rotation / twist
return Pair(swing, twist)
}
fun constrain(rotation: Quaternion, angle: Float): Quaternion {
fun constrainAngle(rotation: Quaternion, angle: Float): Quaternion {
// Use angle to get the maximum magnitude the vector part of rotation can be
// before it has violated a constraint.
// Multiplying by 0.5 uniquely maps angles 0-180 degrees to 0-1 which works
@@ -123,7 +112,7 @@ fun constrain(rotation: Quaternion, angle: Float): Quaternion {
vector.unit() * magnitude,
)
} else {
rotation.unit()
rotation
}
}
@@ -155,10 +144,10 @@ fun constrainOnAxis(
abs(if (distToMin < distToMax) magnitudeSqrMin else magnitudeSqrMax)
Quaternion(
sqrt(1.0f - magnitudeSqr),
sqrt(1f - magnitudeSqr),
vector.unit() * -magnitude,
).unit()
)
} else {
rotation.unit()
rotation
}
}