The RigidBodyComponentSystem maintains the dynamics world for simulating rigid bodies, it also controls global values for the world such as gravity. Note: The RigidBodyComponentSystem is only valid if 3D Physics is enabled in your application. You can enable this in the application settings for your project.

Hierarchy (view full)

Properties

_compounds: RigidBodyComponent[] = []
_dynamic: RigidBodyComponent[] = []
_gravityFloat32: Float32Array = ...
_kinematic: RigidBodyComponent[] = []
_triggers: RigidBodyComponent[] = []
gravity: Vec3 = ...

The world space vector representing global gravity in the physics simulation. Defaults to [0, -9.81, 0] which is an approximation of the gravitational force on Earth.

Methods

  • Private

    Checks for collisions and fires collision events.

    Parameters

    • world: number

      The pointer to the dynamics world that invoked this callback.

    • timeStep: number

      The amount of simulation time processed in the last simulation tick.

    Returns void

  • Private

    Stores a collision between the entity and other in the contacts map and returns true if it is a new collision.

    Parameters

    • entity: Entity

      The entity.

    • other: Entity

      The entity that collides with the first entity.

    Returns boolean

    True if this is a new collision, false otherwise.

  • Fire an event, all additional arguments are passed on to the event listener.

    Parameters

    • name: string

      Name of event to fire.

    • Optional arg1: any

      First argument that is passed to the event handler.

    • Optional arg2: any

      Second argument that is passed to the event handler.

    • Optional arg3: any

      Third argument that is passed to the event handler.

    • Optional arg4: any

      Fourth argument that is passed to the event handler.

    • Optional arg5: any

      Fifth argument that is passed to the event handler.

    • Optional arg6: any

      Sixth argument that is passed to the event handler.

    • Optional arg7: any

      Seventh argument that is passed to the event handler.

    • Optional arg8: any

      Eighth argument that is passed to the event handler.

    Returns EventHandler

    Self for chaining.

    Example

    obj.fire('test', 'This is the message');
    
  • Test if there are any handlers bound to an event name.

    Parameters

    • name: string

      The name of the event to test.

    Returns boolean

    True if the object has handlers bound to the specified event name.

    Example

    obj.on('test', function () { }); // bind an event to 'test'
    obj.hasEvent('test'); // returns true
    obj.hasEvent('hello'); // returns false
  • Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.

    Parameters

    • Optional name: string

      Name of the event to unbind.

    • Optional callback: HandleEventCallback

      Function to be unbound.

    • Optional scope: object

      Scope that was used as the this when the event is fired.

    Returns EventHandler

    Self for chaining.

    Example

    const handler = function () {
    };
    obj.on('test', handler);

    obj.off(); // Removes all events
    obj.off('test'); // Removes all events called 'test'
    obj.off('test', handler); // Removes all handler functions, called 'test'
    obj.off('test', handler, this); // Removes all handler functions, called 'test' with scope this
  • Attach an event handler to an event.

    Parameters

    • name: string

      Name of the event to bind the callback to.

    • callback: HandleEventCallback

      Function that is called when event is fired. Note the callback is limited to 8 arguments.

    • Optional scope: object = ...

      Object to use as 'this' when the event is fired, defaults to current this.

    Returns EventHandle

    Can be used for removing event in the future.

    Example

    obj.on('test', function (a, b) {
    console.log(a + b);
    });
    obj.fire('test', 1, 2); // prints 3 to the console

    Example

    const evt = obj.on('test', function (a, b) {
    console.log(a + b);
    });
    // some time later
    evt.off();
  • Attach an event handler to an event. This handler will be removed after being fired once.

    Parameters

    • name: string

      Name of the event to bind the callback to.

    • callback: HandleEventCallback

      Function that is called when event is fired. Note the callback is limited to 8 arguments.

    • Optional scope: object = ...

      Object to use as 'this' when the event is fired, defaults to current this.

    Returns EventHandle

    • can be used for removing event in the future.

    Example

    obj.once('test', function (a, b) {
    console.log(a + b);
    });
    obj.fire('test', 1, 2); // prints 3 to the console
    obj.fire('test', 1, 2); // not going to get handled
  • Raycast the world and return all entities the ray hits. It returns an array of RaycastResult, one for each hit. If no hits are detected, the returned array will be of length 0. Results are sorted by distance with closest first.

    Parameters

    • start: Vec3

      The world space point where the ray starts.

    • end: Vec3

      The world space point where the ray ends.

    • Optional options: {
          filterCallback: Function;
          filterCollisionGroup: number;
          filterCollisionMask: number;
          filterTags: any[];
          sort: boolean;
      } = {}

      The additional options for the raycasting.

      • filterCallback: Function

        Custom function to use to filter entities. Must return true to proceed with result. Takes the entity to evaluate as argument.

      • filterCollisionGroup: number

        Collision group to apply to the raycast.

      • filterCollisionMask: number

        Collision mask to apply to the raycast.

      • filterTags: any[]

        Tags filters. Defined the same way as a Tags#has query but within an array.

      • sort: boolean

        Whether to sort raycast results based on distance with closest first. Defaults to false.

    Returns RaycastResult[]

    An array of raycast hit results (0 length if there were no hits).

    Example

    // Return all results of a raycast between 0, 2, 2 and 0, -2, -2
    const hits = this.app.systems.rigidbody.raycastAll(new Vec3(0, 2, 2), new Vec3(0, -2, -2));

    Example

    // Return all results of a raycast between 0, 2, 2 and 0, -2, -2
    // where hit entity is tagged with `bird` OR `mammal`
    const hits = this.app.systems.rigidbody.raycastAll(new Vec3(0, 2, 2), new Vec3(0, -2, -2), {
    filterTags: [ "bird", "mammal" ]
    });

    Example

    // Return all results of a raycast between 0, 2, 2 and 0, -2, -2
    // where hit entity has a `camera` component
    const hits = this.app.systems.rigidbody.raycastAll(new Vec3(0, 2, 2), new Vec3(0, -2, -2), {
    filterCallback: (entity) => entity && entity.camera
    });

    Example

    // Return all results of a raycast between 0, 2, 2 and 0, -2, -2
    // where hit entity is tagged with (`carnivore` AND `mammal`) OR (`carnivore` AND `reptile`)
    // and the entity has an `anim` component
    const hits = this.app.systems.rigidbody.raycastAll(new Vec3(0, 2, 2), new Vec3(0, -2, -2), {
    filterTags: [
    [ "carnivore", "mammal" ],
    [ "carnivore", "reptile" ]
    ],
    filterCallback: (entity) => entity && entity.anim
    });
  • Raycast the world and return the first entity the ray hits. Fire a ray into the world from start to end, if the ray hits an entity with a collision component, it returns a RaycastResult, otherwise returns null.

    Parameters

    • start: Vec3

      The world space point where the ray starts.

    • end: Vec3

      The world space point where the ray ends.

    • Optional options: {
          filterCallback: Function;
          filterCollisionGroup: number;
          filterCollisionMask: number;
          filterTags: any[];
      } = {}

      The additional options for the raycasting.

      • filterCallback: Function

        Custom function to use to filter entities. Must return true to proceed with result. Takes one argument: the entity to evaluate.

      • filterCollisionGroup: number

        Collision group to apply to the raycast.

      • filterCollisionMask: number

        Collision mask to apply to the raycast.

      • filterTags: any[]

        Tags filters. Defined the same way as a Tags#has query but within an array.

    Returns RaycastResult

    The result of the raycasting or null if there was no hit.

Events

EVENT_CONTACT: string = 'contact'

Fired when a contact occurs between two rigid bodies. The handler is passed a SingleContactResult object containing details of the contact between the two bodies.

Example

app.systems.rigidbody.on('contact', (result) => {
console.log(`Contact between ${result.a.name} and ${result.b.name}`);
});