Step 5: Physics crates
Molten's physics (Jolt Physics) is driven by three components:
| Component | Makes the object |
|---|---|
collider alone |
A static, solid shape (walls, floors) |
collider + rigidBody |
A body that moves: dynamic (motion: "dynamic") or kinematic (motion: "kinematic") |
characterController |
A walking capsule you move with characterMove (the player from step 2) |
Physics runs on the fixed tick after your behaviors. Dynamic bodies write their position
and rotation back into Transform.
A crate
// objects/crate.object
// A dynamic box: gravity pulls it down and the player can push it.
export default object({
mesh: { mesh: "models/cube.gltf", color: vec4(0.8, 0.55, 0.3, 1) },
rigidBody: { motion: "dynamic", mass: 2, friction: 0.6 },
collider: {},
});
motion:"static","dynamic", or"kinematic".collider: {}is a box ofsize: vec3(1, 1, 1), centered on the object, scaled by the object's scale. Other shapes are"sphere","capsule","cylinder","mesh"(static triangle mesh), and"convexHull".
Stack three crates
// levels/yard/chunks/0_0.chunk
export default chunk({
format: "chunk-local-v1",
coord: [0, 0],
objects: [
{ id: "ground", type: Ground },
{ id: "director", type: Director },
{ id: "music", type: Music },
{ id: "coin-1", type: Coin, position: vec3(0, 1, 6) },
{ id: "coin-2", type: Coin, position: vec3(-4, 1, -3) },
{ id: "coin-3", type: Coin, position: vec3(5, 1, -6), overrides: { coin: { value: 5 } } },
{ id: "crate-1", type: Crate, position: vec3(2, 0.5, 2) },
{ id: "crate-2", type: Crate, position: vec3(2, 1.5, 2), rotation: euler(0, 20, 0) },
{ id: "crate-3", type: Crate, position: vec3(2.2, 4, 2), scale: 0.7 },
],
});
scale: 0.7 is shorthand for vec3(0.7, 0.7, 0.7).
Run it
The top crate falls onto the other two and settles. Walk into the stack and the player pushes it.
Things to try:
-
In devtools, select
crate-3while playing and changeRigidBody.restitutionto0.9, orCollider.size. Physics fields are live-editable; the body keeps its velocity. -
Cast can query the physics world. A ray straight down from 5 m above the origin:
const hit = physicsRaycast(vec3(0, 5, 0), vec3(0, -10, 0), 0xffffffff, placedObject("")); if (hit.object.get()) print("hit at", hit.position);The mask
0xfffffffftests every layer, andplacedObject("")is a convenient null handle for "ignore nothing". See the Engine API for shape casts, overlaps, forces and contact events.