mwg API
    Preparing search index...

    Class Cloth

    A small pinned-point cloth mesh: Verlet integration over a grid of points joined by structural distance constraints, the top row pinned by default. Renderer-free arithmetic (roadmap 423): no physics engine, no allocations per step, deterministic for a fixed update(dt) sequence, so a frame budget is exact rather than hoped for - one update costs O(points + links), two relaxation passes over structural links only, never shear or bend constraints and never a full cloth simulator. A skirt, tunic or scarf follows its character with a slight lag; ClothSprite draws the mesh on the GPU path 414 ships.

    import { Cloth } from '@datamoc/mwg/physics';

    const scarf = new Cloth({ width: 60, height: 20, segmentsX: 6, segmentsY: 2, gravity: { x: 0, y: 900 } });
    for (let i = 0; i < 60; i++) scarf.update(1 / 60); // one fixed second
    console.log(scarf.pointPosition(scarf.indexOf(3, 2)).y > 20); // true - the free end sags down
    console.log(scarf.pointPosition(scarf.indexOf(0, 0))); // { x: 0, y: 0 } - the pinned edge never moves
    Index
    damping: number
    segmentsX: number
    segmentsY: number
    stiffness: number
    • the flat index of the point at (column, row); throws a RangeError outside the grid

      Parameters

      • column: number
      • row: number

      Returns number

    • whether a point is pinned (integration and relaxation both leave it alone)

      Parameters

      • index: number

      Returns boolean

    • index pairs joined by a structural constraint, row links then column links, for drawing the mesh

      Returns (readonly [number, number])[]

    • the current position of one point

      Parameters

      • index: number

      Returns { x: number; y: number }

    • replace the gravity acceleration

      Parameters

      • x: number
      • y: number

      Returns void

    • replace the wind acceleration; validated, so a gust can never smuggle in a NaN

      Parameters

      • x: number
      • y: number

      Returns void

    • release a pinned point; it keeps its current position and moves from there

      Parameters

      • index: number

      Returns void

    • Advance the mesh by dt seconds: one Verlet integration pass, then two relaxation passes over the structural links. Call with a fixed dt (a FixedStepper does that) for a deterministic sequence; keep it small (60 Hz steps), since Verlet answers a huge step with a huge swing rather than an error. A zero dt holds still; a negative or non-finite one throws a RangeError.

      Parameters

      • dt: number

      Returns void

    • Write every current position into into, which must hold exactly pointCount * 2 floats. The mesh side calls this with its GPU position array.

      Parameters

      • into: Float32Array

      Returns void