Class RigidBody3D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
RigidBody3D implements full 3D 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 3D transform or LinearVelocity of a RigidBody3D very often may lead to some unpredictable behaviors. If you need to directly affect the body, prefer _IntegrateForces(PhysicsDirectBodyState3D) as it allows you to directly access the physics state.
public class RigidBody3D : PhysicsBody3D, IDisposable
- Inheritance
-
RigidBody3D
- Implements
- Derived
- Inherited Members
Constructors
RigidBody3D()
public RigidBody3D()
Properties
AngularDamp
Damps the body's rotation. By default, the body will use the Default Angular Damp in Project > Project Settings > Physics > 3d or any value override set by an Area3D 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/3d/default_angular_damp
for more details about damping.
public float AngularDamp { get; set; }
Property Value
AngularDampMode
Defines how AngularDamp is applied. See RigidBody3D.DampMode for possible values.
public RigidBody3D.DampMode AngularDampMode { get; set; }
Property Value
AngularVelocity
The RigidBody3D's rotational velocity in radians per second.
public Vector3 AngularVelocity { get; set; }
Property Value
CanSleep
If true
, the body can enter sleep mode when there is no movement. See Sleeping.
public bool CanSleep { get; set; }
Property Value
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 Vector3 CenterOfMass { get; set; }
Property Value
CenterOfMassMode
Defines the way the body's center of mass is set. See RigidBody3D.CenterOfMassModeEnum for possible values.
public RigidBody3D.CenterOfMassModeEnum CenterOfMassMode { get; set; }
Property Value
ConstantForce
The body's total constant positional forces applied during each physics update.
See AddConstantForce(Vector3, Vector3?) and AddConstantCentralForce(Vector3).
public Vector3 ConstantForce { get; set; }
Property Value
ConstantTorque
The body's total constant rotational forces applied during each physics update.
public Vector3 ConstantTorque { get; set; }
Property Value
ContactMonitor
If true
, the RigidBody3D 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
ContinuousCd
If true
, continuous collision detection is used.
Continuous collision detection tries to predict where a moving body will collide, instead of moving it and correcting its movement if it collided. Continuous collision detection is more precise, and misses fewer impacts by small, fast-moving objects. Not using continuous collision detection is faster to compute, but can miss small, fast-moving objects.
public bool ContinuousCd { get; set; }
Property Value
CustomIntegrator
If true
, internal force integration will be disabled (like gravity or air friction) for this body. Other than collision response, the body will only move as determined by the _IntegrateForces(PhysicsDirectBodyState3D) function, if defined.
public bool CustomIntegrator { get; set; }
Property Value
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 StaticBody3D or AnimatableBody3D instead.
public bool Freeze { get; set; }
Property Value
FreezeMode
The body's freeze mode. Can be used to set the body's behavior when Freeze is enabled. See RigidBody3D.FreezeModeEnum for possible values.
For a body that is always frozen, use StaticBody3D or AnimatableBody3D instead.
public RigidBody3D.FreezeModeEnum FreezeMode { get; set; }
Property Value
GravityScale
This is multiplied by the global 3D gravity setting found in Project > Project Settings > Physics > 3d to produce RigidBody3D's gravity. For example, a value of 1 will be normal gravity, 2 will apply double gravity, and 0.5 will apply half gravity to this object.
public float GravityScale { get; set; }
Property Value
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 on each axis. 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 Vector3.ZERO
, inertia is automatically computed (default value).
Note: This value does not change when inertia is automatically computed. Use PhysicsServer3D to get the computed inertia.
private RigidBody3D _ball;
public override void _Ready()
{
_ball = GetNode<RigidBody3D>("Ball");
}
private Vector3 GetBallInertia()
{
return PhysicsServer3D.BodyGetDirectState(_ball.GetRid()).InverseInertia.Inverse();
}
public Vector3 Inertia { get; set; }
Property Value
LinearDamp
Damps the body's movement. By default, the body will use the Default Linear Damp in Project > Project Settings > Physics > 3d or any value override set by an Area3D 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/3d/default_linear_damp
for more details about damping.
public float LinearDamp { get; set; }
Property Value
LinearDampMode
Defines how LinearDamp is applied. See RigidBody3D.DampMode for possible values.
public RigidBody3D.DampMode LinearDampMode { get; set; }
Property Value
LinearVelocity
The body's linear velocity in units 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(PhysicsDirectBodyState3D) as your process loop for precise control of the body state.
public Vector3 LinearVelocity { get; set; }
Property Value
LockRotation
If true
, the body cannot rotate. Gravity and forces only apply linear movement.
public bool LockRotation { get; set; }
Property Value
Mass
The body's mass.
public float Mass { get; set; }
Property Value
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
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
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(Vector3, Vector3?) or ApplyForce(Vector3, Vector3?) methods.
public bool Sleeping { get; set; }
Property Value
Methods
AddConstantCentralForce(Vector3)
Adds a constant directional force without affecting rotation that keeps being applied over time until cleared with constant_force = Vector3(0, 0, 0)
.
This is equivalent to using AddConstantForce(Vector3, Vector3?) at the body's center of mass.
public void AddConstantCentralForce(Vector3 force)
Parameters
force
Vector3
AddConstantForce(Vector3, Vector3?)
Adds a constant positioned force to the body that keeps being applied over time until cleared with constant_force = Vector3(0, 0, 0)
.
position
is the offset from the body origin in global coordinates.
public void AddConstantForce(Vector3 force, Vector3? position = null)
Parameters
force
Vector3position
Vector3?If the parameter is null, then the default value is
new Vector3(0, 0, 0)
.
AddConstantTorque(Vector3)
Adds a constant rotational force without affecting position that keeps being applied over time until cleared with constant_torque = Vector3(0, 0, 0)
.
public void AddConstantTorque(Vector3 torque)
Parameters
torque
Vector3
ApplyCentralForce(Vector3)
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(Vector3, Vector3?) at the body's center of mass.
public void ApplyCentralForce(Vector3 force)
Parameters
force
Vector3
ApplyCentralImpulse(Vector3)
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(Vector3, Vector3?) at the body's center of mass.
public void ApplyCentralImpulse(Vector3 impulse)
Parameters
impulse
Vector3
ApplyForce(Vector3, Vector3?)
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(Vector3 force, Vector3? position = null)
Parameters
force
Vector3position
Vector3?If the parameter is null, then the default value is
new Vector3(0, 0, 0)
.
ApplyImpulse(Vector3, Vector3?)
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(Vector3 impulse, Vector3? position = null)
Parameters
impulse
Vector3position
Vector3?If the parameter is null, then the default value is
new Vector3(0, 0, 0)
.
ApplyTorque(Vector3)
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 CollisionShape3D must be a child of the node, or you can manually set Inertia.
public void ApplyTorque(Vector3 torque)
Parameters
torque
Vector3
ApplyTorqueImpulse(Vector3)
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 CollisionShape3D must be a child of the node, or you can manually set Inertia.
public void ApplyTorqueImpulse(Vector3 impulse)
Parameters
impulse
Vector3
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<Node3D> GetCollidingBodies()
Returns
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
GetInverseInertiaTensor()
Returns the inverse inertia tensor basis. This is used to calculate the angular acceleration resulting from a torque applied to the RigidBody3D.
public Basis GetInverseInertiaTensor()
Returns
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
SetAxisVelocity(Vector3)
Sets an axis velocity. The velocity in the given vector axis will be set as the given vector length. This is useful for jumping behavior.
public void SetAxisVelocity(Vector3 axisVelocity)
Parameters
axisVelocity
Vector3
_IntegrateForces(PhysicsDirectBodyState3D)
Called during physics processing, allowing you to read and safely modify the simulation state for the object. By default, it works in addition to the usual physics behavior, but the CustomIntegrator property allows you to disable the default behavior and do fully custom force integration for a body.
public virtual void _IntegrateForces(PhysicsDirectBodyState3D state)
Parameters
state
PhysicsDirectBodyState3D
Events
BodyEntered
Emitted when a collision with another PhysicsBody3D or GridMap occurs. Requires ContactMonitor to be set to true
and MaxContactsReported to be set high enough to detect all the collisions. GridMaps are detected if the MeshLibrary has Collision Shape3Ds.
body
the Node, if it exists in the tree, of the other PhysicsBody3D or GridMap.
public event RigidBody3D.BodyEnteredEventHandler BodyEntered
Event Type
BodyExited
Emitted when the collision with another PhysicsBody3D or GridMap ends. Requires ContactMonitor to be set to true
and MaxContactsReported to be set high enough to detect all the collisions. GridMaps are detected if the MeshLibrary has Collision Shape3Ds.
body
the Node, if it exists in the tree, of the other PhysicsBody3D or GridMap.
public event RigidBody3D.BodyExitedEventHandler BodyExited
Event Type
BodyShapeEntered
Emitted when one of this RigidBody3D's Shape3Ds collides with another PhysicsBody3D or GridMap's Shape3Ds. Requires ContactMonitor to be set to true
and MaxContactsReported to be set high enough to detect all the collisions. GridMaps are detected if the MeshLibrary has Collision Shape3Ds.
bodyRid
the Rid of the other PhysicsBody3D or MeshLibrary's CollisionObject3D used by the PhysicsServer3D.
body
the Node, if it exists in the tree, of the other PhysicsBody3D or GridMap.
bodyShapeIndex
the index of the Shape3D of the other PhysicsBody3D or GridMap used by the PhysicsServer3D. Get the CollisionShape3D node with body.shape_owner_get_owner(body.shape_find_owner(body_shape_index))
.
localShapeIndex
the index of the Shape3D of this RigidBody3D used by the PhysicsServer3D. Get the CollisionShape3D node with self.shape_owner_get_owner(self.shape_find_owner(local_shape_index))
.
public event RigidBody3D.BodyShapeEnteredEventHandler BodyShapeEntered
Event Type
BodyShapeExited
Emitted when the collision between one of this RigidBody3D's Shape3Ds and another PhysicsBody3D or GridMap's Shape3Ds ends. Requires ContactMonitor to be set to true
and MaxContactsReported to be set high enough to detect all the collisions. GridMaps are detected if the MeshLibrary has Collision Shape3Ds.
bodyRid
the Rid of the other PhysicsBody3D or MeshLibrary's CollisionObject3D used by the PhysicsServer3D. GridMaps are detected if the Meshes have Shape3Ds.
body
the Node, if it exists in the tree, of the other PhysicsBody3D or GridMap.
bodyShapeIndex
the index of the Shape3D of the other PhysicsBody3D or GridMap used by the PhysicsServer3D. Get the CollisionShape3D node with body.shape_owner_get_owner(body.shape_find_owner(body_shape_index))
.
localShapeIndex
the index of the Shape3D of this RigidBody3D used by the PhysicsServer3D. Get the CollisionShape3D node with self.shape_owner_get_owner(self.shape_find_owner(local_shape_index))
.
public event RigidBody3D.BodyShapeExitedEventHandler BodyShapeExited
Event Type
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