Physics and games

Make layers fall, collide, bounce and answer the keyboard, and build a whole game from them with interactions and variables.

Anything that moves by itself or by keys is physics: a character that runs and jumps, coins to collect, enemies that patrol, a ball that bounces. There is no game engine to learn and no code: each layer says what it is to physics, and the interactions you already know decide what happens when things meet. The Sprout Quest template (New file → Game UI) is a whole platformer built this way. Open it and play it to see every piece below.

Play a platformer built from physics bodies · 0:14
Description

The SPROUT QUEST template with Sprout selected: the inspector’s Behaviors section shows his Physics body, a dynamic body with left, right and jump controls. Present plays it: Sprout runs right, jumps the pit and the slimes, picks up coins along the planks and reaches the flag, and the Level clear banner shows.

Bodies

Select a layer and open Behaviors › Physics in the inspector. A group moves as one, with everything in it.

  • Moves as: static (ground, walls: it stays put), dynamic (gravity pulls it, it collides and you can steer it) or kinematic (it moves at its own speed and nothing pushes it: a moving platform, a patrolling enemy). Travels there and back sends a kinematic body that far and back again.
  • Collides as: solid, oneWay (you land on it from above and jump up through it), sensor (nothing stops at it, but it notices what enters and leaves: coins, hazards, a finish line) or none.
  • Gravity, Velocity, Bounce, Friction (slowing on the ground), Air drag and Top speed tune how it moves. The screen's own Gravity is the world's (px/s²).
  • Tags name a kind of body (enemy, coin) for the interactions below; Passes through lets a body ignore those tags entirely.

Controls

Controls are named, and each lists its Keys and what it does: a speed across or up and down, how fast it gets there (Acceleration, and In the air for floatier jumps). Once per press makes it a jump; Only on the ground keeps it to jumps from the ground; Grace after leaving the ground and Early press counts forgive a jump pressed a moment late or early; Let go: keep this share of speed turns a tap into a short hop.

A layer with On-screen button: holds control is a touch button for that control, for phones.

When things meet

Add an interaction with the trigger On behavior event on a layer, pick Physics and the event, and narrow it with With to the layer (or tag) it meets:

EventWhen
collide, collide-top / bottom / left / righta solid hit this layer (and on which of its sides)
landit came down onto something
enter, enter-top / …, leavesomething entered or left this sensor (and through which side: a stomp is an enemy's enter-top)
pressone of its controls fired (a jump)
outit left the world (fell down a pit)
expireits lifetime ran out

Each fires once when it begins. The action can be anything: Set variable (a score), a Conditional, navigating, or a Behavior command that tells physics to Push, Set speed, Teleport, Reset (back to the start; with no body, everything — a restart), Spawn a copy of a template layer, Remove, or Change a body (turn its controls off when the level is won). In a physics event's action, the body can be this layer or the other one.

Show the state

Writes its state to gives a body variables for its speed, whether it is on the ground, which way it faces and where it is. Bind layers to them: a run pose visible while grounded && vx != 0, a jump pose while !grounded, a score text bound to coins (see Variables and logic).

Playing it

Physics runs in present mode and on a published site, the same way. Arrow keys and the keys you gave it steer; on a phone, the buttons do.