Fix and improve toe snap

This commit is contained in:
Erimel committed 2026-10-14 14:48:18 -04:00
1 parent 8f760d7953
commit 4b02f8d81b
4 files changed
+21 -24

No files matched your search

@@ -12,9 +12,9 @@ const val ROTATION_CORRECTION_VERTICAL = 0.1f
fun computeFootPlantRatio(
ankleHeight: Float,
floorHeight: Float,
floorLevel: Float,
): Float {
val ankleHeightAboveFloor = ankleHeight - floorHeight
val ankleHeightAboveFloor = ankleHeight - floorLevel
val ratioOfRangeFromFloor = (ankleHeightAboveFloor / ROTATION_CORRECTION_VERTICAL).coerceIn(0f, 1f)
// Ratio of range *to* floor
return 1f - ratioOfRangeFromFloor
@@ -9,49 +9,46 @@ import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import kotlin.math.abs
import kotlin.math.asin
const val TOE_SNAP_RANGE_MULTIPLE = 2f
const val MAX_TOE_SNAP_ANGLE = -0.8f
const val TOE_SNAP_RANGE_MULTIPLIER = 2f
const val MAX_TOE_SNAP_RATIO = 0.8f
const val ANKLE_DOWN_MIN = 0.65f
const val ANKLE_DOWN_MAX = 0.85f
fun computeToeSnapRatio(
ankleLocalPosition: Vector3,
ankleHeight: Float,
footLength: Float,
floorHeight: Float,
floorLevel: Float,
ankleLocalPosition: Vector3,
): Float {
val ankleHeightAboveFloor = ankleHeight - floorHeight
// Toe height if the foot is pointing directly down to the floor
val ankleHeightAboveFloor = ankleHeight - floorLevel
// Compute the floor to floor ratio. 0 = heel is touching floor, 1 = tip of foot is touching floor
val footToFloorRatio = ankleHeightAboveFloor.coerceIn(0f, footLength) / footLength
// Compute the range over which the toes can snap to the floor
val potentialToeHeightAboveFloor = ankleHeightAboveFloor - footLength
// The range over which the toes snap to the floor
val toeSnapRange = footLength * TOE_SNAP_RANGE_MULTIPLE
val toeSnapRange = footLength * TOE_SNAP_RANGE_MULTIPLIER
val ratioOfRangeFromFloor = (potentialToeHeightAboveFloor / toeSnapRange).coerceIn(0f, 1f)
// Reduce the range back down as we touch the floor
val footAngleToFloor = asin(ankleHeightAboveFloor.coerceIn(0f, footLength) / footLength)
val ratioOfMaxAngleToFloor = abs(footAngleToFloor * MAX_TOE_SNAP_ANGLE).coerceIn(0f, 1f)
// Ratio of range *to* floor
val rangeToFloorRatio = (1f - ratioOfRangeFromFloor) * ratioOfMaxAngleToFloor
val rangeToFloorRatio = (1f - ratioOfRangeFromFloor) * footToFloorRatio
// Lessen the ratio with the ankle pointing forward.
// Lessen the ratio as the ankle doesn't point down
val ankleDownDirection = abs(ankleLocalPosition.unit().y.coerceAtMost(0f))
val ankleDownRatio = FastMath.clampedRemap(ankleDownDirection, ANKLE_DOWN_MIN, ANKLE_DOWN_MAX, 0f, 1f)
return rangeToFloorRatio * ankleDownRatio
}
private val MAX_TOE_SNAP_PITCH = Quaternion.rotationAroundXAxis(-MAX_TOE_SNAP_RATIO)
fun snapToes(
rotation: Quaternion,
correctionRatio: Float,
): Quaternion {
val heading = rotation.eulerHeading()
val maxPitch = Quaternion.rotationAroundXAxis(MAX_TOE_SNAP_ANGLE * correctionRatio)
// Pitch must be applied first
val maxCorrection = heading * maxPitch
return rotation.interpQ(maxCorrection, correctionRatio)
val maxCorrected = rotation.eulerHeading() * MAX_TOE_SNAP_PITCH
return rotation.interpQ(maxCorrected, correctionRatio)
}
class ToeSnapFkProcessor(val skeleton: Skeleton) : SkeletonFkProcessor {
@@ -74,10 +71,10 @@ class ToeSnapFkProcessor(val skeleton: Skeleton) : SkeletonFkProcessor {
rotation = snapToes(
input.rotation,
computeToeSnapRatio(
ankle.localTailPosition,
foot.headPosition.y,
length,
floorLevel,
ankle.localTailPosition,
),
),
)
@@ -117,7 +117,7 @@ val BONE_SPECS: Map<SkeletonBone, BoneSpec> = mapOf(
SkeletonBone.HIPS_WIDTH to BoneSpec(default = 0.26f, min = 0.01f, max = 0.6f, curve = LINEAR_WITH_HEIGHT),
SkeletonBone.UPPER_LEG to BoneSpec(default = 0.4f, min = 0.01f, max = 0.8f, curve = UPPER_LEG_LENGTH_BY_STATURE),
SkeletonBone.LOWER_LEG to BoneSpec(default = 0.5f, min = 0.01f, max = 0.8f, curve = LOWER_LEG_LENGTH_BY_STATURE),
SkeletonBone.FOOT_LENGTH to BoneSpec(default = 0.125f, min = 0.01f, max = 0.35f, curve = FOOT_LENGTH_BY_STATURE),
SkeletonBone.FOOT_LENGTH to BoneSpec(default = 0.115f, min = 0.01f, max = 0.35f, curve = FOOT_LENGTH_BY_STATURE),
SkeletonBone.FOOT_SHIFT to BoneSpec(default = -0.02f, min = -0.2f, max = 0.2f, curve = LINEAR_WITH_HEIGHT),
SkeletonBone.SHOULDERS_DISTANCE to BoneSpec(default = 0.05f, min = 0.01f, max = 0.3f, curve = LINEAR_WITH_HEIGHT),
SkeletonBone.SHOULDERS_WIDTH to BoneSpec(default = 0.35f, min = 0.01f, max = 0.8f, curve = SHOULDERS_WIDTH_BY_STATURE),