Jolt Manager is responsible to handle the Jolt Physics backend.

Hierarchy

  • PhysicsManager
    • JoltManager

Accessors

  • get backend(): null | JoltBackend
  • 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.

    Returns null | JoltBackend

    Example

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

    // common Jolt interfaces, which the backend has instantiated
    backend.physicsSystem;
    backend.bodyInterface;
    backend.joltInterface;
  • set backend(instance): void
  • Parameters

    Returns void

  • get fixedStep(): number
  • Gets the fixed timestep size that the physics world is stepping with. This was set via JoltInitSettings.fixedStep.

    Returns number

    Default Value

    1/30
    
  • get gravity(): Vec3
  • Gets the current gravity vector.

    Returns Vec3

    Default Value

    Vec3(0, -9.81, 0)
    
  • set gravity(gravity): void
  • Sets the physics world gravity.

    Parameters

    • gravity: Vec3

      Gravity vector.

    Returns void

  • get paused(): boolean
  • Gets the current state of the physics world. Whether it is paused or not.

    Returns boolean

    Default Value

    false
    
  • set paused(bool): 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

    Default Value

    0
    

Methods

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

    Parameters

    • origin: Vec3

      World point where the ray originates from.

    • dir: Vec3

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

    • callback: Function

      Your function that will accept the raycast result.

    • Optional opts: CastRaySettings

      Settings object to customize the query.

    Returns void

    Example

    // 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);
    app.physics.castRay(origin, dir, onResults, { firstOnly: false });

    function onResults(results) {
    if (results.length === 0) {
    return;
    }
    // do something with results
    }
  • Creates a shapecast query to the physics world.

    Parameters

    • shapeIndex: number

      Shape index number. Create one using createShape.

    • pos: Vec3

      World point where the cast is originated from.

    • rot: Quat

      Shape rotation.

    • dir: Vec3

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

    • callback: CastShapeCallback

      Your function that will accept the shapecast result.

    • Optional opts: any

      Settings object to customize the query.

    Returns void

    Example

    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);
    app.physics.castShape(shapeIndex, pos, Quat.IDENTITY, dir, onResults, {
    ignoreSensors: true
    });

    function onResults(results) {
    if (results.length === 0) {
    return;
    }
    // do something with 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.

    Parameters

    • point: Vec3

      World position to test.

    • callback: Function

      Function to take the query results.

    • opts: QuerySettings

      Query customization settings.

    Returns void

    Example

    // get all entities that overlap a world position (0, 5, 0)
    app.physics.collidePoint(new Vec3(0, 5, 0), onResults, { ignoreSensors: true });

    function onResults(results) {
    if (results.length === 0) {
    return;
    }
    // do something with results
    }
  • Gets all entities that collide with a given shape.

    Parameters

    • shapeIndex: number

      Shape index created with createShape.

    • position: Vec3

      World position of the shape.

    • rotation: Vec3

      World rotation of the shape.

    • callback: Function

      Callback function that will take the query results.

    • opts: CollideShapeSettings

      Query customization settings.

    Returns void

    Example

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

    // create a box with a half extent (1, 1, 1) meters
    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);
    app.physics.collideShape(shapeIndex, pos, Quat.IDENTITY, onResults, { scale });

    function onResults(results) {
    if (results.length === 0) {
    return;
    }
    // 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.

    Returns void

    Example

    // 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

    • type: number

      Shape type number. options.

    • Optional options: any = {}

      Optional shape settings.

    Returns number

    Shape index.

    Example

    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
    });

    See

    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.

    Returns void