Table of Contents

Class RigidBody2D

Namespace
Godot
Assembly
GodotSharp.dll

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 LockRotation, Freeze, and FreezeMode. 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 CustomIntegrator.

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

public class RigidBody2D : PhysicsBody2D, IDisposable
Inheritance
RigidBody2D
Implements
Derived
Inherited Members

Constructors

RigidBody2D()

public RigidBody2D()

Properties

AngularDamp

Damps the body's rotation. By default, the body will use the Default Angular Damp in Project > Project Settings > Physics > 2d or any value override set by an Area2D the body is in. Depending on AngularDampMode, you can set AngularDamp to be added to or to replace the body's damping value.

See ProjectSettings.physics/2d/default_angular_damp for more details about damping.

public float AngularDamp { get; set; }

Property Value

float

AngularDampMode

Defines how AngularDamp is applied. See RigidBody2D.DampMode for possible values.

public RigidBody2D.DampMode AngularDampMode { get; set; }

Property Value

RigidBody2D.DampMode

AngularVelocity

The body's rotational velocity in radians per second.

public float AngularVelocity { get; set; }

Property Value

float

CanSleep

If true, the body can enter sleep mode when there is no movement. See Sleeping.

public bool CanSleep { get; set; }

Property Value

bool

CenterOfMass

The body's custom center of mass, relative to the body's origin position, when CenterOfMassMode is set to Custom. This is the balanced point of the body, where applied forces only cause linear acceleration. Applying forces outside of the center of mass causes angular acceleration.

When CenterOfMassMode is set to Auto (default value), the center of mass is automatically computed.

public Vector2 CenterOfMass { get; set; }

Property Value

Vector2

CenterOfMassMode

Defines the way the body's center of mass is set. See RigidBody2D.CenterOfMassModeEnum for possible values.

public RigidBody2D.CenterOfMassModeEnum CenterOfMassMode { get; set; }

Property Value

RigidBody2D.CenterOfMassModeEnum

ConstantForce

The body's total constant positional forces applied during each physics update.

See AddConstantForce(Vector2, Vector2?) and AddConstantCentralForce(Vector2).

public Vector2 ConstantForce { get; set; }

Property Value

Vector2

ConstantTorque

The body's total constant rotational forces applied during each physics update.

See AddConstantTorque(float).

public float ConstantTorque { get; set; }

Property Value

float

ContactMonitor

If true, the RigidBody2D will emit signals when it collides with another body.

Note: By default the maximum contacts reported is set to 0, meaning nothing will be recorded, see MaxContactsReported.

public bool ContactMonitor { get; set; }

Property Value

bool

ContinuousCd

Continuous collision detection mode.

Continuous collision detection tries to predict where a moving body will collide instead of moving it and correcting its movement after collision. Continuous collision detection is slower, but more precise and misses fewer collisions with small, fast-moving objects. Raycasting and shapecasting methods are available. See RigidBody2D.CcdMode for details.

public RigidBody2D.CcdMode ContinuousCd { get; set; }

Property Value

RigidBody2D.CcdMode

CustomIntegrator

If true, internal force integration is disabled for this body. Aside from collision response, the body will only move as determined by the _IntegrateForces(PhysicsDirectBodyState2D) function.

public bool CustomIntegrator { get; set; }

Property Value

bool

Freeze

If true, the body is frozen. Gravity and forces are not applied anymore.

See FreezeMode to set the body's behavior when frozen.

For a body that is always frozen, use StaticBody2D or AnimatableBody2D instead.

public bool Freeze { get; set; }

Property Value

bool

FreezeMode

The body's freeze mode. Can be used to set the body's behavior when Freeze is enabled. See RigidBody2D.FreezeModeEnum for possible values.

For a body that is always frozen, use StaticBody2D or AnimatableBody2D instead.

public RigidBody2D.FreezeModeEnum FreezeMode { get; set; }

Property Value

RigidBody2D.FreezeModeEnum

GravityScale

Multiplies the gravity applied to the body. The body's gravity is calculated from the Default Gravity value in Project > Project Settings > Physics > 2d and/or any additional gravity vector applied by Area2Ds.

public float GravityScale { get; set; }

Property Value

float

Inertia

The body's moment of inertia. This is like mass, but for rotation: it determines how much torque it takes to rotate the body. The moment of inertia is usually computed automatically from the mass and the shapes, but this property allows you to set a custom value.

If set to 0, inertia is automatically computed (default value).

Note: This value does not change when inertia is automatically computed. Use PhysicsServer2D to get the computed inertia.

private RigidBody2D _ball;

public override void _Ready() { _ball = GetNode<RigidBody2D>("Ball"); }

private float GetBallInertia() { return 1.0f / PhysicsServer2D.BodyGetDirectState(_ball.GetRid()).InverseInertia; }

public float Inertia { get; set; }

Property Value

float

LinearDamp

Damps the body's movement. By default, the body will use the Default Linear Damp in Project > Project Settings > Physics > 2d or any value override set by an Area2D the body is in. Depending on LinearDampMode, you can set LinearDamp to be added to or to replace the body's damping value.

See ProjectSettings.physics/2d/default_linear_damp for more details about damping.

public float LinearDamp { get; set; }

Property Value

float

LinearDampMode

Defines how LinearDamp is applied. See RigidBody2D.DampMode for possible values.

public RigidBody2D.DampMode LinearDampMode { get; set; }

Property Value

RigidBody2D.DampMode

LinearVelocity

The body's linear velocity in pixels per second. Can be used sporadically, but don't set this every frame, because physics may run in another thread and runs at a different granularity. Use _IntegrateForces(PhysicsDirectBodyState2D) as your process loop for precise control of the body state.

public Vector2 LinearVelocity { get; set; }

Property Value

Vector2

LockRotation

If true, the body cannot rotate. Gravity and forces only apply linear movement.

public bool LockRotation { get; set; }

Property Value

bool

Mass

The body's mass.

public float Mass { get; set; }

Property Value

float

MaxContactsReported

The maximum number of contacts that will be recorded. Requires a value greater than 0 and ContactMonitor to be set to true to start to register contacts. Use GetContactCount() to retrieve the count or GetCollidingBodies() to retrieve bodies that have been collided with.

Note: The number of contacts is different from the number of collisions. Collisions between parallel edges will result in two contacts (one at each end), and collisions between parallel faces will result in four contacts (one at each corner).

public int MaxContactsReported { get; set; }

Property Value

int

PhysicsMaterialOverride

The physics material override for the body.

If a material is assigned to this property, it will be used instead of any other physics material, such as an inherited one.

public PhysicsMaterial PhysicsMaterialOverride { get; set; }

Property Value

PhysicsMaterial

Sleeping

If true, the body will not move and will not calculate forces until woken up by another body through, for example, a collision, or by using the ApplyImpulse(Vector2, Vector2?) or ApplyForce(Vector2, Vector2?) methods.

public bool Sleeping { get; set; }

Property Value

bool

Methods

AddConstantCentralForce(Vector2)

Adds a constant directional force without affecting rotation that keeps being applied over time until cleared with constant_force = Vector2(0, 0).

This is equivalent to using AddConstantForce(Vector2, Vector2?) at the body's center of mass.

public void AddConstantCentralForce(Vector2 force)

Parameters

force Vector2

AddConstantForce(Vector2, Vector2?)

Adds a constant positioned force to the body that keeps being applied over time until cleared with constant_force = Vector2(0, 0).

position is the offset from the body origin in global coordinates.

public void AddConstantForce(Vector2 force, Vector2? position = null)

Parameters

force Vector2
position Vector2?

If the parameter is null, then the default value is new Vector2(0, 0).

AddConstantTorque(float)

Adds a constant rotational force without affecting position that keeps being applied over time until cleared with constant_torque = 0.

public void AddConstantTorque(float torque)

Parameters

torque float

ApplyCentralForce(Vector2)

Applies a directional force without affecting rotation. A force is time dependent and meant to be applied every physics update.

This is equivalent to using ApplyForce(Vector2, Vector2?) at the body's center of mass.

public void ApplyCentralForce(Vector2 force)

Parameters

force Vector2

ApplyCentralImpulse(Vector2?)

Applies a directional impulse without affecting rotation.

An impulse is time-independent! Applying an impulse every frame would result in a framerate-dependent force. For this reason, it should only be used when simulating one-time impacts (use the "_force" functions otherwise).

This is equivalent to using ApplyImpulse(Vector2, Vector2?) at the body's center of mass.

public void ApplyCentralImpulse(Vector2? impulse = null)

Parameters

impulse Vector2?

If the parameter is null, then the default value is new Vector2(0, 0).

ApplyForce(Vector2, Vector2?)

Applies a positioned force to the body. A force is time dependent and meant to be applied every physics update.

position is the offset from the body origin in global coordinates.

public void ApplyForce(Vector2 force, Vector2? position = null)

Parameters

force Vector2
position Vector2?

If the parameter is null, then the default value is new Vector2(0, 0).

ApplyImpulse(Vector2, Vector2?)

Applies a positioned impulse to the body.

An impulse is time-independent! Applying an impulse every frame would result in a framerate-dependent force. For this reason, it should only be used when simulating one-time impacts (use the "_force" functions otherwise).

position is the offset from the body origin in global coordinates.

public void ApplyImpulse(Vector2 impulse, Vector2? position = null)

Parameters

impulse Vector2
position Vector2?

If the parameter is null, then the default value is new Vector2(0, 0).

ApplyTorque(float)

Applies a rotational force without affecting position. A force is time dependent and meant to be applied every physics update.

Note: Inertia is required for this to work. To have Inertia, an active CollisionShape2D must be a child of the node, or you can manually set Inertia.

public void ApplyTorque(float torque)

Parameters

torque float

ApplyTorqueImpulse(float)

Applies a rotational impulse to the body without affecting the position.

An impulse is time-independent! Applying an impulse every frame would result in a framerate-dependent force. For this reason, it should only be used when simulating one-time impacts (use the "_force" functions otherwise).

Note: Inertia is required for this to work. To have Inertia, an active CollisionShape2D must be a child of the node, or you can manually set Inertia.

public void ApplyTorqueImpulse(float torque)

Parameters

torque float

GetCollidingBodies()

Returns a list of the bodies colliding with this one. Requires ContactMonitor to be set to true and MaxContactsReported to be set high enough to detect all the collisions.

Note: The result of this test is not immediate after moving objects. For performance, list of collisions is updated once per frame and before the physics step. Consider using signals instead.

public Array<Node2D> GetCollidingBodies()

Returns

Array<Node2D>

GetContactCount()

Returns the number of contacts this body has with other bodies. By default, this returns 0 unless bodies are configured to monitor contacts (see ContactMonitor).

Note: To retrieve the colliding bodies, use GetCollidingBodies().

public int GetContactCount()

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

SetAxisVelocity(Vector2)

Sets the body's velocity on the given axis. The velocity in the given vector axis will be set as the given vector length. This is useful for jumping behavior.

public void SetAxisVelocity(Vector2 axisVelocity)

Parameters

axisVelocity Vector2

_IntegrateForces(PhysicsDirectBodyState2D)

Allows you to read and safely modify the simulation state for the object. Use this instead of _PhysicsProcess(double) if you need to directly change the body's position or other physics properties. By default, it works in addition to the usual physics behavior, but CustomIntegrator allows you to disable the default behavior and write custom force integration for a body.

public virtual void _IntegrateForces(PhysicsDirectBodyState2D state)

Parameters

state PhysicsDirectBodyState2D

Events

BodyEntered

Emitted when a collision with another PhysicsBody2D or TileMap occurs. Requires ContactMonitor to be set to true and MaxContactsReported 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.

public event RigidBody2D.BodyEnteredEventHandler BodyEntered

Event Type

RigidBody2D.BodyEnteredEventHandler

BodyExited

Emitted when the collision with another PhysicsBody2D or TileMap ends. Requires ContactMonitor to be set to true and MaxContactsReported 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.

public event RigidBody2D.BodyExitedEventHandler BodyExited

Event Type

RigidBody2D.BodyExitedEventHandler

BodyShapeEntered

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

bodyRid 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.

bodyShapeIndex 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)).

localShapeIndex 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)).

public event RigidBody2D.BodyShapeEnteredEventHandler BodyShapeEntered

Event Type

RigidBody2D.BodyShapeEnteredEventHandler

BodyShapeExited

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

bodyRid 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.

bodyShapeIndex 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)).

localShapeIndex 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)).

public event RigidBody2D.BodyShapeExitedEventHandler BodyShapeExited

Event Type

RigidBody2D.BodyShapeExitedEventHandler

SleepingStateChanged

Emitted when the physics engine changes the body's sleeping state.

Note: Changing the value Sleeping will not trigger this signal. It is only emitted if the sleeping state is changed by the physics engine or emit_signal("sleeping_state_changed") is used.

public event Action SleepingStateChanged

Event Type

Action