bone offset in tracker module

This commit is contained in:
Erimel committed 2026-11-17 19:48:24 -05:00
1 parent c9e0479a51
commit 40848cd8a3
9 files changed
+21 -39

No files matched your search

@@ -83,8 +83,8 @@ class HeightCalibrationManager(
combine(
controllers.map { controller ->
controller.context.state.map { s ->
val position = s.position ?: error("hands (or Controller) will always have a position in this case")
TrackerSnapshot(position = position, rotation = s.rawRotation)
val rawPosition = s.rawPosition ?: error("hands (or Controller) will always have a rawPosition in this case")
TrackerSnapshot(position = rawPosition, rotation = s.rawRotation)
}
},
) { snapshots -> snapshots.minByOrNull { it.position.y } ?: error("snapshots is empty; controllers check above should have prevented this") }
@@ -90,7 +90,6 @@ class YouSpinMeRightRoundBehaviour(val inputHz: Int = 100, val speed: Float = 1f
receiver.context.dispatch(
SkeletonActions.SetBonePose(
BodyPart.LOWER_CHEST,
Vector3.ZERO,
Quaternion.fromRotationVector(Vector3(cos(elapsed), sin(elapsed), 0f)),
Vector3.ZERO,
null,
@@ -99,7 +98,6 @@ class YouSpinMeRightRoundBehaviour(val inputHz: Int = 100, val speed: Float = 1f
receiver.context.dispatch(
SkeletonActions.SetBonePose(
BodyPart.LEFT_LOWER_LEG,
Vector3.ZERO,
Quaternion.fromRotationVector(Vector3(cos(elapsed + 1000), sin(elapsed + 1000), 0f)),
Vector3.ZERO,
null,
@@ -114,7 +112,6 @@ class YouSpinMeRightRoundBehaviour(val inputHz: Int = 100, val speed: Float = 1f
receiver.context.dispatch(
SkeletonActions.SetBonePose(
BodyPart.HEAD,
Vector3.ZERO,
Quaternion.IDENTITY,
Vector3.ZERO,
Vector3(circleX, state.skeletonHeight + jumpHeight, circleZ),
@@ -299,7 +296,7 @@ class ComputedSkeletonBehaviour(
// If head changed position, set the new position in state (for Localizer).
if (bodyPart == BodyPart.HEAD && !boneInput.isPositionActive && boneInput.position != previous?.position) {
receiver.context.dispatch(SkeletonActions.SetBonePose(BodyPart.HEAD, Vector3.ZERO, null, null, boneInput.position, false))
receiver.context.dispatch(SkeletonActions.SetBonePose(BodyPart.HEAD, null, null, boneInput.position, false))
}
}
// Inputs changed; re-run FK
@@ -1,17 +0,0 @@
package dev.slimevr.skeleton.inputprocessors
import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.SkeletonInputProcessor
/**
* Handles tracker offset. For example, an HMD's rotation will be about 10cm in front of the head bone.
*/
class BoneOffsetInputProcessor : SkeletonInputProcessor {
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
for ((bodyPart, bone) in mutableInputSkeleton) {
if (bone.boneOffset.isZero()) continue
val localOffset = bone.rotation.sandwich(bone.boneOffset)
mutableInputSkeleton[bodyPart] = bone.copy(position = bone.position?.plus(localOffset))
}
}
}
@@ -10,7 +10,6 @@ import dev.slimevr.skeleton.fkprocessors.FootPlantFkProcessor
import dev.slimevr.skeleton.fkprocessors.LocalizerFkProcessor
import dev.slimevr.skeleton.fkprocessors.ToeSnapFkProcessor
import dev.slimevr.skeleton.inputprocessors.ArmsInputProcessor
import dev.slimevr.skeleton.inputprocessors.BoneOffsetInputProcessor
import dev.slimevr.skeleton.inputprocessors.ConstraintInputProcessor
import dev.slimevr.skeleton.inputprocessors.DirectLinkInputProcessor
import dev.slimevr.skeleton.inputprocessors.FallbackInputProcessor
@@ -48,7 +47,6 @@ data class BoneInput(
val bodyPart: BodyPart,
val headOffset: Vector3,
val offset: Vector3,
val boneOffset: Vector3,
val poseTime: ComparableTimeMark?,
val rotation: Quaternion,
val acceleration: Vector3,
@@ -64,7 +62,6 @@ data class BoneState(
val bodyPart: BodyPart,
val headOffset: Vector3,
val offset: Vector3,
val boneOffset: Vector3,
val rotation: Quaternion,
val acceleration: Vector3,
val headPosition: Vector3,
@@ -114,7 +111,6 @@ val DEFAULT_BONE_INPUT = BoneInput(
bodyPart = BodyPart.NONE,
headOffset = Vector3.ZERO,
offset = Vector3.ZERO,
boneOffset = Vector3.ZERO,
poseTime = null,
rotation = Quaternion.IDENTITY,
acceleration = Vector3.ZERO,
@@ -155,7 +151,6 @@ fun buildBone(bone: BoneInput, parentBone: BoneState?, velocity: Velocity = ZERO
bodyPart = bone.bodyPart,
headOffset = bone.headOffset,
offset = bone.offset,
boneOffset = bone.boneOffset,
rotation = bone.rotation,
acceleration = bone.acceleration,
headPosition = headPosition,
@@ -181,7 +176,6 @@ fun buildBones(mutableBones: BodyPartMap<BoneState>, boneInputs: InputSkeleton,
sealed interface SkeletonActions {
data class SetBonePose(
val bodyPart: BodyPart,
val boneOffset: Vector3?,
val rotation: Quaternion?,
val acceleration: Vector3?,
val position: Vector3?,
@@ -275,7 +269,6 @@ class Skeleton(
// Run before FK on the inputs
inputProcessors = listOf(
FilteringInputProcessor(settings),
BoneOffsetInputProcessor(),
HeadPositionFallbackInputProcessor(),
FallbackInputProcessor(),
SpineInputProcessor(settings),
@@ -11,7 +11,6 @@ fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when
state.copy(
boneInputs = state.boneInputs.mutateCopy {
it[action.bodyPart] = bone.copy(
boneOffset = action.boneOffset ?: bone.boneOffset,
poseTime = timeSource.markNow(),
rotation = action.rotation ?: bone.rotation,
acceleration = action.acceleration ?: bone.acceleration,
@@ -29,7 +28,6 @@ fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when
state.copy(
boneInputs = state.boneInputs.mutateCopy {
it[action.bodyPart] = bone.copy(
boneOffset = Vector3.ZERO,
poseTime = null,
rotation = Quaternion.IDENTITY,
acceleration = Vector3.ZERO,
@@ -82,10 +80,7 @@ fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when
is SkeletonActions.ResetFloorLevel -> {
val skeletonHeight = state.skeletonHeight
val headHeight = state.boneInputs[BodyPart.HEAD]?.let {
val headOffset = it.rotation.sandwich(it.boneOffset).y
it.position?.y?.plus(headOffset)
} ?: skeletonHeight
val headHeight = state.boneInputs[BodyPart.HEAD]?.position?.y ?: skeletonHeight
state.copy(floorLevel = headHeight - skeletonHeight)
}
@@ -54,7 +54,7 @@ private fun createTracker(device: DeviceState, tracker: TrackerState, trackerMas
trackerId = tracker.id.toUShort(),
status = if (trackerMask.status) tracker.status else TrackerStatus.NONE,
rotation = if (trackerMask.rotation) tracker.rawRotation.toQuat() else null,
position = if (trackerMask.position && tracker.position != null) tracker.position.toVec3f() else null,
position = if (trackerMask.position && tracker.rawPosition != null) tracker.rawPosition.toVec3f() else null,
info = if (trackerMask.info) {
TrackerInfo(
isImu = tracker.imuType != null,
@@ -46,7 +46,6 @@ class TrackerToSkeletonBehaviour : TrackerBehaviour {
val pendingResets = trackerState.pendingSkeletonResets
val bonePoseActions = SkeletonActions.SetBonePose(
bodyPart,
trackerState.boneOffsets[bodyPart],
trackerState.rotation,
trackerState.acceleration,
trackerState.position,
@@ -75,6 +75,7 @@ data class TrackerState(
val rawAcceleration: RawAcceleration?,
val acceleration: CalibratedAcceleration?,
val rawMagnetometer: Vector3,
val rawPosition: Vector3?,
val position: Vector3?,
val imuTemp: Float?,
val accumulatedTicks: UShort,
@@ -217,6 +218,7 @@ class Tracker(
rawAcceleration = null,
acceleration = null,
rawMagnetometer = Vector3.ZERO,
rawPosition = null,
position = null,
imuTemp = null,
accumulatedTicks = 0u,
@@ -38,7 +38,7 @@ fun reduce(
// Other inputs
val rawAcceleration: RawAcceleration? = action.acceleration ?: state.rawAcceleration
val rawMagnetometer = action.magnetometer ?: state.rawMagnetometer
val position = action.position ?: state.position
val rawPosition = action.position ?: state.rawPosition
// Rotation calibration: going from raw rotation to rotation since reset
val rotation: CalibratedRotation =
@@ -67,12 +67,25 @@ fun reduce(
state.acceleration
}
val position =
if (action.position != null) {
val boneOffset = state.boneOffsets[state.bodyPart]
if (boneOffset != null) {
action.position + rotation.sandwich(boneOffset)
} else {
action.position
}
} else {
state.position
}
state.copy(
rawRotation = rawRotation,
rotation = rotation,
rawAcceleration = rawAcceleration,
acceleration = acceleration,
rawMagnetometer = rawMagnetometer,
rawPosition = rawPosition,
position = position,
accumulatedTicks = accumulatedTicks,
rotationDirty = false,