:github_url: hide .. DO NOT EDIT THIS FILE!!! .. Generated automatically from Godot engine sources. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/ShapeCast2D.xml. .. _class_ShapeCast2D: ShapeCast2D =========== **Inherits:** :ref:`Node2D` **<** :ref:`CanvasItem` **<** :ref:`Node` **<** :ref:`Object` A 2D shape that sweeps a region of space to detect :ref:`CollisionObject2D`\ s. .. rst-class:: classref-introduction-group Description ----------- Shape casting allows to detect collision objects by sweeping its :ref:`shape` along the cast direction determined by :ref:`target_position`. This is similar to :ref:`RayCast2D`, but it allows for sweeping a region of space, rather than just a straight line. **ShapeCast2D** can detect multiple collision objects. It is useful for things like wide laser beams or snapping a simple shape to a floor. Immediate collision overlaps can be done with the :ref:`target_position` set to ``Vector2(0, 0)`` and by calling :ref:`force_shapecast_update()` within the same physics frame. This helps to overcome some limitations of :ref:`Area2D` when used as an instantaneous detection area, as collision information isn't immediately available to it. \ **Note:** Shape casting is more computationally expensive than ray casting. .. rst-class:: classref-reftable-group Properties ---------- .. table:: :widths: auto +-------------------------------+----------------------------------------------------------------------------+--------------------+ | :ref:`bool` | :ref:`collide_with_areas` | ``false`` | +-------------------------------+----------------------------------------------------------------------------+--------------------+ | :ref:`bool` | :ref:`collide_with_bodies` | ``true`` | +-------------------------------+----------------------------------------------------------------------------+--------------------+ | :ref:`int` | :ref:`collision_mask` | ``1`` | +-------------------------------+----------------------------------------------------------------------------+--------------------+ | :ref:`Array` | :ref:`collision_result` | ``[]`` | +-------------------------------+----------------------------------------------------------------------------+--------------------+ | :ref:`bool` | :ref:`enabled` | ``true`` | +-------------------------------+----------------------------------------------------------------------------+--------------------+ | :ref:`bool` | :ref:`exclude_parent` | ``true`` | +-------------------------------+----------------------------------------------------------------------------+--------------------+ | :ref:`float` | :ref:`margin` | ``0.0`` | +-------------------------------+----------------------------------------------------------------------------+--------------------+ | :ref:`int` | :ref:`max_results` | ``32`` | +-------------------------------+----------------------------------------------------------------------------+--------------------+ | :ref:`Shape2D` | :ref:`shape` | | +-------------------------------+----------------------------------------------------------------------------+--------------------+ | :ref:`Vector2` | :ref:`target_position` | ``Vector2(0, 50)`` | +-------------------------------+----------------------------------------------------------------------------+--------------------+ .. rst-class:: classref-reftable-group Methods ------- .. table:: :widths: auto +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`add_exception`\ (\ node\: :ref:`CollisionObject2D`\ ) | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`add_exception_rid`\ (\ rid\: :ref:`RID`\ ) | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`clear_exceptions`\ (\ ) | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`force_shapecast_update`\ (\ ) | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`get_closest_collision_safe_fraction`\ (\ ) |const| | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`get_closest_collision_unsafe_fraction`\ (\ ) |const| | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Object` | :ref:`get_collider`\ (\ index\: :ref:`int`\ ) |const| | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`RID` | :ref:`get_collider_rid`\ (\ index\: :ref:`int`\ ) |const| | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_collider_shape`\ (\ index\: :ref:`int`\ ) |const| | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_collision_count`\ (\ ) |const| | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`get_collision_mask_value`\ (\ layer_number\: :ref:`int`\ ) |const| | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Vector2` | :ref:`get_collision_normal`\ (\ index\: :ref:`int`\ ) |const| | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Vector2` | :ref:`get_collision_point`\ (\ index\: :ref:`int`\ ) |const| | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_colliding`\ (\ ) |const| | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`remove_exception`\ (\ node\: :ref:`CollisionObject2D`\ ) | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`remove_exception_rid`\ (\ rid\: :ref:`RID`\ ) | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_collision_mask_value`\ (\ layer_number\: :ref:`int`, value\: :ref:`bool`\ ) | +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Property Descriptions --------------------- .. _class_ShapeCast2D_property_collide_with_areas: .. rst-class:: classref-property :ref:`bool` **collide_with_areas** = ``false`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_collide_with_areas**\ (\ value\: :ref:`bool`\ ) - :ref:`bool` **is_collide_with_areas_enabled**\ (\ ) If ``true``, collisions with :ref:`Area2D`\ s will be reported. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_property_collide_with_bodies: .. rst-class:: classref-property :ref:`bool` **collide_with_bodies** = ``true`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_collide_with_bodies**\ (\ value\: :ref:`bool`\ ) - :ref:`bool` **is_collide_with_bodies_enabled**\ (\ ) If ``true``, collisions with :ref:`PhysicsBody2D`\ s will be reported. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_property_collision_mask: .. rst-class:: classref-property :ref:`int` **collision_mask** = ``1`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_collision_mask**\ (\ value\: :ref:`int`\ ) - :ref:`int` **get_collision_mask**\ (\ ) The shape's collision mask. Only objects in at least one collision layer enabled in the mask will be detected. See `Collision layers and masks <../tutorials/physics/physics_introduction.html#collision-layers-and-masks>`__ in the documentation for more information. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_property_collision_result: .. rst-class:: classref-property :ref:`Array` **collision_result** = ``[]`` :ref:`🔗` .. rst-class:: classref-property-setget - :ref:`Array` **get_collision_result**\ (\ ) Returns the complete collision information from the collision sweep. The data returned is the same as in the :ref:`PhysicsDirectSpaceState2D.get_rest_info()` method. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_property_enabled: .. rst-class:: classref-property :ref:`bool` **enabled** = ``true`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_enabled**\ (\ value\: :ref:`bool`\ ) - :ref:`bool` **is_enabled**\ (\ ) If ``true``, collisions will be reported. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_property_exclude_parent: .. rst-class:: classref-property :ref:`bool` **exclude_parent** = ``true`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_exclude_parent_body**\ (\ value\: :ref:`bool`\ ) - :ref:`bool` **get_exclude_parent_body**\ (\ ) If ``true``, the parent node will be excluded from collision detection. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_property_margin: .. rst-class:: classref-property :ref:`float` **margin** = ``0.0`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_margin**\ (\ value\: :ref:`float`\ ) - :ref:`float` **get_margin**\ (\ ) The collision margin for the shape. A larger margin helps detecting collisions more consistently, at the cost of precision. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_property_max_results: .. rst-class:: classref-property :ref:`int` **max_results** = ``32`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_max_results**\ (\ value\: :ref:`int`\ ) - :ref:`int` **get_max_results**\ (\ ) The number of intersections can be limited with this parameter, to reduce the processing time. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_property_shape: .. rst-class:: classref-property :ref:`Shape2D` **shape** :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_shape**\ (\ value\: :ref:`Shape2D`\ ) - :ref:`Shape2D` **get_shape**\ (\ ) The shape to be used for collision queries. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_property_target_position: .. rst-class:: classref-property :ref:`Vector2` **target_position** = ``Vector2(0, 50)`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_target_position**\ (\ value\: :ref:`Vector2`\ ) - :ref:`Vector2` **get_target_position**\ (\ ) The shape's destination point, relative to this node's :ref:`Node2D.position`. .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Method Descriptions ------------------- .. _class_ShapeCast2D_method_add_exception: .. rst-class:: classref-method |void| **add_exception**\ (\ node\: :ref:`CollisionObject2D`\ ) :ref:`🔗` Adds a collision exception so the shape does not report collisions with the specified node. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_add_exception_rid: .. rst-class:: classref-method |void| **add_exception_rid**\ (\ rid\: :ref:`RID`\ ) :ref:`🔗` Adds a collision exception so the shape does not report collisions with the specified :ref:`RID`. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_clear_exceptions: .. rst-class:: classref-method |void| **clear_exceptions**\ (\ ) :ref:`🔗` Removes all collision exceptions for this shape. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_force_shapecast_update: .. rst-class:: classref-method |void| **force_shapecast_update**\ (\ ) :ref:`🔗` Updates the collision information for the shape immediately, without waiting for the next ``_physics_process`` call. Use this method, for example, when the shape or its parent has changed state. \ **Note:** Setting :ref:`enabled` to ``true`` is not required for this to work. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_get_closest_collision_safe_fraction: .. rst-class:: classref-method :ref:`float` **get_closest_collision_safe_fraction**\ (\ ) |const| :ref:`🔗` Returns the fraction from this cast's origin to its :ref:`target_position` of how far the shape can move without triggering a collision, as a value between ``0.0`` and ``1.0``. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_get_closest_collision_unsafe_fraction: .. rst-class:: classref-method :ref:`float` **get_closest_collision_unsafe_fraction**\ (\ ) |const| :ref:`🔗` Returns the fraction from this cast's origin to its :ref:`target_position` of how far the shape must move to trigger a collision, as a value between ``0.0`` and ``1.0``. In ideal conditions this would be the same as :ref:`get_closest_collision_safe_fraction()`, however shape casting is calculated in discrete steps, so the precise point of collision can occur between two calculated positions. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_get_collider: .. rst-class:: classref-method :ref:`Object` **get_collider**\ (\ index\: :ref:`int`\ ) |const| :ref:`🔗` Returns the collided :ref:`Object` of one of the multiple collisions at ``index``, or ``null`` if no object is intersecting the shape (i.e. :ref:`is_colliding()` returns ``false``). .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_get_collider_rid: .. rst-class:: classref-method :ref:`RID` **get_collider_rid**\ (\ index\: :ref:`int`\ ) |const| :ref:`🔗` Returns the :ref:`RID` of the collided object of one of the multiple collisions at ``index``. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_get_collider_shape: .. rst-class:: classref-method :ref:`int` **get_collider_shape**\ (\ index\: :ref:`int`\ ) |const| :ref:`🔗` 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()` returns ``false``). .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_get_collision_count: .. rst-class:: classref-method :ref:`int` **get_collision_count**\ (\ ) |const| :ref:`🔗` The number of collisions detected at the point of impact. Use this to iterate over multiple collisions as provided by :ref:`get_collider()`, :ref:`get_collider_shape()`, :ref:`get_collision_point()`, and :ref:`get_collision_normal()` methods. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_get_collision_mask_value: .. rst-class:: classref-method :ref:`bool` **get_collision_mask_value**\ (\ layer_number\: :ref:`int`\ ) |const| :ref:`🔗` Returns whether or not the specified layer of the :ref:`collision_mask` is enabled, given a ``layer_number`` between 1 and 32. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_get_collision_normal: .. rst-class:: classref-method :ref:`Vector2` **get_collision_normal**\ (\ index\: :ref:`int`\ ) |const| :ref:`🔗` Returns the normal of one of the multiple collisions at ``index`` of the intersecting object. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_get_collision_point: .. rst-class:: classref-method :ref:`Vector2` **get_collision_point**\ (\ index\: :ref:`int`\ ) |const| :ref:`🔗` Returns the collision point of one of the multiple collisions at ``index`` where the shape intersects the colliding object. \ **Note:** This point is in the **global** coordinate system. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_is_colliding: .. rst-class:: classref-method :ref:`bool` **is_colliding**\ (\ ) |const| :ref:`🔗` Returns whether any object is intersecting with the shape's vector (considering the vector length). .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_remove_exception: .. rst-class:: classref-method |void| **remove_exception**\ (\ node\: :ref:`CollisionObject2D`\ ) :ref:`🔗` Removes a collision exception so the shape does report collisions with the specified node. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_remove_exception_rid: .. rst-class:: classref-method |void| **remove_exception_rid**\ (\ rid\: :ref:`RID`\ ) :ref:`🔗` Removes a collision exception so the shape does report collisions with the specified :ref:`RID`. .. rst-class:: classref-item-separator ---- .. _class_ShapeCast2D_method_set_collision_mask_value: .. rst-class:: classref-method |void| **set_collision_mask_value**\ (\ layer_number\: :ref:`int`, value\: :ref:`bool`\ ) :ref:`🔗` Based on ``value``, enables or disables the specified layer in the :ref:`collision_mask`, given a ``layer_number`` between 1 and 32. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)` .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)` .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)` .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)` .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)` .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)` .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)` .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)` .. |void| replace:: :abbr:`void (No return value.)`