logo
By Lawrence

5 minutes

Make the Ship Move on Its Own

Last lesson the falcon learned which way it points. It still doesn't go anywhere. Set ry, reload, and the ship is at the new heading — it never travelled there. Changing a pose by hand is teleporting, and a fighter that teleports isn't flying.

Flying needs two things this scene doesn't have: a velocity, and something that adds that velocity to the pose every frame, on its own.

Add Velocity6DOF next to the transform

Give the player a Velocity6DOF next to its Transform6DOF:

"Player": {
  "tags": ["player"],
  "components": {
    "Transform6DOF": { "x": 0, "y": 0, "z": 0, "rx": 0, "ry": 0, "rz": 0 },
    "Velocity6DOF": {},
    "Renderable": {
      "shape": "spaceplane", "sx": 3, "sy": 1, "sz": 4,
      "r": 0.55, "g": 0.62, "b": 0.78, "emissive": 0.18,
      "selfShadow": true, "glbSrc": "/spaceshooter/falconship.glb"
    }
  }
}

Empty braces mean the ship starts at rest. Transform6DOF says where the falcon is; Velocity6DOF says where it's going, and it carries two separate velocities:

src/components/Velocity6DOF/Velocity6DOF.defs.ts
export class Velocity6DOFComponent extends Component {
  vx: number;
  vy: number;
  vz: number;
  wx: number;
  wy: number;
  wz: number;

  constructor(data: Partial<Velocity6DOFInput> = {}) {
    super();
    const p = { ...DEFAULT_VELOCITY6DOF_PARAMS, ...data };
    this.vx = p.vx;
    this.vy = p.vy;
    this.vz = p.vz;
    this.wx = p.wx;
    this.wy = p.wy;
    this.wz = p.wz;
  }

Six numbers, and the two halves live in different reference frames on purpose.

vx / vy / vz is linear velocity in world space, units per second. Keeping it in world space is what makes the ship feel like it's in space: coast forward, yaw hard, and you keep sliding along the old line while the nose swings away. There's no air out here to bend your path for you. (It also means next lesson's flight law has to rotate nose-direction thrust into world space before it can land here.)

Continue reading

Unlock the Full Course

Every lesson, the runnable examples, and the finished build — yours to keep.

$9one-time

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search