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
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
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
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
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
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
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
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
GetRid()
Returns the object's Rid.
public Rid GetRid()
Returns
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
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_nameName of the method to check for.
Returns
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_nameName of the signal to check for.
Returns
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_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsShapeOwnerDisabled(uint)
If true
, the shape owner and its shapes are disabled.
public bool IsShapeOwnerDisabled(uint ownerId)
Parameters
ownerId
uint
Returns
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
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
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
ShapeFindOwner(int)
Returns the owner_id
of the given shape.
public uint ShapeFindOwner(int shapeIndex)
Parameters
shapeIndex
int
Returns
ShapeOwnerAddShape(uint, Shape2D)
Adds a Shape2D to the shape owner.
public void ShapeOwnerAddShape(uint ownerId, Shape2D shape)
Parameters
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
ShapeOwnerGetShape(uint, int)
Returns the Shape2D with the given ID from the given shape owner.
public Shape2D ShapeOwnerGetShape(uint ownerId, int shapeId)
Parameters
Returns
ShapeOwnerGetShapeCount(uint)
Returns the number of shapes the given shape owner contains.
public int ShapeOwnerGetShapeCount(uint ownerId)
Parameters
ownerId
uint
Returns
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
Returns
ShapeOwnerGetTransform(uint)
Returns the shape owner's Transform2D.
public Transform2D ShapeOwnerGetTransform(uint ownerId)
Parameters
ownerId
uint
Returns
ShapeOwnerRemoveShape(uint, int)
Removes a shape from the given shape owner.
public void ShapeOwnerRemoveShape(uint ownerId, int shapeId)
Parameters
ShapeOwnerSetDisabled(uint, bool)
If true
, disables the given shape owner.
public void ShapeOwnerSetDisabled(uint ownerId, bool disabled)
Parameters
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
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
ShapeOwnerSetTransform(uint, Transform2D)
Sets the Transform2D of the given shape owner.
public void ShapeOwnerSetTransform(uint ownerId, Transform2D transform)
Parameters
ownerId
uinttransform
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
Viewportevent
InputEventshapeIdx
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
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
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
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
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