using_area_2d.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. .. _doc_using_area_2d:
  2. Using Area2D
  3. ============
  4. Introduction
  5. ------------
  6. Godot offers a number of collision objects to provide both collision detection
  7. and response. Trying to decide which one to use for your project can be confusing.
  8. You can avoid problems and simplify development if you understand how each of them
  9. works and what their pros and cons are. In this tutorial, we'll look at the
  10. :ref:`Area2D <class_Area2D>` node and show some examples of how it can be used.
  11. .. note:: This document assumes you're familiar with Godot's various physics
  12. bodies. Please read :ref:`doc_physics_introduction` first.
  13. What is an area?
  14. ----------------
  15. An Area2D defines a region of 2D space. In this space you can detect other
  16. :ref:`CollisionObject2D <class_CollisionObject2D>` nodes overlapping, entering,
  17. and exiting. Areas also allow for overriding local physics properties. We'll
  18. explore each of these functions below.
  19. Area properties
  20. ---------------
  21. Areas have many properties you can use to customize their behavior.
  22. .. image:: img/area2d_properties.webp
  23. The ``Gravity``, ``Linear Damp``, and ``Angular Damp`` sections
  24. are used to configure the area's physics override behavior.
  25. We'll look at how to use those in the *Area influence* section below.
  26. ``Monitoring`` and ``Monitorable`` are used to enable and disable the area.
  27. The ``Audio Bus`` section allows you to override audio in the area, for example to
  28. apply an audio effect when the player moves through.
  29. Note that Area2D extends :ref:`CollisionObject2D <class_CollisionObject2D>`, so it
  30. also provides properties inherited from that class.
  31. The ``Collision`` section of ``CollisionObject2D`` is where you configure the
  32. area's collision layer(s) and mask(s).
  33. Overlap detection
  34. -----------------
  35. Perhaps the most common use of Area2D nodes is for contact and overlap detection.
  36. When you need to know that two objects have touched, but don't need physical
  37. collision, you can use an area to notify you of the contact.
  38. For example, let's say we're making a coin for the player to pick up. The coin is
  39. not a solid object - the player can't stand on it or push it - we just want it
  40. to disappear when the player touches it.
  41. Here's the node setup for the coin:
  42. .. image:: img/area2d_coin_nodes.webp
  43. To detect the overlap, we'll connect the appropriate signal on the Area2D. Which
  44. signal to use depends on the player's node type. If the player is another area,
  45. use ``area_entered``. However, let's assume our player is a ``CharacterBody2D``
  46. (and therefore a ``CollisionObject2D`` type), so we'll connect the
  47. ``body_entered`` signal.
  48. .. note:: If you're not familiar with using signals, see :ref:`doc_signals` for
  49. an introduction.
  50. .. tabs::
  51. .. code-tab:: gdscript GDScript
  52. extends Area2D
  53. func _on_coin_body_entered(body):
  54. queue_free()
  55. .. code-tab:: csharp
  56. using Godot;
  57. public partial class Coin : Area2D
  58. {
  59. private void OnCoinBodyEntered(PhysicsBody2D body)
  60. {
  61. QueueFree();
  62. }
  63. }
  64. Now our player can collect the coins!
  65. Some other usage examples:
  66. - Areas are great for bullets and other projectiles that hit and deal damage, but don't need any other physics such as bouncing.
  67. - Use a large circular area around an enemy to define its "detect" radius. When the player is outside the area, the enemy can't "see" it.
  68. - "Security cameras" - In a large level with multiple cameras, attach areas to each camera and activate them when the player enters.
  69. See the :ref:`doc_your_first_2d_game` for an example of using Area2D in a game.
  70. Area influence
  71. --------------
  72. The second major use for area nodes is to alter physics. By default, the area
  73. won't do this, but you can enable this with the ``Space Override`` property. When
  74. areas overlap, they are processed in ``Priority`` order (higher priority areas are
  75. processed first). There are four options for override:
  76. - *Combine* - The area adds its values to what has been calculated so far.
  77. - *Replace* - The area replaces physics properties, and lower priority areas are ignored.
  78. - *Combine-Replace* - The area adds its gravity/damping values to whatever has been calculated so far (in priority order), ignoring any lower priority areas.
  79. - *Replace-Combine* - The area replaces any gravity/damping calculated so far, but keeps calculating the rest of the areas.
  80. Using these properties, you can create very complex behavior with multiple
  81. overlapping areas.
  82. The physics properties that can be overridden are:
  83. - *Gravity* - Gravity's strength inside the area.
  84. - *Gravity Direction* - This vector does not need to be normalized.
  85. - *Linear Damp* - How quickly objects stop moving - linear velocity lost per second.
  86. - *Angular Damp* - How quickly objects stop spinning - angular velocity lost per second.
  87. Point gravity
  88. ~~~~~~~~~~~~~
  89. The ``Gravity Point`` property allows you to create an "attractor". Gravity in the
  90. area will be calculated towards a point, given by the ``Point Center`` property.
  91. Values are relative to the Area2D, so for example using ``(0, 0)`` will attract
  92. objects to the center of the area.
  93. Examples
  94. ~~~~~~~~
  95. The example project attached below has three areas demonstrating physics
  96. override.
  97. .. image:: img/area2d_override.gif
  98. You can download this project here:
  99. `area_2d_starter.zip <https://github.com/godotengine/godot-docs-project-starters/releases/download/latest-4.x/area_2d_starter.zip>`_