Table of Contents

Class ShapeCast2D

Namespace
Godot
Assembly
GodotSharp.dll

Shape casting allows to detect collision objects by sweeping its Shape along the cast direction determined by TargetPosition. This is similar to 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 TargetPosition set to Vector2(0, 0) and by calling ForceShapecastUpdate() within the same physics frame. This helps to overcome some limitations of 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.

public class ShapeCast2D : Node2D, IDisposable
Inheritance
ShapeCast2D
Implements
Inherited Members

Constructors

ShapeCast2D()

public ShapeCast2D()

Properties

CollideWithAreas

If true, collisions with Area2Ds will be reported.

public bool CollideWithAreas { get; set; }

Property Value

bool

CollideWithBodies

If true, collisions with PhysicsBody2Ds will be reported.

public bool CollideWithBodies { get; set; }

Property Value

bool

CollisionMask

The shape's collision mask. Only objects in at least one collision layer enabled in the mask will be detected.

public uint CollisionMask { get; set; }

Property Value

uint

CollisionResult

Returns the complete collision information from the collision sweep. The data returned is the same as in the GetRestInfo(PhysicsShapeQueryParameters2D) method.

public Array CollisionResult { get; }

Property Value

Array

Enabled

If true, collisions will be reported.

public bool Enabled { get; set; }

Property Value

bool

ExcludeParent

If true, the parent node will be excluded from collision detection.

public bool ExcludeParent { get; set; }

Property Value

bool

Margin

The collision margin for the shape. A larger margin helps detecting collisions more consistently, at the cost of precision.

public float Margin { get; set; }

Property Value

float

MaxResults

The number of intersections can be limited with this parameter, to reduce the processing time.

public int MaxResults { get; set; }

Property Value

int

Shape

The Shape2D-derived shape to be used for collision queries.

public Shape2D Shape { get; set; }

Property Value

Shape2D

TargetPosition

The shape's destination point, relative to this node's position.

public Vector2 TargetPosition { get; set; }

Property Value

Vector2

Methods

AddException(CollisionObject2D)

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

public void AddException(CollisionObject2D node)

Parameters

node CollisionObject2D

AddExceptionRid(Rid)

Adds a collision exception so the shape does not report collisions with the specified Rid.

public void AddExceptionRid(Rid rid)

Parameters

rid Rid

ClearExceptions()

Removes all collision exceptions for this shape.

public void ClearExceptions()

ForceShapecastUpdate()

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: enabled == true is not required for this to work.

public void ForceShapecastUpdate()

GetClosestCollisionSafeFraction()

The fraction from the ShapeCast2D's origin to its TargetPosition (between 0 and 1) of how far the shape can move without triggering a collision.

public float GetClosestCollisionSafeFraction()

Returns

float

GetClosestCollisionUnsafeFraction()

The fraction from the ShapeCast2D's origin to its TargetPosition (between 0 and 1) of how far the shape must move to trigger a collision.

public float GetClosestCollisionUnsafeFraction()

Returns

float

GetCollider(int)

Returns the collided GodotObject of one of the multiple collisions at index, or null if no object is intersecting the shape (i.e. IsColliding() returns false).

public GodotObject GetCollider(int index)

Parameters

index int

Returns

GodotObject

GetColliderRid(int)

Returns the Rid of the collided object of one of the multiple collisions at index.

public Rid GetColliderRid(int index)

Parameters

index int

Returns

Rid

GetColliderShape(int)

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. IsColliding() returns false).

public int GetColliderShape(int index)

Parameters

index int

Returns

int

GetCollisionCount()

The number of collisions detected at the point of impact. Use this to iterate over multiple collisions as provided by GetCollider(int), GetColliderShape(int), GetCollisionPoint(int), and GetCollisionNormal(int) methods.

public int GetCollisionCount()

Returns

int

GetCollisionMaskValue(int)

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

public bool GetCollisionMaskValue(int layerNumber)

Parameters

layerNumber int

Returns

bool

GetCollisionNormal(int)

Returns the normal of one of the multiple collisions at index of the intersecting object.

public Vector2 GetCollisionNormal(int index)

Parameters

index int

Returns

Vector2

GetCollisionPoint(int)

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.

public Vector2 GetCollisionPoint(int index)

Parameters

index int

Returns

Vector2

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

IsColliding()

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

public bool IsColliding()

Returns

bool

RemoveException(CollisionObject2D)

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

public void RemoveException(CollisionObject2D node)

Parameters

node CollisionObject2D

RemoveExceptionRid(Rid)

Removes a collision exception so the shape does report collisions with the specified Rid.

public void RemoveExceptionRid(Rid rid)

Parameters

rid Rid

SetCollisionMaskValue(int, bool)

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

public void SetCollisionMaskValue(int layerNumber, bool value)

Parameters

layerNumber int
value bool