50% developed

Guide to the Godot game engine/Physics

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Guide to the Godot game engine


Physics is used in many games, for many different things, such as falling, colliding into a wall, jumping, and so forth.

StaticBody2D and StaticBody3D[edit | edit source]

A StaticBody is a collision body that is not meant to move. While it can move, it should not be used for characters.

A static body may be scaled with no problems.

It is the fastest collision body type. It should be used for the environment and other non-movable things.

RigidBody2D and RigidBody3D[edit | edit source]

A RigidBody is an object that is affected by gravity. It usually is not used for characters as it is hard to control, but can be used in physics simulation games as it is quite accurate.

It cannot be scaled in any way!

KinematicBody2D and KinematicBody3D[edit | edit source]

A KinematicBody is a body that is intended to be given a script to move. It can also be used as a way to detect for hitting an object or surface.

Kinematic bodies work well for characters, but should not be used to simulate advanced physics.

Area2D and Area3D[edit | edit source]

An Area can be used to detect for other Areas, RigidBodies, or KinematicBodies. Upon overlap, a signal is emitted, and another is emitted when the object leaves.

If the camera/audio listener is inside the Area, it is possible to change the effects of sounds. E.g: to muffle car noises when jumping into water. You can do this by changing the "AudioBus" properties, and selecting a bus. You should make a "muffled" bus for this to work, and audio_bus_override must be set to true. (This is the overrided default audio bus, meaning the car noises must not use an audio bus. It will not layer the bus on top of others.).

CollisionShape2D and CollisionShape3D[edit | edit source]

In order for a collision object to collide with another, it needs one or more CollisionShape or ColisionPolygon children. CollisionShapes need an object defined to set their shape and size, and CollisionPolygons need multiple edges (at least 3) defined.

Collision mask and layers[edit | edit source]

Each object can have masks and layers. If at least one mask is equal to the overlapping object's layer, the two objects will be considered to have collided. This can be used to make the player collide with the floor, walls and enemies, but not other players, or only the floor and walls.

Each layer can be given a name in Project Settings -> Layer Names -> 2d Physics and Project Settings -> Layer Names -> 3d Physics. This improves readability, and decreases the chance of bugs. Here's an example:

  • Layer 1: World (walls, floors, bridges, ect)
  • Layer 2: Player
  • Layer 3: Enemies
  • Layer 4: Projectiles
  • Etc.

Soft world[edit | edit source]

If your 3D game uses SoftBody, you can simulate objects that deform when another object collides with it. Useful for capes and curtains. If not used, it is best to turn off Project Settings -> Physics -> 3d -> Active Soft World to increase performance.

Physics engines[edit | edit source]

3D
  • GodotPhysics: Simple and functional. Incompatible with some collision shapes.
  • Bullet: The deafult. Constantly checks for collision, missing fewer collisions, but having more performance cost. Use if your game has fast moving objects, like racecars or bullets.
2D
  • GodotPhysics: The default. Simple and functional.



Guide to the Godot game engine

Getting started [edit]
Installation
What is a node?
Programming
Resources and importing
Signals and methods
Your first game
Making it work
Debugging
Input
Physics
Saving and loading
Multiplayer
Making it look good
UI skinning
Animation
Advanced help
Servers (singletons)
Platform specific
Optimisation
Encryption
Exporting
Plugins
Miscellaneous
Helpful links
Authors and contributors
Print version


<-- previous back to top next -->