diff --git a/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt b/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt index 5fbaa1eae..b2df48c8c 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt @@ -15,12 +15,12 @@ import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 import solarxr_protocol.rpc.ResetType -private data class VelocityData( +private data class BoneSample( val rotation: Quaternion, val position: Vector3, ) -private fun computeVelocity(currentVelocityData: VelocityData, lastVelocityData: VelocityData, deltaTime: Float): Velocity { +private fun computeVelocity(currentVelocityData: BoneSample, lastVelocityData: BoneSample, deltaTime: Float): Velocity { val deltaPosition = currentVelocityData.position - lastVelocityData.position val deltaRotation = currentVelocityData.rotation / lastVelocityData.rotation return Velocity( @@ -29,7 +29,7 @@ private fun computeVelocity(currentVelocityData: VelocityData, lastVelocityData: ) } -private const val SMOOTHING_MS = 25f +private const val SMOOTHING_MS = 20f private fun smoothVelocity(currentVelocity: Velocity, lastVelocity: Velocity, deltaTime: Float): Velocity { val t = (deltaTime * (millisecondsInSecond / SMOOTHING_MS)).coerceAtMost(1f) return Velocity( @@ -44,7 +44,7 @@ private fun smoothVelocity(currentVelocity: Velocity, lastVelocity: Velocity, de class VelocityComputedProcessor : SkeletonComputedProcessor, ResettableSkeletonProcessor { - private val lastVelocities: BodyPartMap> = bodyPartMap() + private val lastVelocities: BodyPartMap> = bodyPartMap() private var lastProcessTime = timeSource.markNow() override fun process(mutableComputedSkeleton: ComputedSkeleton, inputSkeleton: InputSkeleton) { @@ -55,13 +55,13 @@ class VelocityComputedProcessor : for ((bodyPart, bone) in mutableComputedSkeleton) { // Compute current velocity - val currentVelocityData = VelocityData(bone.rotation, bone.tailPosition) - val currentVelocity = lastVelocities[bodyPart]?.second?.let { computeVelocity(currentVelocityData, it, deltaTime) } ?: ZERO_VELOCITY + val currentBoneSample = BoneSample(bone.rotation, bone.tailPosition) + val currentVelocity = lastVelocities[bodyPart]?.second?.let { computeVelocity(currentBoneSample, it, deltaTime) } ?: ZERO_VELOCITY val smoothedVelocity = smoothVelocity(currentVelocity, lastVelocities[bodyPart]?.first ?: ZERO_VELOCITY, deltaTime) mutableComputedSkeleton[bodyPart] = bone.copy(velocity = smoothedVelocity) - lastVelocities[bodyPart] = smoothedVelocity to currentVelocityData + lastVelocities[bodyPart] = smoothedVelocity to currentBoneSample } } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt index 1f7377b11..726a299bc 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt @@ -2,9 +2,14 @@ package dev.slimevr.skeleton.inputprocessors import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor +import dev.slimevr.skeleton.bodyPartMap import dev.slimevr.skeleton.findFirstParent +import dev.slimevr.util.timeSource import solarxr_protocol.datatypes.BodyPart import kotlin.collections.set +import kotlin.time.Duration.Companion.seconds + +private val FALLBACK_REFRESH = 1.seconds // Only allow child fallbacks in certain cases val CHILD_FALLBACKS = mapOf( @@ -59,23 +64,38 @@ val CHILD_FALLBACKS = mapOf( * This does not produce reliable data. For that, see the direct link processor. */ class FallbackInputProcessor : SkeletonInputProcessor { + // First is rotation, second is acceleration + val cachedFallbacks = bodyPartMap>() + var timeSinceRefresh = timeSource.markNow() + override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { + // Only lookup fallbacks every so often for optimization's sake + if (timeSinceRefresh.elapsedNow() > FALLBACK_REFRESH) { + for ((bodyPart, bone) in mutableInputSkeleton) { + if (bone.isRotationActive && bone.isAccelerationActive) continue + val rotationFallback = if (!bone.isRotationActive) { + bodyPart.findFirstParent { mutableInputSkeleton[it]?.isRotationActive == true } + ?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isRotationActive == true } + } else { + null + } + val accelFallback = if (!bone.isAccelerationActive) { + bodyPart.findFirstParent { mutableInputSkeleton[it]?.isAccelerationActive == true } + ?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isAccelerationActive == true } + } else { + null + } + cachedFallbacks[bodyPart] = rotationFallback to accelFallback + } + timeSinceRefresh = timeSource.markNow() + } + for ((bodyPart, bone) in mutableInputSkeleton) { if (bone.isRotationActive && bone.isAccelerationActive) continue - val newRotation = if (!bone.isRotationActive) { - val fallbackPart = bodyPart.findFirstParent { mutableInputSkeleton[it]?.isRotationActive == true } - ?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isRotationActive == true } - mutableInputSkeleton[fallbackPart]?.rotation?.eulerHeading() ?: bone.rotation - } else { - bone.rotation - } - val newAcceleration = if (!bone.isAccelerationActive) { - val fallbackPart = bodyPart.findFirstParent { mutableInputSkeleton[it]?.isAccelerationActive == true } - ?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isAccelerationActive == true } - mutableInputSkeleton[fallbackPart]?.acceleration ?: bone.acceleration - } else { - bone.acceleration - } + val cachedFallback = cachedFallbacks[bodyPart] + val newRotation = mutableInputSkeleton[cachedFallback?.first]?.rotation?.eulerHeading() ?: bone.rotation + val newAcceleration = mutableInputSkeleton[cachedFallback?.second]?.acceleration ?: bone.acceleration + mutableInputSkeleton[bodyPart] = bone.copy(rotation = newRotation, acceleration = newAcceleration) } } diff --git a/server/core/src/main/java/io/github/axisangles/ktmath/Matrix3.kt b/server/core/src/main/java/io/github/axisangles/ktmath/Matrix3.kt index ea015ea22..c46b5f177 100644 --- a/server/core/src/main/java/io/github/axisangles/ktmath/Matrix3.kt +++ b/server/core/src/main/java/io/github/axisangles/ktmath/Matrix3.kt @@ -5,6 +5,8 @@ package io.github.axisangles.ktmath import kotlinx.serialization.Serializable import kotlin.math.* +private const val FLOAT_EPSILON = 1e-7f + @Serializable class Matrix3 @Suppress("ktlint") constructor( @@ -339,115 +341,153 @@ class Matrix3 */ fun toEulerAnglesAssumingOrthonormal(order: EulerOrder): EulerAngles { val ETA = 1.5707964f - when (order) { + return when (order) { EulerOrder.XYZ -> { val kc = sqrt(zy * zy + zz * zz) - if (kc < 1e-7f) { - return EulerAngles( + if (kc < FLOAT_EPSILON) { + EulerAngles( EulerOrder.XYZ, atan2(yz, yy), ETA.withSign(zx), 0f ) + } else { + EulerAngles( + EulerOrder.XYZ, + atan2(-zy, zz), + atan2(zx, kc), + atan2(xy * zz - xz * zy, yy * zz - yz * zy) + ) } - - return EulerAngles( - EulerOrder.XYZ, - atan2(-zy, zz), - atan2(zx, kc), - atan2(xy * zz - xz * zy, yy * zz - yz * zy) - ) } EulerOrder.YZX -> { val kc = sqrt(xx * xx + xz * xz) - if (kc < 1e-7f) { - return EulerAngles( + if (kc < FLOAT_EPSILON) { + EulerAngles( EulerOrder.YZX, 0f, atan2(zx, zz), ETA.withSign(xy) ) + } else { + EulerAngles( + EulerOrder.YZX, + atan2(xx * yz - xz * yx, xx * zz - xz * zx), + atan2(-xz, xx), + atan2(xy, kc) + ) } - - return EulerAngles( - EulerOrder.YZX, - atan2(xx * yz - xz * yx, xx * zz - xz * zx), - atan2(-xz, xx), - atan2(xy, kc) - ) } EulerOrder.ZXY -> { val kc = sqrt(yy * yy + yx * yx) - if (kc < 1e-7f) { - return EulerAngles( + if (kc < FLOAT_EPSILON) { + EulerAngles( EulerOrder.ZXY, ETA.withSign(yz), 0f, atan2(xy, xx) ) + } else { + EulerAngles( + EulerOrder.ZXY, + atan2(yz, kc), + atan2(yy * zx - yx * zy, yy * xx - yx * xy), + atan2(-yx, yy) + ) } - - return EulerAngles( - EulerOrder.ZXY, - atan2(yz, kc), - atan2(yy * zx - yx * zy, yy * xx - yx * xy), - atan2(-yx, yy) - ) } EulerOrder.ZYX -> { val kc = sqrt(xy * xy + xx * xx) - if (kc < 1e-7f) { - return EulerAngles( + if (kc < FLOAT_EPSILON) { + EulerAngles( EulerOrder.ZYX, 0f, ETA.withSign(-xz), atan2(-yx, yy) ) + } else { + EulerAngles( + EulerOrder.ZYX, + atan2(zx * xy - zy * xx, yy * xx - yx * xy), + atan2(-xz, kc), + atan2(xy, xx) + ) } - - return EulerAngles( - EulerOrder.ZYX, - atan2(zx * xy - zy * xx, yy * xx - yx * xy), - atan2(-xz, kc), - atan2(xy, xx) - ) } - EulerOrder.YXZ -> { val kc = sqrt(zx * zx + zz * zz) - if (kc < 1e-7f) { - return EulerAngles( + if (kc < FLOAT_EPSILON) { + EulerAngles( EulerOrder.YXZ, ETA.withSign(-zy), atan2(-xz, xx), 0f ) + } else { + EulerAngles( + EulerOrder.YXZ, + atan2(-zy, kc), + atan2(zx, zz), + atan2(yz * zx - yx * zz, xx * zz - xz * zx) + ) } - - return EulerAngles( - EulerOrder.YXZ, - atan2(-zy, kc), - atan2(zx, zz), - atan2(yz * zx - yx * zz, xx * zz - xz * zx) - ) } EulerOrder.XZY -> { val kc = sqrt(yz * yz + yy * yy) - if (kc < 1e-7f) { - return EulerAngles( + if (kc < FLOAT_EPSILON) { + EulerAngles( EulerOrder.XZY, atan2(-zy, zz), 0f, ETA.withSign(-yx) ) + } else { + EulerAngles( + EulerOrder.XZY, + atan2(yz, yy), + atan2(xy * yz - xz * yy, zz * yy - zy * yz), + atan2(-yx, kc) + ) } + } + } + } - return EulerAngles( - EulerOrder.XZY, - atan2(yz, yy), - atan2(xy * yz - xz * yy, zz * yy - zy * yz), - atan2(-yx, kc) - ) + /** + * Returns just the y-axis of the Euler angle. + */ + fun eulerHeadingAssumingOrthonormal(order: EulerOrder): Float { + val ETA = 1.5707964f + return when (order) { + EulerOrder.XYZ -> { + val kc = sqrt(zy * zy + zz * zz) + if (kc < FLOAT_EPSILON) ETA.withSign(zx) + else atan2(zx, kc) + } + EulerOrder.YZX -> { + val kc = sqrt(xx * xx + xz * xz) + if (kc < FLOAT_EPSILON) atan2(zx, zz) + else atan2(-xz, xx) + } + EulerOrder.ZXY -> { + val kc = sqrt(yy * yy + yx * yx) + if (kc < FLOAT_EPSILON) 0f + else atan2(yy * zx - yx * zy, yy * xx - yx * xy) + } + EulerOrder.ZYX -> { + val kc = sqrt(xy * xy + xx * xx) + if (kc < FLOAT_EPSILON) ETA.withSign(-xz) + else atan2(-xz, kc) + } + EulerOrder.YXZ -> { + val kc = sqrt(zx * zx + zz * zz) + if (kc < FLOAT_EPSILON) atan2(-xz, xx) + else atan2(zx, zz) + } + EulerOrder.XZY -> { + val kc = sqrt(yz * yz + yy * yy) + if (kc < FLOAT_EPSILON) 0f + else atan2(xy * yz - xz * yy, zz * yy - zy * yz) } } } diff --git a/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt b/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt index b8932aab9..e8d34e057 100644 --- a/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt +++ b/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt @@ -396,7 +396,7 @@ class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { /** * Returns a Quaternion representing the heading of this Quaternion using Euler angles and preserving polarity. */ - fun eulerHeading() = rotationAroundYAxis(this.toEulerAngles(EulerOrder.YZX).y).twinNearest(this) + fun eulerHeading() = rotationAroundYAxis(this.toMatrix().eulerHeadingAssumingOrthonormal(EulerOrder.YZX)).twinNearest(this) /** * applies this quaternion's rotation to that vector