Theory
12. Physics Engine
What is a Physics Engine?
Physics Engine is a 2D rigid body simulation library for making games and animations. Programmers can use it in their games to make objects move in realistic ways and make the projects more interactive. From the game engine’s point of view, the physics engine is just a system for procedural animation.
Accessing Physics Engine in Block Coding
Following is the process to add Physics Engine capability to the PictoBlox Project.
📂 Open PictoBlox and create a new file.
💻Select the coding environment as Block Coding.
➕Next, click on the Add Extension button and add the Physics Engine extension.
🧩 You can find the Physics Engine blocks available in the project.
Core Concepts
Physics Engine works with several fundamental concepts and objects. We briefly define these objects here and more details are given later in the block definitions and examples.
🔵 Shape: A shape is a 2D geometrical sprite, such as a circle or polygon or the sprite shape.
🏋️♂️ Rigid body: A chunk of matter that is so strong that the distance between any two bits of matter on the chunk is constant. They are hard like a diamond.
🛠 Fixture: A fixture binds a shape to a body and adds material properties such as density, friction, and restitution.
🔗 Constraint: A constraint is a physical connection that removes degrees of freedom from bodies.
🤝 Contact constraint: A special constraint designed to prevent penetration of rigid bodies and to simulate friction and restitution.
🌍 World: A physics world is a collection of bodies, fixtures, and constraints that interact together.
🧮 Solver: The physics world has a solver that is used to advance time and resolve contact and joint constraints.
🏃♂️ Continuous collision: The solver advances bodies in time using discrete time steps.
PictoBlox Blocks
🖼️ The block sets the boundaries for the stage. Available types: boxed with roof, boxed without roof, open with floor, and open without floor.
🌌 The block sets the gravity for the physics engine in the defined x and y directions.
🔄 The block set the sprite characteristics for the Physics Engine as free, fixed, or fixed but can rotate.
📊 The block sets the sprite’s density, roughness, and bounce (restitution) to the defined values.
🔧 The block sets the selected property of the sprite (density, roughness, and bounce) to the specified value.
⚖️ The block sets the sprite’s mass to the specified value.
📍 The blocks make the sprite go to the specified x and y locations according to the specified coordinate system – world, stage, or relative.
⚙️ The block reports the mass of the sprite.
⏭️ The block reports the density of the sprite.
⚙️ The block enables the physics engine for the selected objects with the defined precision level.
⏭️ The block does the step simulation for the Physics Engine. This block is required to run in a loop for the physics to work.
➡️ The block sets the velocity of the sprite to the defined values in the x and y directions.
🌀 The block changes the velocity of the sprite by the defined values in the x and y directions.
🤼♂️The blocks add a force to the sprite with the defined value in the specified direction.
🌀 The block adds a spin force to the sprite with the specified value.
📈 The block reports the x velocity of the sprite.
📉 The block reports the y velocity of the sprite.
🤼♂️ The block reports all the objects that are in contact with the sprite.
🎮 The block sets the stage view to the defined coordinates with respect to the defined world coordinates. This is very helpful in creating games.
🔄 The block changes the stage view by the defined coordinate values with respect to the defined world coordinates.
📍 The block reports the x coordinate value of the stage view.
📏 The block reports the y coordinate value of the stage view.
⏩ The block sets the velocity unit for the physics engine to the specified unit system.
💪 The block sets the force unit for the physics engine to the specified unit system.
⚖️ The block sets the mass unit for the physics engine to the specified unit system.
🔄 The block reset the unit system of the physics engine to default.
📋 The block reports the current unit system used for the specified property.
📐The block converts the specified value of the distance from the input unit system to the target unit system.
💨 The block converts the specified value of the mass from the input unit system to the target unit system.
💪 The block converts the specified value of the time from the input unit system to the target unit system.
💨 The block converts the specified velocity of the time from the input unit system to the target unit system.
💪 The block converts the specified value of the force from the input unit system to the target unit system.
The example demonstrates the ball falling in gravity in a box. Using the pen extension, we are also tracking the trajectory of the ball.
🎳 Bell and Falling Balls
In this example, you understand the effect of the density, roughness, and bounce properties of the sprites. The ball falls from the top randomly, and the bell is fixed but can rotate.
Script of Bell
The bell center is changed to make it swing from the top.
Script for Ball
This script creates clones of the ball every 0.5 seconds.
This script assigns the properties of the clone.
Output
We will change the density of the bell and the ball in the block – set sprite density () roughness () bounce (). This will result in different simulations.
🔔 Bell is very light and the balls are very heavy: The inertia of the balls will affect the bell too much.
🔔 Bells are very light and balls are also very light: The bell is less affected by the ball. Gradually the bell slows down.
🔔 Bell is very heavy and the balls are very light: The bell is not affected at all. The balls are thrown without adding any effect on the bell.
🔔 Bell is very heavy and the balls are also very heavy: Same as option 2 – The bell is less affected by the ball. Gradually the bell slows down.