Skip to content

Commit

Permalink
Update continuedMovementMagnitude calculation in StickManager
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Feb 4, 2024
1 parent 216b223 commit 28adb74
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions appendix/GamePadViewer/src/StickManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class StickManager: ObservableObject {

struct HistoryEntry {
let time = Date()
let deltaMagnitude: Double
let horizontalStickSensorValue: Double
let verticalStickSensorValue: Double
}

class Stick: ObservableObject {
Expand Down Expand Up @@ -52,15 +53,17 @@ public class StickManager: ObservableObject {

deltaMagnitude = max(0.0, magnitude - previousMagnitude)

history.append(HistoryEntry(deltaMagnitude: deltaMagnitude))
history.append(
HistoryEntry(
horizontalStickSensorValue: horizontal.lastDoubleValue,
verticalStickSensorValue: vertical.lastDoubleValue))
history.removeAll(where: { now.timeIntervalSince($0.time) > 0.1 })

let continuedMovementThreshold = 1.0

if magnitude >= continuedMovementThreshold {
if continuedMovementTimer == nil {
continuedMovementMagnitude =
(history.max { a, b in a.deltaMagnitude < b.deltaMagnitude })?.deltaMagnitude ?? 0.0
continuedMovementMagnitude = getMaxDistanceInHistory()

continuedMovementTimer = Timer.publish(
every: 0.3, // 300 ms
Expand Down Expand Up @@ -108,6 +111,24 @@ public class StickManager: ObservableObject {
pointerY -= magnitude * sin(radian) * scale
pointerY = max(0.0, min(1.0, pointerY))
}

@MainActor
private func getMaxDistanceInHistory() -> Double {
var distance = 0.0

history.forEach { h1 in
history.forEach { h2 in
let h = h1.horizontalStickSensorValue - h2.horizontalStickSensorValue
let v = h1.verticalStickSensorValue - h2.verticalStickSensorValue
let d = sqrt(h * h + v * v)
if d > distance {
distance = d
}
}
}

return distance
}
}

@Published var rightStick = Stick()
Expand Down

0 comments on commit 28adb74

Please sign in to comment.