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.
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;
Jolt backend instance.
Private
commandsPrivate
Private
configPrivate
Gets the fixed timestep size that the physics world is stepping with. This was set via JoltInitSettings.fixedStep.
1/30
Gets the current gravity vector.
Vec3(0, -9.81, 0)
Sets the physics world gravity.
Gravity vector.
Gets the current state of the physics world. Whether it is paused or not.
false
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.
If true
, will pause the physics world update.
Private
queryPrivate
Gets the number of times the physics world has been updated (steps count).
0
Private
systemsPrivate
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.
Callback function to execute before stepping the physics world.
Creates a raycast query to the physics world.
World point where the ray originates from.
Non-normalized ray direction. The magnitude is ray's distance.
Your function that will accept the raycast result.
Optional
opts: CastRaySettingsSettings object to customize the query.
// 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.
Shape index number. Create one using createShape.
World point where the cast is originated from.
Shape rotation.
Non-normalized ray direction. The magnitude is ray's distance.
Your function that will accept the shapecast result.
Optional
opts: anySettings object to customize the query.
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.
World position to test.
Function to take the query results.
Query customization settings.
// 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.
Shape index created with createShape.
World position of the shape.
World rotation of the shape.
Callback function that will take the query results.
Query customization settings.
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.
Collision groups.
// 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.
Shape type number. options.
Optional
options: any = {}Optional shape settings.
Shape index.
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.
Destroys a shape that was previously created with createShape.
Shape index number.
Removes a callback that was set via addUpdateCallback.
Toggles a collision between 2 subgroups inside a group.
Group index number.
First subgroup number.
Second subgroup number.
true
to enable, false
to disable collision.
Jolt Manager is responsible to handle the Jolt Physics backend.