Class World

The physics world

Hierarchy (view full)

Constructors

  • Parameters

    • Optional 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: number

Time accumulator for interpolation.

addBodyEvent: {
    body: Body;
    type: "addBody";
}

Dispatched after a body has been added to the world.

Type declaration

  • body: Body
  • type: "addBody"
allowSleep: boolean

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

Default

false
bodies: Body[]

All bodies in this world

bodyOverlapKeeper: OverlapKeeper
broadphase: Broadphase

The broadphase algorithm to use.

Default

NaiveBroadphase
collisionMatrix: ArrayCollisionMatrix

collisionMatrix

collisionMatrixPrevious: ArrayCollisionMatrix

CollisionMatrix from the previous step.

constraints: Constraint[]
contactMaterialTable: TupleDictionary

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

contactmaterials: ContactMaterial[]

All added contactmaterials.

contacts: ContactEquation[]

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

defaultContactMaterial: ContactMaterial

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

defaultMaterial: Material

The default material of the bodies.

default_dt: number

Default and last timestep sizes.

doProfiling: boolean
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.

frictionEquations: FrictionEquation[]
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: Vec3

The gravity of the world.

hasActiveBodies: boolean

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

idToBodyMap: {
    [id: number]: Body;
}

Type declaration

lastCallTime?: number
narrowphase: Narrowphase
nextId: number
profile: {
    broadphase: number;
    integrate: number;
    makeContactConstraints: number;
    narrowphase: number;
    solve: number;
}

Type declaration

  • broadphase: number
  • integrate: number
  • makeContactConstraints: number
  • narrowphase: number
  • solve: number
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: 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: {
    body: Body;
    type: "removeBody";
}

Dispatched after a body has been removed from the world.

Type declaration

  • body: Body
  • type: "removeBody"
shapeOverlapKeeper: OverlapKeeper
solver: Solver

The solver algorithm to use.

Default

GSSolver
stepnumber: number

Number of timesteps taken since start.

subsystems: any[]
time: number

The wall-clock time since simulation start.

Methods

  • Add a rigid body to the simulation.

    Parameters

    Returns void

    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

  • Add a constraint to the simulation.

    Parameters

    Returns void

  • Adds a contact material to the World

    Parameters

    Returns void

  • Sets all body forces in the world to zero.

    Returns void

  • Store old collision state info

    Returns void

  • Returns 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.

    Parameters

    • Optional dt: number

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

    • Optional maxSubSteps: number

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

    Returns void

    See

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

    Example

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

    • id: number

    Returns Body

  • Parameters

    • id: number

    Returns Shape

    Todo

    Make a faster map

  • Check if any event listener of the given type is added

    Parameters

    • type: string

    Returns boolean

  • Check if an event listener is added

    Parameters

    • type: string
    • listener: Function

    Returns boolean

  • Parameters

    • dt: number

    Returns void

  • Raycast test

    Parameters

    Returns void

    Deprecated

    Use .raycastAll, .raycastClosest or .raycastAny instead.

  • Ray cast against all bodies. The provided callback will be executed for each hit with a RaycastResult as single argument.

    Parameters

    Returns boolean

    True if any body was hit.

  • 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.

  • Ray cast, and return information of the closest hit.

    Parameters

    Returns boolean

    True if any body was hit.

  • Remove a rigid body from the simulation.

    Parameters

    Returns void

  • Removes a constraint

    Parameters

    Returns void

  • Removes a contact material from the World.

    Parameters

    Returns void