Skip to content

Input, wheels and force feedback ​

Skidpad's core computes the torque the tires put on the hand wheel (SteeringTorque, see steering); the @skidpad/input package reads keyboards, gamepads, wheels and touch screens into one filtered input frame, and sends that torque back to a wheel. Filtering happens in the input package, before the core, so a recorded replay holds exactly what the car was given, whatever the device.

Keyboard ​

KeyboardInput turns held keys into ramps: steering rises at 2.5 full locks per second and falls back at 5, throttle at 3 and 6, brake at 4 and 8 (pass steer, throttle or brake with riseRate and fallRate to change them). The default mapping is arrows or WASD to drive, Space for the handbrake, E or right Shift to shift up, Q or left Shift to shift down, and C for the clutch; mapping replaces any of the key lists, by KeyboardEvent.code. attach(window) wires the listeners and update(dt) reads a frame.

A key steers to full lock, which is far too much at speed. The core's steering assist (assists.steeringAssist, see assists) caps the road-wheel angle by speed and is the usual fix for keyboard driving; it is off unless the definition turns it on.

Gamepad ​

GamepadInput reads the first pad with the browser's standard mapping: left stick steers, right trigger is throttle, left trigger brake, the bottom face button the handbrake, the bumpers shift and the top face button toggles reverse. Every binding is an option (-1 for none on the shift buttons).

A thumb stick travels a few millimetres for what a wheel does in 900°, so the steering is shaped before it reaches the car:

  • deadzone (default 0.08) ignores stick drift around the centre.
  • steerExponent (default 2) is the response curve: half stick asks for about a quarter of the range. 1 is linear.
  • steerRate (default 4/3) is how fast the steering winds on, in full locks per second. The stick asks for an angle and the wheel turns toward it at this rate, so full stick reaches full lock after 0.75 s rather than in one frame, the way console racing games turn the wheel. It is also a property, so a settings slider can change it while driving; the sandbox's Pad steering slider sets the time to full lock (its inverse). Infinity turns it off.
  • steerReturnRate (default 5) is how fast the steering unwinds toward centre, faster than winding on so letting go straightens the car promptly.
  • steerSpeedHalf (default 20 m/s, about 72 km/h) is the speed at which full stick asks for half lock. The lock falls as 1 / (1 + (v / half)²), so at speed the stick's whole travel covers the few degrees a corner needs and a small thumb movement is a small correction, while full lock is still there when slow. It needs the car's speed: set the speed property (m/s) before each read; left at 0 nothing is scaled. Also a live property (the sandbox's Pad at speed slider, in km/h); Infinity turns it off. It scales the wound-on angle, so turn-in takes the same time at any speed.
  • steerSensitivity (default 1) is the fraction of full lock that full stick asks for, also a live property.

The rates need the time between reads, so pass the step to poll(dt) (or map(axes, buttons, dt) in a test); it defaults to 1/60 s.

Reading a wheel ​

Any wheel the browser exposes as a gamepad works through WheelInput. A profile binds its axes and buttons: which axis is the steering, which pedal is which, where the paddles are, and the wheel's rotation. Three profiles ship (Logitech G PRO with PRO pedals, G29/G923, a generic wheel); the sandbox's Wheel setup panel assigns each control by moving it and records the end stops, so the shipped axis numbers only have to be close. Profiles are plain JSON and persist in localStorage.

Steering is scaled by the car, not the wheel: with a 900° wheel and a car whose steering.ratio × maxWheelAngleDeg is 490°, 490° of hand wheel is full lock and the rest is past the stops, so the road wheels turn exactly as the definition's ratio says and the force-feedback torque means what the core computed.

ts
import { WheelInput, ffbFrameFromTelemetry, LogitechWebHidSink } from "@skidpad/input";

const wheel = new WheelInput({
  steeringLockDeg: def.steering.ratio * def.steering.maxWheelAngleDeg,
});
const frame = wheel.poll(); // undefined until a known wheel is connected

The setup flow assigns controls with AxisFinder, which reports the axis the player is moving. Pass the axes already assigned to result(threshold, exclude) so brushing the throttle while finding the brake does not pick the throttle again, and binding(found, centred, rest) turns the result into a calibrated binding. Profile patterns are JavaScript regular expressions matched against Gamepad.id; a leading (?i) makes one case-insensitive. Pads with the browser's standard gamepad mapping are left to GamepadInput (wheels report a non-standard mapping), unless you pass matchStandardPads: true.

A wheel's steering axis has no dead zone (CENTRED_DEADZONE is 0; pedals keep 3 %). A dead zone there is a band of rim angle where the car goes straight and the core sends no aligning torque, so with column friction the released rim parks anywhere inside it. Profiles saved with the old 2 % load with none.

Several devices and one gearbox ​

Each device class can hold the requested gear itself, but a car has one gearbox. Create one GearSelector with the car's transmission mode and pass it to every device: with mode: "automatic" a shift down from drive selects reverse and a shift up from reverse selects drive, so no device can count forward gears the automatic ignores. InputMixer reads every device each frame and passes on the one the player touched last:

ts
import { GearSelector, InputMixer, KeyboardInput, GamepadInput, WheelInput } from "@skidpad/input";

const gears = new GearSelector({ mode: "automatic" }); // the car's gearbox
const keyboard = new KeyboardInput({ gears });
const gamepad = new GamepadInput({ gears }); // bumpers shift, Y toggles reverse
const wheel = new WheelInput({ gears });
const input = new InputMixer(
  [
    { name: "keyboard", read: (dt) => keyboard.update(dt) },
    { name: "gamepad", read: (dt) => gamepad.poll(dt) },
    { name: "wheel", read: () => wheel.poll() },
  ],
  { gears },
);
// each step
world.setInput(car, input.update(dt));

A device takes over when one of its inputs moves past threshold (default 0.15) or it shifts, so a wheel left turned or a pedal resting slightly above zero does not steal control. GamepadInput.poll() reads the first pad with the standard mapping, so a connected wheel is never read as a gamepad, and leaves it in lastPad for a rumble sink.

Safety first ​

A force-feedback wheel is a motor. A direct-drive base like the G PRO can spin faster than a hand can follow and hard enough to hurt a wrist or a thumb in the spokes, most easily when the torque's sign is wrong (the self-centring force then drives the wheel away from the centre) or when the car spins. The input package defaults are chosen for that:

  • maxOutput (Logitech sink, default 0.4): the largest fraction of the device's peak torque ever sent, about 4.4 N·m on a G PRO. It applies after every gain.
  • gain (default 0.5) on the physical torque, in rim N·m per car N·m (1 is the car at true scale), and a rate limit (slewRate, default 5 full scales per second) so no frame can snap the wheel.
  • A runaway guard: when the hand wheel moves away from the centre faster than runawayRate (default 10 rad/s, about 570°/s) for runawayTime (0.1 s) while a force is applied, the output latches at zero until scaler.rearm(). It needs the hand-wheel angle in the frame (ffbFrameFromTelemetry's third argument).
  • A watchdog: the force goes to zero when the page is hidden or closed, or when no update has arrived for watchdog seconds (default 0.25), so a paused host cannot leave a force playing.
  • Stop on stall (forceTimeout, seconds; 0, the default, is off): the watchdog is a timer on the page's own thread, so it stops with the page. A page paused at a breakpoint, hung or crashed would leave the last force on the wheel. With forceTimeout set (the sandbox uses 0.25), every HID++ constant-force report carries that lifetime and renews it, and the wheel's firmware ends the force by itself when the reports stop. Whether a firmware honours the lifetime has to be checked on the wheel: measureLoop() does, and reports it as timeoutExpires and timeoutRenews. Turn it on only when both are true.

On a new wheel or a new protocol, keep your hands clear, press the sandbox's Test pulse first (it should turn the wheel gently to the right; if it turns left, tick invert), and raise gain and max output only after that.

Setting the gain ​

The car's steering torque at the rim is gain × the car's, clipped at maxOutput × the device's peak torque. Too high a gain clips the peaks, which you feel as a flat, dead wheel at the limit. Too low a gain leaves the wheel light everywhere.

The scaler keeps the car's peak torque over the last minute (scaler.carPeak, a TorquePeak), and scaler.autoGain() returns the gain that puts that peak at 90 % of the rim's limit, as iRacing's auto strength does. It needs 10 s of driving first and stays within 0.05 to 2; it never changes the gain on its own. The sandbox's Auto button applies it, and the Wheel panel shows where the last minute's peak lands on the rim against the limit.

On a G PRO at 40 % (a 4.4 N·m limit), a lap of the sandbox track gave these Auto gains before the assisted cars got their on-centre assist and more assist at speed (ADR-0040). Their peaks are lower now (on a ramp steer at 80 km/h: hatchback 5.9 N·m, pickup 7.4, crossover 8.1, sports car 8.6), so the default gain of 0.5 no longer clips them; the lap has not been driven again:

PresetPeak car torque on the lapAuto gain
Hatchback7.9 N·m0.50
Kart8.2 N·m0.48
Pickup10.5 N·m0.38
Open-wheeler12.1 N·m0.33
Sports car12.6 N·m0.31
Crossover15.3 N·m0.26

Sending torque back ​

The web has no standard force-feedback channel. The Gamepad API only vibrates, so GamepadRumbleSink maps torque magnitude to the strong motor as a fallback. Real force feedback goes through WebHID, which Chromium (Chrome, Edge, Opera, Brave) exposes and Firefox and Safari do not. A page asks for the device with requestDevice() from a click, the browser shows a chooser, and from then on the page can send the wheel its own reports.

Each wheel speaks its own protocol. LogitechWebHidSink implements two, reconstructed from the public Linux drivers:

  • hidpp for the G PRO, G923 and G920: HID++ 2.0 feature 0x8123. A constant-force effect is downloaded once and updated every host step.
  • classic for the G29, G27 and G25 (and the G923 in compatibility mode): the seven-byte command reports.

By default (protocol: "auto") the sink picks the protocol from the device's USB product id when it attaches, using the ids of the Linux drivers (LOGITECH_WHEELS, logitechWheel(productId)), and takes the wheel's peak torque from the same table, so a G29 gets the classic protocol and about 2.5 N·m rather than the G PRO's 11. A wheel the table does not know gets HID++ and 11 N·m; pass protocol (or set sink.protocol before attaching) and maxTorque to override either.

connect() and attach() reject when the force path cannot be set up (the HID++ feature does not answer, or the wheel refuses the constant-force effect), with the device closed again and the reason in the diagnostics log. sink.ready says whether forces are being sent; connected only says the device is open.

Every protocol constant is marked [VERIFY] until it has been tried on hardware. The sink keeps a diagnostics log of every report sent and received; if your wheel does nothing or does the wrong thing, copy the log from the sandbox panel into an issue with the wheel model and the browser version, and try the other protocol and the other update mode (modify re-sends the effect with its slot id, recreate destroys and downloads it).

A wheel exposes several HID interfaces (its gamepad and its HID++ channel); connect() opens the one that declares HID++ long or very long output reports and logs the report ids it found. HID++ 2.0 commands always go out as long or very long reports, as the Linux driver sends them: the G PRO does not accept the 7-byte short report and the browser rejects the write with NotAllowedError: Failed to write the report.

ts
const sink = new LogitechWebHidSink({ rotationDeg: 900 }); // protocol from the product id
button.onclick = () => sink.connect(); // must be a user gesture
// each host step; the hand-wheel angle feeds the runaway guard
sink.update(
  ffbFrameFromTelemetry((c) => world.read(car, c), def.steering, wheel.status.wheelAngleDeg),
  1 / 60,
);

FfbScaler turns N·m into the device's −1 … 1 with a gain, a smoothing time constant, the rate limit, the runaway guard and clipping statistics, so the panel can show how much of the signal the wheel cannot reproduce. A direct-drive G PRO is taken as 11 N·m peak, belt wheels as 2 to 3, and maxOutput scales that down. The HID++ sign was found on a G PRO, where a positive constant force turns the wheel left, so the sink flips it; on a G923 or G920 check it with the test pulse.

Column friction and damping are definition values (steering.columnFriction, N·m, and steering.columnDamping, N·m per rad/s, at the hand wheel). The HID++ sink sends them to the wheel's own friction and damper effects, where the firmware applies them far faster than the host steps, which a damper needs to stay stable. They are what makes the wheel feel settled around the centre, where the tires' aligning torque is small. Each is multiplied by gain and by columnScale (default 1; the sandbox's Column feel slider), divided by the wheel's peak torque and capped at maxOutput. The damper's coefficient also needs the hand-wheel rate at which the firmware gives full output, damperRate, which no public source documents: raise it for less damping. It defaults to the model's measured value (8.5 rad/s on the G PRO, from Measure damping; see ADR-0032) and to 10 rad/s on wheels not yet measured [VERIFY]. Both effects only resist motion, so they stay on when the runaway guard or the watchdog cuts the constant force. The classic protocol does not send them yet.

A definition can raise the damping with speed (steering.columnDampingAtSpeed and steering.columnDampingSpeed, ADR-0033), so the wheel stays light when parking and does not swing when let go at speed. The core reports the current value on the ColumnDamping channel, ffbFrameFromTelemetry sends it, and the sink re-sends the damper only when it moves by more than about 5 %. The road cars reach four to six times their parking damping by 20 m/s, the open-wheeler ten times, and the kart, with its 2.5:1 ratio, 0.6 N·m per rad/s by 10 m/s; the values were set so that a released wheel settles on a measured G PRO (ADR-0033, amendment A).

sink.measureDamper(readAngleDeg) finds the firmware damper's units on the wheel at hand. With the hands off, it spins the rim at three constant torques with the damper at a known coefficient, and fitDamper fits torque against the speed reached for the damping, the friction and the damperRate to use. The sandbox's Measure damping button runs it and remembers the result in the browser.

What the torque signal is ​

ffbFrameFromTelemetry sends SteeringTorqueMean, the mean of the steering torque over the host step's substeps, not the last substep's value, which aliases anything faster than half the host rate into random steps (ADR-0031). The core also splits the torque by source (SteerTorqueMz, SteerTorqueTrail, SteerTorqueScrub, SteerTorqueLift, SteerTorqueAssist) for graphs and tuning; see steering.

A 60 Hz host step still sends the wheel a staircase. The frame also carries the step's torque in eight equal slices of time (SteeringTorqueSlice0 … 7, ADR-0032), and with playoutRate set (Hz, for example 250 to 500; default 0, off) the sink plays them out between host steps through TorquePlayout, one host step behind, with linear interpolation. 500 Hz has been driven on a G PRO.

Playout smooths the staircase but leaves the loop slow: the torque still answers a rim angle read once per 60 Hz frame, and goes out a frame late. A host that can step faster does better without it. The sandbox's Fast wheel loop steps at 240 Hz from a 4 ms timer while a wheel is connected, polls the wheel on every step and passes each step's frame to sink.update() with no samples. The sink sends one report at a time and the newest level next, so a host faster than the wheel (a G PRO takes 200 reports a second) never stacks reports up. That about halves the delay around the rim, from 40 ms to 20 ms on a G PRO, which is what a released wheel needs to settle at speed (hands-off report).

Belt and gear wheels smooth the output themselves; a direct-drive base does not, and shows the scaler's own rate limit and smoothing as lag. DIRECT_DRIVE_SCALER holds a faster slew (50 full scales per second) and 3 ms of smoothing for such a base; the scaler's defaults (5 per second, 10 ms) suit a G29. Raise the slew only once the sign and the gain are known to be right.

sink.setRotation(deg) sets the wheel's rotation, clamped to the model's range (1080° on the G PRO), and tells WheelInput through its rotationDeg. Set it to the car's lock (ratio × maxWheelAngleDeg) and the wheel's end stop is the car's full lock.

Road detail (detail, default 1, and detailCutoff, default 10 Hz; ADR-0036) splits the torque with a first-order low-pass at the cutoff and multiplies the fast part, the road texture and kerbs of Surfaces, before the gain. Above 1 the road comes through stronger without the steering getting heavier; 0 removes it; 1 leaves the torque exactly as the model gave it.

FfbScaler reports requested (the torque asked of the rim, N·m), sent, output, lastClipped, peak and clipFraction, so a setup panel can show how much of the signal the wheel cannot reproduce.

Measuring the loop ​

sink.reportRate is the number of constant-force reports the wheel took in the last second. It is what the wheel really gets: a browser holds a repeating timer to 4 ms, so a playoutRate of 500 delivers about 250, and a busy frame delivers fewer.

sink.measureLoop(readAngleDeg) measures the rest with the hands off the wheel, in a few seconds:

  • reportRate and roundTripMs: zero-force reports sent back to back, each waiting for its reply.
  • latencyMs: the time from asking for a torque to the page seeing its effect. Short pulses turn the free rim, which moves as ½·α·(t − t_d)²; a line through √angle against time gives the delay t_d whatever the rim's inertia (fitPulse). It covers the report out, the motor, and the angle's way back through the browser, which is the loop a stiff force has to be stable through.
  • angleRate: how often the angle the page reads changes while the rim turns. Pass a function that reads the device afresh on every call; WheelInput.readAngleDeg() does.
  • inertia: the rim's, from the same pulses (a little high, as the firmware's own friction is not taken off).
  • timeoutExpires and timeoutRenews: whether stop on stall works on this wheel (see Safety first).

The wheel's rotation has to leave room to turn: with it matched to a car with a short lock (the kart), the pulses stop at once and give no fit.

The sandbox's wheel panel ​

Wheel setup & assists in the sandbox is a working example of all of the above: assign and calibrate controls, connect the wheel (protocol from the model, or forced; update mode), Test pulse, Measure damping, Measure loop, Stop on stall, Fast wheel loop with the live count of loop passes per second, the live count of reports sent per second, Gain, Max output, Column feel (columnScale, 0 to 4×), Road detail (detail, 0 to 4×), invert, a live graph of the torque requested and sent, Sub-frame playout at 250, 360 or 500 Hz, Slew and Smoothing with Direct drive and Gear / belt presets, Match rotation to car, Re-enable after the runaway guard trips, the diagnostics log, and the assists.

The panel starts with the settings that were driven on a G PRO: gain 0.5, max output 40 %, Column feel 2×, Road detail 1×, the Direct drive slew and smoothing, and Match rotation to car on. Fast wheel loop is on as well (not yet driven on a wheel); turning it off goes back to 60 Hz steps with Sub-frame playout at 500 Hz. On a belt or gear wheel, press Gear / belt.

Other makers ​

Protocols for other wheels belong in separate packages that implement FfbSink, so the core and the input package stay clean-room. A desktop build can use the same sinks over node-hid in Electron.

Touch ​

TouchInput is an on-screen control model for phones and tablets: the left half of the screen is a steering strip (drag sideways, returns to centre), the right half a pedal column (upper half throttle, lower half brake).

MIT OR Apache-2.0. Built in public.