Documentation menu

Physics3D

Layers, Triggers & Springs

These tools turn collision into game rules. Named layers decide what can touch. Triggers notice an overlap without blocking it. Springs tie two bodies together with an invisible stretchy connection.

New to coding?

Copy the examples exactly first. Run them. Then change one number or word and run them again. You are not expected to memorize the command lists.

1

Choose who can collide

Layers use easy names instead of hard-to-read bit numbers. Either body may ignore the other body's layer. Pass false to allow that layer again.

Try this code
# Give each body a readable collision layer
Physics3D.layer(world, player, "player");
Physics3D.layer(world, ghost, "ghost");

# The player now passes through ghosts
Physics3D.ignore_layer(world, player, "ghost");

# Turn player-versus-ghost collision back on
Physics3D.ignore_layer(world, player, "ghost", false);
2

Make a trigger zone

A trigger has a shape, but it is not a solid wall. Use one for pickups, checkpoints, doors, damage zones, or level exits.

Try this code
pickup = Physics3D.static_box(world, 4, 1, 0, 2, 2, 2);
Physics3D.trigger(world, pickup);  # Make the box non-solid

if Physics3D.is_triggered(world, pickup) {
    print "The pickup was touched!";
}

# Characters list every trigger they currently touch
info = Physics3D.character_info(world, player);
print info["triggers"];
3

Connect two bodies with a spring

The short spring call remembers the bodies' current distance. set_spring changes the resting length, strength, and damping.

Try this code
joint = Physics3D.spring(world, anchor, weight);

# Resting length 3, strength 60, damping 8
Physics3D.set_spring(world, joint, 3, 60, 8);

info = Physics3D.spring_info(world, joint);
Physics3D.remove_spring(world, joint);
4

Filter a game query

Add a layer name at the end when a ray or overlap should only find one kind of object.

Try this code
# Only hit bodies on the enemy layer
hit = Physics3D.raycast(world, x, y, z, dx, dy, dz, 100, "enemy");

# Only find pickups inside this sphere-shaped area
pickups = Physics3D.overlap_sphere(world, x, y, z, 5, "pickup");