mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-06 08:42:32 -08:00
Improve arm IK and allow leg positional trackers
This commit is contained in:
1 parent
f1578ed3fe
commit
b87b1f3e42
6 files changed
+78
-55
No files matched your search
@@ -4,6 +4,7 @@ import com.jme3.math.FastMath
|
||||
import io.github.axisangles.ktmath.Quaternion
|
||||
import io.github.axisangles.ktmath.Vector3
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
import javax.swing.text.StyledEditorKit
|
||||
|
||||
private val METACARPAL_THUMB_CONSTRAINT = LooseHingeConstraint(
|
||||
-80f * FastMath.DEG_TO_RAD,
|
||||
@@ -191,10 +192,12 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
|
||||
fun constrainSkeleton(
|
||||
bones: InputSkeleton,
|
||||
constraints: BodyPartMap<Constraint>,
|
||||
onlyActive: Boolean,
|
||||
): InputSkeleton {
|
||||
// Apply constraints top-down
|
||||
for ((parentBodyPart, bodyPart) in iterateBodyPartHierarchy()) {
|
||||
val bone = bones[bodyPart] ?: continue
|
||||
if (onlyActive && !bone.isRotationActive) continue
|
||||
val constraint = constraints[bodyPart] ?: continue
|
||||
val parentRotation = bones[parentBodyPart]?.rotation ?: Quaternion.IDENTITY
|
||||
bones[bodyPart] = bone.copy(
|
||||
@@ -212,15 +215,20 @@ fun constrainSkeleton(
|
||||
*/
|
||||
val BODY_PART_PREFERRED_ROTATION = BodyPartMap(
|
||||
mapOf(
|
||||
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_ARM to Quaternion.fromRotationVector(-45f * FastMath.DEG_TO_RAD, 90f * FastMath.DEG_TO_RAD, -100f * FastMath.DEG_TO_RAD),
|
||||
BodyPart.RIGHT_UPPER_ARM to Quaternion.fromRotationVector(-45f * FastMath.DEG_TO_RAD, -90f * FastMath.DEG_TO_RAD, 100f * FastMath.DEG_TO_RAD),
|
||||
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.UPPER_CHEST to Quaternion.rotationAroundXAxis(-10f * FastMath.DEG_TO_RAD),
|
||||
// BodyPart.LOWER_CHEST to Quaternion.rotationAroundXAxis(-5f * FastMath.DEG_TO_RAD),
|
||||
// BodyPart.UPPER_WAIST to Quaternion.rotationAroundXAxis(7.5f * FastMath.DEG_TO_RAD),
|
||||
// BodyPart.LOWER_WAIST to Quaternion.rotationAroundXAxis(7.5f * FastMath.DEG_TO_RAD),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -232,8 +240,8 @@ val BODY_PART_OFFSET_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
|
||||
// TODO Figure out arms
|
||||
BodyPart.LEFT_UPPER_ARM to TwistSwingConstraint(
|
||||
5f * FastMath.DEG_TO_RAD,
|
||||
30f * FastMath.DEG_TO_RAD,
|
||||
Vector3.POS_X,
|
||||
10f * FastMath.DEG_TO_RAD,
|
||||
Vector3.POS_Z,
|
||||
),
|
||||
BodyPart.LEFT_LOWER_ARM to LooseAxisConstraint(
|
||||
1f * FastMath.DEG_TO_RAD,
|
||||
@@ -241,24 +249,32 @@ 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.POS_X,
|
||||
10f * FastMath.DEG_TO_RAD,
|
||||
Vector3.NEG_Z,
|
||||
),
|
||||
BodyPart.RIGHT_LOWER_ARM to LooseAxisConstraint(
|
||||
1f * FastMath.DEG_TO_RAD,
|
||||
),
|
||||
|
||||
BodyPart.UPPER_CHEST to LooseAxisConstraint(
|
||||
15f * FastMath.DEG_TO_RAD,
|
||||
BodyPart.UPPER_CHEST to TwistSwingConstraint(
|
||||
4f * FastMath.DEG_TO_RAD,
|
||||
2f * FastMath.DEG_TO_RAD,
|
||||
Vector3.POS_X,
|
||||
),
|
||||
BodyPart.LOWER_CHEST to LooseAxisConstraint(
|
||||
10f * FastMath.DEG_TO_RAD,
|
||||
BodyPart.LOWER_CHEST to TwistSwingConstraint(
|
||||
3f * FastMath.DEG_TO_RAD,
|
||||
0.5f * FastMath.DEG_TO_RAD,
|
||||
Vector3.POS_X,
|
||||
),
|
||||
BodyPart.UPPER_WAIST to LooseAxisConstraint(
|
||||
10f * FastMath.DEG_TO_RAD,
|
||||
BodyPart.UPPER_WAIST to TwistSwingConstraint(
|
||||
3f * FastMath.DEG_TO_RAD,
|
||||
2f * FastMath.DEG_TO_RAD,
|
||||
Vector3.POS_X,
|
||||
),
|
||||
BodyPart.LOWER_WAIST to LooseAxisConstraint(
|
||||
10f * FastMath.DEG_TO_RAD,
|
||||
BodyPart.LOWER_WAIST to TwistSwingConstraint(
|
||||
2f * FastMath.DEG_TO_RAD,
|
||||
0.5f * FastMath.DEG_TO_RAD,
|
||||
Vector3.POS_X,
|
||||
),
|
||||
|
||||
BodyPart.LEFT_UPPER_LEG to LooseAxisConstraint(
|
||||
|
||||
@@ -17,12 +17,11 @@ val BODY_PART_IK_CHAIN_MAP: BodyPartMap<IKChain> = BodyPartMap(
|
||||
BodyPart.RIGHT_UPPER_ARM,
|
||||
BodyPart.RIGHT_LOWER_ARM,
|
||||
),
|
||||
BodyPart.HIP to listOf(
|
||||
BodyPart.LOWER_WAIST to listOf(
|
||||
BodyPart.UPPER_CHEST,
|
||||
BodyPart.LOWER_CHEST,
|
||||
BodyPart.UPPER_WAIST,
|
||||
BodyPart.LOWER_WAIST,
|
||||
BodyPart.HIP,
|
||||
),
|
||||
BodyPart.LEFT_LOWER_LEG to listOf(
|
||||
BodyPart.LEFT_UPPER_LEG,
|
||||
@@ -32,15 +31,5 @@ val BODY_PART_IK_CHAIN_MAP: BodyPartMap<IKChain> = BodyPartMap(
|
||||
BodyPart.RIGHT_UPPER_LEG,
|
||||
BodyPart.RIGHT_LOWER_LEG,
|
||||
),
|
||||
BodyPart.LEFT_FOOT to listOf(
|
||||
BodyPart.LEFT_UPPER_LEG,
|
||||
BodyPart.LEFT_LOWER_LEG,
|
||||
BodyPart.LEFT_FOOT,
|
||||
),
|
||||
BodyPart.RIGHT_FOOT to listOf(
|
||||
BodyPart.RIGHT_UPPER_LEG,
|
||||
BodyPart.RIGHT_LOWER_LEG,
|
||||
BodyPart.RIGHT_FOOT,
|
||||
),
|
||||
),
|
||||
)
|
||||
@@ -41,10 +41,15 @@ fun adjustToPreferredRotations(
|
||||
) {
|
||||
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 distanceFromTarget = chainDistanceFromTarget(mutableComputedSkeleton, goal.chain, goal.target).toDouble()
|
||||
val maxChainLength = chainOffsetsLength(mutableComputedSkeleton, goal.chain)
|
||||
val chainStartToTarget = (goal.target - requireBone(mutableComputedSkeleton, goal.chain.first()).headPosition).len()
|
||||
val distanceRatio = if (chainStartToTarget < maxChainLength) {
|
||||
(distanceFromTarget / maxChainLength).coerceAtMost(1.0)
|
||||
} else {
|
||||
// Unreachable
|
||||
continue
|
||||
}
|
||||
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
|
||||
|
||||
@@ -27,26 +27,26 @@ val ARM_TO_SOURCE = arrayOf(
|
||||
),
|
||||
|
||||
BodyPart.LEFT_UPPER_ARM to arrayOf(
|
||||
ArmSource(BodyPart.LEFT_LOWER_ARM, true, 0.5f),
|
||||
ArmSource(BodyPart.LEFT_SHOULDER, true, 0.5f),
|
||||
ArmSource(BodyPart.UPPER_CHEST, false, 0.75f),
|
||||
ArmSource(BodyPart.LEFT_HAND),
|
||||
ArmSource(BodyPart.LEFT_LOWER_ARM, true),
|
||||
ArmSource(BodyPart.LEFT_HAND, true),
|
||||
ArmSource(BodyPart.LEFT_SHOULDER, true, 0.4f),
|
||||
ArmSource(BodyPart.UPPER_CHEST, false),
|
||||
),
|
||||
BodyPart.RIGHT_UPPER_ARM to arrayOf(
|
||||
ArmSource(BodyPart.RIGHT_LOWER_ARM, true, 0.5f),
|
||||
ArmSource(BodyPart.RIGHT_SHOULDER, true, 0.5f),
|
||||
ArmSource(BodyPart.UPPER_CHEST, false, 0.75f),
|
||||
ArmSource(BodyPart.RIGHT_HAND),
|
||||
ArmSource(BodyPart.RIGHT_LOWER_ARM, true),
|
||||
ArmSource(BodyPart.RIGHT_HAND, true),
|
||||
ArmSource(BodyPart.RIGHT_SHOULDER, true, 0.4f),
|
||||
ArmSource(BodyPart.UPPER_CHEST, false),
|
||||
),
|
||||
|
||||
BodyPart.LEFT_LOWER_ARM to arrayOf(
|
||||
ArmSource(BodyPart.LEFT_HAND, true, 0.8f),
|
||||
ArmSource(BodyPart.LEFT_HAND, true),
|
||||
ArmSource(BodyPart.LEFT_UPPER_ARM, true),
|
||||
ArmSource(BodyPart.LEFT_SHOULDER, true, 0.2f),
|
||||
ArmSource(BodyPart.UPPER_CHEST, false),
|
||||
),
|
||||
BodyPart.RIGHT_LOWER_ARM to arrayOf(
|
||||
ArmSource(BodyPart.RIGHT_HAND, true, 0.8f),
|
||||
ArmSource(BodyPart.RIGHT_HAND, true),
|
||||
ArmSource(BodyPart.RIGHT_UPPER_ARM, true),
|
||||
ArmSource(BodyPart.RIGHT_SHOULDER, true, 0.2f),
|
||||
ArmSource(BodyPart.UPPER_CHEST, false),
|
||||
@@ -69,6 +69,8 @@ val ARM_TO_SOURCE = arrayOf(
|
||||
/**
|
||||
* Handles imputing missing arm bones from other arm bones and the upper chest.
|
||||
*
|
||||
* It's useful to set a good starting rotation for arms before IK.
|
||||
*
|
||||
* This processor must run after the spine processor.
|
||||
*/
|
||||
class ArmsInputProcessor : SkeletonInputProcessor {
|
||||
|
||||
@@ -8,7 +8,6 @@ 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,
|
||||
@@ -17,6 +16,6 @@ class ConstraintInputProcessor(
|
||||
val skeletonConfig = settings.context.state.value.data.skeletonConfig
|
||||
if (!skeletonConfig.toggles.enforceConstraints) return
|
||||
|
||||
constrainSkeleton(mutableInputSkeleton, constraints)
|
||||
constrainSkeleton(mutableInputSkeleton, constraints, true)
|
||||
}
|
||||
}
|
||||
+23
-11
@@ -1,6 +1,7 @@
|
||||
package dev.slimevr.skeleton.targetprocessors
|
||||
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.skeleton.BodyPartMap
|
||||
import dev.slimevr.skeleton.ComputedSkeleton
|
||||
import dev.slimevr.skeleton.IKTargets
|
||||
import dev.slimevr.skeleton.InputSkeleton
|
||||
@@ -12,6 +13,24 @@ import kotlin.collections.set
|
||||
val POSITIONAL_IK_TARGETS = arrayOf(
|
||||
BodyPart.LEFT_HAND to BodyPart.LEFT_LOWER_ARM,
|
||||
BodyPart.RIGHT_HAND to BodyPart.RIGHT_LOWER_ARM,
|
||||
|
||||
// BodyPart.HIP to BodyPart.LOWER_WAIST,
|
||||
|
||||
BodyPart.LEFT_UPPER_LEG to BodyPart.LEFT_UPPER_LEG,
|
||||
BodyPart.RIGHT_UPPER_LEG to BodyPart.RIGHT_UPPER_LEG,
|
||||
|
||||
BodyPart.LEFT_LOWER_LEG to BodyPart.LEFT_LOWER_LEG,
|
||||
BodyPart.RIGHT_LOWER_LEG to BodyPart.RIGHT_LOWER_LEG,
|
||||
|
||||
BodyPart.LEFT_FOOT to BodyPart.LEFT_LOWER_LEG,
|
||||
BodyPart.RIGHT_FOOT to BodyPart.RIGHT_LOWER_LEG,
|
||||
)
|
||||
|
||||
val requiredInactive = BodyPartMap(
|
||||
mapOf(
|
||||
BodyPart.LEFT_HAND to arrayOf(BodyPart.LEFT_SHOULDER, BodyPart.LEFT_UPPER_ARM, BodyPart.LEFT_LOWER_ARM),
|
||||
BodyPart.RIGHT_HAND to arrayOf(BodyPart.RIGHT_SHOULDER, BodyPart.RIGHT_UPPER_ARM, BodyPart.RIGHT_LOWER_ARM),
|
||||
),
|
||||
)
|
||||
|
||||
// Target IK to positional trackers
|
||||
@@ -19,20 +38,13 @@ class PositionalTargetProcessor(
|
||||
val settings: Settings,
|
||||
val bodyParts: Array<Pair<BodyPart, BodyPart>> = POSITIONAL_IK_TARGETS,
|
||||
) : SkeletonTargetProcessor {
|
||||
val requiredInactive = arrayOf(
|
||||
BodyPart.LEFT_LOWER_ARM,
|
||||
BodyPart.LEFT_UPPER_ARM,
|
||||
BodyPart.RIGHT_LOWER_ARM,
|
||||
BodyPart.RIGHT_UPPER_ARM,
|
||||
)
|
||||
|
||||
override fun process(mutableIkTargets: IKTargets, inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float) {
|
||||
if (!settings.context.state.value.data.skeletonConfig.toggles.useTrackerPositions) return
|
||||
// TODO Use active rotations as IK constraints instead and fill in the blanks
|
||||
// Disable if you have arm trackers
|
||||
if (requiredInactive.any { inputSkeleton[it]?.isRotationActive == true }) return
|
||||
|
||||
for ((bodyPart, target) in bodyParts) {
|
||||
// TODO Use active rotations as IK constraints instead and fill in the blanks
|
||||
// Disable if you have arm trackers
|
||||
if (requiredInactive[bodyPart]?.any { inputSkeleton[it]?.isRotationActive == true } == true) return
|
||||
|
||||
mutableIkTargets[target] = inputSkeleton[bodyPart]?.position ?: continue
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user