Jolt Manager is responsible to handle the Jolt Physics backend.

Hierarchy

  • PhysicsManager
    • JoltManager

Accessors

  • get backend(): any
  • Gets the Jolt Backend instance. This is useful, when components are not sufficient and you wish to access Jolt's API directly.

    Note, this will be null, if the backend runs in a web worker. In this case you don't have access to the instance.

    Returns any

    const backend = app.physics.backend;
    const Jolt = backend.Jolt;
    const joltVec = new Jolt.Vec3(0, 0, 0);

    // common Jolt interfaces you might want, which the backend has already instantiated
    backend.physicsSystem;
    backend.bodyInterface;
    backend.joltInterface;
  • get paused(): boolean
  • Gets the current state of the physics world. Whether it is paused or not.

    Returns boolean

    false
    
  • set paused(bool: boolean): void
  • Allows to pause/unpause physics update. Useful, when you have some UI popup and want to freeze the game world, but still be able to interact with the application.

    Parameters

    • bool: boolean

      If true, will pause the physics world update.

    Returns void

  • get steps(): number
  • Gets the number of times the physics world has been updated (steps count).

    Returns number

    0
    

Methods

  • A callback function to call when all rigid and soft bodies fall asleep. It will be called every frame, while the bodies are not active.

    Note, that if a kinematic body has an BodyComponent.isometryUpdate set to ISOMETRY_DEFAULT or ISOMETRY_FRONT_TO_BACK, then the body will have its transforms get auto-updated in physics world every frame, preventing it from falling asleep. If you need this callback while using a kinematic body, then set its isometry update to ISOMETRY_BACK_TO_FRONT and control its transforms via component's move methods, e.g. BodyComponent.teleport, BodyComponent.linearVelocity etc. That will stop the frontend from sending isometry updates every frame and will rely on you setting its transforms when needed.

    Feature is disabled when web worker is used.

    Parameters

    • func: Function

      Callback function to execute.

    Returns void

  • Sometimes it is useful to have a callback right before the physics world steps. You can set such a callback function via this method.

    Your given callback will be called after all commands have been executed and right before we update virtual characters and step the physics world.

    Note, this feature is disabled, when the backend runs in a web worker.

    Parameters

    • func: Function

      Callback function to execute before stepping the physics world.

    Returns void

  • Creates a raycast query to the physics world.

    Note, that a callback function is optional when running physics on main thread. When using a web worker, the method will not return any results directly, but will send them to your callback function instead once the frontend gets the worker's response. It also means that when using a web worker a callback function is no longer optional and is required.

    Parameters

    • origin: Vec3

      World point where the ray originates from.

    • dir: Vec3

      Non-normalized ray direction. The magnitude is ray's distance.

    • Optionalcallback: null | (results: CastResult[]) => void = null

      A callback function that will accept the raycast results.

      • null
      • (results: CastResult[]) => void
          • (results: CastResult[]): void
          • Interface

            Parameters

            • results: CastResult[]

              An array with query results. An empty array if no results.

            Returns void

    • Optionalopts: null | CastRaySettings = null

      Settings object to customize the query.

    Returns null | CastResult[]

    • Returns an array of cast results (empty array if no result). Will return null, if not using CastRaySettings.immediate mode.
    // Cast a 10 meters ray from (0, 5, 0) straight down.
    const origin = new Vec3(0, 5, 0);
    const dir = new Vec3(0, -10, 0);
    const results = app.physics.castRay(origin, dir, null, { firstOnly: false });
    if (results.length > 0) {
    // do something with the results
    }
  • Creates a shapecast query to the physics world.

    Note, that a callback function is optional when running physics on main thread. When using a web worker, the method will not return any results directly, but will send them to your callback function instead once the frontend gets the worker's response. It also means that when using a web worker a callback function is no longer optional and is required.

    Parameters

    • shapeIndex: number

      Shape index number. Create one using createShape.

    • position: Vec3

      World point where the cast is originated from.

    • rotation: Quat

      Shape rotation.

    • dir: Vec3

      Non-normalized ray direction. The magnitude is ray's distance.

    • Optionalcallback: null | (results: CastResult[]) => void = null

      A callback function that will accept the raycast results.

      • null
      • (results: CastResult[]) => void
          • (results: CastResult[]): void
          • Interface

            Parameters

            • results: CastResult[]

              An array with query results. An empty array if no results.

            Returns void

    • Optionalopts: null | CastShapeSettings = null

      Settings object to customize the query.

    Returns null | CastResult[]

    import { SHAPE_SPHERE } from './physics.dbg.mjs';

    // Do a 10 meters cast with a 0.3 radius sphere from (0, 5, 0) straight down.
    const shapeIndex = app.physics.createShape(SHAPE_SPHERE, { radius: 0.3 });
    const pos = new Vec3(0, 5, 0);
    const dir = new Vec3(0, -10, 0);
    const results = app.physics.castShape(shapeIndex, pos, Quat.IDENTITY, dir, null, {
    ignoreSensors: true
    });
    if (results.length > 0) {
    // do something with the results
    }
  • Check if a world point is inside any body shape. For this test all shapes are treated as if they were solid. For a mesh shape, this test will only provide sensible information if the mesh is a closed manifold.

    Note, that a callback function is optional when running physics on main thread. When using a web worker, the method will not return any results directly, but will send them to your callback function instead once the frontend gets the worker's response. It also means that when using a web worker a callback function is no longer optional and is required.

    Parameters

    • point: Vec3

      World position to test.

    • Optionalcallback: null | (results: Entity[]) => void = null

      Function to take the query results.

      • null
      • (results: Entity[]) => void
          • (results: Entity[]): void
          • Interface

            Parameters

            • results: Entity[]

              An array of entities that the point collided with. Array will be empty, if no entities collided.

            Returns void

    • Optionalopts: null | QuerySettings = null

      Query customization settings.

    Returns null | Entity[]

    • An array of entities that the point collided with (empty array if no result). Will return null, if not using QuerySettings.immediate mode.
    // get all entities that overlap a world position (0, 5, 0)
    const results = app.physics.collidePoint(new Vec3(0, 5, 0), null, { ignoreSensors: true });
    if (results.length > 0) {
    // do something with the results
    }
  • Gets all entities that collide with a given shape.

    Note, that a callback function is optional when running physics on main thread. When using a web worker, the method will not return any results directly, but will send them to your callback function instead once the frontend gets the worker's response. It also means that when using a web worker a callback function is no longer optional and is required.

    Parameters

    • shapeIndex: number

      Shape index created with createShape.

    • position: Vec3

      World position of the shape.

    • Optionalrotation: Quat = Quat.IDENTITY

      World rotation of the shape.

    • Optionalcallback: null | (results: CollideShapeResult[]) => void = null

      Callback function that will take the query results.

    • Optionalopts: null | CollideShapeSettings = null

      Query customization settings.

    Returns null | CollideShapeResult[]

    import { SHAPE_BOX } from './physics.dbg.mjs';

    // create a box with a half extent (1, 1, 1) meters (now we can use the same shape for
    // different casts, but simply modifying its scale during the cast)
    const shapeIndex = app.physics.createShape(SHAPE_BOX, { halfExtent: Vec3.ONE });

    // get all entities that intersect a box with half extent (0.2, 0.5, 0.2) at world position
    // (0, 10, 0)
    const scale = new Vec3(0.2, 0.5, 0.2);
    const pos = new Vec3(0, 10, 0);
    const results = app.physics.collideShape(shapeIndex, pos, Quat.IDENTITY, null, { scale });
    if (results.length > 0) {
    // do something with the results
    }
  • Allows to create collision groups. Note, that collision groups are more expensive than broadphase layers.

    The groups are created by giving an array of numbers, where each element adds a group and its number represents the count of subgroups in it.

    Parameters

    • groups: number[]

      Collision groups.

    • Optionalopts: ImmediateSettings

      Customization options.

    Returns void

    // Create `2` groups. The first group has `3` subgroups, the second `5`.
    app.physics.createGroups([3, 5]);

    For additional information, refer to Jolt's official documentation on Collision Filtering.

  • Creates a shape in the physics backend. Note, that the shape is not added to the physics world after it is created, so it won't affect the simulation.

    This is useful, when you want to use a shape for a shapecast, or want your kinematic character controller to change current shape (e.g. standing, sitting, laying, etc).

    Once you no longer need the shape, you must destroyShape to avoid memory leaks.

    Parameters

    Returns number

    Shape index (uint).

    import { SHAPE_CAPSULE } from './physics.dbg.mjs';

    // create a 2m high and 0.6m wide capsule.
    const shapeIndex = app.physics.createShape(SHAPE_CAPSULE, {
    halfHeight: 1,
    radius: 0.3
    });

    ShapeComponent.shape for available shape type options.

  • Toggles a collision between 2 subgroups inside a group.

    Parameters

    • group: number

      Group index number.

    • subGroup1: number

      First subgroup number.

    • subGroup2: number

      Second subgroup number.

    • enable: boolean

      true to enable, false to disable collision.

    • Optionalopts: ImmediateSettings

      Query customization options.

    Returns void