Skip to main content

RigidBody2D

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

Description

A 2D physics body that is moved by a physics simulation. RigidBody2D implements full 2D physics. It cannot be controlled directly; instead, you must apply forces to it (gravity, impulses, etc.).

Properties

mass
float
default:"1.0"
The body’s mass.
linear_velocity
Vector2
default:"Vector2(0, 0)"
The body’s linear velocity in pixels per second.
angular_velocity
float
default:"0.0"
The body’s rotational velocity in radians per second.
gravity_scale
float
default:"1.0"
Multiplies the gravity applied to the body.
freeze
bool
default:"false"
If true, the body is frozen and forces are not applied.

Methods

apply_central_impulse

void apply_central_impulse(impulse: Vector2 = Vector2(0, 0))
Applies a directional impulse without affecting rotation.
impulse
Vector2
The impulse vector to apply

apply_impulse

void apply_impulse(impulse: Vector2, position: Vector2 = Vector2(0, 0))
Applies a positioned impulse to the body.

apply_force

void apply_force(force: Vector2, position: Vector2 = Vector2(0, 0))
Applies a positioned force to the body.

apply_torque

void apply_torque(torque: float)
Applies a rotational force without affecting position.

Signals

body_entered(body: Node)

Emitted when a body enters into contact with this one.

body_exited(body: Node)

Emitted when a body exits contact with this one.

Example Usage

extends RigidBody2D

func _ready():
    # Set initial properties
    mass = 10.0
    gravity_scale = 1.5

func _input(event):
    if event.is_action_pressed("jump"):
        # Apply upward impulse
        apply_central_impulse(Vector2(0, -500))
    
    if event.is_action_pressed("push_right"):
        # Apply force to the right
        apply_force(Vector2(1000, 0))

Build docs developers (and LLMs) love