RigidBody2D

Inherits: PhysicsBody2D < CollisionObject2D < Node2D < CanvasItem < Node < Object

Category: Core

Brief Description

Rigid body 2D node.

Member Functions

void _integrate_forces ( Physics2DDirectBodyState state ) virtual
void add_force ( Vector2 offset, Vector2 force )
void apply_impulse ( Vector2 offset, Vector2 impulse )
float get_angular_damp ( ) const
float get_angular_velocity ( ) const
Vector2 get_applied_force ( ) const
float get_applied_torque ( ) const
float get_bounce ( ) const
Array get_colliding_bodies ( ) const
int get_continuous_collision_detection_mode ( ) const
float get_friction ( ) const
float get_gravity_scale ( ) const
float get_inertia ( ) const
float get_linear_damp ( ) const
Vector2 get_linear_velocity ( ) const
float get_mass ( ) const
int get_max_contacts_reported ( ) const
int get_mode ( ) const
float get_weight ( ) const
bool is_able_to_sleep ( ) const
bool is_contact_monitor_enabled ( ) const
bool is_sleeping ( ) const
bool is_using_custom_integrator ( )
void set_angular_damp ( float angular_damp )
void set_angular_velocity ( float angular_velocity )
void set_applied_force ( Vector2 force )
void set_applied_torque ( float torque )
void set_axis_velocity ( Vector2 axis_velocity )
void set_bounce ( float bounce )
void set_can_sleep ( bool able_to_sleep )
void set_contact_monitor ( bool enabled )
void set_continuous_collision_detection_mode ( int mode )
void set_friction ( float friction )
void set_gravity_scale ( float gravity_scale )
void set_inertia ( float inertia )
void set_linear_damp ( float linear_damp )
void set_linear_velocity ( Vector2 linear_velocity )
void set_mass ( float mass )
void set_max_contacts_reported ( int amount )
void set_mode ( int mode )
void set_sleeping ( bool sleeping )
void set_use_custom_integrator ( bool enable )
void set_weight ( float weight )
bool test_motion ( Vector2 motion, float margin=0.08, Physics2DTestMotionResult result=NULL )

Signals

  • body_enter ( Object body )
  • body_enter_shape ( int body_id, Object body, int body_shape, int local_shape )
  • body_exit ( Object body )
  • body_exit_shape ( int body_id, Object body, int body_shape, int local_shape )
  • sleeping_state_changed ( )

Numeric Constants

  • MODE_STATIC = 1 — Static mode. The body behaves like a StaticBody2D, and can only move by user code.
  • MODE_KINEMATIC = 3 — Kinematic body. The body behaves like a KinematicBody2D, and can only move by user code.
  • MODE_RIGID = 0 — Rigid body. This is the “natural” state of a rigid body. It is affected by forces, and can move, rotate, and be affected by user code.
  • MODE_CHARACTER = 2 — Character body. This behaves like a rigid body, but can not rotate.
  • CCD_MODE_DISABLED = 0 — Disables continuous collision detection. This is the fastest way to detect body collisions, but can miss small, fast-moving objects.
  • CCD_MODE_CAST_RAY = 1 — Enables continuous collision detection by raycasting. It is faster than shapecasting, but less precise.
  • CCD_MODE_CAST_SHAPE = 2 — Enables continuous collision detection by shapecasting. It is the slowest CCD method, and the most precise.

Description

Rigid body 2D node. This node is used for placing rigid bodies in the scene. It can contain a number of shapes, and also shift state between regular Rigid body, Kinematic, Character or Static.

Character mode forbids the node from being rotated. This node can have a custom force integrator function, for writing complex physics motion behavior per node.

As a warning, don’t change this node position every frame or very often. Sporadic changes work fine, but physics runs at a different granularity (fixed hz) than usual rendering (process callback) and maybe even in a separate thread, so changing this from a process loop will yield strange behavior.

Member Function Description

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 set_use_custom_integrator allows you to disable the default behavior and do fully custom force integration for a body.

Add a positioned force to the applied force and torque. As with apply_impulse, both the force and the offset from the body origin are in global coordinates.

Apply a positioned impulse (which will be affected by the body mass and shape). This is the equivalent of hitting a billiard ball with a cue: a force that is applied once, and only once. Both the impulse and the offset from the body origin are in global coordinates.

  • float get_angular_damp ( ) const

Return the angular damp for this body.

  • float get_angular_velocity ( ) const

Return the body angular velocity. This changes by physics granularity. See set_angular_velocity.

  • Vector2 get_applied_force ( ) const

Return the applied force vector.

  • float get_applied_torque ( ) const

Return the torque which is being applied to this body.

  • float get_bounce ( ) const

Return the body bounciness.

  • Array get_colliding_bodies ( ) const

Return a list of the bodies colliding with this one. By default, number of max contacts reported is at 0 , see set_max_contacts_reported to increase it. You must also enable contact monitor, see set_contact_monitor

  • int get_continuous_collision_detection_mode ( ) const

Return whether this body is using continuous collision detection.

  • float get_friction ( ) const

Return the body friction.

  • float get_gravity_scale ( ) const

Return the gravity factor.

  • float get_inertia ( ) const

Return the body’s moment of inertia. This is usually automatically computed from the mass and the shapes. Note that this doesn’t seem to work in a _ready function: it apparently has not been auto-computed yet.

  • float get_linear_damp ( ) const

Return the linear damp for this body.

  • Vector2 get_linear_velocity ( ) const

Return the body linear velocity. This changes by physics granularity. See set_linear_velocity.

  • float get_mass ( ) const

Return the body mass.

  • int get_max_contacts_reported ( ) const

Return the maximum contacts that can be reported. See set_max_contacts_reported.

  • int get_mode ( ) const

Return the current body mode, see set_mode.

  • float get_weight ( ) const

Return the body weight given standard earth-weight (gravity 9.8).

  • bool is_able_to_sleep ( ) const

Return true if the body has the ability to fall asleep when not moving. See set_can_sleep.

  • bool is_contact_monitor_enabled ( ) const

Return whether contact monitoring is enabled.

  • bool is_sleeping ( ) const

Return whether the body is sleeping.

  • bool is_using_custom_integrator ( )

Return true if the body is not doing any built-in force integration.

  • void set_angular_damp ( float angular_damp )

Set the angular damp for this body. If this value is different from -1, any angular damp derived from the world or areas will be overridden.

  • void set_angular_velocity ( float angular_velocity )

Set the body angular velocity. Can be used sporadically, but DON’T SET THIS IN EVERY FRAME, because physics may be running in another thread and definitely runs at a different granularity. Use _integrate_forces as your process loop if you want to have precise control of the body state.

  • void set_applied_force ( Vector2 force )

Set the applied force vector. This is the equivalent of pushing a box over the ground: the force applied is applied constantly.

  • void set_applied_torque ( float torque )

Set a constant torque which will be applied to this body.

  • void set_axis_velocity ( Vector2 axis_velocity )

Set an axis velocity. The velocity in the given vector axis will be set as the given vector length. This is useful for jumping behavior.

  • void set_bounce ( float bounce )

Set the body bounciness, from 0 (no bounce) to 1 (full bounce).

  • void set_can_sleep ( bool able_to_sleep )

Set the body ability to fall asleep when not moving. This saves an enormous amount of processor time when there are plenty of rigid bodies (non static) in a scene.

Sleeping bodies are not affected by forces until a collision or an apply_impulse / set_applied_force wakes them up. Until then, they behave like a static body.

  • void set_contact_monitor ( bool enabled )

Enable contact monitoring. This allows the body to emit signals when it collides with another.

  • void set_continuous_collision_detection_mode ( int mode )

Set the continuous collision detection mode from the enum CCD_MODE_*.

Continuous collision detection tries to predict where a moving body will collide, instead of moving it and correcting its movement if it collided. The first is more precise, and misses less impacts by small, fast-moving objects. The second is faster to compute, but can miss small, fast-moving objects.

  • void set_friction ( float friction )

Set the body friction, from 0 (frictionless) to 1 (full friction).

  • void set_gravity_scale ( float gravity_scale )

Set the gravity factor. This factor multiplies gravity intensity just for this body.

  • void set_inertia ( float inertia )

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 function allows you to set a custom value. Set 0 (or negative) inertia to return to automatically computing it.

  • void set_linear_damp ( float linear_damp )

Set the linear damp for this body. If this value is different from -1, any linear damp derived from the world or areas will be overridden.

  • void set_linear_velocity ( Vector2 linear_velocity )

Set the body linear velocity. Can be used sporadically, but DON’T SET THIS IN EVERY FRAME, because physics may be running in another thread and definitely runs at a different granularity. Use _integrate_forces as your process loop if you want to have precise control of the body state.

  • void set_mass ( float mass )

Set the body mass.

  • void set_max_contacts_reported ( int amount )

Set the maximum contacts to report. Bodies can keep a log of the contacts with other bodies, this is enabled by setting the maximum amount of contacts reported to a number greater than 0.

  • void set_mode ( int mode )

Set the body mode, from the MODE_* enum. This allows to change to a static body or a character body.

  • void set_sleeping ( bool sleeping )

Set whether a body is sleeping or not. Sleeping bodies are not affected by forces until a collision or an apply_impulse / set_applied_force wakes them up. Until then, they behave like a static body.

  • void set_use_custom_integrator ( bool enable )

Pass true to disable the internal force integration (like gravity or air friction) for this body. Other than collision response, the body will only move as determined by the _integrate_forces function, if defined.

  • void set_weight ( float weight )

Set the body weight given standard earth-weight (gravity 9.8). Not really useful for 2D since most measures for this node are in pixels.

Return whether the body would collide, if it tried to move in the given vector. This method allows two extra parameters: A margin, which increases slightly the size of the shapes involved in the collision detection, and an object of type Physics2DTestMotionResult, which will store additional information about the collision (should there be one).