Body3D¶
Description¶
Body3D is the 3D rigid-body handle built on top of the Jolt Physics library. Obtain one by calling object.getBody3D(). Create one or more shapes, set the body type and material properties, then call load() to register with the physics world. The body transform is synchronized with the parent Object each physics step.
Inherits: EntityHandle
Properties¶
| Type | Name | Default | Languages |
|---|---|---|---|
| BodyType | type | STATIC |
C++ | Lua |
| Body3DMotionQuality | motionQuality | DISCRETE |
C++ | Lua |
| Vector3 | position | — | C++ | Lua |
| Quaternion | rotation | — | C++ | Lua |
| float | mass | — | C++ | Lua |
| float | gravityFactor | 1.0 |
C++ | Lua |
| float | buoyancy | 0.0 |
C++ | Lua |
| float | waterDrag | 0.5 |
C++ | Lua |
| float | waterAngularDrag | 0.01 |
C++ | Lua |
| float | friction | 0.2 |
C++ | Lua |
| float | restitution | 0.0 |
C++ | Lua |
| Vector3 | linearVelocity | (0,0,0) |
C++ | Lua |
| Vector3 | angularVelocity | (0,0,0) |
C++ | Lua |
| bool | allowSleeping | true |
C++ | Lua |
| bool | sensor | false |
C++ | Lua |
| bool | collideKinematicVsNonDynamic | false |
C++ | Lua |
| uint16_t | categoryBitsFilter | 0xFFFF |
C++ | Lua |
| uint16_t | maskBitsFilter | 0xFFFF |
C++ | Lua |
| uint32_t | collisionGroupID | 0 |
C++ | Lua |
Methods¶
| Returns | Name | Languages |
|---|---|---|
| void | load | C++ | Lua |
| int | createBoxShape | C++ | Lua |
| int | createSphereShape | C++ | Lua |
| int | createCapsuleShape | C++ | Lua |
| int | createTaperedCapsuleShape | C++ | Lua |
| int | createCylinderShape | C++ | Lua |
| int | createConvexHullShape | C++ | Lua |
| int | createMeshShape | C++ | Lua |
| int | createHeightFieldShape | C++ | Lua |
| size_t | getNumShapes | C++ | Lua |
| Shape3DType | getShapeType | C++ | Lua |
| void | setShapeDensity | C++ | Lua |
| float | getShapeDensity | C++ | Lua |
| void | setShapeBitsPerSample | C++ | Lua |
| unsigned int | getShapeBitsPerSample | C++ | Lua |
| void | setShapeBuildQuality | C++ | Lua |
| Shape3DBuildQuality | getShapeBuildQuality | C++ | Lua |
| void | activate | C++ | Lua |
| void | deactivate | C++ | Lua |
| bool | canBeKinematicOrDynamic | C++ | Lua |
| void | setAllowedDOFsAll | C++ | Lua |
| void | setAllowedDOFs2DPlane | C++ | Lua |
| void | setAllowedDOFs | C++ | Lua |
| void | setMass | C++ |
| void | setOverrideMassAndInertia | C++ | Lua |
| void | setBitsFilter | C++ | Lua |
| void | setLinearVelocity | C++ |
| void | setLinearVelocityClamped | C++ | Lua |
| void | setAngularVelocity | C++ |
| void | setAngularVelocityClamped | C++ | Lua |
| Vector3 | getPointVelocity | C++ | Lua |
| Vector3 | getAccumulatedForce | C++ | Lua |
| Vector3 | getAccumulatedTorque | C++ | Lua |
| void | applyForce | C++ | Lua |
| void | applyTorque | C++ | Lua |
| void | applyImpulse | C++ | Lua |
| void | applyAngularImpulse | C++ | Lua |
| bool | applyBuoyancyImpulse | C++ | Lua |
| SubmergedVolume3D | getSubmergedVolume | C++ | Lua |
| Vector3 | getCenterOfMassPosition | C++ | Lua |
| Object | getAttachedObject | C++ | Lua |
| Body3DComponent | getBody3DComponent |
Lua |
Enumerations¶
BodyType¶
- STATIC — Infinite mass; never moves. Walls, floors, and terrain.
- KINEMATIC — Controlled by velocity, not forces. Elevators and moving platforms.
- DYNAMIC — Fully simulated by Jolt; reacts to forces and gravity.
Body3DMotionQuality¶
- DISCRETE — Standard collision detection per time step (default). Suitable for most objects.
- LINEAR_CAST — Continuous collision detection: sweeps the shape along its path each step. Use for fast-moving objects that may tunnel through thin geometry.
Shape3DType¶
BOX, SPHERE, CAPSULE, TAPERED_CAPSULE, CYLINDER, CONVEX_HULL, MESH, HEIGHTFIELD
Shape3DBuildQuality¶
How a mesh shape builds its collision tree. See setShapeBuildQuality.
- FAVOR_RUNTIME_PERFORMANCE — Builds a better tree that answers collisions and ray casts faster (default).
- FAVOR_BUILD_SPEED — Builds faster, so the mesh loads sooner, but each query costs more.
Structures¶
SubmergedVolume3D¶
What getSubmergedVolume returns.
| Type | Name | Description |
|---|---|---|
| float | totalVolume |
Volume of the body's shapes, in m³ |
| float | submergedVolume |
Volume below the surface, in m³ |
| float | fraction |
submergedVolume / totalVolume: 0 out of the fluid, 1 all under |
| Vector3 | centerOfBuoyancy |
World-space centre of the part under the surface; the centre of mass when nothing is under |
Property details¶
type¶
- Setter:
void setType(BodyType type) - Getter:
BodyType getType() const
Simulation mode. See BodyType. Can be changed at runtime to switch between, e.g., KINEMATIC and DYNAMIC.
motionQuality¶
- Setter:
void setMotionQuality(Body3DMotionQuality motionQuality) - Getter:
Body3DMotionQuality getMotionQuality() const
Collision detection mode. See Body3DMotionQuality.
mass¶
- Setter:
void setMass(float mass) - Getter:
float getMass() const
The body's mass in kilograms. The setter scales the inertia by the same factor, so a body
made heavier also gets harder to spin. Set it after calling load; see
setMass. The getter returns the mass the body has now,
including a change from setMass or
setOverrideMassAndInertia; for a static body it returns
the mass its shapes would give.
gravityFactor¶
- Setter:
void setGravityFactor(float gravityFactor) - Getter:
float getGravityFactor() const
Scales the global gravity for this body. 0 = gravity-free, 2 = double gravity.
Stored on the body component, so it can be set before load and survives a body reload. In the editor it is the Gravity Factor field of the Body3D component, saved with the scene and applied in exported projects.
buoyancy¶
- Setter:
void setBuoyancy(float buoyancy) - Getter:
float getBuoyancy() const - Setter:
void setWaterDrag(float waterDrag) - Getter:
float getWaterDrag() const - Setter:
void setWaterAngularDrag(float waterAngularDrag) - Getter:
float getWaterAngularDrag() const
How the body floats in a Water. buoyancy compares the water's density to the
body's: 1 is neutral, more floats and less sinks. 0 (the default) ignores the water.
waterDrag is how much the water slows the body down, and waterAngularDrag how much it
slows its rotation.
Every fixed step, a dynamic body with a buoyancy above 0 whose centre of mass is
inside a water's area is pushed up by the part of its shape below the wave surface there,
and tilted by the wave normal, so it bobs and rolls with the waves. No script or
applyBuoyancyImpulse call is needed. Static and kinematic bodies
are not affected, and waters with their own buoyancy
off float nothing.
Like gravityFactor, the values are stored on the body component, so they can be set before load. In the editor they are the Buoyancy section of the Body3D component (shown for dynamic bodies). See Physics — Buoyancy.
friction / restitution¶
- Setters:
void setFriction(float)/void setRestitution(float) - Getters:
float getFriction() const/float getRestitution() const
Surface material properties shared across all shapes on this body. Friction [0, ∞) controls sliding resistance; restitution [0, 1] controls bounciness.
position / rotation¶
- Setters:
void setPosition(Vector3)/void setRotation(Quaternion) - Getters:
Vector3 getPosition() const/Quaternion getRotation() const
The body's own pose in world space, read from and written straight to the simulation, and the only way to place a body from onFixedUpdate. Setting either also updates the entity Object transform so rendering follows and the next step does not undo the move. Both wake the body.
Use these for teleports — respawns, checkpoints, portals. For continuous movement prefer linearVelocity or applyForce, which let the solver resolve collisions instead of pushing bodies through walls.
Parented bodies
The world pose is converted to the entity's local space using the parent's transform as of the last variable-timestep sync. Moving a body's parent and teleporting the body in the same callback therefore places it relative to the parent's previous pose — do one or the other per frame. Bodies with no parent, the common case, are exact.
Do not use Object::setPosition from onFixedUpdate
Writing an entity's Object transform only reaches the body between fixed
steps. Inside onFixedUpdate the physics system syncs the body from the transform
before stepping and writes the stepped pose back after, so a transform written
there is discarded unless you also call updateTransform
to refresh the world transform the sync reads. Prefer Body3D::setPosition /
setRotation: one call, and it writes the simulation directly.
Object::setPosition is fine from onUpdate and outside the update callbacks.
linearVelocity / angularVelocity¶
- Setters:
void setLinearVelocity(Vector3)/void setAngularVelocity(Vector3) - Getters:
Vector3 getLinearVelocity() const/Vector3 getAngularVelocity() const
Current velocity vectors. Linear velocity is in world units/second; angular velocity is in radians/second around each world axis.
sensor¶
- Setter:
void setIsSensor(bool sensor) - Getter:
bool isSensor() const - Lua property:
body.sensor
When true, the body acts as a trigger volume: overlapping bodies generate contact events but no physical impulse is applied.
The flag is stored on the body component, not only on the live simulation body, so it can be set before load() and survives a body reload (a shape change, a body type change, or reopening the scene). In the editor it is the Sensor checkbox of the Body3D component, saved with the scene and applied in exported projects.
Which bodies a sensor detects
A STATIC sensor is the cheapest option, but it only detects awake dynamic and
kinematic bodies — the contact is lost as soon as the other body falls asleep. Making
the sensor dynamic or kinematic (and activating it) also detects sleeping bodies, and
such a sensor never sleeps on its own. Keep dynamic and kinematic sensors on a
category/mask that excludes static bodies and other sensors to avoid broad-phase work.
collideKinematicVsNonDynamic¶
- Setter:
void setCollideKinematicVsNonDynamic(bool) - Getter:
bool isCollideKinematicVsNonDynamic() const
When true, this kinematic body generates collision reports against static and other kinematic bodies (normally skipped for performance).
categoryBitsFilter / maskBitsFilter¶
Collision layer filtering bitmasks. A collision occurs only when (a.category & b.mask) != 0.
collisionGroupID¶
Group index for disabling self-collision within a group (e.g. ragdoll bones).
Method details¶
load¶
void load()
Registers the body and all its shapes with the Jolt physics world. Must be called after defining shapes. Re-call if shapes change at runtime.
createBoxShape¶
int createBoxShape(float width, float height, float depth)int createBoxShape(Vector3 position, Quaternion rotation, float width, float height, float depth)(with local offset)
Creates a solid box collider. Returns the shape index. The offset overload positions the shape relative to the entity origin.
createSphereShape¶
int createSphereShape(float radius)int createSphereShape(Vector3 position, Quaternion rotation, float radius)(with local offset)
Creates a sphere collider. Spheres are the cheapest shape to simulate.
createCapsuleShape¶
int createCapsuleShape(float halfHeight, float radius)int createCapsuleShape(Vector3 position, Quaternion rotation, float halfHeight, float radius)
Creates a vertical capsule (hemisphere-capped cylinder). Standard character controller shape.
createTaperedCapsuleShape¶
int createTaperedCapsuleShape(float halfHeight, float topRadius, float bottomRadius)int createTaperedCapsuleShape(Vector3 position, Quaternion rotation, float halfHeight, float topRadius, float bottomRadius)
Capsule with different radii at each end. Useful for cones, bullets, and stylised characters.
createCylinderShape¶
int createCylinderShape(float halfHeight, float radius)int createCylinderShape(Vector3 position, Quaternion rotation, float halfHeight, float radius)
Upright cylinder collider. Less stable than capsules on uneven ground; prefer capsules for characters.
createConvexHullShape¶
int createConvexHullShape()int createConvexHullShape(std::vector<Vector3> vertices)int createConvexHullShape(Vector3 position, Quaternion rotation, std::vector<Vector3> vertices)
Creates a convex hull from the mesh vertices (no-argument form) or from explicit vertices. Must be convex; Jolt computes the hull automatically.
createMeshShape¶
int createMeshShape()int createMeshShape(std::vector<Vector3> vertices, std::vector<uint16_t> indices)int createMeshShape(Vector3 position, Quaternion rotation, std::vector<Vector3> vertices, std::vector<uint16_t> indices)
Creates a static triangle-mesh collider. Mesh shapes can only be STATIC. Use convex hull for dynamic objects. For a large mesh, setShapeBuildQuality trades query speed for a faster build.
createHeightFieldShape¶
int createHeightFieldShape()int createHeightFieldShape(unsigned int samplesSize)
Creates a height-field collider sized to the terrain mesh in the entity. Used with the Terrain class. samplesSize controls the resolution grid.
setShapeBitsPerSample sets how precisely each height is stored.
activate / deactivate¶
void activate()— Wakes the body if it is sleeping.void deactivate()— Forces the body to sleep immediately.
setAllowedDOFsAll / setAllowedDOFs2DPlane / setAllowedDOFs¶
Constrain which degrees of freedom the body can move in.
- setAllowedDOFsAll — All 6 DOF (default).
- setAllowedDOFs2DPlane — Restricts to translation on X/Z and rotation on Y. Use for top-down or side-view games.
- setAllowedDOFs(translationX, translationY, translationZ, rotationX, rotationY, rotationZ) — Fine-grained control per axis.
Locking all three rotation axes is the usual way to keep a character upright; drive its facing with rotation instead of letting the solver spin it.
Like gravityFactor, the mask is stored on the body component, so it survives a body reload and is saved with the scene and applied in exported projects. Leaving every axis locked is invalid in Jolt — use a static body to freeze one completely.
setMass / setOverrideMassAndInertia¶
void setMass(float mass)— Sets the mass in kilograms and scales the inertia by the same factor, so the body keeps how its mass is spread: twice as heavy also takes twice the torque to spin. The mass must be above0, and the body must be loaded and not static, with at least one translation axis free.void setOverrideMassAndInertia(Vector3 solidBoxSize, float solidBoxDensity)— Computes mass and inertia for a solid box of the given size and density (kg/m³), then applies those values.
setMass changes only the live body: a body reload computes the mass from the shapes
again. To keep a mass, set it through the shape density or the
solid-box override.
The solid-box override is stored on the body component, so it can be set before load and survives a body reload; on a loaded dynamic body it takes effect at once. In the editor it is Override Mass on the Body3D component, with Solid Box Size and Solid Box Density (1000 kg/m³ by default, about the density of water), saved with the scene and applied in exported projects.
setShapeDensity¶
void setShapeDensity(float density)(first shape)void setShapeDensity(size_t index, float density)(specific shape)
Sets the density (kg/m³) used to compute mass. Call after adding shapes and before load, or call load again after runtime changes.
setShapeBitsPerSample¶
void setShapeBitsPerSample(size_t index, unsigned int bitsPerSample)unsigned int getShapeBitsPerSample(size_t index) const
How many bits a height-field shape stores for each height, from
1 to 16 (default 8). The heights are kept in blocks of samples, and the bits divide
each block's own height range, so 8 bits already give small steps on gentle ground. More
bits make collision follow steep terrain closer and take more memory. The shape is rebuilt on
the next update. In the editor it is Bits Per Sample on a heightfield shape.
setShapeBuildQuality¶
void setShapeBuildQuality(size_t index, Shape3DBuildQuality buildQuality)Shape3DBuildQuality getShapeBuildQuality(size_t index) const
How a mesh shape builds its collision tree: see
Shape3DBuildQuality. The default favors runtime performance, which
suits level geometry built once. FAVOR_BUILD_SPEED makes a large mesh load sooner, for
example on the web, at a cost on every collision and ray cast against it. The shape is
rebuilt on the next update. In the editor it is Build Quality on a mesh shape.
setLinearVelocityClamped / setAngularVelocityClamped¶
Velocity setters that clamp the new value to the body's configured speed limits (set in Jolt body settings). Safer for kinematic controllers.
getPointVelocity¶
Vector3 getPointVelocity(Vector3 point) const
Returns the velocity of a world-space point on the body, including the contribution of angular velocity. Use for precise impact and dragging calculations.
applyForce¶
void applyForce(const Vector3& force)void applyForce(const Vector3& force, const Vector3& point)
Applies a force in Newtons each simulation step. The single-argument overload applies force at the center of mass (no torque). The two-argument overload applies force at a world-space point, potentially generating torque.
applyTorque¶
void applyTorque(const Vector3& torque)
Applies a torque (Nm) to rotate the body. The vector direction defines the rotation axis (right-hand rule).
applyImpulse¶
void applyImpulse(const Vector3& impulse)void applyImpulse(const Vector3& impulse, const Vector3& point)
Instantaneous velocity change. Prefer impulses over forces for one-shot events (jumps, explosions, hits).
applyAngularImpulse¶
void applyAngularImpulse(const Vector3& angularImpulse)
Instantaneous angular velocity change. Useful for spinning objects on impact.
applyBuoyancyImpulse¶
bool applyBuoyancyImpulse(Vector3 surfacePosition, Vector3 surfaceNormal, float buoyancy, float linearDrag, float angularDrag, Vector3 fluidVelocity, Vector3 gravity, float deltaTime)
Simulates buoyancy for a body partially submerged in a fluid. Returns true if the body intersects the water plane. Only a dynamic body is pushed, and a sleeping one wakes up. Call once per physics step with the water plane data. A body in a Water gets this every fixed step from its buoyancy settings, so call it yourself only for fluids that are not a Water component.
getSubmergedVolume¶
SubmergedVolume3D getSubmergedVolume(const Vector3& surfacePosition, const Vector3& surfaceNormal) const
How much of the body lies below a fluid surface, given as a plane through surfacePosition
whose surfaceNormal points up, out of the fluid. Returns a
SubmergedVolume3D with the volumes, the submerged fraction and the
centre of buoyancy. It only reads the body, so it applies no force: use it for gameplay, such
as a splash when fraction first rises above 0, or a sinking state once it nears 1. It
works for any body type, but not for a body with a mesh or height-field shape, which has no
volume: that call logs an error and returns zeros.
For a Water, take the surface from Water.getHeight and getNormal at the body's centre of mass, the point the built-in buoyancy uses.
Vector3 center = body.getCenterOfMassPosition();
Vector3 surface(center.x, sea.getHeight(center.x, center.z), center.z);
SubmergedVolume3D under = body.getSubmergedVolume(surface, sea.getNormal(center.x, center.z));
if (under.fraction > 0.0f && !wasInWater){
// splash
}
wasInWater = under.fraction > 0.0f;
local center = body:getCenterOfMassPosition()
local surface = Vector3(center.x, sea:getHeight(center.x, center.z), center.z)
local under = body:getSubmergedVolume(surface, sea:getNormal(center.x, center.z))
if under.fraction > 0 and not self.wasInWater then
-- splash
end
self.wasInWater = under.fraction > 0
getAttachedObject¶
Object getAttachedObject()
Returns the Object that owns this body.
getCenterOfMassPosition¶
Vector3 getCenterOfMassPosition()
Returns the world-space position of the center of mass, which may differ from the entity origin if shapes are offset.