Table of Contents

Class CollisionObject2D

Namespace
Godot
Assembly
GodotSharp.dll

Abstract base class for 2D physics objects. CollisionObject2D can hold any number of Shape2Ds for collision. Each shape must be assigned to a shape owner. Shape owners are not nodes and do not appear in the editor, but are accessible through code using the shape_owner_* methods.

Note: Only collisions between objects within the same canvas (Viewport canvas or CanvasLayer) are supported. The behavior of collisions between objects in different canvases is undefined.

public class CollisionObject2D : Node2D, IDisposable
Inheritance
CollisionObject2D
Implements
Derived
Inherited Members

Properties

CollisionLayer

The physics layers this CollisionObject2D is in. Collision objects can exist in one or more of 32 different layers. See also CollisionMask.

Note: Object A can detect a contact with object B only if object B is in any of the layers that object A scans. See Collision layers and masks in the documentation for more information.

public uint CollisionLayer { get; set; }

Property Value

uint

CollisionMask

The physics layers this CollisionObject2D scans. Collision objects can scan one or more of 32 different layers. See also CollisionLayer.

Note: Object A can detect a contact with object B only if object B is in any of the layers that object A scans. See Collision layers and masks in the documentation for more information.

public uint CollisionMask { get; set; }

Property Value

uint

CollisionPriority

The priority used to solve colliding when occurring penetration. The higher the priority is, the lower the penetration into the object will be. This can for example be used to prevent the player from breaking through the boundaries of a level.

public float CollisionPriority { get; set; }

Property Value

float

DisableMode

Defines the behavior in physics when ProcessMode is set to Disabled. See CollisionObject2D.DisableModeEnum for more details about the different modes.

public CollisionObject2D.DisableModeEnum DisableMode { get; set; }

Property Value

CollisionObject2D.DisableModeEnum

InputPickable

If true, this object is pickable. A pickable object can detect the mouse pointer entering/leaving, and if the mouse is inside it, report input events. Requires at least one CollisionLayer bit to be set.

public bool InputPickable { get; set; }

Property Value

bool

Methods

CreateShapeOwner(GodotObject)

Creates a new shape owner for the given object. Returns owner_id of the new owner for future reference.

public uint CreateShapeOwner(GodotObject owner)

Parameters

owner GodotObject

Returns

uint

GetCollisionLayerValue(int)

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

public bool GetCollisionLayerValue(int layerNumber)

Parameters

layerNumber int

Returns

bool

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

GetRid()

Returns the object's Rid.

public Rid GetRid()

Returns

Rid

GetShapeOwnerOneWayCollisionMargin(uint)

Returns the one_way_collision_margin of the shape owner identified by given ownerId.

public float GetShapeOwnerOneWayCollisionMargin(uint ownerId)

Parameters

ownerId uint

Returns

float

GetShapeOwners()

Returns an Array of owner_id identifiers. You can use these ids in other methods that take owner_id as an argument.

public int[] GetShapeOwners()

Returns

int[]

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

IsShapeOwnerDisabled(uint)

If true, the shape owner and its shapes are disabled.

public bool IsShapeOwnerDisabled(uint ownerId)

Parameters

ownerId uint

Returns

bool

IsShapeOwnerOneWayCollisionEnabled(uint)

Returns true if collisions for the shape owner originating from this CollisionObject2D will not be reported to collided with CollisionObject2Ds.

public bool IsShapeOwnerOneWayCollisionEnabled(uint ownerId)

Parameters

ownerId uint

Returns

bool

RemoveShapeOwner(uint)

Removes the given shape owner.

public void RemoveShapeOwner(uint ownerId)

Parameters

ownerId uint

SetCollisionLayerValue(int, bool)

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

public void SetCollisionLayerValue(int layerNumber, bool value)

Parameters

layerNumber int
value bool

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

ShapeFindOwner(int)

Returns the owner_id of the given shape.

public uint ShapeFindOwner(int shapeIndex)

Parameters

shapeIndex int

Returns

uint

ShapeOwnerAddShape(uint, Shape2D)

Adds a Shape2D to the shape owner.

public void ShapeOwnerAddShape(uint ownerId, Shape2D shape)

Parameters

ownerId uint
shape Shape2D

ShapeOwnerClearShapes(uint)

Removes all shapes from the shape owner.

public void ShapeOwnerClearShapes(uint ownerId)

Parameters

ownerId uint

ShapeOwnerGetOwner(uint)

Returns the parent object of the given shape owner.

public GodotObject ShapeOwnerGetOwner(uint ownerId)

Parameters

ownerId uint

Returns

GodotObject

ShapeOwnerGetShape(uint, int)

Returns the Shape2D with the given ID from the given shape owner.

public Shape2D ShapeOwnerGetShape(uint ownerId, int shapeId)

Parameters

ownerId uint
shapeId int

Returns

Shape2D

ShapeOwnerGetShapeCount(uint)

Returns the number of shapes the given shape owner contains.

public int ShapeOwnerGetShapeCount(uint ownerId)

Parameters

ownerId uint

Returns

int

ShapeOwnerGetShapeIndex(uint, int)

Returns the child index of the Shape2D with the given ID from the given shape owner.

public int ShapeOwnerGetShapeIndex(uint ownerId, int shapeId)

Parameters

ownerId uint
shapeId int

Returns

int

ShapeOwnerGetTransform(uint)

Returns the shape owner's Transform2D.

public Transform2D ShapeOwnerGetTransform(uint ownerId)

Parameters

ownerId uint

Returns

Transform2D

ShapeOwnerRemoveShape(uint, int)

Removes a shape from the given shape owner.

public void ShapeOwnerRemoveShape(uint ownerId, int shapeId)

Parameters

ownerId uint
shapeId int

ShapeOwnerSetDisabled(uint, bool)

If true, disables the given shape owner.

public void ShapeOwnerSetDisabled(uint ownerId, bool disabled)

Parameters

ownerId uint
disabled bool

ShapeOwnerSetOneWayCollision(uint, bool)

If enable is true, collisions for the shape owner originating from this CollisionObject2D will not be reported to collided with CollisionObject2Ds.

public void ShapeOwnerSetOneWayCollision(uint ownerId, bool enable)

Parameters

ownerId uint
enable bool

ShapeOwnerSetOneWayCollisionMargin(uint, float)

Sets the one_way_collision_margin of the shape owner identified by given ownerId to margin pixels.

public void ShapeOwnerSetOneWayCollisionMargin(uint ownerId, float margin)

Parameters

ownerId uint
margin float

ShapeOwnerSetTransform(uint, Transform2D)

Sets the Transform2D of the given shape owner.

public void ShapeOwnerSetTransform(uint ownerId, Transform2D transform)

Parameters

ownerId uint
transform Transform2D

_InputEvent(Viewport, InputEvent, int)

Accepts unhandled InputEvents. shapeIdx is the child index of the clicked Shape2D. Connect to InputEvent to easily pick up these events.

Note: _InputEvent(Viewport, InputEvent, int) requires InputPickable to be true and at least one CollisionLayer bit to be set.

public virtual void _InputEvent(Viewport viewport, InputEvent @event, int shapeIdx)

Parameters

viewport Viewport
event InputEvent
shapeIdx int

_MouseEnter()

Called when the mouse pointer enters any of this object's shapes. Requires InputPickable to be true and at least one CollisionLayer bit to be set. Note that moving between different shapes within a single CollisionObject2D won't cause this function to be called.

public virtual void _MouseEnter()

_MouseExit()

Called when the mouse pointer exits all this object's shapes. Requires InputPickable to be true and at least one CollisionLayer bit to be set. Note that moving between different shapes within a single CollisionObject2D won't cause this function to be called.

public virtual void _MouseExit()

_MouseShapeEnter(int)

Called when the mouse pointer enters any of this object's shapes or moves from one shape to another. shapeIdx is the child index of the newly entered Shape2D. Requires InputPickable to be true and at least one CollisionLayer bit to be called.

public virtual void _MouseShapeEnter(int shapeIdx)

Parameters

shapeIdx int

_MouseShapeExit(int)

Called when the mouse pointer exits any of this object's shapes. shapeIdx is the child index of the exited Shape2D. Requires InputPickable to be true and at least one CollisionLayer bit to be called.

public virtual void _MouseShapeExit(int shapeIdx)

Parameters

shapeIdx int

Events

InputEvent

Emitted when an input event occurs. Requires InputPickable to be true and at least one CollisionLayer bit to be set. See _InputEvent(Viewport, InputEvent, int) for details.

public event CollisionObject2D.InputEventEventHandler InputEvent

Event Type

CollisionObject2D.InputEventEventHandler

MouseEntered

Emitted when the mouse pointer enters any of this object's shapes. Requires InputPickable to be true and at least one CollisionLayer bit to be set. Note that moving between different shapes within a single CollisionObject2D won't cause this signal to be emitted.

Note: Due to the lack of continuous collision detection, this signal may not be emitted in the expected order if the mouse moves fast enough and the CollisionObject2D's area is small. This signal may also not be emitted if another CollisionObject2D is overlapping the CollisionObject2D in question.

public event Action MouseEntered

Event Type

Action

MouseExited

Emitted when the mouse pointer exits all this object's shapes. Requires InputPickable to be true and at least one CollisionLayer bit to be set. Note that moving between different shapes within a single CollisionObject2D won't cause this signal to be emitted.

Note: Due to the lack of continuous collision detection, this signal may not be emitted in the expected order if the mouse moves fast enough and the CollisionObject2D's area is small. This signal may also not be emitted if another CollisionObject2D is overlapping the CollisionObject2D in question.

public event Action MouseExited

Event Type

Action

MouseShapeEntered

Emitted when the mouse pointer enters any of this object's shapes or moves from one shape to another. shapeIdx is the child index of the newly entered Shape2D. Requires InputPickable to be true and at least one CollisionLayer bit to be set.

public event CollisionObject2D.MouseShapeEnteredEventHandler MouseShapeEntered

Event Type

CollisionObject2D.MouseShapeEnteredEventHandler

MouseShapeExited

Emitted when the mouse pointer exits any of this object's shapes. shapeIdx is the child index of the exited Shape2D. Requires InputPickable to be true and at least one CollisionLayer bit to be set.

public event CollisionObject2D.MouseShapeExitedEventHandler MouseShapeExited

Event Type

CollisionObject2D.MouseShapeExitedEventHandler