A physics engine is a system helps to detect the collisions between recreation objects and simulate their motion. That is very helpful whenever you need to create a recreation that has reasonable physics conduct. Frequent video games like puzzle video games and platformers use physics engine to create their core gameplay.
One well-known instance is Indignant Birds. It makes use of the physics engine to create the projectile of the fowl and the reasonable fall of the obstacles after destruction.
On this tutorial, you’ll be taught in regards to the fundamentals of Godot 2D physics engine and how one can use it to construct a Marble drop recreation.
Alongside the way in which, you’ll:
- Be taught totally different sorts of physics node.
- Study PhysicsMaterial and the way it impacts the sport.
- Use RigidBody2D node to create a dynamic physics object.
- Use Area2D node to create a set off.
- Use StatcBody2D node to create a stationary physics object.
- Use Snap to Grid to align the nodes.
- Add sound results and background music to the sport.
The ultimate product of this tutorial is a one-click, physics-based recreation. Gamers click on the enjoying discipline to drop a marble with the aim to attain as many factors as potential.
On the finish of this tutorial, you’ll know perceive the fundamentals of Godot’s 2D physics engine.
Get Began
This tutorial makes use of the latest secure model of Godot 4. You’ll be able to obtain it right here.
Obtain the tutorial supplies by clicking Obtain Supplies on the high or backside of the tutorial. Subsequent, extract the zip file to your chosen venture location.
There are two initiatives included within the supplies:
- starter: The starter venture you’ll use to construct upon.
- closing: The ultimate model of the sport you’ll be able to run and play straight.
This tutorial requires you to have primary information of Godot Engine. In case you don’t really feel aware of Godot but, strive these tutorials first:
File Overview
Import and open the starter venture in Godot Mission Supervisor. After opening the venture, you’ll discover the ready useful resource information, comparable to pictures, sounds, and consumer interfaces.
The venture construction is as follows:
- fonts: Font information
- music: Background music
-
scenes: Scene information
- game_board.tscn: Recreation Board Scene
- game_over_dialog.tscn: GameOver dialog
- gui.tscn: Rating and marble rely Consumer Interface
- main_scene.tscn: Essential scene
- scripts: Script information
- sounds: Sound results
- sprites: Graphics
- credit score.txt: An inventory of attributions for the included belongings
Essential Scene
The Essential Scene is the scene the place the sport takes place. It comprises all nodes that want by the sport.
To open it, double-click main_scene.tscn within the FileSystem dock.
Then, swap to the 2D display to preview the sport board.
Now, you’ve a grasp of what the venture appears to be like like. Subsequent, you’ll find out about some primary ideas of the physics engine.
Understanding 2D Physics Engine
If you find yourself growing a recreation, you might want the next options to craft the gameplay:
- When two recreation objects collide with others, for instance, when a marble hits a rating set off.
- Management the motion of an object after making use of a power or a collision has occurred. For instance, how the marble bounces off the pegs.
A physics engine is right for this; it offers two main capabilities: Collision Detection and Object Motion Simulation.
Collision Detection
The engine repeatedly checks for overlaps between the physics objects within the scene and sends indicators after they collide.
Object Motion Simulation
The engine simulates the motion of objects based mostly on their physics attributes.
These are the physics attributes in Godot:
- Mass: Determines how forces have an effect on an object.
- Gravity: Applies a downward power to simulate falling.
- Friction: Resists the movement between two surfaces in touch.
- Bounce: Determines how a lot velocity is reserved when two our bodies collide.
Now you realize the fundamental ideas of the physics engine, you’ll be able to transfer on to be taught extra in regards to the totally different physics nodes supplied by Godot.
Understanding Completely different Physics Nodes
Godot provides three primary physics nodes: RigidBody2D, StaticBody2D and Area2D. Every node has totally different functions and behaviors and you must select the fitting one for every recreation component.
-
StaticBody2D: This node doesn’t transfer beneath physics simulation however can have an effect on RigidBody2D nodes.
It’s used for stationary setting objects like pegs and partitions. - RigidBody2D: This node has mass and is actively simulated by the physics engine. It will possibly reply to forces, gravity, and collisions. It will possibly additionally have an effect on different RigidBody2D nodes.
- Area2D: This node received’t trigger physics reactions, however it will possibly test for any collisions which have occurred. It’s used for getting into or exiting zones just like the rating set off.
Right here’s a fast comparability between the physics nodes:
With all this in thoughts, it’s time to create your first physics node.
Create a RigidBody2D node
To symbolize the marble within the recreation that may fall beneath gravity, and bounce off pegs, you could create a RigidBody2D node.
The Marble Scene
In an effort to create the marble that can be utilized within the recreation, you could create a brand new scene for it with RigidBody2D as the foundation node. A scene is a set of nodes. On this case, it comprises a RigidBody2D node for the physics simulation and a Sprite2D node for the look of the marble.
To create the scene, choose the scenes folder within the FileSystem dock. Now right-click and choose Create New ▸ Scene. Whereas the scene creation dialog is open, configure it like this:
- Root Sort: Different Node ▸ RigidBody2D
- Scene Title: marble.tscn
- Root Title: Marble
In regards to the PhysicsMaterial
Proper after creating the RigidBody2D node, you could configure the PhysicsMaterial of the node. PhysicsMaterial is a setting that defines how the node behave within the physics simulation.
There are two primary group of settings within the PhysicsMaterial: Friction and Tough and Bounce and Absorbent. Every group has totally different settings that have an effect on the node’s pace when it collides with one other node.
Friction and Tough
Friction is a quantity between 0 and 1. The upper the worth, the node has extra friction and strikes slower when collide. The decrease the worth, the node has much less friction and seem to slip extra simply.
Tough is a boolean. It controls which friction worth is used among the many two colliding nodes. Listed below are the foundations:
- Each nodes Tough worth are ON, the upper friction worth is used.
- Each nodes Tough worth are OFF, the decrease friction worth is used.
- Just one node Tough worth is ON, the physics engine takes the friction worth with tough ON.
The next demonstration reveals how the marble behaves with totally different friction and tough setting.