Logo
  • Features
  • Educators
  • What's New
  • Blog
  • Showcase
  • About us
  • Contact
Try it now!
Physics Engine
Physics Engine

Physics Engine

Physics Engine developed by Griffpatch

The physics engine extension is developed by Griffpatch based on Box2D.

You can watch the video tutorial on his YouTube channel.

Why use the physics engine?

Building a real and physically correct game world from scratch is extremely hard. You need to master a lot of knowledge about Physics and Mathematics.

Modern game engines usually provide build-in Physics engine to creators.

Griffpatch developed a 2D Physics Engine for Scratch. We introduce it to Gandi IDE.

Modules

Module
Description
Setup
- Basic settings to initialize the physics engine
Dynamics
- Set the position and velocity of a sprite - Apply forces and torques to a sprite
Scroll
- Utils for games with scrolling map

Functions and Definitions

Setup

Running the Physics Engine is a kind of simulation. It will calculate the position of each sprite with several inputs and render the result on the stage.

The simulation is not continuous in time. It is step by step.

Use this block to get the result of each step.

image

You can make a loop in your project to see the motion result.

image

Setup the stage area of the project.

image
Selection
Description
range of stage
There are three options: - boxed stage: The boxed stage has a floor at the bottom edge of the stage and walls at the left and right edges of the stage. - open (with floor): The open stage only has a floor at the bottom edge of the stage. - open (no floor): The open stage has no floor, ceiling, and walls.

A ball bounces in a boxed stage:

A ball in a open stage with the floor:

A ball in a open stage without the floor:

Enable the Physics Engine for the sprite.

image
Selection
Description
range
Set the hitbox of the sprite. There are four options: - this costume: Set the hitbox to the frame of the sprite’s costume. - this circle: Set the hitbox to the circle almost covering the costume of the sprite. If you want to design a wheel sprite, you should select this one. - this polygon: Set the hitbox to the polygon almost covering the costume of the sprite. Only for SVG costumes. - all sprites: Enable the Physic Engine for all sprites, and set hitboxes to frames of sprites’ costumes.
mode
There are two options: - normal: The normal mode for sprites with normal speed. - precision: The precision mode for sprites with super high speed to avoid tunneling effect.

For example, a sprite has a costume:

image

The hitbox will be shown as the red line.

If you select ‘this costume’, the hitbox will be like:

image

If you select ‘this circle’, the hitbox will be like:

image

If you select ‘this polygon’, the hitbox will be like:

image

Because of the step by step simulation. If the speed of a sprite is super high and a wall is thin enough, the tunneling effect will happen.

This picture is from the document of Box2D
This picture is from the document of Box2D

Sprites in precision mode could avoid tunneling effect. If a sprite has super high speed, the precision mode will be a better choice.

Dynamics

Set the gravity in the stage. You can change the value of x and y to adjust the value and direction of gravity.

image
Property
Description
Example Value
x
The value of gravity in horizontal direction
0
y
The value of gravity in vertical direction
-10

Set the degrees of freedom of the sprite.

image
Selection
Description
options
There are three options: - free: The sprite is completely free on the stage. - fixed in the place: The sprite is fixed on the stage. It could not move and rotate. - fixed (but can rotate): The sprite can rotate, but can’t move.

Set the velocity of the sprite. You can change the value of x and y to adjust the value and direction of velocity.

image

Change the velocity of the sprite.

image
Property
Description
Example Value
sx
The value of velocity in horizontal direction
0
sy
The value of velocity in vertical direction
0

Get the value in horizontal and vertical directions.

image
image

Apply a force to the sprite.

image
Property
Description
Example Value
value
The value of the force
25
direction
The direction of the force
0

Apply a torque to the sprite.

image
Property
Description
Example Value
value
The value of the torque. If the value is positive, the direction of the torque is clockwise. If the value is negative, the direction of the torque is counterclockwise.
500

Set the density, roughness, and bounce of the sprite.

image
Selection
Description
density
The density will determine the mass of a sprite. There are five options: - very light - light - normal - heavy - very heavy
roughness
The roughness will determine the friction applied to the sprite. There are five options: - none - smooth - normal - rough - extremely rough
bounce
The bounce will determine the elasticity of the sprite. There are six options: - none - little - normal - quite bouncy - very bouncy - unstable

Return the names of sprites which is being touched by the sprite. If the sprite is touching edges of the stage, this block will return ‘Stage’. Results are separated by commas.

image
Selection
Description
part of body
The part of the sprite’s body. There are two options: - any: Sprites are being touched by any part of the sprite’s body. - feet: Sprites are being touched by the feet of the sprite. “The feet of a sprite” is the bottom edge of the costume.

Scroll

In Scratch, the reference system for all sprites is attached on the stage. The center of it is the center of the stage.

Sometimes, Scratchers want to build a big world breaking through the boarders on the stage.

We call it the scrolling map.

In scrolling map projects, the reference system is attached on the plane space shown as the gray area. The stage is like a viewfinder shown as the white area.

image

Set scroll x and y to set the position of stage in the reference system.

image
Property
Description
Example Value
scroll x
The x position of the center of the stage in the reference system.
0
scroll y
The y position of the center of the stage in the reference system.
0

For example:

image

If you set scroll x to 100 and scroll y to 0, the position of stage will be like:

image

To move the stage, you could use this block.

image

Use these blocks to get the value of scroll x and scroll y.

image
image

Set sprite’s position in a reference system.

image
Property
Description
Example Value
x
The x position of the sprite
0
y
The y position of the sprite
0
reference system
The reference system for the position. There are three options: - in world: The reference system is in the plane space. - on stage: The reference system is on the stage. - relative: The reference system is attached to the current position of the sprite.
in world

Credits

The Physics Engine in Scratch is developed by Griffpatch.

Embed GitHub

💡
If you have any questions or suggestions, find us on the discord channel: https://discord.gg/U3nyveCvRa
Logo

Designed by 2ndR with love

Discord