RayCast2D

Inherits: Node2D < CanvasItem < Node < Object

Category: Core

Brief Description

Query the closest object intersecting a ray.

Member Functions

void add_exception ( Object node )
void add_exception_rid ( RID rid )
void clear_exceptions ( )
void force_raycast_update ( )
Vector2 get_cast_to ( ) const
Object get_collider ( ) const
int get_collider_shape ( ) const
Vector2 get_collision_normal ( ) const
Vector2 get_collision_point ( ) const
int get_layer_mask ( ) const
int get_type_mask ( ) const
bool is_colliding ( ) const
bool is_enabled ( ) const
void remove_exception ( Object node )
void remove_exception_rid ( RID rid )
void set_cast_to ( Vector2 local_point )
void set_enabled ( bool enabled )
void set_layer_mask ( int mask )
void set_type_mask ( int mask )

Description

A RayCast2D represents a line from its origin to its destination position cast_to, it is used to query the 2D space in order to find the closest object intersecting with the ray.

RayCast2D can ignore some objects by adding them to the exception list via add_exception, setting proper filtering with layers, or by filtering object types with type masks.

Only enabled raycasts will be able to query the space and report collisions!

RayCast2D calculates intersection every fixed frame (see Node), and the result is cached so it can be used later until the next frame. If multiple queries are required between fixed frames (or during the same frame) use force_raycast_update after adjusting the raycast.

Member Function Description

  • void add_exception ( Object node )

Adds a collision exception so the ray does not report collisions with the specified node.

  • void add_exception_rid ( RID rid )
  • void clear_exceptions ( )

Removes all collision exception for this ray.

  • void force_raycast_update ( )

Updates the collision information in case if this object’s properties changed during the current frame (for example position, rotation or the cast_point). Note, set_enabled is not required for this to work.

Return the destination point of this ray object.

  • Object get_collider ( ) const

Return the closest object the ray is pointing to. Note that this does not consider the length of the vector, so you must also use is_colliding to check if the object returned is actually colliding with the ray.

  • int get_collider_shape ( ) const

Returns the collision shape of the closest object the ray is pointing to.

  • Vector2 get_collision_normal ( ) const

Returns the normal of the intersecting object shape face containing the collision point.

  • Vector2 get_collision_point ( ) const

Returns the collision point in which the ray intersects the closest object. This point is in global coordinate system.

  • int get_layer_mask ( ) const

Returns the layer mask for this ray.

  • int get_type_mask ( ) const

Returns the type mask (types of objects to detect) for this ray. The value is a sum (bitwise OR’d) of constants available for Physics2DDirectSpaceState.

  • bool is_colliding ( ) const

Return whether the closest object the ray is pointing to is colliding with the vector (considering the vector length).

  • bool is_enabled ( ) const

Returns whether this raycast is enabled or not.

  • void remove_exception ( Object node )

Removes a collision exception so the ray does report collisions with the specified node.

  • void remove_exception_rid ( RID rid )
  • void set_cast_to ( Vector2 local_point )

Sets the ray destination point, so that the ray will test from the ray’s origin to local_point

  • void set_enabled ( bool enabled )

Enables the RayCast2D. Only enabled raycasts will be able to query the space and report collisions.

  • void set_layer_mask ( int mask )

Set the mask to filter objects. Only objects with at least the same mask element set will be detected.

  • void set_type_mask ( int mask )

Set the types of objects to detect. For mask use a logic sum (OR operation) of constants defined in Physics2DDirectSpaceState, eg. Physics2DDirectSpaceState.TYPE_MASK_STATIC_BODY | Physics2DDirectSpaceState.TYPE_MASK_KINEMATIC_BODY to detect only those two types.