Box2D

This extension allows you to easily implement proper physics using a physics library called Box2D.

World

Adjust the physics of all sprites.

setup stage [boxed stage v] :: #0FBD8C

Choose a type of containment to keep sprites within the stage.


set gravity to x: [0] y: [-10] :: #0FBD8C

Change the direction and strength of gravity.


step simulation :: #0FBD8C

Move forward in time by one step. Run this in a loop to keep the physics going.

set simulation rate to (30)/s :: #0FBD8C

Set how much simulation steps is considered one second. Usually this should be project's framerate, but can also be used to slow down or speed up time.

You can get the current simulation rate with the (simulation rate) reporter.

Sprites

Manipulate individual sprites.

enable for [this costume v] mode [normal v] :: #0FBD8C

Make physics apply to this sprite. It can also collide with other sprites that have physics enabled.

Precision mode will make the sprite work extra hard to make sure it doesn't overlap with anything. Note that this can decrease performance and even cause the project to get stuck, so use with care.


disable physics for this sprite :: #0FBD8C

Makes physics no longer apply to this sprite.


go to x: [0] y: [0] [in world v] :: #0FBD8C

Make the sprite go to the specified location.


set velocity to sx: [0] sy: [0] :: #0FBD8C

Set the velocity (speed) of the sprite to the specified value.

You can get the velocity of the current sprite with the (x velocity) and (y velocity) reporters.


push with force [25] in direction [0] :: #0FBD8C

Directly send the sprite flying in a certain direction, adding on to its current velocity.


set angular velocity to [30] :: #0FBD8C

Set the angular velocity (rotational speed) of the sprite to the specified value.

You can get the angular velocity of the current sprite with the (angular velocity) reporter.


spin with force [500] :: #0FBD8C

Directly send the sprite spinning, adding on to its current angular (rotational) velocity.


set fixed [fixed in place v] :: #0FBD8C

Choose whether the sprite is fixed in place or can move around.

You can tell if the sprite is currently fixed in place with the (fixed?) reporter.


set density [normal v] :: #0FBD8C

Set the sprite's density, which affects how heavy it is.

You can get the sprite's current density with the (density) reporter.


set friction [normal v] :: #0FBD8C

Set the sprite's roughness. Smoother settings make the sprite slipperier.

You can get the sprite's current friction with the (friction) reporter.


set bounce [normal v] :: #0FBD8C

Set the sprite's bounciness.

You can get the sprite's current bounciness with the (bounce) reporter.


(touching [any v] :: #0FBD8C)

Returns what other sprites the sprite is touching. Also includes the edges of the stage.

If there are multiple sprites touching, it will return a comma-separated list of them.

Screen

Move the camera around the world.

set scroll x: [0] y: [0] :: #0FBD8C

Scroll to the desired location.

You can get the current screen position with the (x scroll) and (y scroll) reporters.

This will not affect the world boundaries set by the [setup stage] block.

Example

when flag clicked setup stage [boxed stage v] :: #0FBD8C set gravity to x: [0] y: [-10] :: #0FBD8C create clone of (myself v) repeat [20] step simulation :: #0FBD8C end create clone of (myself v) forever step simulation :: #0FBD8C end when I start as a clone show enable for [this costume v] mode [normal v] :: #0FBD8C go to x: [-150] y: [240] [in world v] :: #0FBD8C set bounce [quite bouncy v] :: #0FBD8C set angular velocity to [-4.1] :: #0FBD8C