mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-01 06:12:35 -08:00
Skating correction pressure & hand Ik
This commit is contained in:
9 files changed
+405
-59
No files matched your search
@@ -13,7 +13,7 @@ fun slideContributionWeight(ankleA: Vector3, ankleB: Vector3, localBoneTailA: Ve
|
||||
// if +, shorten bone, if -, lengthen bone
|
||||
// sum all contribution weights, divide each bone's weight by the sum, then multiply
|
||||
// by the slide magnitude in order to find the corresponding bone change
|
||||
return localBoneTailAToB.len() * -boneToSlideDirectionRatio
|
||||
return localBoneTailAToB.len() * boneToSlideDirectionRatio
|
||||
}
|
||||
|
||||
// TODO This is a placeholder
|
||||
@@ -48,19 +48,19 @@ fun step(skeletonA: ComputedSkeleton, skeletonB: ComputedSkeleton) {
|
||||
)
|
||||
}.filter { it > 0f }
|
||||
|
||||
// TODO This is a placeholder
|
||||
val slideMag = 1f
|
||||
|
||||
val contributionSum = contributions.sum()
|
||||
val normalizedContributions = contributions.map {
|
||||
it / contributionSum
|
||||
val perBoneSlide = contributions.map {
|
||||
(it * slideMag) / contributionSum
|
||||
}
|
||||
|
||||
// TODO This is a placeholder
|
||||
val config = 1f
|
||||
// TODO This is a placeholder
|
||||
val contribution = 1f
|
||||
val adjustVal = contribution * config
|
||||
|
||||
// TODO Apply config
|
||||
val newConfig = config + adjustVal
|
||||
val newConfigs = perBoneSlide.map {
|
||||
config + it
|
||||
}
|
||||
|
||||
// TODO Test new config to see if it actually reduces error? Or do we just
|
||||
// accept the changes? The contribution scales for each frame and considers
|
||||
|
||||
@@ -287,7 +287,7 @@ class ComputedSkeletonBehaviour(
|
||||
|
||||
// Run IK processors
|
||||
val ikTargets = bodyPartMap<Vector3>()
|
||||
for (processor in targetProcessors) processor.process(ikTargets, fk, targetState.floorLevel)
|
||||
for (processor in targetProcessors) processor.process(ikTargets, boneInputs, fk, targetState.floorLevel)
|
||||
|
||||
// Run IK
|
||||
val ikOutput = ccdIk(
|
||||
|
||||
@@ -9,17 +9,13 @@ val BODY_PART_IK_CHAIN_MAP: BodyPartMap<IKChain> = BodyPartMap(
|
||||
BodyPart.NECK,
|
||||
BodyPart.HEAD,
|
||||
),
|
||||
BodyPart.LEFT_HAND to listOf(
|
||||
BodyPart.LEFT_SHOULDER,
|
||||
BodyPart.LEFT_LOWER_ARM to listOf(
|
||||
BodyPart.LEFT_UPPER_ARM,
|
||||
BodyPart.LEFT_LOWER_ARM,
|
||||
BodyPart.LEFT_HAND,
|
||||
),
|
||||
BodyPart.RIGHT_HAND to listOf(
|
||||
BodyPart.RIGHT_SHOULDER,
|
||||
BodyPart.RIGHT_LOWER_ARM to listOf(
|
||||
BodyPart.RIGHT_UPPER_ARM,
|
||||
BodyPart.RIGHT_LOWER_ARM,
|
||||
BodyPart.RIGHT_HAND,
|
||||
),
|
||||
BodyPart.HIP to listOf(
|
||||
BodyPart.UPPER_CHEST,
|
||||
|
||||
@@ -57,3 +57,169 @@ fun computeComState(time: ComparableTimeMark, last: COMState?, com: Vector3): CO
|
||||
Vector3.ZERO,
|
||||
)
|
||||
}
|
||||
|
||||
// TODO Gravity can probably be defined somewhere more globally or smth?
|
||||
const val GRAVITY_MAGNITUDE = 9.81f
|
||||
val GRAVITY_VECTOR = Vector3(0f, -GRAVITY_MAGNITUDE, 0f)
|
||||
|
||||
const val FLOOR_DISTANCE_CUTOFF = 0.065f
|
||||
val PRESSURE_FALLBACK = 0.1f to 0.1f
|
||||
|
||||
// TODO This feels wrong to be hardcoded, is there a reason we're doing this?
|
||||
val FORCE_VECTOR_TO_PRESSURE: Vector3 = Vector3(0.25f, 1f, 0.25f)
|
||||
|
||||
// In m/s^2
|
||||
val FORCE_ERROR_TOLERANCE_SQR: Float = 4f * 4f
|
||||
|
||||
// Get the pressure prediction for the feet based of the centre of mass (assume mass is
|
||||
// 1). This is assuming that the mass is 1 and the force of gravity is 9.8 m/s^2, this
|
||||
// allows for the force sum to map directly to the acceleration of the centre of mass
|
||||
// since F = ma, and if m is 1, then F = a.
|
||||
fun predictFootPressure(
|
||||
leftFootPosition: Vector3,
|
||||
rightFootPosition: Vector3,
|
||||
centerOfMass: Vector3,
|
||||
centerOfMassAcceleration: Vector3,
|
||||
floorLevel: Float,
|
||||
): Pair<Float, Float> {
|
||||
// From the COM to each foot
|
||||
val leftFootVector: Vector3 = (leftFootPosition - centerOfMass).unit()
|
||||
val rightFootVector: Vector3 = (rightFootPosition - centerOfMass).unit()
|
||||
|
||||
// Ratio of gravity on each foot to support the COM
|
||||
val leftFootMagnitude: Float = GRAVITY_MAGNITUDE * leftFootVector.y
|
||||
val rightFootMagnitude: Float = GRAVITY_MAGNITUDE * rightFootVector.y
|
||||
|
||||
// Get the force vector each foot could apply to the COM
|
||||
val leftFootForce: Vector3 = leftFootVector * (leftFootMagnitude / 2f)
|
||||
val rightFootForce: Vector3 = rightFootVector * (rightFootMagnitude / 2f)
|
||||
|
||||
// Based off the acceleration of the COM, get the force each foot is likely applying
|
||||
// (the expected force sum should be equal to centerOfMassAcceleration since the
|
||||
// mass is 1)
|
||||
val (modifiedLeftFootForce, modifiedRightFootForce) = findForceVectors(
|
||||
leftFootForce,
|
||||
rightFootForce,
|
||||
centerOfMassAcceleration,
|
||||
)
|
||||
|
||||
// See if the force vectors found a reasonable solution, if they did not, we assume
|
||||
// there is another force acting on the COM, and fall back to a low pressure
|
||||
// prediction.
|
||||
if (detectOutsideForces(
|
||||
modifiedLeftFootForce,
|
||||
modifiedRightFootForce,
|
||||
Vector3.ZERO,
|
||||
)
|
||||
) {
|
||||
// Assume user not standing
|
||||
return PRESSURE_FALLBACK
|
||||
}
|
||||
// Assume user standing
|
||||
|
||||
// Set the pressure to the force on each foot times the force to pressure scalar
|
||||
var leftFootPressure = modifiedLeftFootForce.hadamard(FORCE_VECTOR_TO_PRESSURE).len()
|
||||
var rightFootPressure = modifiedRightFootForce.hadamard(FORCE_VECTOR_TO_PRESSURE).len()
|
||||
|
||||
// TODO So we calculate all this, but then if the foot is within the cutoff from the
|
||||
// floor, it just goes to some abstractly enormous value?? Why?
|
||||
// Distance from the ground is a factor in the pressure using the inverse of the
|
||||
// distance to the ground scale the pressure
|
||||
val leftDistance: Float =
|
||||
if (leftFootPosition.y > (floorLevel + FLOOR_DISTANCE_CUTOFF)) {
|
||||
leftFootPosition.y - (floorLevel + FLOOR_DISTANCE_CUTOFF)
|
||||
} else {
|
||||
// Nearly zero
|
||||
0.001f
|
||||
}
|
||||
leftFootPressure *= 1f / leftDistance
|
||||
val rightDistance: Float =
|
||||
if (rightFootPosition.y > (floorLevel + FLOOR_DISTANCE_CUTOFF)) {
|
||||
rightFootPosition.y - (floorLevel + FLOOR_DISTANCE_CUTOFF)
|
||||
} else {
|
||||
// Nearly zero
|
||||
0.001f
|
||||
}
|
||||
rightFootPressure *= 1f / rightDistance
|
||||
|
||||
// Normalize the pressure values
|
||||
val pressureSum = leftFootPressure + rightFootPressure
|
||||
leftFootPressure /= pressureSum
|
||||
rightFootPressure /= pressureSum
|
||||
return leftFootPressure to rightFootPressure
|
||||
}
|
||||
|
||||
// Perform a gradient descent to scale the force vectors to best match the acceleration
|
||||
// of the COM
|
||||
fun findForceVectors(
|
||||
leftFootForceInit: Vector3,
|
||||
rightFootForceInit: Vector3,
|
||||
centerOfMassAcceleration: Vector3,
|
||||
): Pair<Vector3, Vector3> {
|
||||
var leftFootForce: Vector3 = leftFootForceInit
|
||||
var rightFootForce: Vector3 = rightFootForceInit
|
||||
val iterations = 100
|
||||
val stepSize = 0.01f
|
||||
// Set up the temporary variables
|
||||
var tempLeftFootForce1: Vector3
|
||||
var tempLeftFootForce2: Vector3
|
||||
var tempRightFootForce1: Vector3
|
||||
var tempRightFootForce2: Vector3
|
||||
var error: Vector3
|
||||
var error1: Vector3
|
||||
var error2: Vector3
|
||||
var error3: Vector3
|
||||
var error4: Vector3
|
||||
for (i in 0 until iterations) {
|
||||
tempLeftFootForce1 = leftFootForce
|
||||
tempLeftFootForce2 = leftFootForce
|
||||
tempRightFootForce1 = rightFootForce
|
||||
tempRightFootForce2 = rightFootForce
|
||||
|
||||
// Get the error at the current position
|
||||
error = getForceVectorError(leftFootForce, rightFootForce, centerOfMassAcceleration)
|
||||
|
||||
// Add and subtract the error to the force vectors
|
||||
tempLeftFootForce1 *= (1.0f + stepSize)
|
||||
tempLeftFootForce2 *= (1.0f - stepSize)
|
||||
tempRightFootForce1 *= (1.0f + stepSize)
|
||||
tempRightFootForce2 *= (1.0f - stepSize)
|
||||
|
||||
// Get the error at the new position
|
||||
error1 = getForceVectorError(tempLeftFootForce1, rightFootForce, centerOfMassAcceleration)
|
||||
error2 = getForceVectorError(tempLeftFootForce2, rightFootForce, centerOfMassAcceleration)
|
||||
error3 = getForceVectorError(tempRightFootForce1, leftFootForce, centerOfMassAcceleration)
|
||||
error4 = getForceVectorError(tempRightFootForce2, leftFootForce, centerOfMassAcceleration)
|
||||
|
||||
// Set the new force vectors
|
||||
if (error1.len() < error.len()) {
|
||||
leftFootForce = tempLeftFootForce1
|
||||
} else if (error2.len() < error.len()) {
|
||||
leftFootForce = tempLeftFootForce2
|
||||
}
|
||||
if (error3.len() < error.len()) {
|
||||
rightFootForce = tempRightFootForce1
|
||||
} else if (error4.len() < error.len()) {
|
||||
rightFootForce = tempRightFootForce2
|
||||
}
|
||||
}
|
||||
|
||||
return leftFootForce to rightFootForce
|
||||
}
|
||||
|
||||
// Detect any outside forces on the body such as a wall or a chair.
|
||||
// Returns true if there is an outside force.
|
||||
fun detectOutsideForces(
|
||||
f1: Vector3,
|
||||
f2: Vector3,
|
||||
centerOfMassAcceleration: Vector3,
|
||||
): Boolean = getForceVectorError(f1, f2, centerOfMassAcceleration).lenSq() > FORCE_ERROR_TOLERANCE_SQR
|
||||
|
||||
// Simple error function for the force vector gradient descent
|
||||
fun getForceVectorError(
|
||||
f1: Vector3,
|
||||
f2: Vector3,
|
||||
centerOfMassAcceleration: Vector3,
|
||||
// TODO Shouldn't this be `centerOfMassAcceleration + GRAVITY_VECTOR + f1 + f2`?
|
||||
// Gravity is -Y, then feet should be accelerating the mass +Y, cancelling out.
|
||||
): Vector3 = centerOfMassAcceleration - (GRAVITY_VECTOR + f1 + f2)
|
||||
@@ -8,8 +8,8 @@ import dev.slimevr.skeleton.ResettableSkeletonProcessor
|
||||
import dev.slimevr.skeleton.SkeletonFkProcessor
|
||||
import dev.slimevr.skeleton.centreOfMass
|
||||
import dev.slimevr.skeleton.targetprocessors.FLOOR_CALIBRATION_OFFSET
|
||||
import dev.slimevr.skeleton.targetprocessors.SKATING_ACCELERATION_THRESHOLD_SQ
|
||||
import dev.slimevr.skeleton.targetprocessors.SKATING_VELOCITY_THRESHOLD_SQ
|
||||
import dev.slimevr.skeleton.targetprocessors.SKATING_ACCELERATION_THRESHOLD
|
||||
import dev.slimevr.skeleton.targetprocessors.SKATING_LINEAR_VELOCITY_THRESHOLD
|
||||
import dev.slimevr.skeleton.targetprocessors.shouldLock
|
||||
import dev.slimevr.util.MonotonicValueTimeMark
|
||||
import dev.slimevr.util.inFloatingSeconds
|
||||
@@ -111,8 +111,8 @@ object FootLocalizer {
|
||||
* the scaled thresholds for velocity and acceleration
|
||||
*/
|
||||
private fun velocityAccelRatio(bone: BoneState): Float {
|
||||
val velocityPercentage = bone.velocity.linear.lenSq() / SKATING_VELOCITY_THRESHOLD_SQ
|
||||
val accelerationPercentage = bone.acceleration.lenSq() / SKATING_ACCELERATION_THRESHOLD_SQ
|
||||
val velocityPercentage = bone.velocity.linear.len() / SKATING_LINEAR_VELOCITY_THRESHOLD
|
||||
val accelerationPercentage = bone.acceleration.len() / SKATING_ACCELERATION_THRESHOLD
|
||||
return (velocityPercentage + accelerationPercentage) / 2f
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import dev.slimevr.skeleton.inputprocessors.SpineInputProcessor
|
||||
import dev.slimevr.skeleton.inputprocessors.ToeActiveLinkInputProcessor
|
||||
import dev.slimevr.skeleton.inputprocessors.UpperLegsRollAlignInputProcessor
|
||||
import dev.slimevr.skeleton.targetprocessors.FloorClipTargetProcessor
|
||||
import dev.slimevr.skeleton.targetprocessors.PositionalTargetProcessor
|
||||
import dev.slimevr.skeleton.targetprocessors.SkatingCorrectionTargetProcessor
|
||||
import dev.slimevr.util.PreciseWaiter
|
||||
import io.github.axisangles.ktmath.Quaternion
|
||||
@@ -190,7 +191,7 @@ interface SkeletonComputedProcessor {
|
||||
}
|
||||
typealias IKTargets = BodyPartMap<Vector3>
|
||||
interface SkeletonTargetProcessor {
|
||||
fun process(mutableIkTargets: IKTargets, fk: ComputedSkeleton, floorLevel: Float)
|
||||
fun process(mutableIkTargets: IKTargets, inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float)
|
||||
}
|
||||
|
||||
class Skeleton(
|
||||
@@ -237,8 +238,9 @@ class Skeleton(
|
||||
ToeSnapFkProcessor(settings),
|
||||
),
|
||||
targetProcessors = listOf(
|
||||
FloorClipTargetProcessor(settings),
|
||||
PositionalTargetProcessor(settings),
|
||||
SkatingCorrectionTargetProcessor(settings),
|
||||
FloorClipTargetProcessor(settings),
|
||||
),
|
||||
ikComputedProcessors = listOf(
|
||||
VelocityComputedProcessor(),
|
||||
|
||||
@@ -3,6 +3,7 @@ package dev.slimevr.skeleton.targetprocessors
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.skeleton.ComputedSkeleton
|
||||
import dev.slimevr.skeleton.IKTargets
|
||||
import dev.slimevr.skeleton.InputSkeleton
|
||||
import dev.slimevr.skeleton.SkeletonTargetProcessor
|
||||
import io.github.axisangles.ktmath.Vector3
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
@@ -12,7 +13,7 @@ class FloorClipTargetProcessor(
|
||||
val settings: Settings,
|
||||
val bodyParts: Array<BodyPart> = arrayOf(BodyPart.LEFT_LOWER_LEG, BodyPart.RIGHT_LOWER_LEG),
|
||||
) : SkeletonTargetProcessor {
|
||||
override fun process(mutableIkTargets: IKTargets, fk: ComputedSkeleton, floorLevel: Float) {
|
||||
override fun process(mutableIkTargets: IKTargets, inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float) {
|
||||
if (!settings.context.state.value.data.skeletonConfig.toggles.floorClip) return
|
||||
|
||||
for (bodyPart in bodyParts) {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package dev.slimevr.skeleton.targetprocessors
|
||||
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.skeleton.ComputedSkeleton
|
||||
import dev.slimevr.skeleton.IKTargets
|
||||
import dev.slimevr.skeleton.InputSkeleton
|
||||
import dev.slimevr.skeleton.SkeletonTargetProcessor
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
import kotlin.collections.set
|
||||
|
||||
// Target IK to positional trackers
|
||||
class PositionalTargetProcessor(
|
||||
val settings: Settings,
|
||||
val bodyParts: Array<Pair<BodyPart, BodyPart>> = arrayOf(
|
||||
BodyPart.LEFT_HAND to BodyPart.LEFT_LOWER_ARM,
|
||||
BodyPart.RIGHT_HAND to BodyPart.RIGHT_LOWER_ARM,
|
||||
),
|
||||
) : SkeletonTargetProcessor {
|
||||
override fun process(mutableIkTargets: IKTargets, inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float) {
|
||||
if (!settings.context.state.value.data.skeletonConfig.toggles.useTrackerPositions) return
|
||||
for ((bodyPart, target) in bodyParts) {
|
||||
mutableIkTargets[target] = inputSkeleton[bodyPart]?.position ?: continue
|
||||
}
|
||||
}
|
||||
}
|
||||
+192
-36
@@ -5,41 +5,58 @@ import dev.slimevr.skeleton.BodyPartMap
|
||||
import dev.slimevr.skeleton.COMState
|
||||
import dev.slimevr.skeleton.ComputedSkeleton
|
||||
import dev.slimevr.skeleton.IKTargets
|
||||
import dev.slimevr.skeleton.InputSkeleton
|
||||
import dev.slimevr.skeleton.ResettableSkeletonProcessor
|
||||
import dev.slimevr.skeleton.SkeletonTargetProcessor
|
||||
import dev.slimevr.skeleton.Velocity
|
||||
import dev.slimevr.skeleton.bodyPartMap
|
||||
import dev.slimevr.skeleton.centreOfMass
|
||||
import dev.slimevr.skeleton.computeComState
|
||||
import dev.slimevr.skeleton.predictFootPressure
|
||||
import dev.slimevr.util.timeSource
|
||||
import io.github.axisangles.ktmath.Vector3
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
import solarxr_protocol.rpc.ResetType
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.abs
|
||||
|
||||
data class LockState(
|
||||
val locked: Boolean,
|
||||
val position: Vector3 = Vector3.ZERO,
|
||||
)
|
||||
|
||||
val VELOCITY_BODY_PARTS = arrayOf(BodyPart.LEFT_LOWER_LEG, BodyPart.RIGHT_LOWER_LEG)
|
||||
|
||||
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_SQ = 0.25f
|
||||
const val SKATING_LINEAR_VELOCITY_THRESHOLD_SQ = 5.76f
|
||||
const val SKATING_ANGULAR_VELOCITY_THRESHOLD_SQ = 20.25f
|
||||
const val SKATING_VELOCITY_THRESHOLD_SQ = 5.76f
|
||||
const val SKATING_ACCELERATION_THRESHOLD_SQ = 0.49f
|
||||
// TODO These were squared for performance, but I think that requires squaring
|
||||
// everything else, and obscures the actual values. We need a better way of doing so if
|
||||
// we want to still do that. - Butterscotch
|
||||
const val SKATING_DISTANCE_THRESHOLD = 0.5f
|
||||
const val SKATING_ANGULAR_VELOCITY_THRESHOLD = 4.5f
|
||||
const val SKATING_LINEAR_VELOCITY_THRESHOLD = 2.4f
|
||||
const val SKATING_ACCELERATION_THRESHOLD = 0.7f
|
||||
|
||||
const val FLOOR_CALIBRATION_OFFSET = 0.0025f
|
||||
const val FLOOR_DISTANCE_THRESHOLD = 0.065f
|
||||
|
||||
// TODO Implement feet accel/velocity sensitivity calculation
|
||||
// TODO check if the first line (with SKATING_DISTANCE_THRESHOLD) is correct
|
||||
const val PARAM_SCALAR_MAX = 3.2f
|
||||
const val PARAM_SCALAR_MIN = 0.25f
|
||||
const val PARAM_SCALAR_MID = 1.0f
|
||||
|
||||
// The point at which the scalar is at the max or min depending on accel
|
||||
const val MAX_SCALAR_ACCEL = 0.2f
|
||||
const val MIN_SCALAR_ACCEL = 0.9f
|
||||
|
||||
// The point at which the scalar is at it max or min in a double locked foot situation
|
||||
const val MAX_SCALAR_DORMANT = 0.2f
|
||||
const val MIN_SCALAR_DORMANT = 1.50f
|
||||
|
||||
// The point at which the scalar is at it max or min in a single locked foot situation
|
||||
const val MIN_SCALAR_ACTIVE = 1.75f
|
||||
const val MAX_SCALAR_ACTIVE = 0.1f
|
||||
|
||||
// Maximum scalars for the pressure on each foot
|
||||
const val PRESSURE_SCALAR_MIN = 0.1f
|
||||
const val PRESSURE_SCALAR_MAX = 1.9f
|
||||
|
||||
fun shouldLock(
|
||||
position: Vector3,
|
||||
lastPosition: Vector3,
|
||||
@@ -48,14 +65,16 @@ fun shouldLock(
|
||||
wasLocked: Boolean,
|
||||
floorLevel: Float = 0f,
|
||||
correctionStrength: Float = 1f,
|
||||
velocitySensitivity: Float = 1f,
|
||||
accelerationSensitivity: Float = 1f,
|
||||
): Boolean {
|
||||
val thresholdMultiplier = (if (wasLocked) 1f else SKATING_LOCK_ENGAGE_PERCENT) * (correctionStrength * 0.5f + 0.5f)
|
||||
val floorLevel = floorLevel + FLOOR_CALIBRATION_OFFSET
|
||||
return ((position - lastPosition).let { Vector3(it.x, 0f, it.z) }.lenSq() <= SKATING_DISTANCE_THRESHOLD_SQ) &&
|
||||
(velocity.linear.lenSq() <= SKATING_LINEAR_VELOCITY_THRESHOLD_SQ * thresholdMultiplier) &&
|
||||
(velocity.angular.lenSq() <= SKATING_ANGULAR_VELOCITY_THRESHOLD_SQ * thresholdMultiplier) &&
|
||||
(position.y - floorLevel <= FLOOR_DISTANCE_THRESHOLD * thresholdMultiplier) &&
|
||||
(acceleration.lenSq() <= SKATING_ACCELERATION_THRESHOLD_SQ * thresholdMultiplier * correctionStrength)
|
||||
return ((position - lastPosition).let { Vector3(it.x, 0f, it.z) }.len() <= SKATING_DISTANCE_THRESHOLD) &&
|
||||
(velocity.linear.len() <= SKATING_LINEAR_VELOCITY_THRESHOLD * thresholdMultiplier * velocitySensitivity) &&
|
||||
(velocity.angular.len() <= SKATING_ANGULAR_VELOCITY_THRESHOLD * thresholdMultiplier * velocitySensitivity) &&
|
||||
(position.y - floorLevel <= FLOOR_DISTANCE_THRESHOLD) &&
|
||||
(acceleration.len() <= SKATING_ACCELERATION_THRESHOLD * thresholdMultiplier * correctionStrength * accelerationSensitivity)
|
||||
}
|
||||
|
||||
fun computeLockState(
|
||||
@@ -68,8 +87,7 @@ fun computeLockState(
|
||||
position,
|
||||
)
|
||||
} else if (!isLocked && wasLocked) {
|
||||
// Last locked position could be retained if needed, but I can't think
|
||||
// of a use
|
||||
// Last locked position could be retained if needed, but I can't think of a use
|
||||
LockState(
|
||||
false,
|
||||
)
|
||||
@@ -77,46 +95,186 @@ fun computeLockState(
|
||||
null
|
||||
}
|
||||
|
||||
fun computeSensitivity(
|
||||
isLocked: Boolean,
|
||||
altIsLocked: Boolean,
|
||||
accelerationMagnitude: Float,
|
||||
velocity: Vector3,
|
||||
altVelocity: Vector3,
|
||||
pressure: Float,
|
||||
): Pair<Float, Float> {
|
||||
// Get the first set of scalars that are based on acceleration from the IMUs
|
||||
val footScalarAccel: Float = getFootScalarAccel(
|
||||
isLocked,
|
||||
accelerationMagnitude,
|
||||
)
|
||||
|
||||
// Get the second set of scalars that is based off of how close each foot is to a
|
||||
// lock and dynamically adjusting the scalars (based off the assumption that if you
|
||||
// are standing one foot is likely planted on the ground unless you are moving
|
||||
// fast)
|
||||
// TODO Always 3.2?
|
||||
val footScalarVel: Float = getFootLockLikelihood(
|
||||
velocity,
|
||||
altVelocity,
|
||||
isLocked && altIsLocked,
|
||||
)
|
||||
|
||||
// Combine the scalars to get the final scalars
|
||||
val footSensitivityVel = (
|
||||
(
|
||||
footScalarAccel +
|
||||
footScalarVel /
|
||||
2f
|
||||
) *
|
||||
(pressure * 2f).coerceIn(
|
||||
PRESSURE_SCALAR_MIN,
|
||||
PRESSURE_SCALAR_MAX,
|
||||
)
|
||||
)
|
||||
|
||||
// Velocity sensitivity to acceleration sensitivity
|
||||
return footSensitivityVel to footScalarVel
|
||||
}
|
||||
|
||||
// Calculate a scalar using acceleration to apply to the non acceleration based
|
||||
// hyperparameters when calculating lock states
|
||||
fun getFootScalarAccel(
|
||||
isLocked: Boolean,
|
||||
accelerationMagnitude: Float,
|
||||
): Float {
|
||||
if (isLocked) {
|
||||
if (accelerationMagnitude < MAX_SCALAR_ACCEL) {
|
||||
return PARAM_SCALAR_MAX
|
||||
} else if (accelerationMagnitude > MIN_SCALAR_ACCEL) {
|
||||
return (
|
||||
PARAM_SCALAR_MAX
|
||||
*
|
||||
(accelerationMagnitude - MIN_SCALAR_ACCEL) /
|
||||
(MAX_SCALAR_ACCEL - MIN_SCALAR_ACCEL)
|
||||
)
|
||||
}
|
||||
}
|
||||
return PARAM_SCALAR_MID
|
||||
}
|
||||
|
||||
// Calculate a scalar using the velocity of the foot trackers and the lock states to
|
||||
// calculate a scalar to apply to the non acceleration based hyperparameters when
|
||||
// calculating lock states
|
||||
fun getFootLockLikelihood(
|
||||
primaryFootVel: Vector3,
|
||||
otherFootVel: Vector3,
|
||||
bothLocked: Boolean,
|
||||
): Float {
|
||||
if (bothLocked) {
|
||||
var velocityDiff: Vector3 = primaryFootVel - otherFootVel
|
||||
velocityDiff = Vector3(velocityDiff.x, 0f, velocityDiff.z)
|
||||
val velocityDiffMagnitude: Float = velocityDiff.len()
|
||||
if (velocityDiffMagnitude < MAX_SCALAR_DORMANT) {
|
||||
return PARAM_SCALAR_MAX
|
||||
} else if (velocityDiffMagnitude > MIN_SCALAR_DORMANT) {
|
||||
return (
|
||||
PARAM_SCALAR_MAX
|
||||
*
|
||||
(velocityDiffMagnitude - MIN_SCALAR_DORMANT) /
|
||||
(MAX_SCALAR_DORMANT - MIN_SCALAR_DORMANT)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate the 'unlockedness factor' and use that to determine the scalar (go as
|
||||
// low as 0.5 and as high as param_scalar_max)
|
||||
val velocityDiffAbs: Float = abs(primaryFootVel.len() - otherFootVel.len())
|
||||
if (velocityDiffAbs > MIN_SCALAR_ACTIVE) {
|
||||
return PARAM_SCALAR_MIN
|
||||
} else if (velocityDiffAbs < MAX_SCALAR_ACTIVE) {
|
||||
return PARAM_SCALAR_MAX
|
||||
}
|
||||
return (
|
||||
PARAM_SCALAR_MAX
|
||||
*
|
||||
(velocityDiffAbs - MIN_SCALAR_ACTIVE) /
|
||||
(MAX_SCALAR_ACTIVE - MIN_SCALAR_ACTIVE) -
|
||||
PARAM_SCALAR_MID
|
||||
)
|
||||
}
|
||||
|
||||
class SkatingCorrectionTargetProcessor(val settings: Settings) :
|
||||
SkeletonTargetProcessor,
|
||||
ResettableSkeletonProcessor {
|
||||
val skatingBodyParts = arrayOf(
|
||||
BodyPart.LEFT_LOWER_LEG to BodyPart.RIGHT_LOWER_LEG,
|
||||
BodyPart.RIGHT_LOWER_LEG to BodyPart.LEFT_LOWER_LEG,
|
||||
)
|
||||
|
||||
// Centre of mass
|
||||
var comState: COMState? = null
|
||||
|
||||
val lastLockedPositions: BodyPartMap<Vector3> = bodyPartMap()
|
||||
val pressure: BodyPartMap<Float> = bodyPartMap()
|
||||
val lockState: BodyPartMap<LockState> = bodyPartMap()
|
||||
|
||||
override fun process(mutableIkTargets: IKTargets, fk: ComputedSkeleton, floorLevel: Float) {
|
||||
override fun process(mutableIkTargets: IKTargets, inputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float) {
|
||||
val skeletonConfig = settings.context.state.value.data.skeletonConfig
|
||||
if (!skeletonConfig.toggles.skatingCorrection) return
|
||||
|
||||
val curTime = timeSource.markNow()
|
||||
|
||||
// Update centre of mass
|
||||
// TODO Use this to calculate feet pressure
|
||||
comState = computeComState(
|
||||
curTime,
|
||||
val comState = computeComState(
|
||||
timeSource.markNow(),
|
||||
comState,
|
||||
centreOfMass(fk),
|
||||
)
|
||||
this.comState = comState
|
||||
|
||||
val correctionStrength = skeletonConfig.ratios.skatingCorrectionStrength // TODO
|
||||
// TODO Clean up whatever the hell this is
|
||||
// Predict the pressure for each foot
|
||||
val (leftPressure, rightPressure) = predictFootPressure(
|
||||
fk[BodyPart.LEFT_LOWER_LEG]?.tailPosition ?: return,
|
||||
fk[BodyPart.RIGHT_LOWER_LEG]?.tailPosition ?: return,
|
||||
comState.position,
|
||||
comState.acceleration,
|
||||
floorLevel,
|
||||
)
|
||||
pressure[BodyPart.LEFT_LOWER_LEG] = leftPressure
|
||||
pressure[BodyPart.RIGHT_LOWER_LEG] = rightPressure
|
||||
|
||||
for (bodyPart in VELOCITY_BODY_PARTS) {
|
||||
val curBone = fk[bodyPart] ?: continue
|
||||
val curPosition = curBone.tailPosition
|
||||
val correctionStrength = skeletonConfig.ratios.skatingCorrectionStrength
|
||||
|
||||
for ((bodyPart, altBodyPart) in skatingBodyParts) {
|
||||
val locked = lockState[bodyPart]?.locked ?: false
|
||||
val altLocked = lockState[altBodyPart]?.locked ?: false
|
||||
|
||||
val bone = fk[bodyPart] ?: return
|
||||
val altBone = fk[bodyPart] ?: return
|
||||
|
||||
val (velocitySensitivity, accelerationSensitivity) = computeSensitivity(
|
||||
locked,
|
||||
altLocked,
|
||||
bone.acceleration.len(),
|
||||
bone.velocity.linear,
|
||||
altBone.velocity.linear,
|
||||
// TODO Do something better
|
||||
pressure[bodyPart] ?: 0.1f,
|
||||
)
|
||||
|
||||
val curPosition = bone.tailPosition
|
||||
|
||||
// Consider locking BodyPart
|
||||
val lastState = lockState[bodyPart]
|
||||
val wasLocked = lastState?.locked == true
|
||||
val isLocked = shouldLock(
|
||||
curPosition,
|
||||
lastLockedPositions[bodyPart] ?: curPosition,
|
||||
curBone.acceleration,
|
||||
curBone.velocity,
|
||||
if (wasLocked) {
|
||||
lastState.position
|
||||
} else {
|
||||
curPosition
|
||||
},
|
||||
bone.acceleration,
|
||||
bone.velocity,
|
||||
wasLocked,
|
||||
floorLevel,
|
||||
correctionStrength,
|
||||
velocitySensitivity,
|
||||
accelerationSensitivity,
|
||||
)
|
||||
|
||||
val activeState = computeLockState(
|
||||
@@ -131,7 +289,6 @@ class SkatingCorrectionTargetProcessor(val settings: Settings) :
|
||||
|
||||
if (activeState.locked) {
|
||||
mutableIkTargets[bodyPart] = activeState.position
|
||||
lastLockedPositions[bodyPart] = activeState.position
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -139,7 +296,6 @@ class SkatingCorrectionTargetProcessor(val settings: Settings) :
|
||||
override fun reset(resetType: ResetType) {
|
||||
if (resetType == ResetType.FULL) {
|
||||
comState = null
|
||||
lastLockedPositions.clear()
|
||||
lockState.clear()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user