Skip to main content
Map gamepad (controller) axes and buttons to MuJoCo actuators. Supports deadzones and scaling.

Signature

useGamepad(config: {
  axes?: Record<number, string>;
  buttons?: Record<number, string>;
  deadzone?: number;
  scale?: number;
  gamepadIndex?: number;
  enabled?: boolean;
}): void

Usage

import { useGamepad } from "mujoco-react";

function GamepadControl() {
  useGamepad({
    axes: {
      0: "joint1",   // Left stick X → joint1
      1: "joint2",   // Left stick Y → joint2
      2: "joint3",   // Right stick X → joint3
      3: "joint4",   // Right stick Y → joint4
    },
    buttons: {
      0: "gripper",  // A button → gripper
    },
    deadzone: 0.1,
    scale: 1.0,
    gamepadIndex: 0,
    enabled: true,
  });

  return null;
}

Config

FieldTypeDefaultDescription
axesRecord<number, string>Axis index → actuator name
buttonsRecord<number, string>Button index → actuator name
deadzonenumber0.1Axis values below this are treated as zero
scalenumber1.0Multiplier for axis values
gamepadIndexnumber0Which gamepad to use (if multiple connected)
enabledbooleantrueEnable/disable gamepad input

Standard Gamepad Axes

IndexAxis
0Left stick X
1Left stick Y
2Right stick X
3Right stick Y

Standard Gamepad Buttons

IndexButton
0A / Cross
1B / Circle
2X / Square
3Y / Triangle
4Left bumper
5Right bumper
6Left trigger
7Right trigger

Notes

  • Uses the Gamepad API
  • Gamepad state is polled each frame via navigator.getGamepads()
  • Axes values are in the range [-1, 1] after deadzone filtering
  • Button values map to [0, 1] (pressed = 1)