ShapeCast2D
A 2D shape that sweeps a region of space to detect CollisionObject2Ds.
A 2D shape that sweeps a region of space to detect CollisionObject2Ds.
Shape casting allows to detect collision objects by sweeping its ShapeCast2D.Shape along the cast direction determined by ShapeCast2D.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 ShapeCast2D.TargetPosition set to Vector2(0, 0) and by calling ShapeCast2D.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.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
ShapeCast2DConstructor
public ShapeCast2D();Properties10
CollideWithAreasProperty
public bool CollideWithAreas { get; set; }If true, collisions with Area2Ds will be reported.
CollideWithBodiesProperty
public bool CollideWithBodies { get; set; }If true, collisions with PhysicsBody2Ds will be reported.
CollisionMaskProperty
public uint CollisionMask { get; set; }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 in the documentation for more information.
CollisionResultProperty
public Reckfell.Collections.Array CollisionResult { get; }Returns the complete collision information from the collision sweep. The data returned is the same as in the PhysicsDirectSpaceState2D.GetRestInfo method.
EnabledProperty
public bool Enabled { get; set; }If true, collisions will be reported.
ExcludeParentProperty
public bool ExcludeParent { get; set; }If true, the parent node will be excluded from collision detection.
MarginProperty
public float Margin { get; set; }The collision margin for the shape. A larger margin helps detecting collisions more consistently, at the cost of precision.
MaxResultsProperty
public int MaxResults { get; set; }The number of intersections can be limited with this parameter, to reduce the processing time.
ShapeProperty
public Shape2D Shape { get; set; }The shape to be used for collision queries.
TargetPositionProperty
public Vector2 TargetPosition { get; set; }The shape's destination point, relative to this node's Entity2D.Position.
Methods36
AddExceptionvoid
public void AddException(CollisionObject2D node);Adds a collision exception so the shape does not report collisions with the specified node.
| Name | Type | Default |
|---|---|---|
node | CollisionObject2D | — |
AddExceptionRidvoid
public void AddExceptionRid(Rid rid);Adds a collision exception so the shape does not report collisions with the specified Rid.
| Name | Type | Default |
|---|---|---|
rid | Rid | — |
ClearExceptionsvoid
public void ClearExceptions();Removes all collision exceptions for this shape.
ForceShapecastUpdatevoid
public void 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: Setting ShapeCast2D.Enabled to true is not required for this to work.
GetClosestCollisionSafeFractionfloat
public float GetClosestCollisionSafeFraction();Returns the fraction from this cast's origin to its ShapeCast2D.TargetPosition of how far the shape can move without triggering a collision, as a value between 0.0 and 1.0.
floatGetClosestCollisionUnsafeFractionfloat
public float GetClosestCollisionUnsafeFraction();Returns the fraction from this cast's origin to its ShapeCast2D.TargetPosition 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 ShapeCast2D.GetClosestCollisionSafeFraction, however shape casting is calculated in discrete steps, so the precise point of collision can occur between two calculated positions.
floatGetColliderReckfellObject
public ReckfellObject GetCollider(int index);Returns the collided ReckfellObject of one of the multiple collisions at index, or null if no object is intersecting the shape (i.e. ShapeCast2D.IsColliding returns false).
| Name | Type | Default |
|---|---|---|
index | int | — |
ReckfellObjectGetColliderRidRid
public Rid GetColliderRid(int index);Returns the Rid of the collided object of one of the multiple collisions at index.
| Name | Type | Default |
|---|---|---|
index | int | — |
RidGetColliderShapeint
public int GetColliderShape(int index);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. ShapeCast2D.IsColliding returns false).
| Name | Type | Default |
|---|---|---|
index | int | — |
intGetCollisionCountint
public int GetCollisionCount();The number of collisions detected at the point of impact. Use this to iterate over multiple collisions as provided by ShapeCast2D.GetCollider, ShapeCast2D.GetColliderShape, ShapeCast2D.GetCollisionPoint, and ShapeCast2D.GetCollisionNormal methods.
intGetCollisionMaskuint
public uint GetCollisionMask();uintGetCollisionMaskValuebool
public bool GetCollisionMaskValue(int layerNumber);Returns whether or not the specified layer of the ShapeCast2D.CollisionMask is enabled, given a layer_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
layerNumber | int | — |
boolGetCollisionNormalVector2
public Vector2 GetCollisionNormal(int index);Returns the normal of one of the multiple collisions at index of the intersecting object.
| Name | Type | Default |
|---|---|---|
index | int | — |
Vector2GetCollisionPointVector2
public Vector2 GetCollisionPoint(int index);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.
| Name | Type | Default |
|---|---|---|
index | int | — |
Vector2GetCollisionResultReckfell.Collections.Array
public Reckfell.Collections.Array GetCollisionResult();Reckfell.Collections.ArrayGetExcludeParentBodybool
public bool GetExcludeParentBody();boolGetMarginfloat
public float GetMargin();floatGetMaxResultsint
public int GetMaxResults();intGetShapeShape2D
public Shape2D GetShape();Shape2DGetTargetPositionVector2
public Vector2 GetTargetPosition();Vector2IsCollideWithAreasEnabledbool
public bool IsCollideWithAreasEnabled();boolIsCollideWithBodiesEnabledbool
public bool IsCollideWithBodiesEnabled();boolIsCollidingbool
public bool IsColliding();Returns whether any object is intersecting with the shape's vector (considering the vector length).
boolIsEnabledbool
public bool IsEnabled();boolRemoveExceptionvoid
public void RemoveException(CollisionObject2D node);Removes a collision exception so the shape does report collisions with the specified node.
| Name | Type | Default |
|---|---|---|
node | CollisionObject2D | — |
RemoveExceptionRidvoid
public void RemoveExceptionRid(Rid rid);Removes a collision exception so the shape does report collisions with the specified Rid.
| Name | Type | Default |
|---|---|---|
rid | Rid | — |
SetCollideWithAreasvoid
public void SetCollideWithAreas(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
SetCollideWithBodiesvoid
public void SetCollideWithBodies(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
SetCollisionMaskvoid
public void SetCollisionMask(uint mask);| Name | Type | Default |
|---|---|---|
mask | uint | — |
SetCollisionMaskValuevoid
public void SetCollisionMaskValue(int layerNumber, bool value);Based on value, enables or disables the specified layer in the ShapeCast2D.CollisionMask, given a layer_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
layerNumber | int | — |
value | bool | — |
SetEnabledvoid
public void SetEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetExcludeParentBodyvoid
public void SetExcludeParentBody(bool mask);| Name | Type | Default |
|---|---|---|
mask | bool | — |
SetMarginvoid
public void SetMargin(float margin);| Name | Type | Default |
|---|---|---|
margin | float | — |
SetMaxResultsvoid
public void SetMaxResults(int maxResults);| Name | Type | Default |
|---|---|---|
maxResults | int | — |
SetShapevoid
public void SetShape(Shape2D shape);| Name | Type | Default |
|---|---|---|
shape | Shape2D | — |
SetTargetPositionvoid
public void SetTargetPosition(Vector2 localPoint);| Name | Type | Default |
|---|---|---|
localPoint | Vector2 | — |