RayCast2D

package
v0.0.0-...-357ca8a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

A raycast represents a ray from its origin to its TargetPosition that finds the closest object along its path, if it intersects any.

RayCast2D can ignore some objects by adding them to an exception list, by making its detection reporting ignore Area2Ds (CollideWithAreas) or PhysicsBody2Ds (CollideWithBodies), or by configuring physics layers.

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 ForceRaycastUpdate.

To sweep over a region of 2D space, you can approximate the region with multiple RayCast2Ds or use ShapeCast2D.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsRayCast2D() Instance
}

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsCanvasItem

func (self *Extension[T]) AsCanvasItem() CanvasItem.Instance

func (*Extension[T]) AsNode

func (self *Extension[T]) AsNode() Node.Instance

func (*Extension[T]) AsNode2D

func (self *Extension[T]) AsNode2D() Node2D.Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRayCast2D

func (self *Extension[T]) AsRayCast2D() Instance

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.RayCast2D

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) AddException

func (self Instance) AddException(node CollisionObject2D.Instance)

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

func (Instance) AddExceptionRid

func (self Instance) AddExceptionRid(rid RID.Body2D)

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

func (Instance) AsCanvasItem

func (self Instance) AsCanvasItem() CanvasItem.Instance

func (Instance) AsNode

func (self Instance) AsNode() Node.Instance

func (Instance) AsNode2D

func (self Instance) AsNode2D() Node2D.Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRayCast2D

func (self Instance) AsRayCast2D() Instance

func (Instance) ClearExceptions

func (self Instance) ClearExceptions()

Removes all collision exceptions for this ray.

func (Instance) CollideWithAreas

func (self Instance) CollideWithAreas() bool

If true, collisions with Area2Ds will be reported.

func (Instance) CollideWithBodies

func (self Instance) CollideWithBodies() bool

If true, collisions with PhysicsBody2Ds will be reported.

func (Instance) CollisionMask

func (self Instance) CollisionMask() int

The ray's collision mask. Only objects in at least one collision layer enabled in the mask will be detected. See Collision layers and masks in the documentation for more information.

func (Instance) Enabled

func (self Instance) Enabled() bool

If true, collisions will be reported.

func (Instance) ExcludeParent

func (self Instance) ExcludeParent() bool

If true, 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 Node.GetParent and AddException.

func (Instance) ForceRaycastUpdate

func (self Instance) ForceRaycastUpdate()

Updates the collision information for the ray immediately, without waiting for the next _physics_process call. Use this method, for example, when the ray or its parent has changed state.

Note: Enabled does not need to be true for this to work.

func (Instance) GetCollider

func (self Instance) GetCollider() Object.Instance

Returns the first object that the ray intersects, or null if no object is intersecting the ray (i.e. IsColliding returns false).

Note: This object is not guaranteed to be a CollisionObject2D. For example, if the ray intersects a TileMapLayer, the method will return a TileMapLayer instance.

func (Instance) GetColliderRid

func (self Instance) GetColliderRid() RID.Body2D

Returns the Resource.ID of the first object that the ray intersects, or an empty Resource.ID if no object is intersecting the ray (i.e. IsColliding returns false).

func (Instance) GetColliderShape

func (self Instance) GetColliderShape() int

Returns the shape ID of the first object that the ray intersects, or 0 if no object is intersecting the ray (i.e. IsColliding returns false).

To get the intersected shape node, for a CollisionObject2D target, use:

var target = Object.To[CollisionObject2D.Instance](rayCast2D.GetCollider()) // A CollisionObject2D.
var shapeID int = rayCast2D.GetColliderShape()
var ownerID int = target.ShapeFindOwner(shapeID) // The owner ID in the collider.
var shape = target.ShapeOwnerGetOwner(ownerID)

func (Instance) GetCollisionMaskValue

func (self Instance) GetCollisionMaskValue(layer_number int) bool

Returns whether or not the specified layer of the CollisionMask is enabled, given a 'layer_number' between 1 and 32.

func (Instance) GetCollisionNormal

func (self Instance) GetCollisionNormal() Vector2.XY

Returns the normal of the intersecting object's shape at the collision point, or Vector2(0, 0) if the ray starts inside the shape and HitFromInside is true.

Note: Check that IsColliding returns true before calling this method to ensure the returned normal is valid and up-to-date.

func (Instance) GetCollisionPoint

func (self Instance) GetCollisionPoint() Vector2.XY

Returns the collision point at which the ray intersects the closest object, in the global coordinate system. If HitFromInside is true and the ray starts inside of a collision shape, this function will return the origin point of the ray.

Note: Check that IsColliding returns true before calling this method to ensure the returned point is valid and up-to-date.

func (Instance) HitFromInside

func (self Instance) HitFromInside() bool

If true, the ray will detect a hit when starting inside shapes. In this case the collision normal will be Vector2(0, 0). Does not affect concave polygon shapes.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IsColliding

func (self Instance) IsColliding() bool

Returns whether any object is intersecting with the ray's vector (considering the vector length).

func (Instance) RemoveException

func (self Instance) RemoveException(node CollisionObject2D.Instance)

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

func (Instance) RemoveExceptionRid

func (self Instance) RemoveExceptionRid(rid RID.Body2D)

Removes a collision exception so the ray can report collisions with the specified Resource.ID.

func (Instance) SetCollideWithAreas

func (self Instance) SetCollideWithAreas(value bool)

SetCollideWithAreas sets the property returned by [IsCollideWithAreasEnabled].

func (Instance) SetCollideWithBodies

func (self Instance) SetCollideWithBodies(value bool)

SetCollideWithBodies sets the property returned by [IsCollideWithBodiesEnabled].

func (Instance) SetCollisionMask

func (self Instance) SetCollisionMask(value int)

SetCollisionMask sets the property returned by [GetCollisionMask].

func (Instance) SetCollisionMaskValue

func (self Instance) SetCollisionMaskValue(layer_number int, value bool)

Based on 'value', enables or disables the specified layer in the CollisionMask, given a 'layer_number' between 1 and 32.

func (Instance) SetEnabled

func (self Instance) SetEnabled(value bool)

SetEnabled sets the property returned by [IsEnabled].

func (Instance) SetExcludeParent

func (self Instance) SetExcludeParent(value bool)

SetExcludeParent sets the property returned by [GetExcludeParentBody].

func (Instance) SetHitFromInside

func (self Instance) SetHitFromInside(value bool)

SetHitFromInside sets the property returned by [IsHitFromInsideEnabled].

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) SetTargetPosition

func (self Instance) SetTargetPosition(value Vector2.XY)

SetTargetPosition sets the property returned by [GetTargetPosition].

func (Instance) TargetPosition

func (self Instance) TargetPosition() Vector2.XY

The ray's destination point, relative to this raycast's Node2D.Position.

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL