Sunday, June 16, 2024
HomeiOS DevelopmentGetting Began with 2D Physics in Godot

Getting Began with 2D Physics in Godot


A physics engine is a system helps to detect the collisions between sport objects and simulate their motion. That is very helpful while you wish to create a sport that has sensible physics conduct. Widespread 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 chook and the sensible fall of the obstacles after destruction.

On this tutorial, you’ll be taught concerning the fundamentals of Godot 2D physics engine and tips on how to use it to construct a Marble drop sport.

Alongside the way in which, you’ll:

  • Be taught completely 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 sport. Gamers click on the taking part in discipline to drop a marble with the aim to attain as many factors as attainable.
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 possibly can 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 challenge location.

There are two tasks included within the supplies:

  • starter: The starter challenge you’ll use to construct upon.
  • last: The ultimate model of the sport you may run and play immediately.

This tutorial requires you to have fundamental information of Godot Engine. In case you don’t really feel acquainted with Godot but, attempt these tutorials first:

File Overview

Import and open the starter challenge in Godot Venture Supervisor. After opening the challenge, you’ll discover the ready useful resource recordsdata, reminiscent of photographs, sounds, and person interfaces.

The challenge construction is as follows:

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

    • game_board.tscn: Recreation Board Scene
    • game_over_dialog.tscn: GameOver dialog
    • gui.tscn: Rating and marble depend Person Interface
    • main_scene.tscn: Foremost scene
  • scripts: Script recordsdata
  • sounds: Sound results
  • sprites: Graphics
  • credit score.txt: An inventory of attributions for the included property

Foremost Scene

The Foremost Scene is the scene the place the sport takes place. It accommodates all nodes that want by the sport.

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

Then, change to the 2D display to preview the sport board.

Starter Project

Now, you’ve a grasp of what the challenge seems like. Subsequent, you’ll study some fundamental ideas of the physics engine.

Understanding 2D Physics Engine

If you end up creating a sport, chances are you’ll want the next options to craft the gameplay:

  • When two sport 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 pressure or a collision has occurred. For instance, how the marble bounces off the pegs.

A physics engine is good for this; it gives two main features: Collision Detection and Object Motion Simulation.

Collision Detection
The engine repeatedly checks for overlaps between the physics objects within the scene and sends alerts 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 pressure 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 essential ideas of the physics engine, you may transfer on to be taught extra concerning the completely different physics nodes provided by Godot.

Understanding Completely different Physics Nodes

Godot provides three principal physics nodes: RigidBody2D, StaticBody2D and Area2D. Every node has completely different functions and behaviors and it is best to select the appropriate one for every sport factor.

  • StaticBody2D: This node doesn’t transfer beneath physics simulation however can have an effect on RigidBody2D nodes.
    It’s used for stationary surroundings objects like pegs and partitions.
  • RigidBody2D: This node has mass and is actively simulated by the physics engine. It might reply to forces, gravity, and collisions. It might additionally have an effect on different RigidBody2D nodes.
  • Area2D: This node received’t trigger physics reactions, however it may examine 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:

Physics Node Comparison

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

Create a RigidBody2D node

To symbolize the marble within the sport that may fall beneath gravity, and bounce off pegs, that you must create a RigidBody2D node.

The Marble Scene

With a purpose to create the marble that can be utilized within the sport, that you must create a brand new scene for it with RigidBody2D as the basis node. A scene is a group of nodes. On this case, it accommodates 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 Identify: marble.tscn
  • Root Identify: Marble


Create new marble scene

In regards to the PhysicsMaterial

Proper after creating the RigidBody2D node, that you must configure the PhysicsMaterial of the node. PhysicsMaterial is a setting that defines how the node behave within the physics simulation.

There are two principal group of settings within the PhysicsMaterial: Friction and Tough and Bounce and Absorbent. Every group has completely 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 principles:

  • 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 exhibits how the marble behaves with completely different friction and tough setting.


Compare different frictions



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments