using_area_2d.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.png
  23. The first eight properties are used to configure the area's physics override
  24. behavior. We'll look at how to use those in the section below.
  25. *Monitoring* and *Monitorable* are used to enable and disable the area.
  26. The "Collision" section is where you configure the area's collision layer(s)
  27. and mask(s).
  28. The "Audio Bus" section allows you to override audio in the area, for example to
  29. apply an audio effect when the player moves through.
  30. Note that Area2D extends :ref:`CollisionObject2D <class_CollisionObject2D>`, so it
  31. also provides properties inherited from that class, such as ``input_pickable``.
  32. Overlap detection
  33. -----------------
  34. Perhaps the most common use of Area2D nodes is for contact and overlap detection.
  35. When you need to know that two objects have touched, but don't need physical
  36. collision, you can use an area to notify you of the contact.
  37. For example, let's say we're making a coin for the player to pick up. The coin is
  38. not a solid object - the player can't stand on it or push it - we just want it
  39. to disappear when the player touches it.
  40. Here's the node setup for the coin:
  41. .. image:: img/area2d_coin_nodes.png
  42. To detect the overlap, we'll connect the appropriate signal on the Area2d. Which
  43. signal to use depends on the player's node type. If the player is another area,
  44. use ``area_entered``. However, let's assume our player is a ``KinematicBody2D``
  45. (and therefore a ``CollisionObject2D`` type), so we'll connect the
  46. ``body_entered`` signal.
  47. .. note:: If you're not familiar with using signals, see :ref:`doc_signals` for
  48. an introduction.
  49. .. tabs::
  50. .. code-tab:: gdscript GDScript
  51. extends Area2D
  52. func _on_Coin_body_entered(body):
  53. queue_free()
  54. .. code-tab:: csharp
  55. public class Coin : Area2D
  56. {
  57. public void OnCoinBodyEntered(PhysicsBody2D body)
  58. {
  59. QueueFree();
  60. }
  61. }
  62. Now our player can collect the coins!
  63. Some other usage examples:
  64. - Areas are great for bullets and other projectiles that hit and deal damage, but don't need any other physics such as bouncing.
  65. - 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.
  66. - "Security cameras" - In a large level with multiple cameras, attach areas to each camera and activate them when the player enters.
  67. See the :ref:`doc_your_first_2d_game` for an example of using Area2D in a game.
  68. Area influence
  69. --------------
  70. The second major use for area nodes is to alter physics. By default, the area
  71. won't do this, but you can enable this with the *Space Override* property. When
  72. areas overlap, they are processed in *Priority* order (higher priority areas are
  73. processed first). There are four options for override:
  74. - *Combine* - The area adds its values to what has been calculated so far.
  75. - *Replace* - The area replaces physics properties, and lower priority areas are ignored.
  76. - *Combine-Replace* - The area adds its gravity/damping values to whatever has been calculated so far (in priority order), ignoring any lower priority areas.
  77. - *Replace-Combine* - The area replaces any gravity/damping calculated so far, but keeps calculating the rest of the areas.
  78. Using these properties, you can create very complex behavior with multiple
  79. overlapping areas.
  80. The physics properties that can be overridden are:
  81. - *Gravity* - Gravity's strength inside the area.
  82. - *Gravity Vec* - Gravity's direction. This vector does not need to be normalized.
  83. - *Linear Damp* - How quickly objects stop moving - linear velocity lost per second.
  84. - *Angular Damp* - How quickly objects stop spinning - angular velocity lost per second.
  85. Point gravity
  86. ~~~~~~~~~~~~~
  87. The *Gravity Point* property allows you to create an "attractor". Gravity in the
  88. area will be calculated towards a point, given by the *Gravity Vec* property.
  89. Values are relative to the Area2D, so for example using ``(0, 0)`` will attract
  90. objects to the center of the area.
  91. Examples
  92. ~~~~~~~~
  93. The example project attached below has three areas demonstrating physics
  94. override.
  95. .. image:: img/area2d_override.gif
  96. You can download this project here:
  97. :download:`using_area_2d.zip <files/using_area_2d.zip>`