Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

RigidBody2D

Inherits: PhysicsBody2D < CollisionObject2D < Node2D < CanvasItem < Node < Object

Inherited By: PhysicalBone2D

A 2D physics body that is moved by a physics simulation.

Description

RigidBody2D implements full 2D physics. It cannot be controlled directly, instead, you must apply forces to it (gravity, impulses, etc.), and the physics simulation will calculate the resulting movement, rotation, react to collisions, and affect other physics bodies in its path.

The body's behavior can be adjusted via lock_rotation, freeze, and freeze_mode. By changing various properties of the object, such as mass, you can control how the physics simulation acts on it.

A rigid body will always maintain its shape and size, even when forces are applied to it. It is useful for objects that can be interacted with in an environment, such as a tree that can be knocked over or a stack of crates that can be pushed around.

If you need to override the default physics behavior, you can write a custom force integration function. See custom_integrator.

Note: Changing the 2D transform or linear_velocity of a RigidBody2D very often may lead to some unpredictable behaviors. If you need to directly affect the body, prefer _integrate_forces as it allows you to directly access the physics state.

Tutorials

Properties

float

angular_damp

0.0

DampMode

angular_damp_mode

0

float

angular_velocity

0.0

bool

can_sleep

true

Vector2

center_of_mass

Vector2(0, 0)

CenterOfMassMode

center_of_mass_mode

0

Vector2

constant_force

Vector2(0, 0)

float

constant_torque

0.0

bool

contact_monitor

false

CCDMode

continuous_cd

0

bool

custom_integrator

false

bool

freeze

false

FreezeMode

freeze_mode

0

float

gravity_scale

1.0

float

inertia

0.0

float

linear_damp

0.0

DampMode

linear_damp_mode

0

Vector2

linear_velocity

Vector2(0, 0)

bool

lock_rotation

false

float

mass

1.0

int

max_contacts_reported

0

PhysicsMaterial

physics_material_override

bool

sleeping

false

Methods

void

_integrate_forces ( PhysicsDirectBodyState2D state ) virtual

void

add_constant_central_force ( Vector2 force )

void

add_constant_force ( Vector2 force, Vector2 position=Vector2(0, 0) )

void

add_constant_torque ( float torque )

void

apply_central_force ( Vector2 force )

void

apply_central_impulse ( Vector2 impulse=Vector2(0, 0) )

void

apply_force ( Vector2 force, Vector2 position=Vector2(0, 0) )

void

apply_impulse ( Vector2 impulse, Vector2 position=Vector2(0, 0) )

void

apply_torque ( float torque )

void

apply_torque_impulse ( float torque )

Node2D[]

get_colliding_bodies ( ) const

int

get_contact_count ( ) const

void

set_axis_velocity ( Vector2 axis_velocity )


Signals

body_entered ( Node body )

Emitted when a collision with another PhysicsBody2D or TileMap occurs. Requires contact_monitor to be set to true and max_contacts_reported to be set high enough to detect all the collisions. TileMaps are detected if the TileSet has Collision Shape2Ds.

body the Node, if it exists in the tree, of the other PhysicsBody2D or TileMap.


body_exited ( Node body )

Emitted when the collision with another PhysicsBody2D or TileMap ends. Requires contact_monitor to be set to true and max_contacts_reported to be set high enough to detect all the collisions. TileMaps are detected if the TileSet has Collision Shape2Ds.

body the Node, if it exists in the tree, of the other PhysicsBody2D or TileMap.


body_shape_entered ( RID body_rid, Node body, int body_shape_index, int local_shape_index )

Emitted when one of this RigidBody2D's Shape2Ds collides with another PhysicsBody2D or TileMap's Shape2Ds. Requires contact_monitor to be set to true and max_contacts_reported to be set high enough to detect all the collisions. TileMaps are detected if the TileSet has Collision Shape2Ds.

body_rid the RID of the other PhysicsBody2D or TileSet's CollisionObject2D used by the PhysicsServer2D.

body the Node, if it exists in the tree, of the other PhysicsBody2D or TileMap.

body_shape_index the index of the Shape2D of the other PhysicsBody2D or TileMap used by the PhysicsServer2D. Get the CollisionShape2D node with body.shape_owner_get_owner(body.shape_find_owner(body_shape_index)).

local_shape_index the index of the Shape2D of this RigidBody2D used by the PhysicsServer2D. Get the CollisionShape2D node with self.shape_owner_get_owner(self.shape_find_owner(local_shape_index)).


body_shape_exited ( RID body_rid, Node body, int body_shape_index, int local_shape_index )

Emitted when the collision between one of this RigidBody2D's Shape2Ds and another PhysicsBody2D or TileMap's Shape2Ds ends. Requires contact_monitor to be set to true and max_contacts_reported to be set high enough to detect all the collisions. TileMaps are detected if the TileSet has Collision Shape2Ds.

body_rid the RID of the other PhysicsBody2D or TileSet's CollisionObject2D used by the PhysicsServer2D.