0.2.0-dev Download ZIP
150 lines ·8.4 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="RayCast2D" inherits="Node2D" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | A ray in 2D space, used to find the first collision object it intersects. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | A raycast represents a ray from its origin to its [member target_position] that finds the closest object along its path, if it intersects any. |
| 8 | [RayCast2D] can ignore some objects by adding them to an exception list, by making its detection reporting ignore [Area2D]s ([member collide_with_areas]) or [PhysicsBody2D]s ([member collide_with_bodies]), or by configuring physics layers. |
| 9 | [RayCast2D] calculates intersection every physics frame, and it holds the result until the next physics frame. For an immediate raycast, or if you want to configure a [RayCast2D] multiple times within the same physics frame, use [method force_raycast_update]. |
| 10 | To sweep over a region of 2D space, you can approximate the region with multiple [RayCast2D]s or use [ShapeCast2D]. |
| 11 | </description> |
| 12 | <tutorials> |
| 13 | <link title="Ray-casting">$DOCS_URL/tutorials/physics/ray-casting.html</link> |
| 14 | </tutorials> |
| 15 | <methods> |
| 16 | <method name="add_exception"> |
| 17 | <return type="void" /> |
| 18 | <param index="0" name="node" type="CollisionObject2D" /> |
| 19 | <description> |
| 20 | Adds a collision exception so the ray does not report collisions with the specified [param node]. |
| 21 | </description> |
| 22 | </method> |
| 23 | <method name="add_exception_rid"> |
| 24 | <return type="void" /> |
| 25 | <param index="0" name="rid" type="RID" /> |
| 26 | <description> |
| 27 | Adds a collision exception so the ray does not report collisions with the specified [RID]. |
| 28 | </description> |
| 29 | </method> |
| 30 | <method name="clear_exceptions"> |
| 31 | <return type="void" /> |
| 32 | <description> |
| 33 | Removes all collision exceptions for this ray. |
| 34 | </description> |
| 35 | </method> |
| 36 | <method name="force_raycast_update"> |
| 37 | <return type="void" /> |
| 38 | <description> |
| 39 | Updates the collision information for the ray immediately, without waiting for the next [code]_physics_process[/code] call. Use this method, for example, when the ray or its parent has changed state. |
| 40 | [b]Note:[/b] [member enabled] does not need to be [code]true[/code] for this to work. |
| 41 | </description> |
| 42 | </method> |
| 43 | <method name="get_collider" qualifiers="const"> |
| 44 | <return type="Object" /> |
| 45 | <description> |
| 46 | Returns the first object that the ray intersects, or [code]null[/code] if no object is intersecting the ray (i.e. [method is_colliding] returns [code]false[/code]). |
| 47 | [b]Note:[/b] This object is not guaranteed to be a [CollisionObject2D]. For example, if the ray intersects a [TileMapLayer], the method will return a [TileMapLayer] instance. |
| 48 | </description> |
| 49 | </method> |
| 50 | <method name="get_collider_rid" qualifiers="const"> |
| 51 | <return type="RID" /> |
| 52 | <description> |
| 53 | Returns the [RID] of the first object that the ray intersects, or an empty [RID] if no object is intersecting the ray (i.e. [method is_colliding] returns [code]false[/code]). |
| 54 | </description> |
| 55 | </method> |
| 56 | <method name="get_collider_shape" qualifiers="const"> |
| 57 | <return type="int" /> |
| 58 | <description> |
| 59 | Returns the shape ID of the first object that the ray intersects, or [code]0[/code] if no object is intersecting the ray (i.e. [method is_colliding] returns [code]false[/code]). |
| 60 | To get the intersected shape node, for a [CollisionObject2D] target, use: |
| 61 | [codeblocks] |
| 62 | [gdscript] |
| 63 | var target = get_collider() # A CollisionObject2D. |
| 64 | var shape_id = get_collider_shape() # The shape index in the collider. |
| 65 | var owner_id = target.shape_find_owner(shape_id) # The owner ID in the collider. |
| 66 | var shape = target.shape_owner_get_owner(owner_id) |
| 67 | [/gdscript] |
| 68 | [csharp] |
| 69 | var target = (CollisionObject2D)GetCollider(); // A CollisionObject2D. |
| 70 | var shapeId = GetColliderShape(); // The shape index in the collider. |
| 71 | var ownerId = target.ShapeFindOwner(shapeId); // The owner ID in the collider. |
| 72 | var shape = target.ShapeOwnerGetOwner(ownerId); |
| 73 | [/csharp] |
| 74 | [/codeblocks] |
| 75 | </description> |
| 76 | </method> |
| 77 | <method name="get_collision_mask_value" qualifiers="const"> |
| 78 | <return type="bool" /> |
| 79 | <param index="0" name="layer_number" type="int" /> |
| 80 | <description> |
| 81 | Returns whether or not the specified layer of the [member collision_mask] is enabled, given a [param layer_number] between 1 and 32. |
| 82 | </description> |
| 83 | </method> |
| 84 | <method name="get_collision_normal" qualifiers="const"> |
| 85 | <return type="Vector2" /> |
| 86 | <description> |
| 87 | Returns the normal of the intersecting object's shape at the collision point, or [code]Vector2(0, 0)[/code] if the ray starts inside the shape and [member hit_from_inside] is [code]true[/code]. |
| 88 | [b]Note:[/b] Check that [method is_colliding] returns [code]true[/code] before calling this method to ensure the returned normal is valid and up-to-date. |
| 89 | </description> |
| 90 | </method> |
| 91 | <method name="get_collision_point" qualifiers="const"> |
| 92 | <return type="Vector2" /> |
| 93 | <description> |
| 94 | Returns the collision point at which the ray intersects the closest object, in the global coordinate system. If [member hit_from_inside] is [code]true[/code] and the ray starts inside of a collision shape, this function will return the origin point of the ray. |
| 95 | [b]Note:[/b] Check that [method is_colliding] returns [code]true[/code] before calling this method to ensure the returned point is valid and up-to-date. |
| 96 | </description> |
| 97 | </method> |
| 98 | <method name="is_colliding" qualifiers="const"> |
| 99 | <return type="bool" /> |
| 100 | <description> |
| 101 | Returns whether any object is intersecting with the ray's vector (considering the vector length). |
| 102 | </description> |
| 103 | </method> |
| 104 | <method name="remove_exception"> |
| 105 | <return type="void" /> |
| 106 | <param index="0" name="node" type="CollisionObject2D" /> |
| 107 | <description> |
| 108 | Removes a collision exception so the ray can report collisions with the specified [param node]. |
| 109 | </description> |
| 110 | </method> |
| 111 | <method name="remove_exception_rid"> |
| 112 | <return type="void" /> |
| 113 | <param index="0" name="rid" type="RID" /> |
| 114 | <description> |
| 115 | Removes a collision exception so the ray can report collisions with the specified [RID]. |
| 116 | </description> |
| 117 | </method> |
| 118 | <method name="set_collision_mask_value"> |
| 119 | <return type="void" /> |
| 120 | <param index="0" name="layer_number" type="int" /> |
| 121 | <param index="1" name="value" type="bool" /> |
| 122 | <description> |
| 123 | Based on [param value], enables or disables the specified layer in the [member collision_mask], given a [param layer_number] between 1 and 32. |
| 124 | </description> |
| 125 | </method> |
| 126 | </methods> |
| 127 | <members> |
| 128 | <member name="collide_with_areas" type="bool" setter="set_collide_with_areas" getter="is_collide_with_areas_enabled" default="false"> |
| 129 | If [code]true[/code], collisions with [Area2D]s will be reported. |
| 130 | </member> |
| 131 | <member name="collide_with_bodies" type="bool" setter="set_collide_with_bodies" getter="is_collide_with_bodies_enabled" default="true"> |
| 132 | If [code]true[/code], collisions with [PhysicsBody2D]s will be reported. |
| 133 | </member> |
| 134 | <member name="collision_mask" type="int" setter="set_collision_mask" getter="get_collision_mask" default="1"> |
| 135 | The ray's collision mask. Only objects in at least one collision layer enabled in the mask will be detected. See [url=$DOCS_URL/tutorials/physics/physics_introduction.html#collision-layers-and-masks]Collision layers and masks[/url] in the documentation for more information. |
| 136 | </member> |
| 137 | <member name="enabled" type="bool" setter="set_enabled" getter="is_enabled" default="true"> |
| 138 | If [code]true[/code], collisions will be reported. |
| 139 | </member> |
| 140 | <member name="exclude_parent" type="bool" setter="set_exclude_parent_body" getter="get_exclude_parent_body" default="true"> |
| 141 | If [code]true[/code], this raycast will not report collisions with its parent node. This property only has an effect if the parent node is a [CollisionObject2D]. See also [method Node.get_parent] and [method add_exception]. |
| 142 | </member> |
| 143 | <member name="hit_from_inside" type="bool" setter="set_hit_from_inside" getter="is_hit_from_inside_enabled" default="false"> |
| 144 | If [code]true[/code], the ray will detect a hit when starting inside shapes. In this case the collision normal will be [code]Vector2(0, 0)[/code]. Does not affect concave polygon shapes. |
| 145 | </member> |
| 146 | <member name="target_position" type="Vector2" setter="set_target_position" getter="get_target_position" default="Vector2(0, 50)"> |
| 147 | The ray's destination point, relative to this raycast's [member Node2D.position]. |
| 148 | </member> |
| 149 | </members> |
| 150 | </class> |