diff --git a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt index bd013112e..5d27e80c1 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt @@ -187,10 +187,11 @@ class ComputedSkeletonBehaviour( val waiter: PreciseWaiter, ) : SkeletonBehaviour { private val ALWAYS_ACTIVE_PARTS = arrayOf(BodyPart.HEAD, BodyPart.NECK) - private val IK_THRESHOLD = 0.005f // 0.002 would be ideal for accuracy, but we need performance + private val IK_THRESHOLD = 0.005f // 0.002 would be ideal for accuracy, but we want performance private val IK_MAX_ITERATIONS = 100 private val intervalDuration = (1.0 / hz).seconds + /** Shortest gap between two tick starts before the later one is pushed to the next slot */ private val minimumGap = intervalDuration * 0.75 diff --git a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-structure.kt b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-structure.kt index c6191d243..1b4c9464c 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-structure.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-structure.kt @@ -101,11 +101,17 @@ inline fun BodyPart.findFirstParent(predicate: (BodyPart) -> Boolean): BodyPart? val headPartSet = setOf(BodyPart.HEAD) fun highestBodyParts(bodyParts: Set): Set { if (BodyPart.HEAD in bodyParts) return headPartSet + + // Construct boolean array once + val bodyPartsPresent = BooleanArray(BodyPart.entries.size) + for (bodyPart in bodyParts) bodyPartsPresent[bodyPart.ordinal] = true + val result = bodyParts.toMutableSet() for (bodyPart in bodyParts) { var parent = parentOf(bodyPart) while (parent != null) { - if (parent in bodyParts) { + // Use the array here for performance + if (bodyPartsPresent[parent.ordinal]) { result.remove(bodyPart) break } @@ -115,7 +121,7 @@ fun highestBodyParts(bodyParts: Set): Set { return result } -private fun buildHierarchy(root: BodyPart, onlyChildren: Boolean): List> { +private fun buildHierarchy(root: BodyPart, onlyChildren: Boolean): Array> { val result = mutableListOf>() fun visit(parentBone: BodyPart?, bone: BodyPart, skipSelf: Boolean) { if (!skipSelf) result += parentBone to bone @@ -123,16 +129,16 @@ private fun buildHierarchy(root: BodyPart, onlyChildren: Boolean): List>> = +// FallbackProcessor asks for one per active bone per frame. +private val BODY_PART_HIERARCHIES: Array>> = Array(ALL_BODY_PARTS.size * 2) { index -> buildHierarchy(ALL_BODY_PARTS[index / 2], index % 2 == 1) } fun iterateBodyPartHierarchy( root: BodyPart = BodyPart.HEAD, onlyChildren: Boolean = false, -): List> = BODY_PART_HIERARCHIES[root.ordinal * 2 + if (onlyChildren) 1 else 0] +): Array> = BODY_PART_HIERARCHIES[root.ordinal * 2 + if (onlyChildren) 1 else 0] 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 d71021f89..a8ea9dc1b 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 @@ -61,8 +61,8 @@ val CHILD_FALLBACKS = mapOf( */ class FallbackInputProcessor : SkeletonInputProcessor { override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { - mutableInputSkeleton.forEachBone { bodyPart, bone -> - if (bone.isRotationActive && bone.isAccelerationActive) return@forEachBone + mutableInputSkeleton.forEach { (bodyPart, bone) -> + if (bone.isRotationActive && bone.isAccelerationActive) return@forEach val newRotation = if (!bone.isRotationActive) { val fallbackPart = bodyPart.findFirstParent { mutableInputSkeleton[it]?.isRotationActive == true } ?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isRotationActive == true } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/tracker-offset.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/tracker-offset.kt index d0b7e04ba..1beef5403 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/tracker-offset.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/tracker-offset.kt @@ -10,7 +10,7 @@ import io.github.axisangles.ktmath.Vector3 class TrackerOffsetInputProcessor : SkeletonInputProcessor { override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { mutableInputSkeleton.forEach { (part, input) -> - if (input.trackerOffset == Vector3.ZERO) return@forEach + if (input.trackerOffset.isZero()) return@forEach val localOffset = input.rotation.sandwich(input.trackerOffset) mutableInputSkeleton[part] = input.copy(position = input.position?.plus(localOffset)) } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/module.kt b/server/core/src/main/java/dev/slimevr/skeleton/module.kt index e061e3f4f..af71ca9c9 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -74,7 +74,7 @@ data class BoneState( // computed on demand rather than per bone private val orientationOffset: Quaternion get() = when { - offset.len() == 0f -> Quaternion.IDENTITY + offset.isZero() -> Quaternion.IDENTITY offset.unit().y == 1f -> Quaternion.I else -> Quaternion.fromTo(Vector3.NEG_Y, offset) } @@ -141,9 +141,14 @@ val DEFAULT_SKELETON_STATE = run { } fun buildBone(bone: BoneInput, parentBone: BoneState?, velocity: Velocity = ZERO_VELOCITY): BoneState { - // Raw position of the bone input is used for BodyPart.HEAD since it has no parent - val headPosition = parentBone?.let { it.tailPosition + it.rotation.sandwich(bone.headOffset) } - ?: bone.position ?: Vector3.ZERO + // Position of the bone input is used for the root since it has no parent + val headPosition = parentBone?.let { + if (bone.headOffset.isZero()) { + it.tailPosition + } else { + it.tailPosition + it.rotation.sandwich(bone.headOffset) + } + } ?: bone.position ?: Vector3.ZERO return BoneState( parentBone = parentBone, bodyPart = bone.bodyPart, @@ -154,6 +159,7 @@ fun buildBone(bone: BoneInput, parentBone: BoneState?, velocity: Velocity = ZERO acceleration = bone.acceleration, headPosition = headPosition, tailPosition = headPosition + bone.rotation.sandwich(bone.offset), + // Velocity is written directly during the skeleton loop computed bones; keep it. velocity = velocity, ) } @@ -163,10 +169,9 @@ fun buildBone(bone: BoneInput, parentBone: BoneState?, velocity: Velocity = ZERO */ fun buildBones(mutableBones: BodyPartMap, boneInputs: InputSkeleton, changedParts: Set = headPartSet) { for (bodyPart in highestBodyParts(changedParts)) { - for ((parentPart, childPart) in iterateBodyPartHierarchy(parentOf(bodyPart) ?: bodyPart, bodyPart != BodyPart.HEAD)) { + for ((parentPart, childPart) in iterateBodyPartHierarchy(bodyPart, false)) { val rawBone = boneInputs[childPart] ?: continue - val parentBone = parentPart?.let { mutableBones[it] } - // Velocity is written directly during the skeleton loop computed bones; keep it. + val parentBone = (parentPart ?: parentOf(childPart))?.let { mutableBones[it] } mutableBones[childPart] = buildBone(rawBone, parentBone, mutableBones[childPart]?.velocity ?: ZERO_VELOCITY) } } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt index ee661e533..8e18e141e 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt @@ -32,9 +32,11 @@ class FloorClipTargetProcessor( if (target.y < lowestTargetY) lowestTargetY = target.y } - // Snap the parent up - val offsetY = (floorLevel - lowestTargetY).coerceAtLeast(0f) - mutableIkTargets[parentPart] = Vector3(parentTarget.x, parentTarget.y + offsetY, parentTarget.z) + val offsetY = floorLevel - lowestTargetY + if (offsetY > 0f) { + // Snap the parent up + mutableIkTargets[parentPart] = Vector3(parentTarget.x, parentTarget.y + offsetY, parentTarget.z) + } } } } diff --git a/server/core/src/main/java/dev/slimevr/solarxr/module.kt b/server/core/src/main/java/dev/slimevr/solarxr/module.kt index fef06bd3d..407167d2d 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/module.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/module.kt @@ -37,8 +37,8 @@ import dev.slimevr.solarxr.rpc.SkeletonSettingsBehaviour import dev.slimevr.solarxr.rpc.StayAlignedBehaviour import dev.slimevr.solarxr.rpc.TapDetectionBehaviour import dev.slimevr.solarxr.rpc.TelemetryBehaviour -import dev.slimevr.solarxr.rpc.TrackerImuTelemetryBehaviour import dev.slimevr.solarxr.rpc.TimeoutSettingsBehaviour +import dev.slimevr.solarxr.rpc.TrackerImuTelemetryBehaviour import dev.slimevr.solarxr.rpc.TrackingChecklistBehaviour import dev.slimevr.solarxr.rpc.UpdateTrackerBehaviour import dev.slimevr.solarxr.rpc.VmcBehaviour diff --git a/server/core/src/main/java/io/github/axisangles/ktmath/Vector3.kt b/server/core/src/main/java/io/github/axisangles/ktmath/Vector3.kt index 75bf7dc41..861267536 100644 --- a/server/core/src/main/java/io/github/axisangles/ktmath/Vector3.kt +++ b/server/core/src/main/java/io/github/axisangles/ktmath/Vector3.kt @@ -75,6 +75,8 @@ class Vector3(val x: Float, val y: Float, val z: Float) { **/ fun len() = sqrt(x * x + y * y + z * z) + fun isZero() = x == 0f && y == 0f && z == 0f + /** * @return the normalized vector **/ diff --git a/server/core/src/test/java/dev/slimevr/skeleton/BodyPartHierarchyTest.kt b/server/core/src/test/java/dev/slimevr/skeleton/BodyPartHierarchyTest.kt index c30e5beca..5bb1813d3 100644 --- a/server/core/src/test/java/dev/slimevr/skeleton/BodyPartHierarchyTest.kt +++ b/server/core/src/test/java/dev/slimevr/skeleton/BodyPartHierarchyTest.kt @@ -2,6 +2,7 @@ package dev.slimevr.skeleton import solarxr_protocol.datatypes.BodyPart import kotlin.test.Test +import kotlin.test.assertContentEquals import kotlin.test.assertEquals import kotlin.test.assertSame import kotlin.test.assertTrue @@ -14,21 +15,21 @@ import kotlin.test.assertTrue */ class BodyPartHierarchyTest { - private fun walk(root: BodyPart, onlyChildren: Boolean): List> { + private fun walk(root: BodyPart, onlyChildren: Boolean): Array> { val expected = mutableListOf>() fun visit(parent: BodyPart?, bone: BodyPart, skipSelf: Boolean) { if (!skipSelf) expected += parent to bone for (child in BODY_PART_HIERARCHY_MAP[bone] ?: return) visit(bone, child, false) } visit(null, root, onlyChildren) - return expected + return expected.toTypedArray() } @Test fun `cached traversal matches a fresh walk for every root`() { for (root in BodyPart.entries) { - for (onlyChildren in listOf(false, true)) { - assertEquals( + for (onlyChildren in arrayOf(false, true)) { + assertContentEquals( walk(root, onlyChildren), iterateBodyPartHierarchy(root, onlyChildren), "traversal drifted for root=$root onlyChildren=$onlyChildren",