Home / Modules / Actor Motion / AirplaneModelController

AirplaneModelController

three

Applies flight motion state to airplane visual transforms, including position, yaw, pitch, and roll.

CategoryActor Motion
Dependency tierthree
Internal depsWorldBasis
Related modulesnone
Demo sceneflight · Flight ↓
Take just this module
modules/actor-motion/aircraft/AirplaneModelController.js

Copy it together with its internal dependencies, preserving the relative directory structure.

Flight

three

Auto-piloting — or click and use arrow keys to steer, Space to boost.

Source

import { Matrix4 } from 'three';
import { DEFAULT_WORLD_BASIS } from '../../math/WorldBasis.js';

export class AirplaneModelController {
  constructor(planeModel = null, jetFlames = [], basis = DEFAULT_WORLD_BASIS) {
    this.planeModel = planeModel;
    this.jetFlames = jetFlames;
    this.basis = basis;

    this.modelMatrix = new Matrix4();
  }

  reset() {
    if (!this.planeModel) return;
    this.planeModel.position.set(0, 0, 0);
    this.planeModel.quaternion.identity();
  }

  step({
    position,
    yaw,
    pitch,
    roll,
    throttle,
    isBoosting,
    elapsedTimeSeconds,
    deltaSeconds = 1 / 60
  }) {
    if (!this.planeModel) return;

    this.planeModel.position.set(position.x, position.y, position.z);

    const frame = this.basis.yawPitchRollFrame(yaw, pitch, roll);
    this.modelMatrix.makeBasis(frame.right, frame.up, frame.back ?? frame.forward.clone().negate());
    this.planeModel.quaternion.setFromRotationMatrix(this.modelMatrix);

    for (const flame of this.jetFlames) {
      flame.step({
        throttle,
        isBoosting,
        timeSeconds: elapsedTimeSeconds,
        deltaSeconds
      });
    }
  }
}