Controller+BETADownload

Rally Court

A local 3D tennis example for the Remote. Your phone is the racket: the racket on court turns exactly as the phone turns, the way its strings face picks where the ball goes, and a swing hits it. One player faces a bot; two play singles; choose doubles for two teams, with bots filling empty seats. Seats alternate teams: 1 and 3 face 2 and 4. Bot difficulty is easy, normal or hard.

Run with Node 22 or newer after installing the SDK's dependencies:

node examples/games/tennis-host.mjs

Open the local link printed by the command, normally http://127.0.0.1:49100/. Connect a phone, or tick Keyboard / gamepad, and press Play. There is no build step or runtime CDN. The example uses ports 49100–49199 and binds to loopback. Its local phone link is suitable for browser tests on the same computer. Real phones need a host reachable from the phone and a secure browser context for motion; integrate the game with your normal SDK room for that setup.

Play with Remote

  1. Tap MOTION on the phone and allow access. Hold the phone like a racket handle: top up, screen facing you, facing the TV. The racket on court follows the phone from the first second, in the lobby too, and a ring on the far court shows where your shot would land. CENTER sets straight ahead.
  2. Swing the phone, or tap Play, to start. After a two-second count it is your serve: swing the phone, or tap Serve. A serve always lands in the diagonal box; the way the racket faces picks wide or down the middle.
  3. Your player runs to each ball for you. A thin ring closes on the ball as it arrives: swing as it reaches you. Turn the racket left or right to aim, tip it back to lob. A faster swing hits a faster ball; good timing places it better.

With motion off, drag the glass to aim, tap Serve to serve and Swing as the ball comes. With two buttons, Lob lobs. The TV and the phone's screen say when motion is off or blocked and what to tap.

Play with a gamepad or keyboard

On the phone gamepad, A serves, A hits as the ball comes, B lobs, and the left stick or D-pad aims. On the host, Space serves and hits, Shift lobs, and the arrows or A and D aim. A physical gamepad uses the same local seat. At match end, tap Play again on Remote, A or START on the gamepad, or Enter on the keyboard.

Rules and help

Points count 15, 30, 40 with no advantage: at 40–40 the next point wins the game. First to three games wins the match. Service changes every game and rotates through all four doubles seats.

The game helps without taking over. It predicts where your player meets the ball, runs them there and brings the racket to the ball. A phone stroke that starts or peaks from 0.25 s before to 0.3 s after that moment hits; a key hits from 0.65 s before to 0.3 s after. The TV shows the ball about a tenth of a second after the game moves it, so Perfect, Early and Late are judged against what you see. The aim is the face you held calmly before the swing, through the Racket Demo's own aim (faceAngles, lift and wallAim in racket-assist.mjs): twenty degrees of turn reaches a corner, the game learns your resting lean, and the open court pulls a nearby aim toward it. Shots clear the net and land in unless the face was held far off: tipped hard down goes into the net, tipped far back goes long. The TV says which, in one line.

The bot never misses the first shots of a match, rarely misses the first returns of a point, misses more in long rallies and against strong shots, and misses more when the person it plays is behind. It runs at its own speed, so a well-placed shot can beat it. Between people, a side that is a game or two points behind gets timing windows 0.08 s wider at both ends.

Use it in a host

In a browser host served through serveControllerAssets, supply your existing room, players, canvas, playerId and playButton. This fragment owns the tennis simulation and shared scene; call tick from your game loop and destroy the scene on exit.

import {createGame} from '/sdk/examples/games/index.mjs';
import {createScene} from '/sdk/examples/games/scene.mjs';
import * as tennis from '/sdk/examples/games/tennis.mjs';

const game = createGame('rally-court', players, 42, {
  mode: 'doubles', difficulty: 'normal',
});
const scene = createScene(canvas);
scene.start();
const controls = tennis.connect(room, game.state, {autoStart: false});
// Inputs, strokes, phone views, rumble and reconnect resets use connectGame.
function tick(dt) {
  controls.tick(dt);
  scene.push(structuredClone(game.state.public));
}
// For a lobby before creating the match:
function showLobby() {
  room.view(playerId, tennis.lobbyView());
  scene.setLobby({gameId: 'rally-court', title: 'Rally Court', players});
}
// Attach tennis.inputPreview(latestInput) to each lobby player to show their racket
// following the phone before the match exists.
playButton.onclick = () => scene.enableAudio();
// On exit:
// controls.close(); scene.destroy();

The game reads each phone's live sensor frames: motion.orientation turns the racket, and motion.gyro finds strokes (from 3 rad/s, ending below 2, the Remote's own swing speeds). It also accepts the Remote's swing event, a thinned-frame fallback, and remote-center. Keep the phone's gesture t unchanged; it orders a phone's own events and is never compared with game time. Hits produce private, power-scaled rumble and the phone's hit sound through the standard binding. A host that owns its own loop can drain feedback(prev, next) instead. view sends the score and one instruction in the three-line instruments.lcd contract, and promptFor(state, player, {mode, motion}) returns the same short instruction for a TV.

The public state is plain JSON and seeded randomness lives in rngBox. restoreGame restores a saved match, including strokes and keys in progress. Physics uses a 120 Hz fixed step; host deltas up to 250 ms are accepted. The module also exports render(ctx, state, opts) for a normal game host. When the host supplies its composed scene through opts.fx, render reuses it without opening a second WebGL renderer. Standalone callers load the same scene lazily. The scene loads the vendored three.js modules only for renderer: 'tennis-3d' and composites into the host's own canvas, so other game renderers keep working. Between snapshots scene.mjs asks the game's interpolate, which moves the ball with the game's own physics (it touches the court on every bounce) and turns the racket smoothly. WebGL2 is required; an unavailable context gives a visible error. WebAudio effects are synthesized locally. Sound needs a user gesture, and physical rumble depends on the phone/browser's support and settings.

On the TV

The camera sits high behind your baseline with a long lens, so the whole court and the far player stay large. When people play on both teams, the screen splits and each side sees the match from behind its own player, so turning right always aims right. One short line at the bottom says what to do next; it goes quiet once you are rallying. A ring on the far court shows your aim, a small ring shows where the incoming ball will land, and a thin ring closes on the ball as it reaches you. Perfect, Early or Late appears at the racket after each hit. A key press that hits no ball still swings the racket, so keys visibly work from the countdown on.

The scene is cheap to draw: the whole stadium is one merged mesh, every player part is an instance shared by all players, only moving things cast shadows into one 1024-pixel map that covers the court, and the crowd cheers in its vertex shader. It draws at up to 1920 × 1080 pixels and scales to the canvas. The Reduce Motion setting removes the trail, the crowd's cheering and the contact flash, and slows the camera.

Checks

node --test tests/tennis.test.mjs tests/games.test.mjs tests/games-solo.test.mjs
npm run typecheck
CONTROLLER_PLUS_TEST_TMP=/path/to/work TENNIS_ARTIFACTS=/path/to/screenshots \
  node --test tests/tennis-start-browser.test.mjs

The unit tests cover scoring, serving, the timing windows (keys and phone strokes, with Perfect, Early and Late), aiming by the racket face, faults and their explanations, net clearance from any meeting point, the bot's help, every word the phone shows, JSON restore and the in-between drawing. The browser test uses one headless Chrome, a real browser phone and synthetic DOM motion: it taps MOTION through denied and granted permission, checks the racket follows the phone in the lobby, serves with a swing, returns the first ball with a swing within ten seconds of Play, checks every Remote layout, plays with the keys and motion off, and serves from the host keyboard. Synthetic sensor events cannot validate real grip, sensor alignment, Safari permission prompts, sound quality or physical vibration.

Vendored three.js is covered by its MIT license.

All start pages · Search developer docs · Ship a game