The use of Area2D in physics in game development

Introduction

Godot provides many collision objects to provide collision detection and response. Trying to determine which option to use for your project can be confusing. If you understand the working principle and advantages and disadvantages of each problem, you can avoid these problems and simplify development. In this tutorial, we will look at the Area2D node and show some examples of how to use it.

note

This document assumes that you are familiar with the various physical institutions of Godot. Please read the introduction to physics first.

What is the area?

Area2D defines an area of ​​2D space. In this space, you can detect that other CollisionObject2D nodes overlap, enter and exit. Areas also allow to override local physical attributes. We will explore these features below.

Area attributes

Areas have many attributes that can be used to customize their behavior.

../../_images/area2d_properties.png

The first eight attributes are used to configure the physical substitution behavior of the area. We will describe how to use them in the following sections.

Monitoring and monitoring are used to enable and disable areas.

In the "Collision" section, you can configure the collision layer and mask of the area.

The "Audio Bus" section allows you to overlay audio in this area, for example to apply audio effects when the player moves.

Please note that Area2D extends CollisionObject2D, so it also provides properties inherited from this class, such as input_pickable.

Overlap detection

The most common use of Area2D nodes may be for contact and overlap detection. When you need to know that two objects have been touched but do not need to physically collide, you can use the area to notify you of the contact.

For example, suppose we are picking coins for the player. The coin is not a solid object-the player cannot stand or push it-we only want it to disappear when the player touches it.

This is the node setting of the coin:

../../_images/area2d_coin_nodes.png

To detect overlap, we connect the appropriate signal to Area2d. Which signal is used depends on the node type of the player. If the player is in another area, please use area_entered. But, assuming our player is a KinematicBody2D (hence a CollisionObject2D type), then we will connect the body_entered signal.

note

If you are not familiar with using signals, please refer to the introduction of signals.

public class Coin : Area2D
{
    
    

    public void OnCoinBodyEntered(PhysicsBody2D body)
    {
    
    
        QueueFree();
    }
}

Now our players can collect coins!

Some other usage examples:

  • These areas are ideal for bullets and other projectiles that hit and cause damage, but do not require any other physical processes, such as bounce.
  • Use a larger circular area around the enemy to define its "detection" radius. When the player is outside the area, the enemy cannot "see" it.
  • "Security cameras"-On larger levels with multiple cameras, attach areas to each camera and activate them when the player enters.

For an example of using Area2D in a game, see your first game.

Regional influence

The second main use of regional nodes is to change physics. By default, this area does not do this, but you can enable it using the "Space Overlay" attribute. When regions overlap, they will be processed in order of priority (regions with higher priority will be processed first). There are four alternative options:

  • Consolidation-The area adds its value to the value calculated so far.
  • Replacement-replace the physical attribute of the area, the lower priority area will be ignored.
  • Combination Replacement-This area adds its gravity/damping value to everything calculated so far (in order of priority), ignoring any lower priority areas.
  • Replace-Combine-Replace this area to replace all the gravity/damping calculated so far, but continue to calculate the remaining areas.

Using these properties, you can create very complex behaviors with multiple overlapping areas.

The physical properties that can be overridden are:

  • Gravity-the strength of gravity in the area.
  • Gravity Vec- the direction of gravity. This vector does not need to be normalized.
  • Linear damping-the speed at which the object stops moving-the linear velocity lost per second.
  • Angular damping-the speed at which the object stops rotating-the angular velocity lost per second.

Point gravity

The gravity point attribute allows you to create "primers". The gravity of the area will be calculated based on the points given by the Gravity Vec property. The value is relative to Area2D, so for example using (0,0) will attract the object to the center of the area.

example

The sample project attached below contains three areas that illustrate physical coverage.

Insert picture description here

You can download the project here: using_area_2d.zip

Guess you like

Origin blog.csdn.net/qq_44273429/article/details/111312144