Skip to main content

Module physics

Module physics 

Source
Expand description

Simple, predictable 3D physics. Available as ctx.physics.

Made for the needs of audio games: walls you bump into, floors you stand on, things that fall, triggers that tell you when the player enters a room, and ray casts to “look” around. There are only two shapes (boxes and spheres) and no rotation, which keeps everything easy to reason about.

The engine steps the world automatically at a fixed rate (60 Hz by default), so you only add bodies and react to PhysicsEvents.

ctx.physics.set_gravity(EARTH_GRAVITY);
ctx.physics.add(Body::fixed(vec3(0.0, 0.0, -0.5)).with_box(100.0, 100.0, 1.0).with_tag("floor"));
let player = ctx.physics.add(Body::dynamic(vec3(0.0, 0.0, 1.0)).with_box(0.6, 0.6, 1.8).with_tag("player"));

// Later: walk forward and find out if a wall was in the way.
let result = ctx.physics.move_by(player, vec3(0.0, 1.0, 0.0));
if result.blocked {
    ctx.audio.play("bump");
}

See the coordinate system for axis directions.

Structs§

Body
A physical object.
BodyId
Identifies a body in the World.
MoveResult
Result of World::move_by.
RayHit
Result of World::raycast.
World
The physics world. Reach it through ctx.physics.

Enums§

BodyKind
How a body takes part in the simulation.
PhysicsEvent
Something that happened during the physics steps of this frame.
Shape
Collision shape. Shapes never rotate.

Constants§

EARTH_GRAVITY
Gravity on earth: 9.81 m/s² downwards (towards -z).