RigidBody3D
Inherits:PhysicsBody3D<CollisionObject3D<Node3D<Node<Object
Inherited By:VehicleBody3D
A 3D physics body that is moved by a physics simulation.
Description
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 vialock_rotation,freeze, andfreeze_mode. By changing various properties of the object, such asmass, 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. Seecustom_integrator.
Note: Changing the 3D transform orlinear_velocity of aRigidBody3D 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
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
|
Methods
add_constant_force(force:Vector3, position:Vector3 = Vector3(0, 0, 0)) | |
add_constant_torque(torque:Vector3) | |
apply_central_force(force:Vector3) | |
apply_central_impulse(impulse:Vector3) | |
apply_force(force:Vector3, position:Vector3 = Vector3(0, 0, 0)) | |
apply_impulse(impulse:Vector3, position:Vector3 = Vector3(0, 0, 0)) | |
apply_torque(torque:Vector3) | |
apply_torque_impulse(impulse:Vector3) | |
set_axis_velocity(axis_velocity:Vector3) |
Signals
Emitted when a collision with anotherPhysicsBody3D orGridMap occurs. Requirescontact_monitor to be set totrue
andmax_contacts_reported to be set high enough to detect all the collisions.GridMaps are detected if theMeshLibrary has CollisionShape3Ds.
body
theNode, if it exists in the tree, of the otherPhysicsBody3D orGridMap.
Emitted when the collision with anotherPhysicsBody3D orGridMap ends. Requirescontact_monitor to be set totrue
andmax_contacts_reported to be set high enough to detect all the collisions.GridMaps are detected if theMeshLibrary has CollisionShape3Ds.
body
theNode, if it exists in the tree, of the otherPhysicsBody3D orGridMap.
body_shape_entered(body_rid:RID, body:Node, body_shape_index:int, local_shape_index:int)🔗
Emitted when one of this RigidBody3D'sShape3Ds collides with anotherPhysicsBody3D orGridMap'sShape3Ds. Requirescontact_monitor to be set totrue
andmax_contacts_reported to be set high enough to detect all the collisions.GridMaps are detected if theMeshLibrary has CollisionShape3Ds.
body_rid
theRID of the otherPhysicsBody3D orMeshLibrary'sCollisionObject3D used by thePhysicsServer3D.
body
theNode, if it exists in the tree, of the otherPhysicsBody3D orGridMap.
body_shape_index
the index of theShape3D of the otherPhysicsBody3D orGridMap used by thePhysicsServer3D. Get theCollisionShape3D node withbody.shape_owner_get_owner(body.shape_find_owner(body_shape_index))
.
local_shape_index
the index of theShape3D of this RigidBody3D used by thePhysicsServer3D. Get theCollisionShape3D node withself.shape_owner_get_owner(self.shape_find_owner(local_shape_index))
.
body_shape_exited(body_rid:RID, body:Node, body_shape_index:int, local_shape_index:int)🔗
Emitted when the collision between one of this RigidBody3D'sShape3Ds and anotherPhysicsBody3D orGridMap'sShape3Ds ends. Requirescontact_monitor to be set totrue
andmax_contacts_reported to be set high enough to detect all the collisions.GridMaps are detected if theMeshLibrary has CollisionShape3Ds.
body_rid
theRID of the otherPhysicsBody3D orMeshLibrary'sCollisionObject3D used by thePhysicsServer3D.GridMaps are detected if the Meshes haveShape3Ds.
body
theNode, if it exists in the tree, of the otherPhysicsBody3D orGridMap.
body_shape_index
the index of theShape3D of the otherPhysicsBody3D orGridMap used by thePhysicsServer3D. Get theCollisionShape3D node withbody.shape_owner_get_owner(body.shape_find_owner(body_shape_index))
.
local_shape_index
the index of theShape3D of this RigidBody3D used by thePhysicsServer3D. Get theCollisionShape3D node withself.shape_owner_get_owner(self.shape_find_owner(local_shape_index))
.
sleeping_state_changed()🔗
Emitted when the physics engine changes the body's sleeping state.
Note: Changing the valuesleeping will not trigger this signal. It is only emitted if the sleeping state is changed by the physics engine oremit_signal("sleeping_state_changed")
is used.
Enumerations
enumFreezeMode:🔗
FreezeModeFREEZE_MODE_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.
FreezeModeFREEZE_MODE_KINEMATIC =1
Kinematic body freeze mode. Similar toFREEZE_MODE_STATIC, but collides with other bodies along its path when moved. Useful for a frozen body that needs to be animated.
enumCenterOfMassMode:🔗
CenterOfMassModeCENTER_OF_MASS_MODE_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.
CenterOfMassModeCENTER_OF_MASS_MODE_CUSTOM =1
In this mode, the body's center of mass is set throughcenter_of_mass. Defaults to the body's origin position.
enumDampMode:🔗
DampModeDAMP_MODE_COMBINE =0
In this mode, the body's damping value is added to any value set in areas or the default value.
DampModeDAMP_MODE_REPLACE =1
In this mode, the body's damping value replaces any value set in areas or the default value.
Property Descriptions
floatget_angular_damp()
Damps the body's rotation. By default, the body will use theProjectSettings.physics/3d/default_angular_damp project setting or any value override set by anArea3D the body is in. Depending onangular_damp_mode, you can setangular_damp to be added to or to replace the body's damping value.
SeeProjectSettings.physics/3d/default_angular_damp for more details about damping.
DampModeget_angular_damp_mode()
Defines howangular_damp is applied. SeeDampMode for possible values.
Vector3angular_velocity =Vector3(0,0,0)
🔗
Vector3get_angular_velocity()
The RigidBody3D's rotational velocity inradians per second.
boolis_able_to_sleep()
Iftrue
, the body can enter sleep mode when there is no movement. Seesleeping.
Vector3center_of_mass =Vector3(0,0,0)
🔗
Vector3get_center_of_mass()
The body's custom center of mass, relative to the body's origin position, whencenter_of_mass_mode is set toCENTER_OF_MASS_MODE_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.
Whencenter_of_mass_mode is set toCENTER_OF_MASS_MODE_AUTO (default value), the center of mass is automatically computed.
CenterOfMassModecenter_of_mass_mode =0
🔗
CenterOfMassModeget_center_of_mass_mode()
Defines the way the body's center of mass is set. SeeCenterOfMassMode for possible values.
Vector3constant_force =Vector3(0,0,0)
🔗
Vector3get_constant_force()
The body's total constant positional forces applied during each physics update.
Seeadd_constant_force() andadd_constant_central_force().
Vector3constant_torque =Vector3(0,0,0)
🔗
Vector3get_constant_torque()
The body's total constant rotational forces applied during each physics update.
boolis_contact_monitor_enabled()
Iftrue
, 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, seemax_contacts_reported.
boolis_using_continuous_collision_detection()
Iftrue
, 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.
boolis_using_custom_integrator()
Iftrue
, 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_integrate_forces() method, if that virtual method is overridden.
Setting this property will call the methodPhysicsServer3D.body_set_omit_force_integration() internally.
boolis_freeze_enabled()
Iftrue
, the body is frozen. Gravity and forces are not applied anymore.
Seefreeze_mode to set the body's behavior when frozen.
For a body that is always frozen, useStaticBody3D orAnimatableBody3D instead.
FreezeModefreeze_mode =0
🔗
FreezeModeget_freeze_mode()
The body's freeze mode. Can be used to set the body's behavior whenfreeze is enabled. SeeFreezeMode for possible values.
For a body that is always frozen, useStaticBody3D orAnimatableBody3D instead.
floatget_gravity_scale()
This is multiplied byProjectSettings.physics/3d/default_gravity to produce this body's gravity. For example, a value of1.0
will apply normal gravity,2.0
will apply double the gravity, and0.5
will apply half the gravity to this body.
Vector3inertia =Vector3(0,0,0)
🔗
Vector3get_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 toVector3.ZERO, inertia is automatically computed (default value).
Note: This value does not change when inertia is automatically computed. UsePhysicsServer3D to get the computed inertia.
@onreadyvarball=$Ballfuncget_ball_inertia():returnPhysicsServer3D.body_get_direct_state(ball.get_rid()).inverse_inertia.inverse()
privateRigidBody3D_ball;publicoverridevoid_Ready(){_ball=GetNode<RigidBody3D>("Ball");}privateVector3GetBallInertia(){returnPhysicsServer3D.BodyGetDirectState(_ball.GetRid()).InverseInertia.Inverse();}
floatget_linear_damp()
Damps the body's movement. By default, the body will use theProjectSettings.physics/3d/default_linear_damp project setting or any value override set by anArea3D the body is in. Depending onlinear_damp_mode, you can setlinear_damp to be added to or to replace the body's damping value.
SeeProjectSettings.physics/3d/default_linear_damp for more details about damping.
DampModeget_linear_damp_mode()
Defines howlinear_damp is applied. SeeDampMode for possible values.
Vector3linear_velocity =Vector3(0,0,0)
🔗
Vector3get_linear_velocity()
The body's linear velocity in units per second. Can be used sporadically, butdon't set this every frame, because physics may run in another thread and runs at a different granularity. Use_integrate_forces() as your process loop for precise control of the body state.
boolis_lock_rotation_enabled()
Iftrue
, the body cannot rotate. Gravity and forces only apply linear movement.
floatget_mass()
The body's mass.
intget_max_contacts_reported()
The maximum number of contacts that will be recorded. Requires a value greater than 0 andcontact_monitor to be set totrue
to start to register contacts. Useget_contact_count() to retrieve the count orget_colliding_bodies() 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).
PhysicsMaterialphysics_material_override🔗
PhysicsMaterialget_physics_material_override()
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.
boolis_sleeping()
Iftrue
, the body will not move and will not calculate forces until woken up by another body through, for example, a collision, or by using theapply_impulse() orapply_force() methods.
Method Descriptions
void_integrate_forces(state:PhysicsDirectBodyState3D)virtual🔗
Called during physics processing, allowing you to read and safely modify the simulation state for the object. By default, it is called before the standard force integration, but thecustom_integrator property allows you to disable the standard force integration and do fully custom force integration for a body.
voidadd_constant_central_force(force:Vector3)🔗
Adds a constant directional force without affecting rotation that keeps being applied over time until cleared withconstant_force=Vector3(0,0,0)
.
This is equivalent to usingadd_constant_force() at the body's center of mass.
voidadd_constant_force(force:Vector3, position:Vector3 = Vector3(0, 0, 0))🔗
Adds a constant positioned force to the body that keeps being applied over time until cleared withconstant_force=Vector3(0,0,0)
.
position
is the offset from the body origin in global coordinates.
voidadd_constant_torque(torque:Vector3)🔗
Adds a constant rotational force without affecting position that keeps being applied over time until cleared withconstant_torque=Vector3(0,0,0)
.
voidapply_central_force(force: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 usingapply_force() at the body's center of mass.
voidapply_central_impulse(impulse: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 usingapply_impulse() at the body's center of mass.
voidapply_force(force:Vector3, position:Vector3 = Vector3(0, 0, 0))🔗
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.
voidapply_impulse(impulse:Vector3, position:Vector3 = Vector3(0, 0, 0))🔗
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.
voidapply_torque(torque: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 haveinertia, an activeCollisionShape3D must be a child of the node, or you can manually setinertia.
voidapply_torque_impulse(impulse: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 haveinertia, an activeCollisionShape3D must be a child of the node, or you can manually setinertia.
Array[Node3D]get_colliding_bodies()const🔗
Returns a list of the bodies colliding with this one. Requirescontact_monitor to be set totrue
andmax_contacts_reported 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.
Returns the number of contacts this body has with other bodies. By default, this returns 0 unless bodies are configured to monitor contacts (seecontact_monitor).
Note: To retrieve the colliding bodies, useget_colliding_bodies().
Basisget_inverse_inertia_tensor()const🔗
Returns the inverse inertia tensor basis. This is used to calculate the angular acceleration resulting from a torque applied to theRigidBody3D.
voidset_axis_velocity(axis_velocity: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.