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.
- Basic settings to initialize the physics engine
- Set the position and velocity of a sprite - Apply forces and torques to a sprite
- Utils for games with scrolling map
Functions and Definitions
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.
You can make a loop in your project to see the motion result.
Setup the stage area of the project.
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.
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.
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:
The hitbox will be shown as the red line.
If you select ‘this costume’, the hitbox will be like:
If you select ‘this circle’, the hitbox will be like:
If you select ‘this polygon’, the hitbox will be like:
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.
Sprites in precision mode could avoid tunneling effect. If a sprite has super high speed, the precision mode will be a better choice.
Set the gravity in the stage. You can change the value of x and y to adjust the value and direction of gravity.
The value of gravity in horizontal direction
The value of gravity in vertical direction
Set the degrees of freedom of the sprite.
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.
Change the velocity of the sprite.
The value of velocity in horizontal direction
The value of velocity in vertical direction
Get the value in horizontal and vertical directions.
Apply a force to the sprite.
The value of the force
The direction of the force
Apply a torque to the sprite.
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.
Set the density, roughness, and bounce of the sprite.
The density will determine the mass of a sprite. There are five options: - very light - light - normal - heavy - very heavy
The roughness will determine the friction applied to the sprite. There are five options: - none - smooth - normal - rough - extremely rough
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.
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.
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.
Set scroll x and y to set the position of stage in the reference system.
The x position of the center of the stage in the reference system.
The y position of the center of the stage in the reference system.
If you set scroll x to 100 and scroll y to 0, the position of stage will be like:
To move the stage, you could use this block.
Use these blocks to get the value of scroll x and scroll y.
Set sprite’s position in a reference system.
The x position of the sprite
The y position of the sprite
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.
The Physics Engine in Scratch is developed by Griffpatch.