Class reference

PhysicsDirectBodyState2D

Inherits Object

Provides direct access to a physics body in the PhysicsServer2D.

Description

Provides direct access to a physics body in the PhysicsServer2D, allowing safe changes to physics properties. This object is passed via the direct state callback of RigidBody2D, and is intended for changing the direct state of that body. See RigidBody2D._integrate_forces().

Properties

float angular_velocity

The body's rotational velocity in radians per second.

Vector2 center_of_mass

The body's center of mass position relative to the body's center in the global coordinate system.

Vector2 center_of_mass_local

The body's center of mass position in the body's local coordinate system.

int collision_layer

The body's collision layer.

int collision_mask

The body's collision mask.

float inverse_inertia

The inverse of the inertia of the body.

float inverse_mass

The inverse of the mass of the body.

Vector2 linear_velocity

The body's linear velocity in pixels per second.

bool sleeping

If true, this body is currently sleeping (not active).

float step

The timestep (delta) used for the simulation.

float total_angular_damp

The rate at which the body stops rotating, if there are not any other forces moving it.

Vector2 total_gravity

The total gravity vector being currently applied to this body.

float total_linear_damp

The rate at which the body stops moving, if there are not any other forces moving it.

Methods

void add_constant_central_force(Vector2 force = Vector2(0, 0))

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 add_constant_force() at the body's center of mass.

void add_constant_force(Vector2 force, Vector2 position = Vector2(0, 0))

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.

void add_constant_torque(float torque)

Adds a constant rotational force without affecting position that keeps being applied over time until cleared with constant_torque = 0.

void apply_central_force(Vector2 force = Vector2(0, 0))

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 apply_force() at the body's center of mass.

void apply_central_impulse(Vector2 impulse)

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 apply_impulse() at the body's center of mass.

void apply_force(Vector2 force, Vector2 position = Vector2(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.

void apply_impulse(Vector2 impulse, Vector2 position = Vector2(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.

void apply_torque(float torque)

Applies a rotational force without affecting position. A force is time dependent and meant to be applied every physics update. Note: inverse_inertia is required for this to work. To have inverse_inertia, an active CollisionShape2D must be a child of the node, or you can manually set inverse_inertia.

void apply_torque_impulse(float impulse)

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: inverse_inertia is required for this to work. To have inverse_inertia, an active CollisionShape2D must be a child of the node, or you can manually set inverse_inertia.

float get_constant_torque() const

Returns the body's total constant rotational forces applied during each physics update. See add_constant_torque().

RID get_contact_collider(int contact_idx) const

Returns the collider's RID.

int get_contact_collider_id(int contact_idx) const

Returns the collider's object id.

Object get_contact_collider_object(int contact_idx) const

Returns the collider object. This depends on how it was created (will return a scene node if such was used to create it).

Vector2 get_contact_collider_position(int contact_idx) const

Returns the position of the contact point on the collider in the global coordinate system.

int get_contact_collider_shape(int contact_idx) const

Returns the collider's shape index.

Vector2 get_contact_collider_velocity_at_position(int contact_idx) const

Returns the velocity vector at the collider's contact point.

int get_contact_count() const

Returns the number of contacts this body has with other bodies. Note: By default, this returns 0 unless bodies are configured to monitor contacts. See RigidBody2D.contact_monitor.

Vector2 get_contact_impulse(int contact_idx) const

Returns the impulse created by the contact.

Vector2 get_contact_local_normal(int contact_idx) const

Returns the local normal at the contact point.

Vector2 get_contact_local_position(int contact_idx) const

Returns the position of the contact point on the body in the global coordinate system.

int get_contact_local_shape(int contact_idx) const

Returns the local shape index of the collision.

Vector2 get_contact_local_velocity_at_position(int contact_idx) const

Returns the velocity vector at the body's contact point.

Vector2 get_velocity_at_local_position(Vector2 local_position) const

Returns the body's velocity at the given relative position, including both translation and rotation.

void integrate_forces()

Updates the body's linear and angular velocity by applying gravity and damping for the equivalent of one physics tick.

void set_constant_torque(float torque)

Sets the body's total constant rotational forces applied during each physics update. See add_constant_torque().

Tutorials

Source revision 4f5b14abade2
Connection interrupted. Reload×

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.