Physics
Physics makes things fall, collide, bounce, roll and stack on their own. Give an entity a body and a collider and the engine moves it for you while the game runs.
Renzora has its own physics engine built in, for both 3D and 2D scenes. There is nothing to install.
Bodies and colliders
Physics on an entity is two parts, and each is its own section in the Inspector:
- Physics Body says how the entity moves.
- Collision Shape says what shape it is when it touches things.
A body needs at least one collider to do anything. Adding a Physics Body from the Inspector adds a default Collision Shape with it, unless the entity (or one of its children) already has one.
Body types
| Body Type | Use it for | What it does |
|---|---|---|
| Rigid Body | Crates, balls, debris, anything that should fall and be knocked about | Gravity and collisions move it. It has mass, falls, bounces and gets pushed. |
| Static Body | Floors, walls, terrain, buildings | Never moves. Other things collide with it. |
| Kinematic Body | Moving platforms, doors, characters you steer yourself | Moves only when you move it (from a script, or an animation). It pushes rigid bodies out of the way but nothing pushes it. |
Adding physics
Shapes come ready. A cube, sphere, wall or any other shape you add from the shape library arrives with a Static Body and a matching collider already on it. Change Body Type to Rigid Body and it will fall.
Physics presets. In the Hierarchy, click Add Entity and look under Physics for Rigid Body, Static Body, Kinematic Body, Box Collider, Sphere Collider, Capsule Collider and Cylinder Collider. Each spawns an empty entity with a body and a collider, ready for you to place.
On anything else. Select the entity, click Add Component in the Inspector and pick Physics Body. The collider it adds sizes itself to the entity's mesh.
A whole imported model. Select the model's root and, in its Collision Shape section, use Apply to children:
- Stamp gives every mesh under it a static mesh collider, a batch at a time, with a progress bar in the Hierarchy. Use it to make a level you imported solid in one click.
- Strip removes every body and collider underneath again.
The Physics Body section
| Field | Default | What it does |
|---|---|---|
| Body Type | Rigid Body | See Body types. |
| Mass | 1 | How heavy it is. Heavier bodies push lighter ones aside. |
| Gravity Scale | 1 | 0 floats, 1 is normal, 2 falls twice as hard. Negative floats upward. |
| Linear Damping | 0 | Air resistance: slows the body down over time. |
| Angular Damping | 0.05 | Slows its spinning. |
| Continuous (CCD) | Off | Stops a fast body (a bullet, a thrown ball) from passing straight through a thin wall. Costs a little, so turn it on only where you need it. |
| Dominance | 0 | When two moving bodies meet, the one with the higher value is not pushed at all. A player at 1 walks through crates at 0 without slowing. |
| Max Speed | 0 | The fastest the body may move, in units a second. 0 means no limit. |
| Max Spin | 0 | The fastest it may turn, in radians a second. 0 means no limit. |
| Never Sleep | Off | Bodies that come to rest "sleep" to save work and wake when something touches them. Turn this on to keep a body always awake. |
| Custom Center / Center of Mass | Off | Where the body balances, instead of the middle of its colliders. Put it low on a car so it does not roll over in a turn. |
| Custom Inertia / Inertia | Off | How hard the body is to spin about each axis. Higher turns more sluggishly. |
| Lock Axes | None | Tick Translation X, Y or Z to stop the body moving along that axis, or Rotation X, Y or Z to stop it turning about it. Locking rotation on X and Z keeps a character upright. |
The Collision Shape section
Shapes
| Shape | Sized by | Good for |
|---|---|---|
| Box | Half Extents (half the width, height and depth) | Crates, walls, platforms |
| Sphere | Radius | Balls, round pickups |
| Capsule | Radius and Half Height | Characters |
| Cylinder | Radius and Half Height | Barrels, pillars |
| Mesh | The entity's own mesh | Level geometry, anything with holes, steps or overhangs |
| Polygon | A list of points you draw | 2D shapes of any outline |
Mesh follows the exact surface of the model. On an entity with no mesh of its own, such as an imported model's root, it uses every mesh underneath, merged. It is the right choice for scenery that never moves; for something that moves, a simpler shape is faster and behaves better.
Polygon is for 2D. You draw and edit its points in the 2D view, and it is solid inside, even when the outline is concave.
Placing and sizing the collider
- Offset moves the collider away from the entity's centre, and Rotation turns it, without needing a child entity.
- Edit Collider switches the viewport to collider editing: the move gizmo is replaced by handles that resize and move the collider directly. Click it again to finish.
Surface
| Field | Default | What it does |
|---|---|---|
| Friction | 0.5 | Grip. 0 is ice; higher is grippier. |
| Restitution | 0 | Bounce. 0 does not bounce; 1 bounces back as high as it fell. |
| Friction Combine / Restitution Combine | Average | How this surface's value mixes with whatever it touches: Average, Geometric Mean, Min, Multiply or Max. When two surfaces disagree, the one later in that list wins, so ice set to Min stays slippery against anything and a trampoline set to Max bounces everything. |
| Rolling Resistance | 0.02 | How quickly a ball rolling on it slows down. |
| Density | 1 | How heavy the collider is for its size. A body made of several colliders is heavier where they are denser, like the head of a hammer. |
Triggers and layers
- Is Sensor turns the collider into a trigger: things pass through it, but the engine still reports that they touched it. Use it for pickups, checkpoints, doors that open as you approach, and kill zones.
- One-Way (2D) makes a 2D platform you can jump up through from below and stand on from above. Turn the entity to change which side is solid.
- Layers and Collides With decide what can touch what. There are 32 layers. Every collider starts on the first layer and collides with all of them. Two colliders only touch when each is on a layer the other collides with, so either one can opt out. For example, put enemy bullets on their own layer and leave that layer out of the enemies' Collides With, and enemies can no longer shoot each other.
Trying it out: Simulate
You do not have to start the whole game to watch physics. Click the caret beside Play in the top bar and choose Simulate.
Physics runs on the scene you are editing, right in the viewport. The viewport border turns green, and the camera, gizmos and Inspector all keep working, so you can watch bodies fall and settle and check values as they change. Press Play (it now reads Stop) to end it, and the scene goes back to exactly how it was. Nothing a simulation moved is saved.
Pressing Play runs the full game, physics included, in its own window.
2D physics
2D and 3D scenes each have their own physics world, and the same Physics Body and Collision Shape sections work in both. Sprites, and anything inside a 2D scene, use 2D physics automatically. In 2D a box is a rectangle and a sphere is a circle, and things move in the X and Y plane only.
For an invisible 2D collider, such as a trigger area with no sprite, add the Physics2d component (Inspector, Add Component) so it joins the 2D world.
Vehicles
Add the Vehicle component to a car model's root, alongside a Rigid Body, to make it drivable. Its wheels are found by name: set Front Axle and Rear Axle to the names of the wheel parts in the model. Choose which wheels are driven (Front, Rear or All), and tune the engine, brakes, top speed, steering, suspension and grip.
With Drive With Input on, the car drives from the move action (W and S to drive and brake, A and D or the left stick to steer) and the accelerate and brake actions (the gamepad triggers). See Input.
Seeing what physics is doing
The Physics Stats panel (in the panel menu, under Debug) shows, for each world: how many bodies are moving, how many are asleep, how many colliders, joints and contacts there are, and how long each physics step takes. If a scene runs slowly, a large number of awake bodies is the first thing to look for.
Physics from a script
Scripts can push bodies, read their speed, find out what they are touching, and cast rays. All of it lives in renzora::physics. New to scripts? Start with Scripting with Rust.
Push a body
Physics bodies move through commands you queue. Each runs before the next physics step.
use bevy::prelude::*;
use renzora::physics::{PhysicsCommand, PhysicsCommands};
use renzora::{ActionState, ScriptCtx};
fn update(ctx: &mut ScriptCtx) {
let jump = ctx
.get_resource::<ActionState>()
.is_some_and(|input| input.just_pressed("jump"));
if !jump {
return;
}
let me = ctx.entity();
if let Some(mut physics) = ctx.get_resource_mut::<PhysicsCommands>() {
physics.push(PhysicsCommand::AddImpulse { entity: me, impulse: Vec3::Y * 5.0 });
}
}
renzora::script!(update);
| Command | What it does |
|---|---|
AddImpulse { entity, impulse } | A sudden push, like a jump or an explosion |
AddForce { entity, force } | A steady push for this frame. Send it every frame for a constant force, like a thruster |
AddTorque { entity, torque } / AddAngularImpulse { entity, impulse } | The same, for spinning |
AddForceAtPoint / AddImpulseAtPoint | A push at a point on the body, which also makes it spin |
SetLinearVelocity { entity, velocity } | Set its speed and direction outright |
SetAngularVelocity { entity, velocity } | Set its spin outright |
Sleep { entity } / Wake { entity } | Put it to sleep, or wake it up |
These work on Rigid Body entities. To move a Kinematic Body, change its Transform yourself.
For forces you apply over time, use the FixedUpdate hook rather than update, so the result is the same at any frame rate.
Read its speed
Every body carries a BodyVelocity, updated after each step:
use renzora::physics::BodyVelocity;
let speed = ctx.get::<BodyVelocity>().map_or(0.0, |v| v.linear.length());
What is it touching?
Every body carries PhysicsContacts, the list of everything it is touching this frame, with the point, the direction and how hard they hit:
use renzora::physics::PhysicsContacts;
if let Some(contacts) = ctx.get::<PhysicsContacts>() {
for contact in &contacts.0 {
if contact.impulse > 2.0 {
info!("hard landing on {:?}", contact.entity);
}
}
}
contacts.touches(other) asks about one entity, and contacts.entities() lists them. Sensors show up here too. There is a pickup recipe on the Scripting with Rust page.
Cast a ray
To ask "what is below me?" or "what is the player looking at?":
use bevy::prelude::*;
use renzora::physics::{Cast, PhysicsBackends, PhysicsDim};
use renzora::ScriptCtx;
fn update(ctx: &mut ScriptCtx) {
let me = ctx.entity();
let Some(from) = ctx.get::<GlobalTransform>().map(|t| t.translation()) else { return };
let down = Cast::ray(PhysicsDim::D3, from, Vec3::NEG_Y, 10.0).excluding([me]);
if let Some(hit) = PhysicsBackends::cast_one(ctx.world(), down) {
info!("{:.2} above the ground", hit.distance);
}
}
renzora::script!(update);
A hit gives you the distance, the point, the surface normal and the entity it hit. Use PhysicsDim::D2 in a 2D scene.
Gravity
Gravity is world-wide, in the PhysicsSettings resource. A script can change it while the game runs:
use renzora::physics::PhysicsSettings;
if let Some(mut settings) = ctx.get_resource_mut::<PhysicsSettings>() {
settings.gravity = Vec3::new(0.0, -1.6, 0.0); // the Moon
}
PhysicsSettings also holds time_scale, for slow motion.
Joints
A joint links two bodies: a hinge for a door, a rope, a weld. Joints are created while the game runs, from a script, as an entity of their own. Despawn the entity to break the joint.
use renzora::physics::{JointKind, PhysicsJoint};
// `door` and `frame` are the two bodies' entities.
ctx.world().spawn(PhysicsJoint::new(
door,
frame,
JointKind::Revolute { axis: Vec3::Y, limits: Some((-1.5, 1.5)) },
));
| Joint | What the two bodies can do |
|---|---|
Fixed | Nothing: welded together |
Revolute { axis, limits } | Turn about one axis, like a hinge |
Prismatic { axis, limits } | Slide along one axis, like a drawer |
Distance { min, max } | Stay between two distances apart, like a rope |
Spherical { .. } | Turn any way about a point, like a shoulder (3D only) |
Using a different physics engine
Physics engines from the Marketplace can run your scenes instead of the built-in one. Install one, then pick it in Settings, under Project, in the Physics section: 3D Backend and 2D Backend are chosen separately. The editor switches straight away; the game uses the new choice from its next start.
For how physics engines plug in, see Physics backends.