Class CollisionObject2D
Abstract base class for 2D physics objects.
- Inheritance
-
CollisionObject2D
- Derived
Remarks
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.
Properties
collision_layer
The physics layers this CollisionObject2D is in. Collision objects can exist in one or more of 32 different layers. See also collision_mask.
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.
var collision_layer : int = 1
Property Value
Remarks
collision_mask
The physics layers this CollisionObject2D scans. Collision objects can scan one or more of 32 different layers. See also collision_layer.
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.
var collision_mask : int = 1
Property Value
Remarks
collision_priority
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.
var collision_priority : float = 1.0
Property Value
Remarks
disable_mode
Defines the behavior in physics when process_mode is set to Node.PROCESS_MODE_DISABLED. See DisableMode for more details about the different modes.
var disable_mode : int = 0
Property Value
Remarks
input_pickable
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 collision_layer bit to be set.
var input_pickable : bool = true
Property Value
Remarks
Methods
_input_event(Viewport, InputEvent, int)
Qualifiers: virtual
Accepts unhandled InputEvents. shape_idx
is the child index of the clicked Shape2D. Connect to CollisionObject2D.input_event to easily pick up these events.
Note: CollisionObject2D._input_event requires input_pickable to be true
and at least one collision_layer bit to be set.
void _input_event(Viewport viewport, InputEvent event, int shape_idx)
Parameters
viewport
Viewportevent
InputEventshape_idx
int
_mouse_enter
Qualifiers: virtual
Called when the mouse pointer enters any of this object's shapes. Requires input_pickable to be true
and at least one collision_layer bit to be set. Note that moving between different shapes within a single CollisionObject2D won't cause this function to be called.
void _mouse_enter
_mouse_exit
Qualifiers: virtual
Called when the mouse pointer exits all this object's shapes. Requires input_pickable to be true
and at least one collision_layer bit to be set. Note that moving between different shapes within a single CollisionObject2D won't cause this function to be called.
void _mouse_exit
_mouse_shape_enter(int)
Qualifiers: virtual
Called when the mouse pointer enters any of this object's shapes or moves from one shape to another. shape_idx
is the child index of the newly entered Shape2D. Requires input_pickable to be true
and at least one collision_layer bit to be called.
void _mouse_shape_enter(int shape_idx)
Parameters
shape_idx
int
_mouse_shape_exit(int)
Qualifiers: virtual
Called when the mouse pointer exits any of this object's shapes. shape_idx
is the child index of the exited Shape2D. Requires input_pickable to be true
and at least one collision_layer bit to be called.
void _mouse_shape_exit(int shape_idx)
Parameters
shape_idx
int
create_shape_owner(Object)
Creates a new shape owner for the given object. Returns owner_id
of the new owner for future reference.
int create_shape_owner(Object owner)
Parameters
owner
Object
get_collision_layer_value(int)
Qualifiers: const
Returns whether or not the specified layer of the collision_layer is enabled, given a layer_number
between 1 and 32.
bool get_collision_layer_value(int layer_number)
Parameters
layer_number
int
get_collision_mask_value(int)
Qualifiers: const
Returns whether or not the specified layer of the collision_mask is enabled, given a layer_number
between 1 and 32.
bool get_collision_mask_value(int layer_number)
Parameters
layer_number
int
get_rid
Qualifiers: const
Returns the object's RID.
RID get_rid
get_shape_owner_one_way_collision_margin(int)
Qualifiers: const
Returns the one_way_collision_margin
of the shape owner identified by given owner_id
.
float get_shape_owner_one_way_collision_margin(int owner_id)
Parameters
owner_id
int
get_shape_owners
Returns an Array of owner_id
identifiers. You can use these ids in other methods that take owner_id
as an argument.
PackedInt32Array get_shape_owners
is_shape_owner_disabled(int)
Qualifiers: const
If true
, the shape owner and its shapes are disabled.
bool is_shape_owner_disabled(int owner_id)
Parameters
owner_id
int
is_shape_owner_one_way_collision_enabled(int)
Qualifiers: const
Returns true
if collisions for the shape owner originating from this CollisionObject2D will not be reported to collided with CollisionObject2Ds.
bool is_shape_owner_one_way_collision_enabled(int owner_id)
Parameters
owner_id
int
remove_shape_owner(int)
Removes the given shape owner.
void remove_shape_owner(int owner_id)
Parameters
owner_id
int
set_collision_layer_value(int, bool)
Based on value
, enables or disables the specified layer in the collision_layer, given a layer_number
between 1 and 32.
void set_collision_layer_value(int layer_number, bool value)
Parameters
set_collision_mask_value(int, bool)
Based on value
, enables or disables the specified layer in the collision_mask, given a layer_number
between 1 and 32.
void set_collision_mask_value(int layer_number, bool value)
Parameters
shape_find_owner(int)
Qualifiers: const
Returns the owner_id
of the given shape.
int shape_find_owner(int shape_index)
Parameters
shape_index
int
shape_owner_add_shape(int, Shape2D)
Adds a Shape2D to the shape owner.
void shape_owner_add_shape(int owner_id, Shape2D shape)
Parameters
shape_owner_clear_shapes(int)
Removes all shapes from the shape owner.
void shape_owner_clear_shapes(int owner_id)
Parameters
owner_id
int
shape_owner_get_owner(int)
Qualifiers: const
Returns the parent object of the given shape owner.
Object shape_owner_get_owner(int owner_id)
Parameters
owner_id
int
shape_owner_get_shape(int, int)
Qualifiers: const
Returns the Shape2D with the given ID from the given shape owner.
Shape2D shape_owner_get_shape(int owner_id, int shape_id)
Parameters
shape_owner_get_shape_count(int)
Qualifiers: const
Returns the number of shapes the given shape owner contains.
int shape_owner_get_shape_count(int owner_id)
Parameters
owner_id
int
shape_owner_get_shape_index(int, int)
Qualifiers: const
Returns the child index of the Shape2D with the given ID from the given shape owner.
int shape_owner_get_shape_index(int owner_id, int shape_id)
Parameters
shape_owner_get_transform(int)
Qualifiers: const
Returns the shape owner's Transform2D.
Transform2D shape_owner_get_transform(int owner_id)
Parameters
owner_id
int
shape_owner_remove_shape(int, int)
Removes a shape from the given shape owner.
void shape_owner_remove_shape(int owner_id, int shape_id)
Parameters
shape_owner_set_disabled(int, bool)
If true
, disables the given shape owner.
void shape_owner_set_disabled(int owner_id, bool disabled)
Parameters
shape_owner_set_one_way_collision(int, bool)
If enable
is true
, collisions for the shape owner originating from this CollisionObject2D will not be reported to collided with CollisionObject2Ds.
void shape_owner_set_one_way_collision(int owner_id, bool enable)
Parameters
shape_owner_set_one_way_collision_margin(int, float)
Sets the one_way_collision_margin
of the shape owner identified by given owner_id
to margin
pixels.
void shape_owner_set_one_way_collision_margin(int owner_id, float margin)
Parameters
shape_owner_set_transform(int, Transform2D)
Sets the Transform2D of the given shape owner.
void shape_owner_set_transform(int owner_id, Transform2D transform)
Parameters
owner_id
inttransform
Transform2D
Events
input_event(Node, InputEvent, int)
Emitted when an input event occurs. Requires input_pickable to be true
and at least one collision_layer bit to be set. See CollisionObject2D._input_event for details.
signal input_event(Node viewport, InputEvent event, int shape_idx)
Parameters
viewport
Nodeevent
InputEventshape_idx
int
mouse_entered
Emitted when the mouse pointer enters any of this object's shapes. Requires input_pickable to be true
and at least one collision_layer 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.
signal mouse_entered
mouse_exited
Emitted when the mouse pointer exits all this object's shapes. Requires input_pickable to be true
and at least one collision_layer 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.
signal mouse_exited
mouse_shape_entered(int)
Emitted when the mouse pointer enters any of this object's shapes or moves from one shape to another. shape_idx
is the child index of the newly entered Shape2D. Requires input_pickable to be true
and at least one collision_layer bit to be set.
signal mouse_shape_entered(int shape_idx)
Parameters
shape_idx
int
mouse_shape_exited(int)
Emitted when the mouse pointer exits any of this object's shapes. shape_idx
is the child index of the exited Shape2D. Requires input_pickable to be true
and at least one collision_layer bit to be set.
signal mouse_shape_exited(int shape_idx)
Parameters
shape_idx
int