Boat buoyancy in Three.js

A height sample at the centre of a boat can make it bob. To get pitch and roll, the bow, stern and both sides need to respond to the wave beneath them. This example fits the Tidewater launch to an existing Three.js scene and adds keyboard controls.

A white launch floats beside a timber jetty in the Tidewater harbour.
The same launch model in the harbour demo.

Fit the hull

Start with a Scene, a PerspectiveCamera and an initialised renderer from Tidewater’s createRenderer(). Use the import map from the Three.js water setup guide. This example assumes the unpacked kit is in ./tidewater/ beside your game’s page. If you already created a Tidewater runtime for this scene, reuse it.

import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import { createWater } from "tidewater/Tidewater.js";

const waterLevel = 0;
const water = await createWater({
  scene, camera, renderer, waterLevel,
  mode: "tranquil", quality: "med",
});

const { scene: model } = await new GLTFLoader().loadAsync(
  "./tidewater/assets/models/custom/tidewater-launch.glb",
);
const boat = water.addBoat(model, {
  kind: "rigid",
  forward: "z",
  length: 8.6,
  waterline: 0,
  draftFrac: 0.075,
  smoothNormals: false,
});

boat.object.position.set(0, waterLevel, 0);

Work in metres with Y up. The supplied launch has its bow along +Z and its authored waterline at model-space Y = 0. The runtime fits it into boat.object and adds that pivot to the scene. Set the spawn position on the pivot so it starts at the surface.

  • forward is the direction the bow faces. Use "-z" for a model pointing along negative Z. "auto" guesses from the longer horizontal axis.
  • length scales the model to the specified length in metres. Omit it if the model is already to scale. draftFrac sets the design draft as a fraction of the measured hull length. Draft is the distance from the waterline down to the keel.
  • waterline sets the model’s seating offset. Omit it to seat the hull from its keel and design draft. To use a known mass, pass mass in kilograms. The default mass floats the rigid hull at its design draft.

Drive the boat

Leave the boat at rest for a few seconds and check the waterline from the side. Then drive with W and S and steer with A and D. Both inputs run from −1 to 1. Put the frame body in your existing render loop if you already have one.

const keys = new Set();
const input = new AbortController();
const options = { signal: input.signal };
window.addEventListener("keydown", (e) => keys.add(e.code), options);
window.addEventListener("keyup", (e) => keys.delete(e.code), options);
window.addEventListener("blur", () => keys.clear(), options);

let previous = performance.now();
renderer.setAnimationLoop((now) => {
  const dt = Math.min((now - previous) / 1000, 0.05);
  previous = now;

  boat.setInput({
    throttle: Number(keys.has("KeyW")) - Number(keys.has("KeyS")),
    steer: Number(keys.has("KeyD")) - Number(keys.has("KeyA")),
  });
  water.update(dt);
  renderer.render(scene, camera);
});

Pass delta time in seconds. water.update() advances the waves and buoyancy and updates the wake before the frame is drawn. On level exit, call input.abort() and water.removeBoat(boat) to remove the listeners and the hull.

Check the fit

If the hull sits too high on calm water, check the authored waterline and the measured boat.length, boat.width and boat.draft before changing damping. A wrong scale changes the buoyancy measurements as well as the rendered size. If it drives stern-first, correct forward on the model.

If the surface has a gap beside the hull, try mask: false when registering the boat. If the gap closes, turn masking back on and shrink maskScale so the mask stays inside the hull. Its components are width, height and length.

Dock collisions belong in your game’s physics world. The hull mask keeps water out of the interior, and the buoyancy solver handles contact with the water.

Once the hull follows the water, record where its stern passes through a turn. Build a boat wake in Three.js.