Docs / World
Controller calibration
No thumbstick rests at exactly zero, and a worn one rests further out. A fixed dead zone is a guess wide enough to cover a bad pad, so it also throws away the first part of a good one's travel. PadMeasurement replaces the guess: it watches both sticks for a few seconds while nobody touches them, and the result, a PadCalibration, takes each stick's resting point off before anything reads it and gives it a dead zone sized to how much it actually wanders.
Two faults that look the same
A player whose character creeps forward on its own has one of two problems:
- The stick rests off center. It pulls the same way all the time. Subtracting the offset fixes it outright.
- The stick's potentiometer is worn. It comes back to a slightly different place each time, depending on which way it was last pushed. There is no single center to subtract. What fixes it is a dead zone wide enough to swallow the wander.
StickRest watches one stick and measures both: center is the median of each axis, so a single twitch during the sample does not move it, and wander is the furthest any sample sat from that center. calibration(minimumSamples:) turns them into a StickCalibration:
StickCalibration | What it is |
|---|---|
center | where the stick sits at rest, subtracted from every reading |
deadzone | wander times StickRest.margin (1.6), never below StickRest.floor (0.06) |
The floor is the payoff. With the resting point gone and the wander known, a healthy stick gets a 0.06 dead zone instead of the 0.16 and 0.10 the player's settings default to, and moves on the first millimeter of push.
calibration(minimumSamples:) returns nil when the sample cannot be trusted: fewer than 20 readings by default, or any reading past StickRest.heldThreshold (0.5), which means a thumb was on the stick. A worn stick does not reach half travel on its own.
Measuring from a settings screen
PadMeasurement runs a StickRest for each stick over three seconds by default. It is a state rather than a callback, because the screen has to draw the waiting as well as the answer. The clock is passed in, so a test needs no waiting.
// your game: the settings screen's "Calibrate sticks" row var measurement = PadMeasurement() measurement.begin(at: ProcessInfo.processInfo.systemUptime) // Each frame while the screen is open: if let sticks = input.rawSticks { let now = ProcessInfo.processInfo.systemUptime switch measurement.observe(left: sticks.left, right: sticks.right, at: now) { case .idle: break case .running(let done): progress = done // 0…1, for a bar case .finished(let pad): settings.padCalibration = pad input.apply(settings) try settingsStore.save(settings) // a SaveStore<PlayerSettings> case .refused(let reason): message = reason } }
InputState.rawSticks gives both sticks as the pad reports them, before any dead zone or calibration, and is nil with no pad. Tell the player to let go of both sticks before they start.
PadMeasurement.State | What it means |
|---|---|
.idle | not started, or cancel() was called |
.running(Double) | how far through, 0…1 |
.finished(PadCalibration) | both sticks measured |
.refused(String) | something was touching a stick; the string says so, ready to show |
A held stick is refused, never recorded. Recording a resting thumb as the center would make the pad unusable, which is worse than doing nothing. A run too short to collect 20 readings per stick is refused with the same message, so keep seconds at a length that sees plenty of frames. begin(at:seconds:) clamps it to at least 0.1.
Call cancel() if the player backs out or the pad disconnects mid-run. Once a run has finished or been refused, observe(left:right:at:) returns that state unchanged until the next begin(at:seconds:).
Applying it
PlayerSettings.padCalibration holds the result, and InputState.apply(_:) hands it to InputState.padCalibration with everything else. From then on movement and lookRate shape each measured stick through StickResponse.shaped(_:calibration:exponent:) instead of the dead zone alone:
- The stick's
centeris subtracted. - Its own
deadzoneis applied, radially. - The result is rescaled by how far that direction can actually reach. A stick resting 0.1 forward only reaches 0.9 forward once the offset is taken off, so without this full forward would no longer be full speed on the very pad this exists to rescue.
moveExponentorlookExponentcurves it, as for any stick.
The tests hold it to that: on a stick resting 0.12 forward, where a fixed 0.18 dead zone walked one way and did nothing the other, equal pushes either way now move nearly equally.
PadCalibration has a left and a right, each optional. A stick left nil uses the player's dead zone as set (moveDeadzone for the left, lookDeadzone for the right). PadCalibration.unmeasured is neither, and isMeasured says whether either was. To undo a calibration, set settings.padCalibration = .unmeasured and apply again.
A measured stick ignores the dead-zone slider. Its own dead zone replaces moveDeadzone or lookDeadzone. A settings screen that offers both should say which is in force, or offer a reset next to the slider.
Saving it
PadCalibration and StickCalibration are Codable, and PlayerSettings encodes padCalibration with the rest, so saving the settings saves the calibration. A settings file from before a game offered calibration loads as .unmeasured, everything else intact.
A pad that stopped sending
A pad can stop sending new readings and keep repeating its last one. An Xbox Wireless Controller was measured reporting the bit-identical pair (0.0146, 0.6082) for thirty seconds with nobody touching it. That reading is past any dead zone, so the player walks off on their own.
A person cannot hold an analog stick to five decimal places; every real hold wanders in the fourth digit within a second. FrozenStickWatch uses that: a non-zero reading that has not changed at all for FrozenStickWatch.staleAfter (2 seconds) is disbelieved until the report moves again. A stick resting inside restBelow repeating its rest is not a fault.
InputState runs one for you. While it holds, both sticks read as centered, and isPadReportFrozen is true, so your game can tell the player to reconnect the controller. The buttons are left alone: a stuck stick says nothing about them. rawSticks is not filtered. To watch another source, feed observe(left:right:at:restBelow:) once per reading; it returns true while the report should be disbelieved.
What it does not do
- It does not measure on its own. The player asks for it, from a settings screen.
- One calibration per settings file, not one per controller. The engine does not record which pad was measured, so a player who switches pads should measure again.
- Only the sticks' resting point and dead zone. There is no outer dead zone, no per-axis range, and no trigger calibration.
See input and settings for dead zones, the stick curve and PlayerSettings, and saving for SaveStore.