Skip to content

Latest commit

Β 

History

History
664 lines (371 loc) Β· 31.2 KB

class_area2d.rst

File metadata and controls

664 lines (371 loc) Β· 31.2 KB
github_url:hide

Area2D

Inherits: :ref:`CollisionObject2D<class_CollisionObject2D>` < :ref:`Node2D<class_Node2D>` < :ref:`CanvasItem<class_CanvasItem>` < :ref:`Node<class_Node>` < :ref:`Object<class_Object>`

A region of 2D space that detects other :ref:`CollisionObject2D<class_CollisionObject2D>`s entering or exiting it.

.. rst-class:: classref-introduction-group

Description

Area2D is a region of 2D space defined by one or multiple :ref:`CollisionShape2D<class_CollisionShape2D>` or :ref:`CollisionPolygon2D<class_CollisionPolygon2D>` child nodes. It detects when other :ref:`CollisionObject2D<class_CollisionObject2D>`s enter or exit it, and it also keeps track of which collision objects haven't exited it yet (i.e. which one are overlapping it).

This node can also locally alter or override physics parameters (gravity, damping) and route audio to custom audio buses.

Note: Areas and bodies created with :ref:`PhysicsServer2D<class_PhysicsServer2D>` might not interact as expected with Area2Ds, and might not emit signals or track objects correctly.

.. rst-class:: classref-introduction-group

Tutorials

.. rst-class:: classref-reftable-group

Properties

:ref:`float<class_float>` :ref:`angular_damp<class_Area2D_property_angular_damp>` 1.0
:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` :ref:`angular_damp_space_override<class_Area2D_property_angular_damp_space_override>` 0
:ref:`StringName<class_StringName>` :ref:`audio_bus_name<class_Area2D_property_audio_bus_name>` &"Master"
:ref:`bool<class_bool>` :ref:`audio_bus_override<class_Area2D_property_audio_bus_override>` false
:ref:`float<class_float>` :ref:`gravity<class_Area2D_property_gravity>` 980.0
:ref:`Vector2<class_Vector2>` :ref:`gravity_direction<class_Area2D_property_gravity_direction>` Vector2(0, 1)
:ref:`bool<class_bool>` :ref:`gravity_point<class_Area2D_property_gravity_point>` false
:ref:`Vector2<class_Vector2>` :ref:`gravity_point_center<class_Area2D_property_gravity_point_center>` Vector2(0, 1)
:ref:`float<class_float>` :ref:`gravity_point_unit_distance<class_Area2D_property_gravity_point_unit_distance>` 0.0
:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` :ref:`gravity_space_override<class_Area2D_property_gravity_space_override>` 0
:ref:`float<class_float>` :ref:`linear_damp<class_Area2D_property_linear_damp>` 0.1
:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` :ref:`linear_damp_space_override<class_Area2D_property_linear_damp_space_override>` 0
:ref:`bool<class_bool>` :ref:`monitorable<class_Area2D_property_monitorable>` true
:ref:`bool<class_bool>` :ref:`monitoring<class_Area2D_property_monitoring>` true
:ref:`int<class_int>` :ref:`priority<class_Area2D_property_priority>` 0
.. rst-class:: classref-reftable-group

Methods

:ref:`Array<class_Array>`[:ref:`Area2D<class_Area2D>`] :ref:`get_overlapping_areas<class_Area2D_method_get_overlapping_areas>`() |const|
:ref:`Array<class_Array>`[:ref:`Node2D<class_Node2D>`] :ref:`get_overlapping_bodies<class_Area2D_method_get_overlapping_bodies>`() |const|
:ref:`bool<class_bool>` :ref:`has_overlapping_areas<class_Area2D_method_has_overlapping_areas>`() |const|
:ref:`bool<class_bool>` :ref:`has_overlapping_bodies<class_Area2D_method_has_overlapping_bodies>`() |const|
:ref:`bool<class_bool>` :ref:`overlaps_area<class_Area2D_method_overlaps_area>`(area: :ref:`Node<class_Node>`) |const|
:ref:`bool<class_bool>` :ref:`overlaps_body<class_Area2D_method_overlaps_body>`(body: :ref:`Node<class_Node>`) |const|
.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Signals

.. rst-class:: classref-signal

area_entered(area: :ref:`Area2D<class_Area2D>`) :ref:`πŸ”—<class_Area2D_signal_area_entered>`

Emitted when the received area enters this area. Requires :ref:`monitoring<class_Area2D_property_monitoring>` to be set to true.

.. rst-class:: classref-item-separator


.. rst-class:: classref-signal

area_exited(area: :ref:`Area2D<class_Area2D>`) :ref:`πŸ”—<class_Area2D_signal_area_exited>`

Emitted when the received area exits this area. Requires :ref:`monitoring<class_Area2D_property_monitoring>` to be set to true.

.. rst-class:: classref-item-separator


.. rst-class:: classref-signal

area_shape_entered(area_rid: :ref:`RID<class_RID>`, area: :ref:`Area2D<class_Area2D>`, area_shape_index: :ref:`int<class_int>`, local_shape_index: :ref:`int<class_int>`) :ref:`πŸ”—<class_Area2D_signal_area_shape_entered>`

Emitted when a :ref:`Shape2D<class_Shape2D>` of the received area enters a shape of this area. Requires :ref:`monitoring<class_Area2D_property_monitoring>` to be set to true.

local_shape_index and area_shape_index contain indices of the interacting shapes from this area and the other area, respectively. area_rid contains the :ref:`RID<class_RID>` of the other area. These values can be used with the :ref:`PhysicsServer2D<class_PhysicsServer2D>`.

Example of getting the :ref:`CollisionShape2D<class_CollisionShape2D>` node from the shape index:

.. tabs::

 .. code-tab:: gdscript

    var other_shape_owner = area.shape_find_owner(area_shape_index)
    var other_shape_node = area.shape_owner_get_owner(other_shape_owner)

    var local_shape_owner = shape_find_owner(local_shape_index)
    var local_shape_node = shape_owner_get_owner(local_shape_owner)



.. rst-class:: classref-item-separator


.. rst-class:: classref-signal

area_shape_exited(area_rid: :ref:`RID<class_RID>`, area: :ref:`Area2D<class_Area2D>`, area_shape_index: :ref:`int<class_int>`, local_shape_index: :ref:`int<class_int>`) :ref:`πŸ”—<class_Area2D_signal_area_shape_exited>`

Emitted when a :ref:`Shape2D<class_Shape2D>` of the received area exits a shape of this area. Requires :ref:`monitoring<class_Area2D_property_monitoring>` to be set to true.

See also :ref:`area_shape_entered<class_Area2D_signal_area_shape_entered>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-signal

body_entered(body: :ref:`Node2D<class_Node2D>`) :ref:`πŸ”—<class_Area2D_signal_body_entered>`

Emitted when the received body enters this area. body can be a :ref:`PhysicsBody2D<class_PhysicsBody2D>` or a :ref:`TileMap<class_TileMap>`. :ref:`TileMap<class_TileMap>`s are detected if their :ref:`TileSet<class_TileSet>` has collision shapes configured. Requires :ref:`monitoring<class_Area2D_property_monitoring>` to be set to true.

.. rst-class:: classref-item-separator


.. rst-class:: classref-signal

body_exited(body: :ref:`Node2D<class_Node2D>`) :ref:`πŸ”—<class_Area2D_signal_body_exited>`

Emitted when the received body exits this area. body can be a :ref:`PhysicsBody2D<class_PhysicsBody2D>` or a :ref:`TileMap<class_TileMap>`. :ref:`TileMap<class_TileMap>`s are detected if their :ref:`TileSet<class_TileSet>` has collision shapes configured. Requires :ref:`monitoring<class_Area2D_property_monitoring>` to be set to true.

.. rst-class:: classref-item-separator


.. rst-class:: classref-signal

body_shape_entered(body_rid: :ref:`RID<class_RID>`, body: :ref:`Node2D<class_Node2D>`, body_shape_index: :ref:`int<class_int>`, local_shape_index: :ref:`int<class_int>`) :ref:`πŸ”—<class_Area2D_signal_body_shape_entered>`

Emitted when a :ref:`Shape2D<class_Shape2D>` of the received body enters a shape of this area. body can be a :ref:`PhysicsBody2D<class_PhysicsBody2D>` or a :ref:`TileMap<class_TileMap>`. :ref:`TileMap<class_TileMap>`s are detected if their :ref:`TileSet<class_TileSet>` has collision shapes configured. Requires :ref:`monitoring<class_Area2D_property_monitoring>` to be set to true.

local_shape_index and body_shape_index contain indices of the interacting shapes from this area and the interacting body, respectively. body_rid contains the :ref:`RID<class_RID>` of the body. These values can be used with the :ref:`PhysicsServer2D<class_PhysicsServer2D>`.

Example of getting the :ref:`CollisionShape2D<class_CollisionShape2D>` node from the shape index:

.. tabs::

 .. code-tab:: gdscript

    var body_shape_owner = body.shape_find_owner(body_shape_index)
    var body_shape_node = body.shape_owner_get_owner(body_shape_owner)

    var local_shape_owner = shape_find_owner(local_shape_index)
    var local_shape_node = shape_owner_get_owner(local_shape_owner)



.. rst-class:: classref-item-separator


.. rst-class:: classref-signal

body_shape_exited(body_rid: :ref:`RID<class_RID>`, body: :ref:`Node2D<class_Node2D>`, body_shape_index: :ref:`int<class_int>`, local_shape_index: :ref:`int<class_int>`) :ref:`πŸ”—<class_Area2D_signal_body_shape_exited>`

Emitted when a :ref:`Shape2D<class_Shape2D>` of the received body exits a shape of this area. body can be a :ref:`PhysicsBody2D<class_PhysicsBody2D>` or a :ref:`TileMap<class_TileMap>`. :ref:`TileMap<class_TileMap>`s are detected if their :ref:`TileSet<class_TileSet>` has collision shapes configured. Requires :ref:`monitoring<class_Area2D_property_monitoring>` to be set to true.

See also :ref:`body_shape_entered<class_Area2D_signal_body_shape_entered>`.

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Enumerations

.. rst-class:: classref-enumeration

enum SpaceOverride: :ref:`πŸ”—<enum_Area2D_SpaceOverride>`

.. rst-class:: classref-enumeration-constant

:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` SPACE_OVERRIDE_DISABLED = 0

This area does not affect gravity/damping.

.. rst-class:: classref-enumeration-constant

:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` SPACE_OVERRIDE_COMBINE = 1

This area adds its gravity/damping values to whatever has been calculated so far (in :ref:`priority<class_Area2D_property_priority>` order).

.. rst-class:: classref-enumeration-constant

:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` SPACE_OVERRIDE_COMBINE_REPLACE = 2

This area adds its gravity/damping values to whatever has been calculated so far (in :ref:`priority<class_Area2D_property_priority>` order), ignoring any lower priority areas.

.. rst-class:: classref-enumeration-constant

:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` SPACE_OVERRIDE_REPLACE = 3

This area replaces any gravity/damping, even the defaults, ignoring any lower priority areas.

.. rst-class:: classref-enumeration-constant

:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` SPACE_OVERRIDE_REPLACE_COMBINE = 4

This area replaces any gravity/damping calculated so far (in :ref:`priority<class_Area2D_property_priority>` order), but keeps calculating the rest of the areas.

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Property Descriptions

.. rst-class:: classref-property

:ref:`float<class_float>` angular_damp = 1.0 :ref:`πŸ”—<class_Area2D_property_angular_damp>`

.. rst-class:: classref-property-setget

The rate at which objects stop spinning in this area. Represents the angular velocity lost per second.

See :ref:`ProjectSettings.physics/2d/default_angular_damp<class_ProjectSettings_property_physics/2d/default_angular_damp>` for more details about damping.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` angular_damp_space_override = 0 :ref:`πŸ”—<class_Area2D_property_angular_damp_space_override>`

.. rst-class:: classref-property-setget

Override mode for angular damping calculations within this area. See :ref:`SpaceOverride<enum_Area2D_SpaceOverride>` for possible values.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`StringName<class_StringName>` audio_bus_name = &"Master" :ref:`πŸ”—<class_Area2D_property_audio_bus_name>`

.. rst-class:: classref-property-setget

The name of the area's audio bus.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`bool<class_bool>` audio_bus_override = false :ref:`πŸ”—<class_Area2D_property_audio_bus_override>`

.. rst-class:: classref-property-setget

If true, the area's audio bus overrides the default audio bus.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` gravity = 980.0 :ref:`πŸ”—<class_Area2D_property_gravity>`

.. rst-class:: classref-property-setget

The area's gravity intensity (in pixels per second squared). This value multiplies the gravity direction. This is useful to alter the force of gravity without altering its direction.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`Vector2<class_Vector2>` gravity_direction = Vector2(0, 1) :ref:`πŸ”—<class_Area2D_property_gravity_direction>`

.. rst-class:: classref-property-setget

The area's gravity vector (not normalized).

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`bool<class_bool>` gravity_point = false :ref:`πŸ”—<class_Area2D_property_gravity_point>`

.. rst-class:: classref-property-setget

If true, gravity is calculated from a point (set via :ref:`gravity_point_center<class_Area2D_property_gravity_point_center>`). See also :ref:`gravity_space_override<class_Area2D_property_gravity_space_override>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`Vector2<class_Vector2>` gravity_point_center = Vector2(0, 1) :ref:`πŸ”—<class_Area2D_property_gravity_point_center>`

.. rst-class:: classref-property-setget

If gravity is a point (see :ref:`gravity_point<class_Area2D_property_gravity_point>`), this will be the point of attraction.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` gravity_point_unit_distance = 0.0 :ref:`πŸ”—<class_Area2D_property_gravity_point_unit_distance>`

.. rst-class:: classref-property-setget

The distance at which the gravity strength is equal to :ref:`gravity<class_Area2D_property_gravity>`. For example, on a planet 100 pixels in radius with a surface gravity of 4.0 px/sΒ², set the :ref:`gravity<class_Area2D_property_gravity>` to 4.0 and the unit distance to 100.0. The gravity will have falloff according to the inverse square law, so in the example, at 200 pixels from the center the gravity will be 1.0 px/sΒ² (twice the distance, 1/4th the gravity), at 50 pixels it will be 16.0 px/sΒ² (half the distance, 4x the gravity), and so on.

The above is true only when the unit distance is a positive number. When this is set to 0.0, the gravity will be constant regardless of distance.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` gravity_space_override = 0 :ref:`πŸ”—<class_Area2D_property_gravity_space_override>`

.. rst-class:: classref-property-setget

Override mode for gravity calculations within this area. See :ref:`SpaceOverride<enum_Area2D_SpaceOverride>` for possible values.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` linear_damp = 0.1 :ref:`πŸ”—<class_Area2D_property_linear_damp>`

.. rst-class:: classref-property-setget

The rate at which objects stop moving in this area. Represents the linear velocity lost per second.

See :ref:`ProjectSettings.physics/2d/default_linear_damp<class_ProjectSettings_property_physics/2d/default_linear_damp>` for more details about damping.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`SpaceOverride<enum_Area2D_SpaceOverride>` linear_damp_space_override = 0 :ref:`πŸ”—<class_Area2D_property_linear_damp_space_override>`

.. rst-class:: classref-property-setget

Override mode for linear damping calculations within this area. See :ref:`SpaceOverride<enum_Area2D_SpaceOverride>` for possible values.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`bool<class_bool>` monitorable = true :ref:`πŸ”—<class_Area2D_property_monitorable>`

.. rst-class:: classref-property-setget

If true, other monitoring areas can detect this area.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`bool<class_bool>` monitoring = true :ref:`πŸ”—<class_Area2D_property_monitoring>`

.. rst-class:: classref-property-setget

If true, the area detects bodies or areas entering and exiting it.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`int<class_int>` priority = 0 :ref:`πŸ”—<class_Area2D_property_priority>`

.. rst-class:: classref-property-setget

The area's priority. Higher priority areas are processed first. The :ref:`World2D<class_World2D>`'s physics is always processed last, after all areas.

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Method Descriptions

.. rst-class:: classref-method

:ref:`Array<class_Array>`[:ref:`Area2D<class_Area2D>`] get_overlapping_areas() |const| :ref:`πŸ”—<class_Area2D_method_get_overlapping_areas>`

Returns a list of intersecting Area2Ds. The overlapping area's :ref:`CollisionObject2D.collision_layer<class_CollisionObject2D_property_collision_layer>` must be part of this area's :ref:`CollisionObject2D.collision_mask<class_CollisionObject2D_property_collision_mask>` in order to be detected.

For performance reasons (collisions are all processed at the same time) this list is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Array<class_Array>`[:ref:`Node2D<class_Node2D>`] get_overlapping_bodies() |const| :ref:`πŸ”—<class_Area2D_method_get_overlapping_bodies>`

Returns a list of intersecting :ref:`PhysicsBody2D<class_PhysicsBody2D>`s and :ref:`TileMap<class_TileMap>`s. The overlapping body's :ref:`CollisionObject2D.collision_layer<class_CollisionObject2D_property_collision_layer>` must be part of this area's :ref:`CollisionObject2D.collision_mask<class_CollisionObject2D_property_collision_mask>` in order to be detected.

For performance reasons (collisions are all processed at the same time) this list is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` has_overlapping_areas() |const| :ref:`πŸ”—<class_Area2D_method_has_overlapping_areas>`

Returns true if intersecting any Area2Ds, otherwise returns false. The overlapping area's :ref:`CollisionObject2D.collision_layer<class_CollisionObject2D_property_collision_layer>` must be part of this area's :ref:`CollisionObject2D.collision_mask<class_CollisionObject2D_property_collision_mask>` in order to be detected.

For performance reasons (collisions are all processed at the same time) the list of overlapping areas is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` has_overlapping_bodies() |const| :ref:`πŸ”—<class_Area2D_method_has_overlapping_bodies>`

Returns true if intersecting any :ref:`PhysicsBody2D<class_PhysicsBody2D>`s or :ref:`TileMap<class_TileMap>`s, otherwise returns false. The overlapping body's :ref:`CollisionObject2D.collision_layer<class_CollisionObject2D_property_collision_layer>` must be part of this area's :ref:`CollisionObject2D.collision_mask<class_CollisionObject2D_property_collision_mask>` in order to be detected.

For performance reasons (collisions are all processed at the same time) the list of overlapping bodies is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` overlaps_area(area: :ref:`Node<class_Node>`) |const| :ref:`πŸ”—<class_Area2D_method_overlaps_area>`

Returns true if the given Area2D intersects or overlaps this Area2D, false otherwise.

Note: The result of this test is not immediate after moving objects. For performance, the list of overlaps is updated once per frame and before the physics step. Consider using signals instead.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` overlaps_body(body: :ref:`Node<class_Node>`) |const| :ref:`πŸ”—<class_Area2D_method_overlaps_body>`

Returns true if the given physics body intersects or overlaps this Area2D, false otherwise.

Note: The result of this test is not immediate after moving objects. For performance, list of overlaps is updated once per frame and before the physics step. Consider using signals instead.

The body argument can either be a :ref:`PhysicsBody2D<class_PhysicsBody2D>` or a :ref:`TileMap<class_TileMap>` instance. While TileMaps are not physics bodies themselves, they register their tiles with collision shapes as a virtual physics body.