RigidBody2D¶
Inherits: PhysicsBody2D < CollisionObject2D < Node2D < CanvasItem < Node < Object
A body that is controlled by the 2D physics engine.
Description¶
This node implements simulated 2D physics. You do not control a RigidBody2D directly. Instead you apply forces to it (gravity, impulses, etc.) and the physics simulation calculates the resulting movement based on its mass, friction, and other physical properties.
A RigidBody2D has 4 behavior modes: Rigid, Static, Character, and Kinematic.
Note: You should not change a RigidBody2D’s position
or linear_velocity
every frame or even very often. If you need to directly affect the body’s state, use _integrate_forces, which allows you to directly access the physics state.
Please also keep in mind that physics bodies manage their own transform which overwrites the ones you set. So any direct or indirect transformation (including scaling of the node or its parent) will be visible in the editor only, and immediately reset at runtime.
If you need to override the default physics behavior or add a transformation at runtime, you can write a custom force integration. See custom_integrator.
Properties¶
float | angular_damp | -1.0 |
float | angular_velocity | 0.0 |
Vector2 | applied_force | Vector2( 0, 0 ) |
float | applied_torque | 0.0 |
float | bounce | |
bool | can_sleep | true |
bool | contact_monitor | false |
int | contacts_reported | 0 |
CCDMode | continuous_cd | 0 |
bool | custom_integrator | false |
float | friction | |
float | gravity_scale | 1.0 |
float | inertia | |
float | linear_damp | -1.0 |
Vector2 | linear_velocity | Vector2( 0, 0 ) |
float | mass | 1.0 |
Mode | mode | 0 |
PhysicsMaterial | physics_material_override | |
bool | sleeping | false |
float | weight | 9.8 |
Methods¶
void | _integrate_forces ( Physics2DDirectBodyState state ) virtual |
void | add_central_force ( Vector2 force ) |
void | add_force ( Vector2 offset, Vector2 force ) |
void | add_torque ( float torque ) |
void | apply_central_impulse ( Vector2 impulse ) |
void | apply_impulse ( Vector2 offset, Vector2 impulse ) |
void | apply_torque_impulse ( float torque ) |
Array | get_colliding_bodies ( ) const |
void | set_axis_velocity ( Vector2 axis_velocity ) |
bool | test_motion ( Vector2 motion, bool infinite_inertia=true, float margin=0.08, Physics2DTestMotionResult result=null ) |
Signals¶
- body_entered ( Node body )
Emitted when a body enters into contact with this one. contact_monitor must be true
and contacts_reported greater than 0
.
- body_exited ( Node body )
Emitted when a body exits contact with this one. contact_monitor must be true
and contacts_reported greater than 0
.
Emitted when a body enters into contact with this one. Reports colliding shape information. See CollisionObject2D for shape index information. contact_monitor must be true
and contacts_reported greater than 0
.
Emitted when a body shape exits contact with this one. Reports colliding shape information. See CollisionObject2D for shape index information. contact_monitor must be true
and contacts_reported greater than 0
.
Emitted when sleeping changes.
Enumerations¶
- MODE_RIGID = 0 — Rigid mode. The body behaves as a physical object. It collides with other bodies and responds to forces applied to it. This is the default mode.
- MODE_STATIC = 1 — Static mode. The body behaves like a StaticBody2D and does not move.
- MODE_CHARACTER = 2 — Character mode. Similar to MODE_RIGID, but the body can not rotate.
- MODE_KINEMATIC = 3 — Kinematic mode. The body behaves like a KinematicBody2D, and must be moved by code.
- CCD_MODE_DISABLED = 0 — Continuous collision detection disabled. This is the fastest way to detect body collisions, but can miss small, fast-moving objects.
- CCD_MODE_CAST_RAY = 1 — Continuous collision detection enabled using raycasting. This is faster than shapecasting but less precise.
- CCD_MODE_CAST_SHAPE = 2 — Continuous collision detection enabled using shapecasting. This is the slowest CCD method and the most precise.
Property Descriptions¶
- float angular_damp
Default | -1.0 |
Setter | set_angular_damp(value) |
Getter | get_angular_damp() |
Damps the body’s angular_velocity. If -1
, the body will use the Default Angular Damp defined in Project > Project Settings > Physics > 2d.
- float angular_velocity
Default | 0.0 |
Setter | set_angular_velocity(value) |
Getter | get_angular_velocity() |
The body’s rotational velocity.
- Vector2 applied_force
Default | Vector2( 0, 0 ) |
Setter | set_applied_force(value) |
Getter | get_applied_force() |
The body’s total applied force.
- float applied_torque
Default | 0.0 |
Setter | set_applied_torque(value) |
Getter | get_applied_torque() |
The body’s total applied torque.
- float bounce
Setter | set_bounce(value) |
Getter | get_bounce() |
The body’s bounciness. Values range from 0
(no bounce) to 1
(full bounciness).
Deprecated, use PhysicsMaterial.bounce instead via physics_material_override.
- bool can_sleep
Default | true |
Setter | set_can_sleep(value) |
Getter | is_able_to_sleep() |
If true
, the body will not calculate forces and will act as a static body if there is no movement. The body will wake up when other forces are applied via collisions or by using apply_impulse or add_force.
- bool contact_monitor
Default | false |
Setter | set_contact_monitor(value) |
Getter | is_contact_monitor_enabled() |
If true
, the body will emit signals when it collides with another RigidBody2D. See also contacts_reported.
- int contacts_reported
Default | 0 |
Setter | set_max_contacts_reported(value) |
Getter | get_max_contacts_reported() |
The maximum number of contacts to report.
- CCDMode continuous_cd
Default | 0 |
Setter | set_continuous_collision_detection_mode(value) |
Getter | get_continuous_collision_detection_mode() |
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 CCDMode for details.
- bool custom_integrator
Default | false |
Setter | set_use_custom_integrator(value) |
Getter | is_using_custom_integrator() |
If true
, internal force integration is disabled for this body. Aside from collision response, the body will only move as determined by the _integrate_forces function.
- float friction
Setter | set_friction(value) |
Getter | get_friction() |
The body’s friction. Values range from 0
(frictionless) to 1
(maximum friction).
Deprecated, use PhysicsMaterial.friction instead via physics_material_override.
- float gravity_scale
Default | 1.0 |
Setter | set_gravity_scale(value) |
Getter | get_gravity_scale() |
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.
- float inertia
Setter | set_inertia(value) |
Getter | get_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 function allows you to set a custom value. Set 0 inertia to return to automatically computing it.
- float linear_damp
Default | -1.0 |
Setter | set_linear_damp(value) |
Getter | get_linear_damp() |
Damps the body’s linear_velocity. If -1
, the body will use the Default Linear Damp in Project > Project Settings > Physics > 2d.
- Vector2 linear_velocity
Default | Vector2( 0, 0 ) |
Setter | set_linear_velocity(value) |
Getter | get_linear_velocity() |
The body’s linear velocity.
- float mass
Default | 1.0 |
Setter | set_mass(value) |
Getter | get_mass() |
The body’s mass.
- Mode mode
Default | 0 |
Setter | set_mode(value) |
Getter | get_mode() |
The body’s mode. See Mode for possible values.
- PhysicsMaterial physics_material_override
Setter | set_physics_material_override(value) |
Getter | get_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.
- bool sleeping
Default | false |
Setter | set_sleeping(value) |
Getter | is_sleeping() |
If true
, the body is sleeping and will not calculate forces until woken up by a collision or by using apply_impulse or add_force.
- float weight
Default | 9.8 |
Setter | set_weight(value) |
Getter | get_weight() |
The body’s weight based on its mass and the Default Gravity value in Project > Project Settings > Physics > 2d.
Method Descriptions¶
- void _integrate_forces ( Physics2DDirectBodyState state ) virtual
Allows you to read and safely modify the simulation state for the object. Use this instead of Node._physics_process 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 custom_integrator allows you to disable the default behavior and write custom force integration for a body.
- void add_central_force ( Vector2 force )
Adds a constant directional force without affecting rotation.
Adds a positioned force to the body. Both the force and the offset from the body origin are in global coordinates.
- void add_torque ( float torque )
Adds a constant rotational force.
- void apply_central_impulse ( Vector2 impulse )
Applies a directional impulse without affecting rotation.
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). The position uses the rotation of the global coordinate system, but is centered at the object’s origin.
- void apply_torque_impulse ( float torque )
Applies a rotational impulse to the body.
- Array get_colliding_bodies ( ) const
Returns a list of the bodies colliding with this one. Use contacts_reported to set the maximum number reported. You must also set contact_monitor to true
.
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.
- void set_axis_velocity ( Vector2 axis_velocity )
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.
- bool test_motion ( Vector2 motion, bool infinite_inertia=true, float margin=0.08, Physics2DTestMotionResult result=null )
Returns true
if a collision would result from moving in the given vector. margin
increases the size of the shapes involved in the collision detection, and result
is an object of type Physics2DTestMotionResult, which contains additional information about the collision (should there be one).