Physics
Introduction
Studio has a built-in physics system intended for handling robust and dynamic interactions in your scene.Adding Colliders
- Colliders can be nested in some configurations, but not others. For example, a static collider inside a dynamic collider, or two nested dynamic colliders, could misbehave.
- Unless you have a Geometry component that is also a valid Collider Shape, you’ll need to specify a shape manually. For example there isn’t a supported matching collider for the tetrahedron primitive.
- GLTF model collider generation for complex models might affect performance.
Example
The following example shows how to add a collider to an entity at runtime.
ecs.Collider.set(world, component.eid, {
shape: ecs.ColliderShape.Sphere,
radius,
mass: 1,
eventOnly: false,
lockXAxis: false,
lockYAxis: false,
lockZAxis: false,
friction: 0.5,
restitution: 0.5,
linearDamping: 0,
angularDamping: 0,
rollingFriction: 0.1,
spinningFriction: 0.1,
})
Functions
You can directly apply forces (linear and angular) to any entity with a physics collider. These forces are applied in the next physics simulation update, which takes place at regular intervals. The function accepts a 3D vector to define the force direction and magnitude.
Velocity
Linear
ecs.physics.setLinearVelocity(world, component.eid, forceX, forceY, forceZ)
Force & Torque
Force
ecs.physics.applyForce(world, component.eid, forceX, forceY, forceZ)
Torque
ecs.physics.applyTorque(world, component.eid, torqueX, torqueY, torqueZ)
Impulse
This function is used to apply a one-time instantaneous force to a physics collider, altering its velocity based on the given impulse vector. This method is useful for events that require a quick, single action response, such as jumping, punching, or a sudden push.
ecs.physics.applyImpulse(world, component.eid, impulseX, impulseY, impulseZ)
Gravity
Gravity Factor
In each scene, gravity acts as a constant force known as “World Gravity.” This force affects every collider in the scene that is set to dynamic. To customize how World Gravity impacts an individual collider, we provide an attribute called “Gravity Factor.”
Set World Gravity
ecs.physics.setWorldGravity(world, gravity)