class_shapecast2d.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/doc/classes/ShapeCast2D.xml.
  6. .. _class_ShapeCast2D:
  7. ShapeCast2D
  8. ===========
  9. **Inherits:** :ref:`Node2D<class_Node2D>` **<** :ref:`CanvasItem<class_CanvasItem>` **<** :ref:`Node<class_Node>` **<** :ref:`Object<class_Object>`
  10. Node for physics collision sweep and immediate overlap queries. Similar to the :ref:`RayCast2D<class_RayCast2D>` node.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. Shape casting allows to detect collision objects by sweeping the :ref:`shape<class_ShapeCast2D_property_shape>` along the cast direction determined by :ref:`target_position<class_ShapeCast2D_property_target_position>` (useful for things like beam weapons).
  15. Immediate collision overlaps can be done with the :ref:`target_position<class_ShapeCast2D_property_target_position>` set to ``Vector2(0, 0)`` and by calling :ref:`force_shapecast_update<class_ShapeCast2D_method_force_shapecast_update>` within the same **physics_frame**. This also helps to overcome some limitations of :ref:`Area2D<class_Area2D>` when used as a continuous detection area, often requiring waiting a couple of frames before collision information is available to :ref:`Area2D<class_Area2D>` nodes, and when using the signals creates unnecessary complexity.
  16. The node can detect multiple collision objects, but it's usually used to detect the first collision.
  17. \ **Note:** Shape casting is more computationally expensive compared to ray casting.
  18. .. rst-class:: classref-reftable-group
  19. Properties
  20. ----------
  21. .. table::
  22. :widths: auto
  23. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  24. | :ref:`bool<class_bool>` | :ref:`collide_with_areas<class_ShapeCast2D_property_collide_with_areas>` | ``false`` |
  25. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  26. | :ref:`bool<class_bool>` | :ref:`collide_with_bodies<class_ShapeCast2D_property_collide_with_bodies>` | ``true`` |
  27. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  28. | :ref:`int<class_int>` | :ref:`collision_mask<class_ShapeCast2D_property_collision_mask>` | ``1`` |
  29. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  30. | :ref:`Array<class_Array>` | :ref:`collision_result<class_ShapeCast2D_property_collision_result>` | |
  31. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  32. | :ref:`bool<class_bool>` | :ref:`enabled<class_ShapeCast2D_property_enabled>` | ``true`` |
  33. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  34. | :ref:`bool<class_bool>` | :ref:`exclude_parent<class_ShapeCast2D_property_exclude_parent>` | ``true`` |
  35. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  36. | :ref:`float<class_float>` | :ref:`margin<class_ShapeCast2D_property_margin>` | ``0.0`` |
  37. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  38. | :ref:`int<class_int>` | :ref:`max_results<class_ShapeCast2D_property_max_results>` | ``32`` |
  39. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  40. | :ref:`Shape2D<class_Shape2D>` | :ref:`shape<class_ShapeCast2D_property_shape>` | |
  41. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  42. | :ref:`Vector2<class_Vector2>` | :ref:`target_position<class_ShapeCast2D_property_target_position>` | ``Vector2( 0, 50 )`` |
  43. +-------------------------------+----------------------------------------------------------------------------+----------------------+
  44. .. rst-class:: classref-reftable-group
  45. Methods
  46. -------
  47. .. table::
  48. :widths: auto
  49. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | void | :ref:`add_exception<class_ShapeCast2D_method_add_exception>` **(** :ref:`Object<class_Object>` node **)** |
  51. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | void | :ref:`add_exception_rid<class_ShapeCast2D_method_add_exception_rid>` **(** :ref:`RID<class_RID>` rid **)** |
  53. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | void | :ref:`clear_exceptions<class_ShapeCast2D_method_clear_exceptions>` **(** **)** |
  55. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | void | :ref:`force_shapecast_update<class_ShapeCast2D_method_force_shapecast_update>` **(** **)** |
  57. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`float<class_float>` | :ref:`get_closest_collision_safe_fraction<class_ShapeCast2D_method_get_closest_collision_safe_fraction>` **(** **)** |const| |
  59. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :ref:`float<class_float>` | :ref:`get_closest_collision_unsafe_fraction<class_ShapeCast2D_method_get_closest_collision_unsafe_fraction>` **(** **)** |const| |
  61. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`Object<class_Object>` | :ref:`get_collider<class_ShapeCast2D_method_get_collider>` **(** :ref:`int<class_int>` index **)** |const| |
  63. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`RID<class_RID>` | :ref:`get_collider_rid<class_ShapeCast2D_method_get_collider_rid>` **(** :ref:`int<class_int>` index **)** |const| |
  65. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`int<class_int>` | :ref:`get_collider_shape<class_ShapeCast2D_method_get_collider_shape>` **(** :ref:`int<class_int>` index **)** |const| |
  67. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`int<class_int>` | :ref:`get_collision_count<class_ShapeCast2D_method_get_collision_count>` **(** **)** |const| |
  69. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | :ref:`bool<class_bool>` | :ref:`get_collision_mask_value<class_ShapeCast2D_method_get_collision_mask_value>` **(** :ref:`int<class_int>` layer_number **)** |const| |
  71. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`Vector2<class_Vector2>` | :ref:`get_collision_normal<class_ShapeCast2D_method_get_collision_normal>` **(** :ref:`int<class_int>` index **)** |const| |
  73. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`Vector2<class_Vector2>` | :ref:`get_collision_point<class_ShapeCast2D_method_get_collision_point>` **(** :ref:`int<class_int>` index **)** |const| |
  75. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`bool<class_bool>` | :ref:`is_colliding<class_ShapeCast2D_method_is_colliding>` **(** **)** |const| |
  77. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. | void | :ref:`remove_exception<class_ShapeCast2D_method_remove_exception>` **(** :ref:`Object<class_Object>` node **)** |
  79. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  80. | void | :ref:`remove_exception_rid<class_ShapeCast2D_method_remove_exception_rid>` **(** :ref:`RID<class_RID>` rid **)** |
  81. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | void | :ref:`set_collision_mask_value<class_ShapeCast2D_method_set_collision_mask_value>` **(** :ref:`int<class_int>` layer_number, :ref:`bool<class_bool>` value **)** |
  83. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. .. rst-class:: classref-section-separator
  85. ----
  86. .. rst-class:: classref-descriptions-group
  87. Property Descriptions
  88. ---------------------
  89. .. _class_ShapeCast2D_property_collide_with_areas:
  90. .. rst-class:: classref-property
  91. :ref:`bool<class_bool>` **collide_with_areas** = ``false``
  92. .. rst-class:: classref-property-setget
  93. - void **set_collide_with_areas** **(** :ref:`bool<class_bool>` value **)**
  94. - :ref:`bool<class_bool>` **is_collide_with_areas_enabled** **(** **)**
  95. If ``true``, collision with :ref:`Area2D<class_Area2D>`\ s will be reported.
  96. .. rst-class:: classref-item-separator
  97. ----
  98. .. _class_ShapeCast2D_property_collide_with_bodies:
  99. .. rst-class:: classref-property
  100. :ref:`bool<class_bool>` **collide_with_bodies** = ``true``
  101. .. rst-class:: classref-property-setget
  102. - void **set_collide_with_bodies** **(** :ref:`bool<class_bool>` value **)**
  103. - :ref:`bool<class_bool>` **is_collide_with_bodies_enabled** **(** **)**
  104. If ``true``, collision with :ref:`PhysicsBody2D<class_PhysicsBody2D>`\ s will be reported.
  105. .. rst-class:: classref-item-separator
  106. ----
  107. .. _class_ShapeCast2D_property_collision_mask:
  108. .. rst-class:: classref-property
  109. :ref:`int<class_int>` **collision_mask** = ``1``
  110. .. rst-class:: classref-property-setget
  111. - void **set_collision_mask** **(** :ref:`int<class_int>` value **)**
  112. - :ref:`int<class_int>` **get_collision_mask** **(** **)**
  113. The shape's collision mask. Only objects in at least one collision layer enabled in the mask will be detected.
  114. .. rst-class:: classref-item-separator
  115. ----
  116. .. _class_ShapeCast2D_property_collision_result:
  117. .. rst-class:: classref-property
  118. :ref:`Array<class_Array>` **collision_result**
  119. Returns the complete collision information from the collision sweep. The data returned is the same as in the :ref:`Physics2DDirectSpaceState.get_rest_info<class_Physics2DDirectSpaceState_method_get_rest_info>` method.
  120. .. rst-class:: classref-item-separator
  121. ----
  122. .. _class_ShapeCast2D_property_enabled:
  123. .. rst-class:: classref-property
  124. :ref:`bool<class_bool>` **enabled** = ``true``
  125. .. rst-class:: classref-property-setget
  126. - void **set_enabled** **(** :ref:`bool<class_bool>` value **)**
  127. - :ref:`bool<class_bool>` **is_enabled** **(** **)**
  128. If ``true``, collisions will be reported.
  129. .. rst-class:: classref-item-separator
  130. ----
  131. .. _class_ShapeCast2D_property_exclude_parent:
  132. .. rst-class:: classref-property
  133. :ref:`bool<class_bool>` **exclude_parent** = ``true``
  134. .. rst-class:: classref-property-setget
  135. - void **set_exclude_parent_body** **(** :ref:`bool<class_bool>` value **)**
  136. - :ref:`bool<class_bool>` **get_exclude_parent_body** **(** **)**
  137. If ``true``, the parent node will be excluded from collision detection.
  138. .. rst-class:: classref-item-separator
  139. ----
  140. .. _class_ShapeCast2D_property_margin:
  141. .. rst-class:: classref-property
  142. :ref:`float<class_float>` **margin** = ``0.0``
  143. .. rst-class:: classref-property-setget
  144. - void **set_margin** **(** :ref:`float<class_float>` value **)**
  145. - :ref:`float<class_float>` **get_margin** **(** **)**
  146. The collision margin for the shape. A larger margin helps detecting collisions more consistently, at the cost of precision.
  147. .. rst-class:: classref-item-separator
  148. ----
  149. .. _class_ShapeCast2D_property_max_results:
  150. .. rst-class:: classref-property
  151. :ref:`int<class_int>` **max_results** = ``32``
  152. .. rst-class:: classref-property-setget
  153. - void **set_max_results** **(** :ref:`int<class_int>` value **)**
  154. - :ref:`int<class_int>` **get_max_results** **(** **)**
  155. The number of intersections can be limited with this parameter, to reduce the processing time.
  156. .. rst-class:: classref-item-separator
  157. ----
  158. .. _class_ShapeCast2D_property_shape:
  159. .. rst-class:: classref-property
  160. :ref:`Shape2D<class_Shape2D>` **shape**
  161. .. rst-class:: classref-property-setget
  162. - void **set_shape** **(** :ref:`Shape2D<class_Shape2D>` value **)**
  163. - :ref:`Shape2D<class_Shape2D>` **get_shape** **(** **)**
  164. The :ref:`Shape2D<class_Shape2D>`-derived shape to be used for collision queries.
  165. .. rst-class:: classref-item-separator
  166. ----
  167. .. _class_ShapeCast2D_property_target_position:
  168. .. rst-class:: classref-property
  169. :ref:`Vector2<class_Vector2>` **target_position** = ``Vector2( 0, 50 )``
  170. .. rst-class:: classref-property-setget
  171. - void **set_target_position** **(** :ref:`Vector2<class_Vector2>` value **)**
  172. - :ref:`Vector2<class_Vector2>` **get_target_position** **(** **)**
  173. The shape's destination point, relative to this node's ``position``.
  174. .. rst-class:: classref-section-separator
  175. ----
  176. .. rst-class:: classref-descriptions-group
  177. Method Descriptions
  178. -------------------
  179. .. _class_ShapeCast2D_method_add_exception:
  180. .. rst-class:: classref-method
  181. void **add_exception** **(** :ref:`Object<class_Object>` node **)**
  182. Adds a collision exception so the shape does not report collisions with the specified :ref:`CollisionObject2D<class_CollisionObject2D>` node.
  183. .. rst-class:: classref-item-separator
  184. ----
  185. .. _class_ShapeCast2D_method_add_exception_rid:
  186. .. rst-class:: classref-method
  187. void **add_exception_rid** **(** :ref:`RID<class_RID>` rid **)**
  188. Adds a collision exception so the shape does not report collisions with the specified :ref:`RID<class_RID>`.
  189. .. rst-class:: classref-item-separator
  190. ----
  191. .. _class_ShapeCast2D_method_clear_exceptions:
  192. .. rst-class:: classref-method
  193. void **clear_exceptions** **(** **)**
  194. Removes all collision exceptions for this shape.
  195. .. rst-class:: classref-item-separator
  196. ----
  197. .. _class_ShapeCast2D_method_force_shapecast_update:
  198. .. rst-class:: classref-method
  199. void **force_shapecast_update** **(** **)**
  200. Updates the collision information for the shape. Use this method to update the collision information immediately instead of waiting for the next ``_physics_process`` call, for example if the shape or its parent has changed state.
  201. \ **Note:** ``enabled`` is not required for this to work.
  202. .. rst-class:: classref-item-separator
  203. ----
  204. .. _class_ShapeCast2D_method_get_closest_collision_safe_fraction:
  205. .. rst-class:: classref-method
  206. :ref:`float<class_float>` **get_closest_collision_safe_fraction** **(** **)** |const|
  207. The fraction from the **ShapeCast2D**'s origin to its :ref:`target_position<class_ShapeCast2D_property_target_position>` (between 0 and 1) of how far the shape can move without triggering a collision.
  208. .. rst-class:: classref-item-separator
  209. ----
  210. .. _class_ShapeCast2D_method_get_closest_collision_unsafe_fraction:
  211. .. rst-class:: classref-method
  212. :ref:`float<class_float>` **get_closest_collision_unsafe_fraction** **(** **)** |const|
  213. The fraction from the **ShapeCast2D**'s origin to its :ref:`target_position<class_ShapeCast2D_property_target_position>` (between 0 and 1) of how far the shape must move to trigger a collision.
  214. .. rst-class:: classref-item-separator
  215. ----
  216. .. _class_ShapeCast2D_method_get_collider:
  217. .. rst-class:: classref-method
  218. :ref:`Object<class_Object>` **get_collider** **(** :ref:`int<class_int>` index **)** |const|
  219. Returns the collided :ref:`Object<class_Object>` of one of the multiple collisions at ``index``, or ``null`` if no object is intersecting the shape (i.e. :ref:`is_colliding<class_ShapeCast2D_method_is_colliding>` returns ``false``).
  220. .. rst-class:: classref-item-separator
  221. ----
  222. .. _class_ShapeCast2D_method_get_collider_rid:
  223. .. rst-class:: classref-method
  224. :ref:`RID<class_RID>` **get_collider_rid** **(** :ref:`int<class_int>` index **)** |const|
  225. Returns the :ref:`RID<class_RID>` of the collided object of one of the multiple collisions at ``index``.
  226. .. rst-class:: classref-item-separator
  227. ----
  228. .. _class_ShapeCast2D_method_get_collider_shape:
  229. .. rst-class:: classref-method
  230. :ref:`int<class_int>` **get_collider_shape** **(** :ref:`int<class_int>` index **)** |const|
  231. Returns the shape ID of the colliding shape of one of the multiple collisions at ``index``, or ``0`` if no object is intersecting the shape (i.e. :ref:`is_colliding<class_ShapeCast2D_method_is_colliding>` returns ``false``).
  232. .. rst-class:: classref-item-separator
  233. ----
  234. .. _class_ShapeCast2D_method_get_collision_count:
  235. .. rst-class:: classref-method
  236. :ref:`int<class_int>` **get_collision_count** **(** **)** |const|
  237. The number of collisions detected at the point of impact. Use this to iterate over multiple collisions as provided by :ref:`get_collider<class_ShapeCast2D_method_get_collider>`, :ref:`get_collider_shape<class_ShapeCast2D_method_get_collider_shape>`, :ref:`get_collision_point<class_ShapeCast2D_method_get_collision_point>`, and :ref:`get_collision_normal<class_ShapeCast2D_method_get_collision_normal>` methods.
  238. .. rst-class:: classref-item-separator
  239. ----
  240. .. _class_ShapeCast2D_method_get_collision_mask_value:
  241. .. rst-class:: classref-method
  242. :ref:`bool<class_bool>` **get_collision_mask_value** **(** :ref:`int<class_int>` layer_number **)** |const|
  243. Returns whether or not the specified layer of the :ref:`collision_mask<class_ShapeCast2D_property_collision_mask>` is enabled, given a ``layer_number`` between 1 and 32.
  244. .. rst-class:: classref-item-separator
  245. ----
  246. .. _class_ShapeCast2D_method_get_collision_normal:
  247. .. rst-class:: classref-method
  248. :ref:`Vector2<class_Vector2>` **get_collision_normal** **(** :ref:`int<class_int>` index **)** |const|
  249. Returns the normal of one of the multiple collisions at ``index`` of the intersecting object.
  250. .. rst-class:: classref-item-separator
  251. ----
  252. .. _class_ShapeCast2D_method_get_collision_point:
  253. .. rst-class:: classref-method
  254. :ref:`Vector2<class_Vector2>` **get_collision_point** **(** :ref:`int<class_int>` index **)** |const|
  255. Returns the collision point of one of the multiple collisions at ``index`` where the shape intersects the colliding object.
  256. \ **Note:** this point is in the **global** coordinate system.
  257. .. rst-class:: classref-item-separator
  258. ----
  259. .. _class_ShapeCast2D_method_is_colliding:
  260. .. rst-class:: classref-method
  261. :ref:`bool<class_bool>` **is_colliding** **(** **)** |const|
  262. Returns whether any object is intersecting with the shape's vector (considering the vector length).
  263. .. rst-class:: classref-item-separator
  264. ----
  265. .. _class_ShapeCast2D_method_remove_exception:
  266. .. rst-class:: classref-method
  267. void **remove_exception** **(** :ref:`Object<class_Object>` node **)**
  268. Removes a collision exception so the shape does report collisions with the specified :ref:`CollisionObject2D<class_CollisionObject2D>` node.
  269. .. rst-class:: classref-item-separator
  270. ----
  271. .. _class_ShapeCast2D_method_remove_exception_rid:
  272. .. rst-class:: classref-method
  273. void **remove_exception_rid** **(** :ref:`RID<class_RID>` rid **)**
  274. Removes a collision exception so the shape does report collisions with the specified :ref:`RID<class_RID>`.
  275. .. rst-class:: classref-item-separator
  276. ----
  277. .. _class_ShapeCast2D_method_set_collision_mask_value:
  278. .. rst-class:: classref-method
  279. void **set_collision_mask_value** **(** :ref:`int<class_int>` layer_number, :ref:`bool<class_bool>` value **)**
  280. Based on ``value``, enables or disables the specified layer in the :ref:`collision_mask<class_ShapeCast2D_property_collision_mask>`, given a ``layer_number`` between 1 and 32.
  281. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  282. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  283. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  284. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`