Options
All
  • Public
  • Public/Protected
  • All
Menu

Class World

The physics world

Hierarchy

Index

Constructors

constructor

  • new World(options?: { allowSleep?: boolean; broadphase?: Broadphase; frictionGravity?: Vec3; gravity?: Vec3; quatNormalizeFast?: boolean; quatNormalizeSkip?: number; solver?: Solver }): World
  • Parameters

    • options: { allowSleep?: boolean; broadphase?: Broadphase; frictionGravity?: Vec3; gravity?: Vec3; quatNormalizeFast?: boolean; quatNormalizeSkip?: number; solver?: Solver } = {}
      • Optional allowSleep?: boolean

        Makes bodies go to sleep when they've been inactive.

        default

        false

      • Optional broadphase?: Broadphase

        The broadphase algorithm to use.

        default

        NaiveBroadphase

      • Optional frictionGravity?: Vec3

        Gravity to use when approximating the friction max force (mumassgravity). If undefined, global gravity will be used.

      • Optional gravity?: Vec3

        The gravity of the world.

      • Optional quatNormalizeFast?: boolean

        Set to true to use fast quaternion normalization. It is often enough accurate to use. If bodies tend to explode, set to false.

        default

        false

      • Optional quatNormalizeSkip?: number

        How often to normalize quaternions. Set to 0 for every step, 1 for every second etc.. A larger value increases performance. If bodies tend to explode, set to a smaller value (zero to be sure nothing can go wrong).

        default

        0

      • Optional solver?: Solver

        The solver algorithm to use.

        default

        GSSolver

    Returns World

Properties

accumulator

accumulator: number

Time accumulator for interpolation.

see

https://gafferongames.com/game-physics/fix-your-timestep/

addBodyEvent

addBodyEvent: {}

Dispatched after a body has been added to the world.

Type declaration

    allowSleep

    allowSleep: boolean

    Makes bodies go to sleep when they've been inactive.

    default

    false

    bodies

    bodies: Body[]

    All bodies in this world

    broadphase

    broadphase: Broadphase

    The broadphase algorithm to use.

    default

    NaiveBroadphase

    collisionMatrix

    collisionMatrix: ArrayCollisionMatrix

    collisionMatrix

    collisionMatrixPrevious

    collisionMatrixPrevious: ArrayCollisionMatrix

    CollisionMatrix from the previous step.

    contactMaterialTable

    contactMaterialTable: TupleDictionary

    Used to look up a ContactMaterial given two instances of Material.

    contactmaterials

    contactmaterials: ContactMaterial[]

    All added contactmaterials.

    contacts

    contacts: ContactEquation[]

    All the current contacts (instances of ContactEquation) in the world.

    defaultContactMaterial

    defaultContactMaterial: ContactMaterial

    This contact material is used if no suitable contactmaterial is found for a contact.

    defaultMaterial

    defaultMaterial: Material

    The default material of the bodies.

    default_dt

    default_dt: number

    Default and last timestep sizes.

    dt

    dt: number

    Currently / last used timestep. Is set to -1 if not available. This value is updated before each internal step, which means that it is "fresh" inside event callbacks.

    Optional frictionGravity

    frictionGravity?: Vec3

    Gravity to use when approximating the friction max force (mu * mass * gravity). If undefined, global gravity will be used. Use to enable friction in a World with a null gravity vector (no gravity).

    gravity

    gravity: Vec3

    The gravity of the world.

    hasActiveBodies

    hasActiveBodies: boolean

    True if any bodies are not sleeping, false if every body is sleeping.

    quatNormalizeFast

    quatNormalizeFast: boolean

    Set to true to use fast quaternion normalization. It is often enough accurate to use. If bodies tend to explode, set to false.

    default

    false

    quatNormalizeSkip

    quatNormalizeSkip: number

    How often to normalize quaternions. Set to 0 for every step, 1 for every second etc.. A larger value increases performance. If bodies tend to explode, set to a smaller value (zero to be sure nothing can go wrong).

    default

    0

    removeBodyEvent

    removeBodyEvent: {}

    Dispatched after a body has been removed from the world.

    Type declaration

      solver

      solver: Solver

      The solver algorithm to use.

      default

      GSSolver

      stepnumber

      stepnumber: number

      Number of timesteps taken since start.

      time

      time: number

      The wall-clock time since simulation start.

      Methods

      addBody

      • addBody(body: Body): void
      • Add a rigid body to the simulation.

        todo

        If the simulation has not yet started, why recrete and copy arrays for each body? Accumulate in dynamic arrays in this case.

        todo

        Adding an array of bodies should be possible. This would save some loops too

        Parameters

        Returns void

      addConstraint

      addContactMaterial

      addEventListener

      • addEventListener(type: string, listener: Function): EventTarget

      clearForces

      • clearForces(): void

      collisionMatrixTick

      • collisionMatrixTick(): void

      dispatchEvent

      fixedStep

      • fixedStep(dt?: number, maxSubSteps?: number): void
      • Step the simulation forward keeping track of last called time to be able to step the world at a fixed rate, independently of framerate.

        see

        https://gafferongames.com/post/fix_your_timestep/

        example
        // Run the simulation independently of framerate every 1 / 60 ms
        world.fixedStep()

        Parameters

        • dt: number = ...

          The fixed time step size to use (default: 1 / 60).

        • maxSubSteps: number = 10

          Maximum number of fixed steps to take per function call (default: 10).

        Returns void

      getContactMaterial

      hasAnyEventListener

      • hasAnyEventListener(type: string): boolean

      hasEventListener

      • hasEventListener(type: string, listener: Function): boolean

      rayTest

      raycastAll

      • raycastAll(from?: Vec3, to?: Vec3, options?: RayOptions, callback?: RaycastCallback): boolean
      • Ray cast against all bodies. The provided callback will be executed for each hit with a RaycastResult as single argument.

        Parameters

        • Optional from: Vec3
        • Optional to: Vec3
        • options: RayOptions = {}
        • Optional callback: RaycastCallback

        Returns boolean

        True if any body was hit.

      raycastAny

      • Ray cast, and stop at the first result. Note that the order is random - but the method is fast.

        Parameters

        Returns boolean

        True if any body was hit.

      raycastClosest

      removeBody

      • removeBody(body: Body): void

      removeConstraint

      removeContactMaterial

      removeEventListener

      • removeEventListener(type: string, listener: Function): EventTarget

      step

      • step(dt: number, timeSinceLastCalled?: number, maxSubSteps?: number): void