RigidBody2D
A 2D dynamic body driven by forces and contacts.
RigidBody2D receives motion from the physics integrator. Use forces for thrust and impulses for one-off pushes. Repeated Position/Rotation writes in _Process compete with simulation. Request a controlled reset through _IntegrateForces and modify PhysicsDirectBodyState.
BodyEntered requires ContactMonitor and a positive MaxContactsReported. Sleeping, Freeze and shape Disabled differ. CustomIntegrator disables ordinary force integration, not every contact. The example checks falling, impulse, reset and freeze, not every stack/speed/platform.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
RigidBody2DConstructor
public RigidBody2D();Properties23
AngularDampProperty
public float AngularDamp { get; set; }Damps the body's rotation. By default, the body will use the ProjectSettings.physics/2d/default_angular_damp setting or any value override set by an Area2D the body is in. Depending on RigidBody2D.AngularDampMode, you can set RigidBody2D.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.
AngularDampModeProperty
public RigidBody2D.DampMode AngularDampMode { get; set; }Defines how RigidBody2D.AngularDamp is applied.
AngularVelocityProperty
public float AngularVelocity { get; set; }The body's rotational velocity in radians per second.
CanSleepProperty
public bool CanSleep { get; set; }If true, the body can enter sleep mode when there is no movement. See RigidBody2D.Sleeping.
CenterOfMassProperty
public Vector2 CenterOfMass { get; set; }The body's custom center of mass, relative to the body's origin position, when RigidBody2D.CenterOfMassMode is set to RigidBody2D.CenterOfMassModeEnum.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 RigidBody2D.CenterOfMassMode is set to RigidBody2D.CenterOfMassModeEnum.Auto (default value), the center of mass is automatically determined, but this does not update the value of RigidBody2D.CenterOfMass.
CenterOfMassModeProperty
public RigidBody2D.CenterOfMassModeEnum CenterOfMassMode { get; set; }Defines the way the body's center of mass is set.
ConstantForceProperty
public Vector2 ConstantForce { get; set; }The body's total constant positional forces applied during each physics update.
See RigidBody2D.AddConstantForce and RigidBody2D.AddConstantCentralForce.
ConstantTorqueProperty
public float ConstantTorque { get; set; }The body's total constant rotational forces applied during each physics update.
See RigidBody2D.AddConstantTorque.
ContactMonitorProperty
public bool ContactMonitor { get; set; }Enables body contact monitoring. Signals also need MaxContactsReported > 0 and actual contact between suitable shapes, not arbitrary visual overlap.
ContinuousCdProperty
public RigidBody2D.CcdMode ContinuousCd { get; set; }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.
CustomIntegratorProperty
public bool CustomIntegrator { get; set; }If true, the standard force integration (like gravity or damping) will be disabled for this body. Other than collision response, the body will only move as determined by the RigidBody2D._IntegrateForces method, if that virtual method is overridden.
Setting this property will call the method PhysicsServer2D.BodySetOmitForceIntegration internally.
FreezeProperty
public bool Freeze { get; set; }Switches the body from ordinary dynamic simulation to the mode selected by FreezeMode. The executed frozen body did not move after an impulse. Does not promise to clear stored velocity or reset position.
FreezeModeProperty
public RigidBody2D.FreezeModeEnum FreezeMode { get; set; }The body's freeze mode. Determines the body's behavior when RigidBody2D.Freeze is true.
Note: For a body that is always frozen, use StaticBody2D or AnimatableBody2D instead.
GravityScaleProperty
public float GravityScale { get; set; }Multiplies the gravity applied to the body. The body's gravity is calculated from the ProjectSettings.physics/2d/default_gravity project setting and/or any additional gravity vector applied by Area2Ds.
InertiaProperty
public float Inertia { get; set; }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 = GetEntity<RigidBody2D>("Ball");
}
private float GetBallInertia()
{
return 1.0f / PhysicsServer2D.BodyGetDirectState(_ball.GetRid()).InverseInertia;
}LinearDampProperty
public float LinearDamp { get; set; }Damps the body's movement. By default, the body will use the ProjectSettings.physics/2d/default_linear_damp setting or any value override set by an Area2D the body is in. Depending on RigidBody2D.LinearDampMode, you can set RigidBody2D.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.
LinearDampModeProperty
public RigidBody2D.DampMode LinearDampMode { get; set; }Defines how RigidBody2D.LinearDamp is applied.
LinearVelocityProperty
public Vector2 LinearVelocity { get; set; }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 RigidBody2D._IntegrateForces as your process loop for precise control of the body state.
LockRotationProperty
public bool LockRotation { get; set; }If true, the body cannot rotate. Gravity and forces only apply linear movement.
MassProperty
public float Mass { get; set; }Positive body mass. The same impulse changes velocity less at greater mass. Values at or below zero are rejected; do not use mass 0 to create a stationary body.
MaxContactsReportedProperty
public int MaxContactsReported { get; set; }The maximum number of contacts that will be recorded. Requires a value greater than 0 and RigidBody2D.ContactMonitor to be set to true to start to register contacts. Use RigidBody2D.GetContactCount to retrieve the count or RigidBody2D.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).
PhysicsMaterialOverrideProperty
public PhysicsMaterial PhysicsMaterialOverride { get; set; }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.
SleepingProperty
public bool Sleeping { get; set; }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 RigidBody2D.ApplyImpulse or RigidBody2D.ApplyForce methods.
Methods61
AddConstantCentralForcevoid
public void AddConstantCentralForce(Vector2 force);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 RigidBody2D.AddConstantForce at the body's center of mass.
| Name | Type | Default |
|---|---|---|
force | Vector2 | — |
AddConstantForcevoid
public void AddConstantForce(Vector2 force, Vector2? position = default);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.
| Name | Type | Default |
|---|---|---|
force | Vector2 | — |
position | Vector2? | default |
positionIf the parameter is null, then the default value is new Vector2(0.0f, 0.0f).
AddConstantTorquevoid
public void AddConstantTorque(float torque);Adds a constant rotational force without affecting position that keeps being applied over time until cleared with constant_torque = 0.
| Name | Type | Default |
|---|---|---|
torque | float | — |
ApplyCentralForcevoid
public void ApplyCentralForce(Vector2 force);Adds central force for the current physics tick. For thrust, call on every required tick; the integrator accounts for time. A single call does not install a persistent force.
| Name | Type | Default |
|---|---|---|
force | Vector2 | — |
forceForce vector in global axes, not displacement or force premultiplied by delta.
ApplyCentralImpulsevoid
public void ApplyCentralImpulse(Vector2? impulse = default);Applies a one-off central impulse. Do not multiply by delta or repeat each frame for continuous thrust. In the check, impulse 20 on X at mass 2 produced velocity 10 without gravity or damping.
| Name | Type | Default |
|---|---|---|
impulse | Vector2? | default |
impulseImpulse vector in global axes. Omitting the optional value supplies a zero vector.
ApplyForcevoid
public void ApplyForce(Vector2 force, Vector2? position = default);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.
| Name | Type | Default |
|---|---|---|
force | Vector2 | — |
position | Vector2? | default |
positionIf the parameter is null, then the default value is new Vector2(0.0f, 0.0f).
ApplyImpulsevoid
public void ApplyImpulse(Vector2 impulse, Vector2? position = default);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.
| Name | Type | Default |
|---|---|---|
impulse | Vector2 | — |
position | Vector2? | default |
positionIf the parameter is null, then the default value is new Vector2(0.0f, 0.0f).
ApplyTorquevoid
public void ApplyTorque(float torque);Applies a rotational force without affecting position. A force is time dependent and meant to be applied every physics update.
Note: RigidBody2D.Inertia is required for this to work. To have RigidBody2D.Inertia, an active CollisionShape2D must be a child of the node, or you can manually set RigidBody2D.Inertia.
| Name | Type | Default |
|---|---|---|
torque | float | — |
ApplyTorqueImpulsevoid
public void ApplyTorqueImpulse(float torque);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: RigidBody2D.Inertia is required for this to work. To have RigidBody2D.Inertia, an active CollisionShape2D must be a child of the node, or you can manually set RigidBody2D.Inertia.
| Name | Type | Default |
|---|---|---|
torque | float | — |
GetAngularDampfloat
public float GetAngularDamp();floatGetAngularDampModeRigidBody2D.DampMode
public RigidBody2D.DampMode GetAngularDampMode();RigidBody2D.DampModeGetAngularVelocityfloat
public float GetAngularVelocity();floatGetCenterOfMassVector2
public Vector2 GetCenterOfMass();Vector2GetCenterOfMassModeRigidBody2D.CenterOfMassModeEnum
public RigidBody2D.CenterOfMassModeEnum GetCenterOfMassMode();RigidBody2D.CenterOfMassModeEnumGetCollidingBodiesReckfell.Collections.Array<Entity2D>
public Reckfell.Collections.Array<Entity2D> GetCollidingBodies();Returns a list of the bodies colliding with this one. Requires RigidBody2D.ContactMonitor to be set to true and RigidBody2D.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.
Reckfell.Collections.Array<Entity2D>GetConstantForceVector2
public Vector2 GetConstantForce();Vector2GetConstantTorquefloat
public float GetConstantTorque();floatGetContactCountint
public int 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 RigidBody2D.ContactMonitor).
Note: To retrieve the colliding bodies, use RigidBody2D.GetCollidingBodies.
intGetContinuousCollisionDetectionModeRigidBody2D.CcdMode
public RigidBody2D.CcdMode GetContinuousCollisionDetectionMode();RigidBody2D.CcdModeGetFreezeModeRigidBody2D.FreezeModeEnum
public RigidBody2D.FreezeModeEnum GetFreezeMode();RigidBody2D.FreezeModeEnumGetGravityScalefloat
public float GetGravityScale();floatGetInertiafloat
public float GetInertia();floatGetLinearDampfloat
public float GetLinearDamp();floatGetLinearDampModeRigidBody2D.DampMode
public RigidBody2D.DampMode GetLinearDampMode();RigidBody2D.DampModeGetLinearVelocityVector2
public Vector2 GetLinearVelocity();Vector2GetMassfloat
public float GetMass();floatGetMaxContactsReportedint
public int GetMaxContactsReported();intGetPhysicsMaterialOverridePhysicsMaterial
public PhysicsMaterial GetPhysicsMaterialOverride();PhysicsMaterialGetVelocityAtLocalPositionVector2
public Vector2 GetVelocityAtLocalPosition(Vector2 localPosition);Returns the body's velocity at the given relative position.
local_position is the offset from the body origin in global coordinates.
See also RigidBody2D.GetVelocityAtPosition.
| Name | Type | Default |
|---|---|---|
localPosition | Vector2 | — |
Vector2GetVelocityAtPositionVector2
public Vector2 GetVelocityAtPosition(Vector2 globalPoint);Returns the body's velocity at the given global position.
This is equivalent to get_velocity_at_local_position(global_point - global_position).
See also RigidBody2D.GetVelocityAtLocalPosition.
| Name | Type | Default |
|---|---|---|
globalPoint | Vector2 | — |
Vector2IsAbleToSleepbool
public bool IsAbleToSleep();boolIsContactMonitorEnabledbool
public bool IsContactMonitorEnabled();boolIsFreezeEnabledbool
public bool IsFreezeEnabled();boolIsLockRotationEnabledbool
public bool IsLockRotationEnabled();boolIsSleepingbool
public bool IsSleeping();boolIsUsingCustomIntegratorbool
public bool IsUsingCustomIntegrator();boolSetAngularDampvoid
public void SetAngularDamp(float angularDamp);| Name | Type | Default |
|---|---|---|
angularDamp | float | — |
SetAngularDampModevoid
public void SetAngularDampMode(RigidBody2D.DampMode angularDampMode);| Name | Type | Default |
|---|---|---|
angularDampMode | RigidBody2D.DampMode | — |
SetAngularVelocityvoid
public void SetAngularVelocity(float angularVelocity);| Name | Type | Default |
|---|---|---|
angularVelocity | float | — |
SetAxisVelocityvoid
public void SetAxisVelocity(Vector2 axisVelocity);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.
| Name | Type | Default |
|---|---|---|
axisVelocity | Vector2 | — |
SetCanSleepvoid
public void SetCanSleep(bool ableToSleep);| Name | Type | Default |
|---|---|---|
ableToSleep | bool | — |
SetCenterOfMassvoid
public void SetCenterOfMass(Vector2 centerOfMass);| Name | Type | Default |
|---|---|---|
centerOfMass | Vector2 | — |
SetCenterOfMassModevoid
public void SetCenterOfMassMode(RigidBody2D.CenterOfMassModeEnum mode);| Name | Type | Default |
|---|---|---|
mode | RigidBody2D.CenterOfMassModeEnum | — |
SetConstantForcevoid
public void SetConstantForce(Vector2 force);| Name | Type | Default |
|---|---|---|
force | Vector2 | — |
SetConstantTorquevoid
public void SetConstantTorque(float torque);| Name | Type | Default |
|---|---|---|
torque | float | — |
SetContactMonitorvoid
public void SetContactMonitor(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetContinuousCollisionDetectionModevoid
public void SetContinuousCollisionDetectionMode(RigidBody2D.CcdMode mode);| Name | Type | Default |
|---|---|---|
mode | RigidBody2D.CcdMode | — |
SetFreezeEnabledvoid
public void SetFreezeEnabled(bool freezeMode);| Name | Type | Default |
|---|---|---|
freezeMode | bool | — |
SetFreezeModevoid
public void SetFreezeMode(RigidBody2D.FreezeModeEnum freezeMode);| Name | Type | Default |
|---|---|---|
freezeMode | RigidBody2D.FreezeModeEnum | — |
SetGravityScalevoid
public void SetGravityScale(float gravityScale);| Name | Type | Default |
|---|---|---|
gravityScale | float | — |
SetInertiavoid
public void SetInertia(float inertia);| Name | Type | Default |
|---|---|---|
inertia | float | — |
SetLinearDampvoid
public void SetLinearDamp(float linearDamp);| Name | Type | Default |
|---|---|---|
linearDamp | float | — |
SetLinearDampModevoid
public void SetLinearDampMode(RigidBody2D.DampMode linearDampMode);| Name | Type | Default |
|---|---|---|
linearDampMode | RigidBody2D.DampMode | — |
SetLinearVelocityvoid
public void SetLinearVelocity(Vector2 linearVelocity);| Name | Type | Default |
|---|---|---|
linearVelocity | Vector2 | — |
SetLockRotationEnabledvoid
public void SetLockRotationEnabled(bool lockRotation);| Name | Type | Default |
|---|---|---|
lockRotation | bool | — |
SetMassvoid
public void SetMass(float mass);| Name | Type | Default |
|---|---|---|
mass | float | — |
SetMaxContactsReportedvoid
public void SetMaxContactsReported(int amount);| Name | Type | Default |
|---|---|---|
amount | int | — |
SetPhysicsMaterialOverridevoid
public void SetPhysicsMaterialOverride(PhysicsMaterial physicsMaterialOverride);| Name | Type | Default |
|---|---|---|
physicsMaterialOverride | PhysicsMaterial | — |
SetSleepingvoid
public void SetSleeping(bool sleeping);| Name | Type | Default |
|---|---|---|
sleeping | bool | — |
SetUseCustomIntegratorvoid
public void SetUseCustomIntegrator(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
_IntegrateForcesvoid
public virtual void _IntegrateForces(PhysicsDirectBodyState2D state);A physics callback for controlled state modification. For teleporting, set state.Transform and explicitly clear required velocities. Do not substitute repeated Position writes in _Process for the integrator.
| Name | Type | Default |
|---|---|---|
state | PhysicsDirectBodyState2D | — |
Signals and events5
BodyEnteredEvent
public event RigidBody2D.BodyEnteredEventHandler BodyEntered;Emitted when a collision with another PhysicsBody2D or TileMap occurs. Requires RigidBody2D.ContactMonitor to be set to true and RigidBody2D.MaxContactsReported to be set high enough to detect all the collisions. TileMaps are detected if the TileSet has Collision Shape2Ds.
body the Entity, if it exists in the tree, of the other PhysicsBody2D or TileMap.
| Name | Type | Default |
|---|---|---|
body | Entity | — |
BodyExitedEvent
public event RigidBody2D.BodyExitedEventHandler BodyExited;Emitted when the collision with another PhysicsBody2D or TileMap ends. Requires RigidBody2D.ContactMonitor to be set to true and RigidBody2D.MaxContactsReported to be set high enough to detect all the collisions. TileMaps are detected if the TileSet has Collision Shape2Ds.
body the Entity, if it exists in the tree, of the other PhysicsBody2D or TileMap.
| Name | Type | Default |
|---|---|---|
body | Entity | — |
BodyShapeEnteredEvent
public event RigidBody2D.BodyShapeEnteredEventHandler BodyShapeEntered;Emitted when one of this RigidBody2D's Shape2Ds collides with another PhysicsBody2D or TileMap's Shape2Ds. Requires RigidBody2D.ContactMonitor to be set to true and RigidBody2D.MaxContactsReported 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 Entity, 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)).
| Name | Type | Default |
|---|---|---|
bodyRid | Rid | — |
body | Entity | — |
bodyShapeIndex | long | — |
localShapeIndex | long | — |
BodyShapeExitedEvent
public event RigidBody2D.BodyShapeExitedEventHandler BodyShapeExited;Emitted when the collision between one of this RigidBody2D's Shape2Ds and another PhysicsBody2D or TileMap's Shape2Ds ends. Requires RigidBody2D.ContactMonitor to be set to true and RigidBody2D.MaxContactsReported 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 Entity, 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)).
| Name | Type | Default |
|---|---|---|
bodyRid | Rid | — |
body | Entity | — |
bodyShapeIndex | long | — |
localShapeIndex | long | — |
SleepingStateChangedEvent
public event Action SleepingStateChanged;Emitted when the physics engine changes the body's sleeping state.
Note: Changing the value RigidBody2D.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.
RigidBody2D.CcdMode
| Value | Description |
|---|---|
CastRay = 1 | Continuous collision detection enabled using raycasting. This is faster than shapecasting but less precise. |
CastShape = 2 | Continuous collision detection enabled using shapecasting. This is the slowest CCD method and the most precise. |
Disabled = 0 | Continuous collision detection disabled. This is the fastest way to detect body collisions, but can miss small, fast-moving objects. |
RigidBody2D.CenterOfMassModeEnum
| Value | Description |
|---|---|
Auto = 0 | In this mode, the body's center of mass is calculated automatically based on its shapes. This assumes that the shapes' origins are also their center of mass. |
Custom = 1 | In this mode, the body's center of mass is set through |
RigidBody2D.DampMode
| Value | Description |
|---|---|
Combine = 0 | In this mode, the body's damping value is added to any value set in areas or the default value. |
Replace = 1 | In this mode, the body's damping value replaces any value set in areas or the default value. |
RigidBody2D.FreezeModeEnum
| Value | Description |
|---|---|
Kinematic = 1 | Kinematic body freeze mode. Similar to |
Static = 0 | Static body freeze mode (default). The body is not affected by gravity and forces. It can be only moved by user code and doesn't collide with other bodies along its path. |