APPLICATIONS

Getting Started with 2D Physics in Godot

A physics engine is a system helps to detect the collisions between game objects and simulate their movement. This is very useful when you want to create a game that has realistic physics behavior. Common games like puzzle games and platformers use physics engine to create their core gameplay.

One famous example is Angry Birds. It uses the physics engine to create the projectile of the bird and the realistic fall of the obstacles after destruction.

In this tutorial, you’ll learn about the basics of Godot 2D physics engine and how to use it to build a Marble drop game.

Along the way, you’ll:

  • Learn different kinds of physics node.
  • Learn about PhysicsMaterial and how it affects the game.
  • Use RigidBody2D node to create a dynamic physics object.
  • Use Area2D node to create a trigger.
  • Use StatcBody2D node to create a stationary physics object.
  • Use Snap to Grid to align the nodes.
  • Add sound effects and background music to the game.

The final product of this tutorial is a one-click, physics-based game. Players click the playing field to drop a marble with the goal to score as many points as possible.
At the end of this tutorial, you’ll know understand the basics of Godot’s 2D physics engine.

Get Started

This tutorial uses the most recent stable version of Godot 4. You can download it here.

Download the tutorial materials by clicking Download Materials at the top or bottom of the tutorial. Next, extract the zip file to your chosen project location.

There are two projects included in the materials:

  • starter: The starter project you’ll use to build upon.
  • final: The final version of the game you can run and play directly.

This tutorial requires you to have basic knowledge of Godot Engine. If you don’t feel familiar with Godot yet, try these tutorials first:

File Overview

Import and open the starter project in Godot Project Manager. After opening the project, you’ll find the prepared resource files, such as images, sounds, and user interfaces.

The project structure is as follows:

  • fonts: Font files
  • music: Background music
  • scenes: Scene files

    • game_board.tscn: Game Board Scene
    • game_over_dialog.tscn: GameOver dialog
    • gui.tscn: Score and marble count User Interface
    • main_scene.tscn: Main scene
  • scripts: Script files
  • sounds: Sound effects
  • sprites: Graphics
  • credit.txt: A list of attributions for the included assets

Main Scene

The Main Scene is the scene where the game takes place. It contains all nodes that need by the game.

To open it, double-click main_scene.tscn in the FileSystem dock.

Then, switch to the 2D screen to preview the game board.

Now, you’ve a grasp of what the project looks like. Next, you’ll learn about some basic concepts of the physics engine.

Understanding 2D Physics Engine

When you are developing a game, you may need the following features to craft the gameplay:

  • When two game objects collide with others, for example, when a marble hits a score trigger.
  • Control the movement of an object after applying a force or a collision has happened. For example, how the marble bounces off the pegs.

A physics engine is ideal for this; it provides two major functions: Collision Detection and Object Movement Simulation.

Collision Detection
The engine continuously checks for overlaps between the physics objects in the scene and sends signals when they collide.

Object Movement Simulation
The engine simulates the movement of objects based on their physics attributes.

These are the physics attributes in Godot:

  • Mass: Determines how forces affect an object.
  • Gravity: Applies a downward force to simulate falling.
  • Friction: Resists the motion between two surfaces in contact.
  • Bounce: Determines how much velocity is reserved when two bodies collide.

Now you know the basic concepts of the physics engine, you can move on to learn more about the different physics nodes offered by Godot.

Understanding Different Physics Nodes

Godot offers three main physics nodes: RigidBody2D, StaticBody2D and Area2D. Each node has different purposes and behaviors and you should choose the right one for each game element.

  • StaticBody2D: This node doesn’t move under physics simulation but can affect RigidBody2D nodes.
    It’s used for stationary environment objects like pegs and walls.
  • RigidBody2D: This node has mass and is actively simulated by the physics engine. It can respond to forces, gravity, and collisions. It can also affect other RigidBody2D nodes.
  • Area2D: This node won’t cause physics reactions, but it can check for any collisions that have occurred. It’s used for entering or exiting zones like the score trigger.

Here’s a quick comparison between the physics nodes:

Physics Node Comparison

With all this in mind, it’s time to create your first physics node.

Create a RigidBody2D node

To represent the marble in the game that can fall under gravity, and bounce off pegs, you need to create a RigidBody2D node.

The Marble Scene

In order to create the marble that can be used in the game, you need to create a new scene for it with RigidBody2D as the root node. A scene is a collection of nodes. In this case, it contains a RigidBody2D node for the physics simulation and a Sprite2D node for the look of the marble.

To create the scene, select the scenes folder in the FileSystem dock. Now right-click and select Create New â–¸ Scene. While the scene creation dialog is open, configure it like this:

  • Root Type: Other Node â–¸ RigidBody2D
  • Scene Name: marble.tscn
  • Root Name: Marble

Create new marble scene

About the PhysicsMaterial

Right after creating the RigidBody2D node, you need to configure the PhysicsMaterial of the node. PhysicsMaterial is a setting that defines how the node behave in the physics simulation.

There are two main group of settings in the PhysicsMaterial: Friction and Rough and Bounce and Absorbent. Each group has different settings that affect the node’s speed when it collides with another node.

Friction and Rough

Friction is a number between 0 and 1. The higher the value, the node has more friction and moves slower when collide. The lower the value, the node has less friction and appear to slide more easily.

Rough is a boolean. It controls which friction value is used among the two colliding nodes. Here are the rules:

  • Both nodes Rough value are ON, the higher friction value is used.
  • Both nodes Rough value are OFF, the lower friction value is used.
  • Only one node Rough value is ON, the physics engine takes the friction value with rough ON.

The following demonstration shows how the marble behaves with different friction and rough setting.

Compare different frictions


Source link

Related Articles

Back to top button