Skip to content

Commit

Permalink
holdingAcceleration -> strokeAcceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Oct 15, 2023
1 parent 87f71ac commit fdc48c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 31 deletions.
33 changes: 7 additions & 26 deletions appendix/GamePadViewer/src/StickManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ public class StickManager: ObservableObject {
@Published var vertical = StickSensor()
@Published var radian = 0.0
@Published var magnitude = 0.0
@Published var startingStroke = false
@Published var deadzoneEnteredAt = Date()
@Published var deadzoneLeftAt = Date()
@Published var holdingAcceleration = 0.0
@Published var holdingMagnitude = 0.0
@Published var strokeAcceleration = 0.0
var histories: [History] = []
let remainDeadzoneThresholdMilliseconds: UInt64 = 100
let strokeAccelerationMeasurementTime = 0.05 // 50 ms

var deadzoneTask: Task<(), Never>?
Expand All @@ -55,16 +54,14 @@ public class StickManager: ObservableObject {
if abs(vertical.lastDoubleValue) < deadzone && abs(horizontal.lastDoubleValue) < deadzone {
deadzoneTask = Task { @MainActor in
do {
try await Task.sleep(nanoseconds: 200 * NSEC_PER_MSEC)
try await Task.sleep(nanoseconds: remainDeadzoneThresholdMilliseconds * NSEC_PER_MSEC)

if let last = histories.last {
if last.timeStamp == now {
deadzoneEnteredAt = now
startingStroke = false

histories.removeAll()
holdingAcceleration = 0.0
holdingMagnitude = 0.0
strokeAcceleration = 0.0
}
}
} catch {
Expand All @@ -77,12 +74,6 @@ public class StickManager: ObservableObject {
if deadzoneEnteredAt > deadzoneLeftAt {
deadzoneLeftAt = now
}

if now.timeIntervalSince(deadzoneLeftAt) > strokeAccelerationMeasurementTime {
startingStroke = false
} else {
startingStroke = true
}
}

histories.removeAll(where: {
Expand All @@ -99,24 +90,14 @@ public class StickManager: ObservableObject {
radian: radian,
magnitude: magnitude))

if startingStroke {
if now.timeIntervalSince(deadzoneLeftAt) < strokeAccelerationMeasurementTime {
let minMagnitude = histories.min(by: { $0.magnitude < $1.magnitude })
let maxMagnitude = histories.max(by: { $0.magnitude < $1.magnitude })
let a = (maxMagnitude?.magnitude ?? 0) - (minMagnitude?.magnitude ?? 0)

if holdingAcceleration < a {
// Increase acceleration if magnitude is increased.
if magnitude > holdingMagnitude {
holdingAcceleration = a
}
} else {
// Decrease acceleration if magnitude is decreased.
if magnitude < holdingMagnitude - 0.1 {
holdingAcceleration = a
}
if a > strokeAcceleration {
strokeAcceleration = a
}

holdingMagnitude = magnitude
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions appendix/GamePadViewer/src/View/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ struct ContentView: View {
Text("counter: \(eventObserver.counter)")
Text("deadzoneEnteredAt: \(rightStick.deadzoneEnteredAt)")
Text("deadzoneLeftAt: \(rightStick.deadzoneLeftAt)")
Text("startingStroke: \(rightStick.startingStroke ? "true":"false")")
Text("Right Stick radian: \(rightStick.radian)")
Text("Right Stick magnitude: \(rightStick.magnitude)")
Text("Right stick holding acceleration: \(rightStick.holdingAcceleration)")
Text("Right stick holding magnitude: \(rightStick.holdingMagnitude)")
Text("Right stick stroke acceleration: \(rightStick.strokeAcceleration)")

StickSensorInfo(label: "Right Stick X", stick: rightStick.horizontal)
StickSensorInfo(label: "Right Stick Y", stick: rightStick.vertical)
Expand Down Expand Up @@ -49,10 +47,10 @@ struct ContentView: View {
path.addLine(
to: CGPoint(
x: circleSize / 2.0 + cos(rightStick.radian)
* rightStick.holdingAcceleration * accelerationScale * circleSize
* rightStick.strokeAcceleration * accelerationScale * circleSize
/ 2.0,
y: circleSize / 2.0 + sin(rightStick.radian)
* rightStick.holdingAcceleration * accelerationScale * circleSize / 2.0
* rightStick.strokeAcceleration * accelerationScale * circleSize / 2.0
))
}
.stroke(Color.red, lineWidth: 2)
Expand Down

0 comments on commit fdc48c3

Please sign in to comment.