Finish moving skating correction

This commit is contained in:
Butterscotch! committed 2026-08-29 15:52:01 -04:00
1 parent 4876cfa417
commit 8b0aae537b
6 files changed
+199 -168

No files matched your search

@@ -2,11 +2,34 @@ package dev.slimevr.skeleton.fkprocessors
import dev.slimevr.config.Settings
import dev.slimevr.skeleton.ComputedSkeleton
import dev.slimevr.skeleton.IKTargets
import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.SkeletonFkProcessor
import dev.slimevr.skeleton.SkeletonTargetProcessor
import dev.slimevr.tracker.eulerHeading
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import kotlin.collections.set
// The max height to plant feet by
val ROTATION_CORRECTION_VERTICAL = 0.1f
fun computeFootPlantRatio(
ankleHeight: Float,
floorHeight: Float,
// Normalized distance from ankle to floor within the correction range
): Float = 1f - ((ankleHeight - floorHeight) / ROTATION_CORRECTION_VERTICAL).coerceIn(0f, 1f)
fun correctFootAttitude(
rotation: Quaternion,
correctionRatio: Float,
// eulerHeading is already twinNearest, so we can just use interpQ
): Quaternion = rotation.interpQ(eulerHeading(rotation), correctionRatio)
class FootPlantFkProcessor(val settings: Settings) : SkeletonFkProcessor {
val bodyParts: Array<BodyPart> = arrayOf(BodyPart.LEFT_FOOT, BodyPart.RIGHT_FOOT)
override fun process(inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float): InputSkeleton {
if (!settings.context.state.value.data.skeletonConfig.toggles.footPlant) return inputSkeleton
@@ -4,8 +4,28 @@ import dev.slimevr.config.Settings
import dev.slimevr.skeleton.ComputedSkeleton
import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.SkeletonFkProcessor
import dev.slimevr.tracker.eulerHeading
import io.github.axisangles.ktmath.Quaternion
import solarxr_protocol.datatypes.BodyPart
val TOE_SNAP_RANGE_MULTIPLE = 2f
fun computeToeSnapRatio(
ankleHeight: Float,
footLength: Float,
floorHeight: Float,
// Normalized distance
): Float = 1f - ((ankleHeight - floorHeight - footLength) / (footLength * TOE_SNAP_RANGE_MULTIPLE)).coerceIn(0f, 1f)
fun snapToes(
rotation: Quaternion,
correctionRatio: Float,
// eulerHeading is already twinNearest, so we can just use interpQ
): Quaternion = rotation.interpQ(eulerHeading(rotation), correctionRatio)
class ToeSnapFkProcessor(val settings: Settings) : SkeletonFkProcessor {
val bodyParts: Array<BodyPart> = arrayOf(BodyPart.LEFT_FOOT, BodyPart.RIGHT_FOOT)
override fun process(inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float): InputSkeleton {
if (!settings.context.state.value.data.skeletonConfig.toggles.toeSnap) return inputSkeleton
@@ -1,154 +0,0 @@
package dev.slimevr.skeleton
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import kotlin.time.ComparableTimeMark
import kotlin.time.DurationUnit
// TODO We should probably just have globally available velocity on the whole skeleton,
// we could do that as a processing step
data class VelocityState(
val time: ComparableTimeMark,
val position: Vector3,
val rotation: Quaternion,
val horizontalDistance: Float,
val velocity: Vector3,
val angularVelocityMagnitude: Float,
val acceleration: Vector3,
)
data class COMState(
val time: ComparableTimeMark,
val position: Vector3,
val velocity: Vector3,
val acceleration: Vector3,
)
data class LockState(
val locked: Boolean,
val position: Vector3 = Vector3.NULL,
)
val VELOCITY_BODY_PARTS = arrayOf(BodyPart.LEFT_FOOT, BodyPart.RIGHT_FOOT)
const val FOOT_VELOCITY_SENSITIVITY = 1f
const val FOOT_ACCELERATION_SENSITIVITY = 1f
const val SKATING_LOCK_ENGAGE_PERCENT = 1.1f
const val SKATING_DISTANCE_THRESHOLD = 0.5f
const val SKATING_VELOCITY_THRESHOLD = 2.4f
const val SKATING_ROTATIONAL_VELOCITY_THRESHOLD = 4.5f
const val SKATING_ACCELERATION_THRESHOLD = 0.7f
const val FLOOR_CALIBRATION_OFFSET = 0.0025f
const val FLOOR_DISTANCE_THRESHOLD = 0.065f
// TODO Where are these numbers from?
val BODY_PART_MASSES = mapOf(
BodyPart.HEAD to 0.0827f,
BodyPart.LEFT_UPPER_ARM to 0.0263f,
BodyPart.RIGHT_UPPER_ARM to 0.0263f,
BodyPart.LEFT_LOWER_ARM to 0.0224f,
BodyPart.RIGHT_LOWER_ARM to 0.0224f,
BodyPart.CHEST to 0.1870f,
BodyPart.WAIST to 0.1320f,
BodyPart.HIP to 0.1530f,
BodyPart.LEFT_UPPER_LEG to 0.1122f,
BodyPart.RIGHT_UPPER_LEG to 0.1122f,
BodyPart.LEFT_LOWER_LEG to 0.0620f,
BodyPart.RIGHT_LOWER_LEG to 0.0620f,
)
// TODO Implement feet accel/velocity sensitivity calculation
fun shouldLock(
velocity: VelocityState,
thresholdMultiplier: Float,
floorLevel: Float,
): Boolean = (velocity.horizontalDistance <= SKATING_DISTANCE_THRESHOLD * thresholdMultiplier) &&
(velocity.velocity.len() <= SKATING_VELOCITY_THRESHOLD * thresholdMultiplier) &&
(velocity.angularVelocityMagnitude <= SKATING_ROTATIONAL_VELOCITY_THRESHOLD * thresholdMultiplier) &&
(velocity.position.y - floorLevel <= FLOOR_DISTANCE_THRESHOLD * thresholdMultiplier) &&
(velocity.acceleration.len() <= SKATING_ACCELERATION_THRESHOLD * thresholdMultiplier)
// TODO Use this to calculate feet pressure
fun centerOfMass(
bones: ComputedSkeleton,
): Vector3 = BODY_PART_MASSES.entries.fold(Vector3.NULL) { acc: Vector3, massEntry ->
val bone = bones[massEntry.key] ?: return@fold acc
val boneCenter = (bone.headPosition + bone.tailPosition) / 2f
return@fold acc + (boneCenter * massEntry.value)
}
fun limitedDeltaTime(
from: ComparableTimeMark,
to: ComparableTimeMark,
): Float = (to - from).toDouble(DurationUnit.SECONDS).toFloat()
.coerceAtLeast(0.001f)
fun computeComState(time: ComparableTimeMark, last: COMState?, com: Vector3): COMState = if (last != null) {
val deltaTime = limitedDeltaTime(last.time, time)
val comVelocity = (com - last.position) / deltaTime
val comAcceleration = (comVelocity - last.velocity) / deltaTime
COMState(
time,
com,
comVelocity,
comAcceleration,
)
} else {
COMState(
time,
com,
Vector3.NULL,
Vector3.NULL,
)
}
fun computeVelocityState(
time: ComparableTimeMark,
last: VelocityState?,
bone: BoneState,
): VelocityState = if (last != null) {
val deltaPosition = bone.tailPosition - last.position
val deltaTime = limitedDeltaTime(last.time, time)
VelocityState(
time,
bone.tailPosition,
bone.rotation,
deltaPosition.len(),
deltaPosition / deltaTime,
// May need to be `angleToR` while polarity tracking is not implemented
last.rotation.angleToQ(bone.rotation) / deltaTime,
Vector3.NULL,
)
} else {
VelocityState(
time,
bone.tailPosition,
bone.rotation,
0f,
Vector3.NULL,
0f,
Vector3.NULL,
)
}
fun computeLockState(
wasLocked: Boolean,
isLocked: Boolean,
position: Vector3,
): LockState? = if (isLocked && !wasLocked) {
LockState(
true,
position,
)
} else if (!isLocked && wasLocked) {
// Last locked position could be retained if needed, but I can't think
// of a use
LockState(
false,
)
} else {
null
}
@@ -19,7 +19,7 @@ class FloorClipTargetProcessor(
// Get existing target or make a new one at the current bone position
val target = ikTargets[bodyPart] ?: fk[bodyPart]?.tailPosition ?: continue
// Snap the target up to the floor if it's under
ikTargets[bodyPart] = Vector3(target.x, target.y.coerceAtLeast(0f), target.z)
ikTargets[bodyPart] = Vector3(target.x, target.y.coerceAtLeast(floorLevel), target.z)
}
return ikTargets
}
@@ -2,22 +2,164 @@ package dev.slimevr.skeleton.targetprocessors
import dev.slimevr.config.Settings
import dev.slimevr.skeleton.BodyPartMap
import dev.slimevr.skeleton.COMState
import dev.slimevr.skeleton.BoneState
import dev.slimevr.skeleton.ComputedSkeleton
import dev.slimevr.skeleton.FLOOR_CALIBRATION_OFFSET
import dev.slimevr.skeleton.IKTargets
import dev.slimevr.skeleton.LockState
import dev.slimevr.skeleton.SKATING_LOCK_ENGAGE_PERCENT
import dev.slimevr.skeleton.SkeletonTargetProcessor
import dev.slimevr.skeleton.VELOCITY_BODY_PARTS
import dev.slimevr.skeleton.VelocityState
import dev.slimevr.skeleton.bodyPartMap
import dev.slimevr.skeleton.centerOfMass
import dev.slimevr.skeleton.computeComState
import dev.slimevr.skeleton.computeLockState
import dev.slimevr.skeleton.computeVelocityState
import dev.slimevr.skeleton.shouldLock
import dev.slimevr.util.inFloatingSeconds
import dev.slimevr.util.timeSource
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import kotlin.time.ComparableTimeMark
// TODO We should probably just have globally available velocity on the whole skeleton,
// we could do that as a processing step
data class VelocityState(
val time: ComparableTimeMark,
val position: Vector3,
val rotation: Quaternion,
val horizontalDistance: Float,
val velocity: Vector3,
val angularVelocityMagnitude: Float,
val acceleration: Vector3,
)
data class COMState(
val time: ComparableTimeMark,
val position: Vector3,
val velocity: Vector3,
val acceleration: Vector3,
)
data class LockState(
val locked: Boolean,
val position: Vector3 = Vector3.NULL,
)
val VELOCITY_BODY_PARTS = arrayOf(BodyPart.LEFT_FOOT, BodyPart.RIGHT_FOOT)
const val FOOT_VELOCITY_SENSITIVITY = 1f
const val FOOT_ACCELERATION_SENSITIVITY = 1f
const val SKATING_LOCK_ENGAGE_PERCENT = 1.1f
const val SKATING_DISTANCE_THRESHOLD = 0.5f
const val SKATING_VELOCITY_THRESHOLD = 2.4f
const val SKATING_ROTATIONAL_VELOCITY_THRESHOLD = 4.5f
const val SKATING_ACCELERATION_THRESHOLD = 0.7f
const val FLOOR_CALIBRATION_OFFSET = 0.0025f
const val FLOOR_DISTANCE_THRESHOLD = 0.065f
// TODO Where are these numbers from?
val BODY_PART_MASSES = mapOf(
BodyPart.HEAD to 0.0827f,
BodyPart.LEFT_UPPER_ARM to 0.0263f,
BodyPart.RIGHT_UPPER_ARM to 0.0263f,
BodyPart.LEFT_LOWER_ARM to 0.0224f,
BodyPart.RIGHT_LOWER_ARM to 0.0224f,
BodyPart.CHEST to 0.1870f,
BodyPart.WAIST to 0.1320f,
BodyPart.HIP to 0.1530f,
BodyPart.LEFT_UPPER_LEG to 0.1122f,
BodyPart.RIGHT_UPPER_LEG to 0.1122f,
BodyPart.LEFT_LOWER_LEG to 0.0620f,
BodyPart.RIGHT_LOWER_LEG to 0.0620f,
)
// TODO Implement feet accel/velocity sensitivity calculation
fun shouldLock(
velocity: VelocityState,
thresholdMultiplier: Float,
floorLevel: Float,
): Boolean = (velocity.horizontalDistance <= SKATING_DISTANCE_THRESHOLD * thresholdMultiplier) &&
(velocity.velocity.len() <= SKATING_VELOCITY_THRESHOLD * thresholdMultiplier) &&
(velocity.angularVelocityMagnitude <= SKATING_ROTATIONAL_VELOCITY_THRESHOLD * thresholdMultiplier) &&
(velocity.position.y - floorLevel <= FLOOR_DISTANCE_THRESHOLD * thresholdMultiplier) &&
(velocity.acceleration.len() <= SKATING_ACCELERATION_THRESHOLD * thresholdMultiplier)
// TODO Use this to calculate feet pressure
fun centerOfMass(
bones: ComputedSkeleton,
): Vector3 = BODY_PART_MASSES.entries.fold(Vector3.NULL) { acc: Vector3, massEntry ->
val bone = bones[massEntry.key] ?: return@fold acc
val boneCenter = (bone.headPosition + bone.tailPosition) / 2f
return@fold acc + (boneCenter * massEntry.value)
}
fun limitedDeltaTime(
from: ComparableTimeMark,
to: ComparableTimeMark,
minimum: Float = 0.001f,
): Float = (to - from).inFloatingSeconds.coerceAtLeast(minimum)
fun computeComState(time: ComparableTimeMark, last: COMState?, com: Vector3): COMState = if (last != null) {
val deltaTime = limitedDeltaTime(last.time, time)
val comVelocity = (com - last.position) / deltaTime
val comAcceleration = (comVelocity - last.velocity) / deltaTime
COMState(
time,
com,
comVelocity,
comAcceleration,
)
} else {
COMState(
time,
com,
Vector3.NULL,
Vector3.NULL,
)
}
fun computeVelocityState(
time: ComparableTimeMark,
last: VelocityState?,
bone: BoneState,
): VelocityState = if (last != null) {
val deltaPosition = bone.tailPosition - last.position
val deltaTime = limitedDeltaTime(last.time, time)
VelocityState(
time,
bone.tailPosition,
bone.rotation,
deltaPosition.len(),
deltaPosition / deltaTime,
// May need to be `angleToR` while polarity tracking is not implemented
last.rotation.angleToQ(bone.rotation) / deltaTime,
Vector3.NULL,
)
} else {
VelocityState(
time,
bone.tailPosition,
bone.rotation,
0f,
Vector3.NULL,
0f,
Vector3.NULL,
)
}
fun computeLockState(
wasLocked: Boolean,
isLocked: Boolean,
position: Vector3,
): LockState? = if (isLocked && !wasLocked) {
LockState(
true,
position,
)
} else if (!isLocked && wasLocked) {
// Last locked position could be retained if needed, but I can't think
// of a use
LockState(
false,
)
} else {
null
}
class SkatingCorrectionTargetProcessor(val settings: Settings) : SkeletonTargetProcessor {
// Centre of mass
@@ -68,10 +68,10 @@ fun undoCalibration(
.sandwich(calibratedAcceleration)
// Used to get yaw. Works better for IMU trackers.
private fun eulerHeading(q: Quaternion): Quaternion = Quaternion.rotationAroundYAxis(q.toEulerAngles(EulerOrder.YZX).y).twinNearest(q)
fun eulerHeading(q: Quaternion): Quaternion = Quaternion.rotationAroundYAxis(q.toEulerAngles(EulerOrder.YZX).y).twinNearest(q)
// Used to get yaw. Works better on an HMD.
private fun inverseYProjection(q: Quaternion) = q.project(Vector3.POS_Y).unit().inv()
fun inverseYProjection(q: Quaternion) = q.project(Vector3.POS_Y).unit().inv()
fun estimateHeadingCorrect(
rotation: Quaternion,