Skip to content

Commit 25967a1

Browse files
committed
Remove applyVelocityPolicyTo(), Replace System.nanoTime with Timesource, Replace allowVelocity with hasVelocity in ProtobufBridge.
1 parent b4e111f commit 25967a1

4 files changed

Lines changed: 12 additions & 23 deletions

File tree

server/core/src/main/java/dev/slimevr/VRServer.kt

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,6 @@ class VRServer @JvmOverloads constructor(
186186
instance = this
187187
}
188188

189-
/**
190-
* TODO: The design of this method is chosen for future expandability in case we want to have more complex velocity policies that depend on tracker properties or other config values.
191-
* Initiates the velocity policy application process for the specified [Tracker] (or all trackers if null).
192-
*
193-
* This method serves as the initiator.
194-
* The actual policy logic is not handled here,
195-
* but is delegated to [dev.slimevr.config.VRConfig.applyVelocityPolicy].
196-
*/
197-
private fun applyVelocityPolicyTo(tracker: Tracker?) {
198-
val targets = tracker?.let { listOf(it) } ?: trackers
199-
for (t in targets) {
200-
configManager.vrConfig.applyVelocityPolicy(t)
201-
}
202-
}
203-
204189
fun hasBridge(bridgeClass: Class<out Bridge?>): Boolean {
205190
for (bridge in bridges) {
206191
if (bridgeClass.isAssignableFrom(bridge.javaClass)) {
@@ -247,7 +232,7 @@ class VRServer @JvmOverloads constructor(
247232
configManager.saveConfig()
248233

249234
// Requires a fresh TrackerConfig on Update, so executed after we save a new state.
250-
applyVelocityPolicyTo(tracker)
235+
tracker?.let { configManager.vrConfig.applyVelocityPolicy(it) }
251236
}
252237
}
253238

server/core/src/main/java/dev/slimevr/tracking/processor/skeleton/HumanSkeleton.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ class HumanSkeleton(
11711171
it.position = trackerBone.getTailPosition()
11721172
it.setRotation(trackerBone.getGlobalRotation() * trackerBone.rotationOffset.inv())
11731173
it.dataTick()
1174-
it.updateDerivedVelocity(System.nanoTime())
1174+
it.updateDerivedVelocity()
11751175
}
11761176
}
11771177

server/core/src/main/java/dev/slimevr/tracking/trackers/Tracker.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import io.eiren.util.BufferedTimer
1212
import io.github.axisangles.ktmath.Quaternion
1313
import io.github.axisangles.ktmath.Vector3
1414
import kotlin.properties.Delegates
15+
import kotlin.time.DurationUnit
16+
import kotlin.time.TimeMark
17+
import kotlin.time.TimeSource
1518

1619
const val TIMEOUT_MS = 2_000L
1720
const val DISCONNECT_MS = 3_000L + TIMEOUT_MS
@@ -123,7 +126,7 @@ class Tracker @JvmOverloads constructor(
123126
* Velocity state server-side differentiation based on sent poses
124127
*/
125128
private data class VelocityState(
126-
var prevTimeNs: Long = 0L,
129+
var prevMark: TimeMark? = null,
127130
var prevPos: Vector3 = Vector3(0f, 0f, 0f),
128131
)
129132

@@ -370,7 +373,7 @@ class Tracker @JvmOverloads constructor(
370373
* last update, applying a sanity check on the time delta to filter out noise and ensure data stability.
371374
*
372375
*/
373-
fun updateDerivedVelocity(nowNs: Long) {
376+
fun updateDerivedVelocity() {
374377
if (!allowVelocity || !hasPosition) {
375378
velocityState = null
376379
_velocity = Vector3.NULL
@@ -382,8 +385,9 @@ class Tracker @JvmOverloads constructor(
382385
velocityState = it
383386
}
384387

385-
if (state.prevTimeNs != 0L) {
386-
val dt = (nowNs - state.prevTimeNs) * 1e-9
388+
val prevMark = state.prevMark
389+
if (prevMark != null) {
390+
val dt = prevMark.elapsedNow().toDouble(DurationUnit.SECONDS)
387391
if (dt in 1e-4..0.25) {
388392
_velocity = Vector3(
389393
((pos.x - state.prevPos.x) / dt).toFloat(),
@@ -394,7 +398,7 @@ class Tracker @JvmOverloads constructor(
394398
_velocity = Vector3.NULL
395399
}
396400
}
397-
state.prevTimeNs = nowNs
401+
state.prevMark = TimeSource.Monotonic.markNow()
398402
state.prevPos = pos
399403
}
400404

server/desktop/src/main/java/dev/slimevr/desktop/platform/ProtobufBridge.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ abstract class ProtobufBridge(@JvmField protected val bridgeName: String) : ISte
124124
builder.setQw(rot.w)
125125
}
126126

127-
if (tracker.allowVelocity) {
127+
if (tracker.hasVelocity) {
128128
val vel = tracker.getVelocity()
129129
builder.setVx(vel.x)
130130
builder.setVy(vel.y)

0 commit comments

Comments
 (0)