Skip to content

Ammo.js Physics ​

Integrates the ammo.js physics engine with the ecs package.

  • click (without dragging) or press Space to shoot a ball
  • drag with the left mouse button to look around, W A S D to move
  • Enter resets the scene, the panel raises the wrecking ball
ts
import {
  CameraComponent,
  EcsGame,
  KeyboardInputSystem,
  LightComponent,
  ModelComponent,
  MouseInputSystem,
  TransformComponent,
  WASDComponent,
} from '@gglib/components'
import { CommonMaterial, IblSampler, skyboxMaterial, TonemapOperator } from '@gglib/effects'
import { KeyboardKeys, MouseListener } from '@gglib/game'
import { boxGeometry, PlatformId, TRUE } from '@gglib/graphics'
import { HDR } from '@gglib/loaders'
import {
  IVec3,
  IVec4,
  mat3CreateIdentity,
  mat4$initScaleXYZ,
  mat4Identity,
  quatCreateAxisAngle,
  vec3,
  vec3Add,
  vec3MultiplyScalar,
  vec4,
} from '@gglib/math'
import { Model } from '@gglib/model'
import { opaquePassMSAA, Renderer, TonemapPass, transparentPassMSAA } from '@gglib/render'
import Ammo from 'ammojs-typed'
import { mount } from 'tweak-ui'
import { ShapeComponent } from './game-object'
import { HingeJoint, PointJoint } from './physics-constraints'
import { PhysicsProxy, PhysicsShape } from './physics-proxy'
import { PhysicsWorld } from './physics-world'

export default async (canvas: HTMLCanvasElement, tools: HTMLElement, platform: PlatformId) => {
  await Ammo.call({}, Ammo)
  const game = new Game({ canvas, platform, autosize: true })
  await game.run()

  const panel = mount(tools, (ui) => {
    ui.button('Reset', { onClick: () => game.resetScene() })
    ui.button('Wrecking ball', { onClick: () => game.raiseWreckingBall() })
  })

  return () => {
    game.destroy()
    panel.dispose()
  }
}

const NO_ROTATION = vec4(0, 0, 0, 1)
const QUARTER_TURN = quatCreateAxisAngle(vec3(0, 1, 0), Math.PI / 2)

const BALL_COUNT = 30
const BALL_SPEED = 40

// the chain hangs next to the tower, the wrecking ball at its end
const CHAIN_ANCHOR = vec3(6, 13, 0)
const CHAIN_LINKS = 6
const LINK_LENGTH = 1.2

/**
 * A body and where it starts after a reset
 */
interface Placement {
  proxy: PhysicsProxy
  position: IVec3
  rotation: IVec4
}

class Game extends EcsGame {
  private keyboard!: KeyboardInputSystem
  private mouse!: MouseInputSystem
  private camera!: CameraComponent
  private ibl!: IblSampler

  private placements: Placement[] = []
  private chain: PhysicsProxy[] = []
  private balls: PhysicsProxy[] = []
  private nextBall = 0
  private clickX = 0
  private clickY = 0

  private palette!: {
    ground: CommonMaterial
    wall: CommonMaterial
    ball: CommonMaterial
    wood: CommonMaterial
    colors: CommonMaterial[]
  }

  public override onCreate(): void {
    const canvas = this.device.canvas as HTMLCanvasElement
    this.keyboard = new KeyboardInputSystem()
    this.mouse = new MouseInputSystem({ provider: new MouseListener({ eventTarget: canvas, captureTarget: canvas }) })
    this.world.addSystem(this.keyboard)
    this.world.addSystem(this.mouse)
    this.world.addSystem(new PhysicsWorld())
    // the materials render in linear HDR, the tonemap pass maps the result to the screen
    this.world.addSystem(
      new Renderer(this.device, {
        linearToSrgb: false,
        pipeline: {
          passes: [
            opaquePassMSAA(),
            transparentPassMSAA(),
            new TonemapPass(this.device, { enabled: true, operator: TonemapOperator.PBR_NEUTRAL, srgb: true }),
          ],
        },
      }),
    )
  }

  protected override async onLoadContent() {
    this.content.registerLoader(HDR.Loader)

    // filters the panorama into the maps for image based lighting
    this.ibl = new IblSampler(this.device, {})
    await this.ibl.compiled
    this.ibl.update(await this.content.loadTexture('/textures/hdr/footprint_court.hdr'))

    const inputs = this.world.getSystem(Renderer).inputs
    inputs.createBlock('ibl')
    inputs.setByBlockAndName('ibl', 'intensity', 1)
    inputs.setByBlockAndName('ibl', 'rotation', mat3CreateIdentity())
    inputs.setByBlockAndName('ibl', 'brdfMap', this.ibl.lutMapGGX)
    inputs.setByBlockAndName('ibl', 'radianceMap', this.ibl.envMapGGX)
    inputs.setByBlockAndName('ibl', 'irradianceMap', this.ibl.envMapLambert!)
    inputs.setByBlockAndName('ibl', 'mipCount', this.ibl.envMapGGX.mipLevelCount)

    this.createSkybox()
    await this.createMaterials()
    this.createCamera()
    this.createLight()
    this.createArena()
    this.createTower(vec3(0, 0.5, 0))
    this.createPyramid(vec3(-9, 0.5, -3))
    this.createDoor(vec3(6, 0.5, 6))
    this.createChain()
    this.createLooseShapes(16)
    this.resetScene()
  }

  private createSkybox() {
    this.createEntity({
      name: 'Sky',
      parent: this.scene.entity,
      components: [
        new ModelComponent({
          model: new Model(this.device, {
            meshes: [
              {
                geometries: [boxGeometry(this.device, { name: 'Skybox', invert: true })],
                materials: [skyboxMaterial(this.device, { cubemap: this.ibl.envMapGGX, blur: 0.3, intensity: 1 })],
                parts: [{ geometryIndex: 0, materialIndex: 0 }],
              },
            ],
          }),
        }),
      ],
    })
  }

  private async createMaterials() {
    const material = (roughness: number, metallic = 0) => {
      const result = new CommonMaterial(this.device, { properties: {} })
      result.Roughness = roughness
      result.Metallic = metallic
      result.UseIBL = TRUE
      result.UseLights = TRUE
      return result
    }
    /**
     * A prototype grid texture, repeated `tiles` times on each face
     */
    const prototype = async (color: string, texture: string, tiles = 1, roughness = 0.6) => {
      const result = material(roughness)
      result.BaseMap = await this.content.loadTexture(`/textures/prototype/${color}/texture_${texture}.png`, {
        color: 'srgb',
      })
      result.UseBaseMap = TRUE
      if (tiles !== 1) {
        result.TextureMod = mat4$initScaleXYZ(mat4Identity(), tiles, tiles, 1)
        result.UseTextureMod = TRUE
      }
      return result
    }
    const chrome = material(0.15, 1)
    chrome.BaseColor = vec3(0.9, 0.9, 0.92)

    const [ground, wall, wood, ...colors] = await Promise.all([
      prototype('dark', '01', 25, 0.9),
      prototype('light', '01', 10, 0.8),
      prototype('orange', '08', 1, 0.7),
      prototype('red', '02', 1, 0.5),
      prototype('orange', '02', 1, 0.4),
      prototype('green', '02', 1, 0.6),
      prototype('purple', '02', 1, 0.3),
      prototype('light', '02', 1, 0.3),
      prototype('dark', '02', 1, 0.5),
    ])
    this.palette = { ground, wall, ball: chrome, wood, colors }
  }

  private createCamera() {
    const entity = this.createEntity({
      name: 'Camera',
      parent: this.scene.entity,
      transform: new TransformComponent({ position: vec3(1, 7, 16) }),
      components: [new CameraComponent({ type: 'perspective', far: 500 }), new WASDComponent()],
    })
    // look down at the tower
    entity.component(WASDComponent).targetVertical = -0.25
    this.camera = entity.component(CameraComponent)
    this.scene.setCamera(0, this.camera)
  }

  private createLight() {
    this.createEntity({
      name: 'Sun',
      parent: this.scene.entity,
      transform: new TransformComponent({ rotation: quatCreateAxisAngle(vec3(1, 0.3, 0), -1) }),
      components: [new LightComponent()],
    })
  }

  /**
   * Creates an entity with a physics body and a model of the same shape
   */
  private createBody(
    name: string,
    shape: PhysicsShape,
    size: IVec3,
    material: CommonMaterial,
    mass: number,
    ...components: any[]
  ): PhysicsProxy {
    const proxy = new PhysicsProxy({ shape, size, mass, dynamic: mass > 0, ccd: shape === PhysicsShape.Sphere })
    this.createEntity({
      name,
      parent: this.scene.entity,
      components: [new ModelComponent(), proxy, new ShapeComponent(material), ...components],
    })
    return proxy
  }

  /**
   * Creates a body that returns to the given position on reset
   */
  private place(proxy: PhysicsProxy, position: IVec3, rotation: IVec4 = NO_ROTATION) {
    this.placements.push({ proxy, position, rotation })
    return proxy
  }

  private createArena() {
    const { ground, wall } = this.palette
    this.place(this.createBody('Ground', PhysicsShape.Box, vec3(100, 1, 100), ground, 0), vec3(0, 0, 0))
    for (const [x, z, w, d] of [
      [0, -50, 100, 2],
      [0, 50, 100, 2],
      [-50, 0, 2, 100],
      [50, 0, 2, 100],
    ]) {
      this.place(this.createBody('Wall', PhysicsShape.Box, vec3(w, 2, d), wall, 0), vec3(x, 1, z))
    }
  }

  /**
   * Layers of three bricks, each layer turned by 90 degrees
   */
  private createTower(base: IVec3) {
    const layers = 12
    const height = 0.8
    for (let layer = 0; layer < layers; layer++) {
      const turned = layer % 2 === 1
      for (let i = -1; i <= 1; i++) {
        const material = this.palette.colors[(layer + i + 1) % this.palette.colors.length]
        const brick = this.createBody('Brick', PhysicsShape.Box, vec3(3, height, 1), material, 1)
        const y = base.y + height / 2 + layer * height
        this.place(
          brick,
          turned ? vec3(base.x + i, y, base.z) : vec3(base.x, y, base.z + i),
          turned ? QUARTER_TURN : NO_ROTATION,
        )
      }
    }
  }

  /**
   * Rows of cylinders, each row on top of the gaps of the row below, cones on top
   */
  private createPyramid(base: IVec3) {
    const rows = 5
    const size = vec3(1, 1.2, 1)
    for (let row = 0; row < rows; row++) {
      const count = rows - row
      for (let i = 0; i < count; i++) {
        const top = row === rows - 1
        const proxy = top
          ? this.createBody('Cone', PhysicsShape.Cone, vec3(0.5, 1.2, 0.5), this.palette.colors[1], 0.5)
          : this.createBody('Barrel', PhysicsShape.Cylinder, size, this.palette.colors[row % 3], 1)
        this.place(proxy, vec3(base.x + (i - (count - 1) / 2) * 1.05, base.y + 0.6 + row * 1.2, base.z))
      }
    }
  }

  /**
   * A door on a static post, connected with a hinge that swings up to 90 degrees each way
   */
  private createDoor(base: IVec3) {
    const post = this.createBody('Post', PhysicsShape.Box, vec3(0.4, 6, 0.4), this.palette.wall, 0)
    this.place(post, vec3(base.x, base.y + 3, base.z))

    const width = 4
    const door = this.createBody(
      'Door',
      PhysicsShape.Box,
      vec3(width, 5.5, 0.3),
      this.palette.wood,
      3,
      new HingeJoint(post, {
        // the left edge of the door turns around the right side of the post
        pivot: vec3(-width / 2 - 0.1, 0, 0),
        otherPivot: vec3(0.3, 0, 0),
        axis: vec3(0, 1, 0),
        limit: Math.PI / 2,
      }),
    )
    // same height as the post, so the hinge pivots match
    this.place(door, vec3(base.x + 0.3 + width / 2 + 0.1, base.y + 3, base.z))
  }

  /**
   * Links that hang from a static anchor, each linked to the one above, with a heavy ball at the end
   */
  private createChain() {
    const anchor = this.createBody('Anchor', PhysicsShape.Sphere, vec3(0.3), this.palette.wall, 0)
    this.place(anchor, CHAIN_ANCHOR)

    const half = LINK_LENGTH / 2
    let above = anchor
    for (let i = 0; i < CHAIN_LINKS; i++) {
      const last = i === CHAIN_LINKS - 1
      const radius = last ? 1.2 : 0.3
      const link = this.createBody(
        last ? 'Wrecking ball' : 'Link',
        PhysicsShape.Sphere,
        vec3(radius),
        last ? this.palette.ball : this.palette.colors[4],
        last ? 30 : 0.5,
        new PointJoint(above, {
          // the top of this link hangs at the bottom of the one above
          pivot: vec3(0, last ? radius : half, 0),
          otherPivot: vec3(0, above === anchor ? 0 : -half, 0),
        }),
      )
      this.chain.push(link)
      above = link
    }
    this.hangChain(false)
  }

  /**
   * Places the chain links straight down, or sideways so the ball swings down into the tower
   */
  private hangChain(raised: boolean) {
    // the distance of each link center from the anchor, along the chain
    let offset = -LINK_LENGTH / 2
    this.chain.forEach((link, i) => {
      const last = i === this.chain.length - 1
      offset += last ? LINK_LENGTH / 2 + link.size.x : LINK_LENGTH
      const position = raised
        ? vec3(CHAIN_ANCHOR.x + offset, CHAIN_ANCHOR.y, CHAIN_ANCHOR.z)
        : vec3(CHAIN_ANCHOR.x, CHAIN_ANCHOR.y - offset, CHAIN_ANCHOR.z)
      link.setTransform(position, NO_ROTATION)
    })
  }

  private createLooseShapes(count: number) {
    const shapes = [
      { shape: PhysicsShape.Box, size: vec3(1.2) },
      { shape: PhysicsShape.Sphere, size: vec3(0.6) },
      { shape: PhysicsShape.Cylinder, size: vec3(1, 1.5, 1) },
      { shape: PhysicsShape.Cone, size: vec3(0.6, 1.4, 0.6) },
    ]
    for (let i = 0; i < count; i++) {
      const { shape, size } = shapes[i % shapes.length]
      const material = this.palette.colors[i % this.palette.colors.length]
      const proxy = this.createBody('Shape', shape, size, material, 1)
      this.place(proxy, vec3(8 + (i % 4) * 2.5, 12 + Math.floor(i / 4) * 3, -8 + (i % 3) * 2))
    }
  }

  public resetScene() {
    for (const it of this.placements) {
      it.proxy.setTransform(it.position, it.rotation)
    }
    this.hangChain(false)
  }

  /**
   * Lifts the chain away from the tower, it swings down into it
   */
  public raiseWreckingBall() {
    this.hangChain(true)
  }

  /**
   * Fires a ball from the camera along the given ray. The oldest ball is reused once all are in use
   */
  private shoot(xNormalized: number, yNormalized: number) {
    const ray = this.camera.createRay(xNormalized, yNormalized)
    if (this.balls.length < BALL_COUNT) {
      this.balls.push(this.createBody('Ball', PhysicsShape.Sphere, vec3(0.5), this.palette.ball, 5))
    }
    const ball = this.balls[this.nextBall++ % BALL_COUNT]
    // starts a bit in front of the camera, so it does not hit the camera near plane
    const start = vec3Add(ray.position, ray.direction)
    ball.launch(start, NO_ROTATION, vec3MultiplyScalar(ray.direction, BALL_SPEED))
  }

  public override onUpdate() {
    this.camera.aspect = this.device.output.aspectRatio

    if (this.keyboard.justPressed(KeyboardKeys.Enter)) {
      this.resetScene()
    }
    if (this.keyboard.justPressed(KeyboardKeys.Space)) {
      this.shoot(0.5, 0.5)
    }
    // dragging with the left button turns the camera, a click without moving the mouse shoots
    const mouse = this.mouse
    if (mouse.leftButtonJustPressed) {
      this.clickX = mouse.xNormalized
      this.clickY = mouse.yNormalized
    }
    if (mouse.leftButtonJustReleased) {
      const moved = Math.hypot(mouse.xNormalized - this.clickX, mouse.yNormalized - this.clickY)
      if (moved < 0.01) {
        this.shoot(mouse.xNormalized, mouse.yNormalized)
      }
    }
  }
}
ts
import { BehaviorComponent, TransformComponent } from '@gglib/components'
import { GameComponent, GameEntity } from '@gglib/ecs'
import { IVec3, IVec4, mat4Decompose, vec3, vec3$init, vec3$initFrom, vec4, vec4$init } from '@gglib/math'
import { brand, Brand } from '@gglib/utils'
import Ammo from 'ammojs-typed'
import { PhysicsWorld } from './physics-world'

export type PhysicsShape = Brand<number, 'shape'>
export const PhysicsShape = {
  Box: brand<PhysicsShape>(0),
  Sphere: brand<PhysicsShape>(1),
  Cylinder: brand<PhysicsShape>(2),
  Cone: brand<PhysicsShape>(3),
}

export interface PhysicsProxyOptions {
  mass: number
  size: IVec3
  dynamic: boolean
  shape: PhysicsShape
  // Enables continuous collision detection, so fast bodies do not pass through others
  ccd?: boolean
}

export class PhysicsProxy implements GameComponent, BehaviorComponent {
  public physics!: PhysicsWorld
  public transform!: TransformComponent

  /**
   * The rigid body, `null` until the component is initialized
   */
  public get body() {
    return this.rigidBody
  }

  /**
   * Whether the body exists and is at the position of the entity
   */
  public get isReady() {
    return !!this.rigidBody && !this.needsReset
  }

  private shape!: Ammo.btCollisionShape
  private rigidBody: Ammo.btRigidBody = null!
  private needsReset = false
  private velocity = vec3()
  private temp: Ammo.btVector3 | null = null
  public mass!: number
  public size!: IVec3
  public dynamic!: boolean
  public ccd!: boolean
  public shapeType!: PhysicsShape

  public constructor(options: PhysicsProxyOptions) {
    this.mass = options.mass ?? 0
    this.size = options.size ?? vec3(1)
    this.dynamic = !!options.dynamic
    this.ccd = !!options.ccd
    this.shapeType = options.shape ?? PhysicsShape.Box
  }

  public entity!: GameEntity
  public initialize(): void {
    this.physics = this.entity.service(PhysicsWorld)
    this.transform = this.entity.$(TransformComponent)

    const mass = this.mass
    const shape = createShape(this.shapeType, this.size)
    const localInertia = new Ammo.btVector3(0, 0, 0)

    if (this.dynamic) {
      shape.calculateLocalInertia(mass, localInertia)
    }

    const motionState = new Ammo.btDefaultMotionState()
    const rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, motionState, shape, localInertia)
    const body = new Ammo.btRigidBody(rbInfo)
    Ammo.destroy(rbInfo)
    Ammo.destroy(localInertia)
    if (this.ccd) {
      const radius = Math.min(this.size.x, this.size.y, this.size.z) / 2
      body.setCcdMotionThreshold(radius)
      body.setCcdSweptSphereRadius(radius * 0.8)
    }

    this.shape = shape
    this.rigidBody = body
    this.physics.world.addRigidBody(this.rigidBody)
    this.needsReset = true
  }

  public destroy(): void {
    if (this.rigidBody) {
      this.physics.world.removeRigidBody(this.rigidBody)
      Ammo.destroy(this.rigidBody)
      Ammo.destroy(this.shape)
      if (this.temp) {
        Ammo.destroy(this.temp)
      }
      this.rigidBody = null!
      this.shape = null!
    }
  }

  /**
   * Moves the body to the given position and rotation and stops it
   */
  public setTransform(position: IVec3, rotation: IVec4) {
    this.launch(position, rotation, ZERO)
  }

  /**
   * Moves the body to the given position and rotation and gives it a velocity
   */
  public launch(position: IVec3, rotation: IVec4, velocity: IVec3) {
    // may be called before initialize
    const transform = this.transform ?? this.entity.getTransform<TransformComponent>()
    transform.setPositionV(position)
    transform.setRotation(rotation)
    vec3$initFrom(this.velocity, velocity)
    this.needsReset = true
  }

  public updateBehavior() {
    const body = this.rigidBody
    if (body && this.needsReset) {
      this.needsReset = false

      const origin = body.getWorldTransform().getOrigin()
      const rotation = body.getWorldTransform().getRotation()

      this.transform.updateIfNeeded()
      mat4Decompose(
        this.transform.world,
        vec3.$0, // scale
        vec4.$0, // rotation
        vec3.$1, // position
      )
      origin.setValue(vec3.$1.x, vec3.$1.y, vec3.$1.z)
      // getRotation returns a copy, it must be written back
      rotation.setValue(vec4.$0.x, vec4.$0.y, vec4.$0.z, vec4.$0.w)
      body.getWorldTransform().setRotation(rotation)

      this.temp ||= new Ammo.btVector3(0, 0, 0)
      body.setLinearVelocity(setVector(this.temp, this.velocity))
      body.setAngularVelocity(setVector(this.temp, ZERO))
      body.activate()
    }

    if (body) {
      const t = body.getWorldTransform()
      const o = t.getOrigin()
      const r = t.getRotation()
      vec4$init(this.transform.rotation, r.x(), r.y(), r.z(), r.w())
      vec3$init(this.transform.translation, o.x(), o.y(), o.z())
      this.transform.markAsChanged()
    }
  }
}

const ZERO = vec3(0)

function setVector(target: Ammo.btVector3, value: IVec3) {
  target.setValue(value.x, value.y, value.z)
  return target
}

function createShape(type: PhysicsShape, size: IVec3) {
  switch (type) {
    case PhysicsShape.Box: {
      return new Ammo.btBoxShape(new Ammo.btVector3(size.x / 2, size.y / 2, size.z / 2))
    }
    case PhysicsShape.Sphere: {
      return new Ammo.btSphereShape(size.x)
    }
    case PhysicsShape.Cylinder: {
      return new Ammo.btCylinderShape(new Ammo.btVector3(size.x / 2, size.y / 2, size.z / 2))
    }
    case PhysicsShape.Cone: {
      return new Ammo.btConeShape(size.x, size.y)
    }
  }
  return new Ammo.btEmptyShape()
}
ts
import { BehaviorComponent } from '@gglib/components'
import { GameComponent, GameEntity, InitializableComponent } from '@gglib/ecs'
import { IVec3 } from '@gglib/math'
import Ammo from 'ammojs-typed'
import { PhysicsProxy } from './physics-proxy'
import { PhysicsWorld } from './physics-world'

/**
 * Connects the body of this entity with the body of another entity.
 *
 * @remarks
 * The constraint is created once both bodies exist and are placed
 */
abstract class JointComponent implements GameComponent, InitializableComponent, BehaviorComponent {
  public entity!: GameEntity
  private physics!: PhysicsWorld
  private self!: PhysicsProxy
  private constraint: Ammo.btTypedConstraint | null = null

  public constructor(protected other: PhysicsProxy) {}

  protected abstract create(self: Ammo.btRigidBody, other: Ammo.btRigidBody): Ammo.btTypedConstraint

  public initialize(): void {
    this.physics = this.entity.service(PhysicsWorld)
    this.self = this.entity.component(PhysicsProxy)
  }

  public updateBehavior(): void {
    if (this.constraint || !this.self.isReady || !this.other.isReady) {
      return
    }
    this.constraint = this.create(this.self.body, this.other.body)
    // linked bodies do not collide with each other
    this.physics.world.addConstraint(this.constraint, true)
  }

  public destroy(): void {
    if (this.constraint) {
      this.physics.world.removeConstraint(this.constraint)
      Ammo.destroy(this.constraint)
      this.constraint = null
    }
  }
}

/**
 * Rotates around one axis, like a door on its hinges
 */
export class HingeJoint extends JointComponent {
  public constructor(
    other: PhysicsProxy,
    private options: { pivot: IVec3; otherPivot: IVec3; axis: IVec3; limit: number },
  ) {
    super(other)
  }

  protected create(self: Ammo.btRigidBody, other: Ammo.btRigidBody) {
    const { pivot, otherPivot, axis, limit } = this.options
    const v = (it: IVec3) => new Ammo.btVector3(it.x, it.y, it.z)
    const hinge = new Ammo.btHingeConstraint(self, other, v(pivot), v(otherPivot), v(axis), v(axis))
    hinge.setLimit(-limit, limit, 0.9, 0.3, 1)
    return hinge
  }
}

/**
 * Keeps two points together and rotates freely, like the links of a chain
 */
export class PointJoint extends JointComponent {
  public constructor(
    other: PhysicsProxy,
    private options: { pivot: IVec3; otherPivot: IVec3 },
  ) {
    super(other)
  }

  protected create(self: Ammo.btRigidBody, other: Ammo.btRigidBody) {
    const { pivot, otherPivot } = this.options
    return new Ammo.btPoint2PointConstraint(
      self,
      other,
      new Ammo.btVector3(pivot.x, pivot.y, pivot.z),
      new Ammo.btVector3(otherPivot.x, otherPivot.y, otherPivot.z),
    )
  }
}
ts
import { GameSystem } from '@gglib/ecs'
import Ammo from 'ammojs-typed'

export class PhysicsWorld extends GameSystem {
  public readonly config = new Ammo.btDefaultCollisionConfiguration()
  public readonly dispatcher = new Ammo.btCollisionDispatcher(this.config)
  public readonly pairCache = new Ammo.btDbvtBroadphase()
  public readonly solver = new Ammo.btSequentialImpulseConstraintSolver()
  public readonly world = new Ammo.btDiscreteDynamicsWorld(
    this.dispatcher,
    this.pairCache as any,
    this.solver,
    this.config,
  )

  public initialize(): void {
    //
  }

  public destroy(): void {
    Ammo.destroy(this.world)
    Ammo.destroy(this.solver)
    Ammo.destroy(this.pairCache)
    Ammo.destroy(this.dispatcher)
    Ammo.destroy(this.config)
  }

  public override update(time: number, dt: number) {
    // fixed 120 Hz sub steps keep the stacks stable
    this.world.stepSimulation(dt, 10, 1 / 120)
  }
}
ts
import { ModelComponent } from '@gglib/components'
import { GameComponent, GameEntity } from '@gglib/ecs'
import { CommonMaterial } from '@gglib/effects'
import { boxGeometry, cylinderGeometry, Device, Geometry, Mesh, sphereGeometry } from '@gglib/graphics'
import { Model } from '@gglib/model'
import { PhysicsProxy, PhysicsShape } from './physics-proxy'

/**
 * One model per shape, size and material. Entities draw instances of it, so the renderer can batch them
 */
const models = new WeakMap<Device, Map<string, Model>>()

/**
 * Gives the entity a model that matches the shape of its {@link PhysicsProxy}
 */
export class ShapeComponent implements GameComponent {
  public renderable!: ModelComponent
  public entity!: GameEntity

  public constructor(private material: CommonMaterial) {}

  public initialize() {
    this.renderable = this.entity.component(ModelComponent)
    const device = this.entity.service(Device)
    const physics = this.entity.component(PhysicsProxy)
    const size = physics.size

    if (!models.has(device)) {
      models.set(device, new Map())
    }
    const cache = models.get(device)!
    const key = `${physics.shapeType} ${size.x} ${size.y} ${size.z} ${this.material.uid}`
    if (!cache.has(key)) {
      const geometry = createGeometry(device, physics.shapeType, size.x, size.y, size.z)
      const mesh = new Mesh(device, {
        geometries: [geometry],
        materials: [this.material],
        parts: [{ geometryIndex: 0, materialIndex: 0 }],
      })
      cache.set(key, new Model(device, { meshes: [mesh] }))
    }
    this.renderable.model = cache.get(key)!.instantiate()
  }

  public destroy(): void {
    this.renderable.model?.dispose()
    this.renderable.model = null!
  }
}

function createGeometry(device: Device, shape: PhysicsShape, x: number, y: number, z: number): Geometry {
  switch (shape) {
    case PhysicsShape.Sphere:
      return sphereGeometry(device, { radius: x })
    case PhysicsShape.Cone:
      return cylinderGeometry(device, { radius: x, height: y, topRadius: 0, closeBottom: true })
    case PhysicsShape.Cylinder:
      return cylinderGeometry(device, { radius: x / 2, height: y, closeTop: true, closeBottom: true })
    default:
      return boxGeometry(device, { width: x, height: y, depth: z })
  }
}

Released under the MIT License.