RigidBodyComponentSystem
Extends: ComponentSystem
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.
Summary
Properties
gravity | The world space vector representing global gravity in the physics simulation. |
Methods
raycastAll | Raycast the world and return all entities the ray hits. |
raycastFirst | Raycast the world and return the first entity the ray hits. |
Events
contact | Fired when a contact occurs between two rigid bodies. |
Inherited
Methods
fire | Fire an event, all additional arguments are passed on to the event listener. |
hasEvent | Test if there are any handlers bound to an event name. |
off | Detach an event handler from an event. |
on | Attach an event handler to an event. |
once | Attach an event handler to an event. |
Details
Properties
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
raycastAll(start, end, [options])
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.
// 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));
// 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" ]
});
// 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
});
// 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
});
Parameters
start | Vec3 | The world space point where the ray starts. |
end | Vec3 | The world space point where the ray ends. |
options | object | The additional options for the raycasting. |
options.sort | boolean | Whether to sort raycast results based on distance with closest first. Defaults to false. |
options.filterCollisionGroup | number | Collision group to apply to the raycast. |
options.filterCollisionMask | number | Collision mask to apply to the raycast. |
options.filterTags | any[] | Tags filters. Defined the same way as a Tags#has query but within an array. |
options.filterCallback | function | Custom function to use to filter entities. Must return true to proceed with result. Takes the entity to evaluate as argument. |
Returns
RaycastResult[]An array of raycast hit results (0 length if there were no hits).
raycastFirst(start, end, [options])
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. |
options | object | The additional options for the raycasting. |
options.filterCollisionGroup | number | Collision group to apply to the raycast. |
options.filterCollisionMask | number | Collision mask to apply to the raycast. |
options.filterTags | any[] | Tags filters. Defined the same way as a Tags#has query but within an array. |
options.filterCallback | function | Custom function to use to filter entities. Must return true to proceed with result. Takes one argument: the entity to evaluate. |
Returns
RaycastResult, nullThe result of the raycasting or null if there was no hit.
Events
contact
Fired when a contact occurs between two rigid bodies.
Parameters
result | SingleContactResult | Details of the contact between the two bodies. |
Inherited
Methods
fire(name, [arg1], [arg2], [arg3], [arg4], [arg5], [arg6], [arg7], [arg8])
Fire an event, all additional arguments are passed on to the event listener.
obj.fire('test', 'This is the message');
Parameters
name | string | Name of event to fire. |
arg1 | * | First argument that is passed to the event handler. |
arg2 | * | Second argument that is passed to the event handler. |
arg3 | * | Third argument that is passed to the event handler. |
arg4 | * | Fourth argument that is passed to the event handler. |
arg5 | * | Fifth argument that is passed to the event handler. |
arg6 | * | Sixth argument that is passed to the event handler. |
arg7 | * | Seventh argument that is passed to the event handler. |
arg8 | * | Eighth argument that is passed to the event handler. |
Returns
EventHandlerSelf for chaining.
hasEvent(name)
Test if there are any handlers bound to an event name.
obj.on('test', function () { }); // bind an event to 'test'
obj.hasEvent('test'); // returns true
obj.hasEvent('hello'); // returns false
Parameters
name | string | The name of the event to test. |
Returns
booleanTrue if the object has handlers bound to the specified event name.
off([name], [callback], [scope])
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.
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
Parameters
name | string | Name of the event to unbind. |
callback | HandleEventCallback | Function to be unbound. |
scope | object | Scope that was used as the this when the event is fired. |
Returns
EventHandlerSelf for chaining.
on(name, callback, [scope])
Attach an event handler to an event.
obj.on('test', function (a, b) {
console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
const evt = obj.on('test', function (a, b) {
console.log(a + b);
});
// some time later
evt.off();
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. |
scope | object | Object to use as 'this' when the event is fired, defaults to current this. |
Returns
EventHandleCan be used for removing event in the future.
once(name, callback, [scope])
Attach an event handler to an event. This handler will be removed after being fired once.
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
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. |
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.