Skip to content

Commit

Permalink
Remove deltaHorizontal,deltaVertical in GamePadViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jan 8, 2024
1 parent 971054a commit 8388d76
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
20 changes: 6 additions & 14 deletions appendix/GamePadViewer/src/StickManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ public class StickManager: ObservableObject {
@Published var vertical = StickSensor()
@Published var radian = 0.0
@Published var magnitude = 0.0
@Published var deltaHorizontal = 0.0
@Published var deltaVertical = 0.0
@Published var deltaMagnitude = 0.0
@Published var pointerX = 0.5 // 0.0 ... 1.0
@Published var pointerY = 0.5 // 0.0 ... 1.0
@Published var continuedMovementMagnitude = 0.0
@Published var history: [HistoryEntry] = []
var previousHorizontalDoubleValue = 0.0
var previousVerticalDoubleValue = 0.0
var previousMagnitude = 0.0

var continuedMovementTimer: Cancellable?
Expand All @@ -49,15 +45,13 @@ public class StickManager: ObservableObject {
public func update() {
let now = Date()

deltaHorizontal = horizontal.lastDoubleValue - previousHorizontalDoubleValue
deltaVertical = vertical.lastDoubleValue - previousVerticalDoubleValue
deltaMagnitude = min(1.0, sqrt(pow(deltaHorizontal, 2) + pow(deltaVertical, 2)))

radian = atan2(vertical.lastDoubleValue, horizontal.lastDoubleValue)
magnitude = min(
1.0,
sqrt(pow(vertical.lastDoubleValue, 2) + pow(horizontal.lastDoubleValue, 2)))

deltaMagnitude = max(0.0, magnitude - previousMagnitude)

history.append(HistoryEntry(deltaMagnitude: deltaMagnitude))
history.removeAll(where: { now.timeIntervalSince($0.time) > 0.1 })

Expand Down Expand Up @@ -94,16 +88,14 @@ public class StickManager: ObservableObject {
}

let deltaMagnitudeThreshold = 0.01
if deltaMagnitude < deltaMagnitudeThreshold {
return
}
if 0 < deltaMagnitude {
if deltaMagnitude < deltaMagnitudeThreshold {
return
}

if magnitude >= previousMagnitude {
updatePointerXY(magnitude: deltaMagnitude, radian: radian)
}

previousHorizontalDoubleValue = horizontal.lastDoubleValue
previousVerticalDoubleValue = vertical.lastDoubleValue
previousMagnitude = magnitude
}

Expand Down
2 changes: 0 additions & 2 deletions appendix/GamePadViewer/src/View/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ struct InformationView: View {
}
Divider()
Group {
Text("deltaHorizontal: \(rightStick.deltaHorizontal)")
Text("deltaVertical: \(rightStick.deltaVertical)")
Text("deltaMagnitude: \(rightStick.deltaMagnitude)")
Text("history.count: \(rightStick.history.count)")
}
Expand Down

0 comments on commit 8388d76

Please sign in to comment.