Two browser games steered by tilting your MacBook. A small Python server reads the Mac's built-in IMU (gyroscope + accelerometer) and streams roll/pitch over WebSocket; the games subscribe and use it as input.
Tested on a MacBook Air M4 (Apple Silicon, macOS).
imu_server.py— reads the Bosch BMI286 IMU viamacimuand broadcasts tilt at 60 Hz onws://localhost:8765bike_game.html— endless bike-riding game; lean left/right to steermarble_tilt.html— marble-in-a-maze game; tilt in any direction to roll
Both pages also accept keyboard input (arrow keys) so they work without the server running.
python3 -m venv .venv
source .venv/bin/activate
pip install macimu websocketsThe IMU server needs root to open the IOKit HID device:
sudo .venv/bin/python3 imu_server.pyHold the Mac flat during the ~1 s calibration, then open one of the HTML files directly in a browser. The page connects to the WebSocket automatically.
bike_game.html
- Tilt left/right (or arrow keys) to steer
Space— action (see in-game prompt)I— invert tilt direction
marble_tilt.html
- Tilt in any direction (or arrow keys) to roll the marble
With the MacBook lying flat, screen up, hinge away from you:
| axis | meaning |
|---|---|
| accel.x | left/right (positive = right side down) |
| accel.y | front/back (positive = front tilted down) |
| accel.z | up/down (≈ -1 g when flat) |
Roll and pitch are derived from the accelerometer; raw gyro values are also included in the WebSocket payload for future use.