RigidBody2D

package
v0.0.0-...-c858641 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2025 License: MIT Imports: 30 Imported by: 0

Documentation

Overview

graphics.gd/classdb/RigidBody2D implements full 2D 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 via Instance.LockRotation, Instance.Freeze, and Instance.FreezeMode. By changing various properties of the object, such as Instance.Mass, 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. See Instance.CustomIntegrator.

Note: Changing the 2D transform or Instance.LinearVelocity of a graphics.gd/classdb/RigidBody2D very often may lead to some unpredictable behaviors. If you need to directly affect the body, prefer [Interface.IntegrateForces] as it allows you to directly access the physics state.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsRigidBody2D() Instance
}

type CCDMode

type CCDMode int //gd:RigidBody2D.CCDMode
const (
	// Continuous collision detection disabled. This is the fastest way to detect body collisions, but can miss small, fast-moving objects.
	CcdModeDisabled CCDMode = 0
	// Continuous collision detection enabled using raycasting. This is faster than shapecasting but less precise.
	CcdModeCastRay CCDMode = 1
	// Continuous collision detection enabled using shapecasting. This is the slowest CCD method and the most precise.
	CcdModeCastShape CCDMode = 2
)

type CenterOfMassMode

type CenterOfMassMode int //gd:RigidBody2D.CenterOfMassMode
const (
	// 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.
	CenterOfMassModeAuto CenterOfMassMode = 0
	// In this mode, the body's center of mass is set through [Instance.CenterOfMass]. Defaults to the body's origin position.
	CenterOfMassModeCustom CenterOfMassMode = 1
)

type DampMode

type DampMode int //gd:RigidBody2D.DampMode
const (
	// In this mode, the body's damping value is added to any value set in areas or the default value.
	DampModeCombine DampMode = 0
	// In this mode, the body's damping value replaces any value set in areas or the default value.
	DampModeReplace DampMode = 1
)

type Expanded

type Expanded [1]gdclass.RigidBody2D

func (Expanded) AddConstantForce

func (self Expanded) AddConstantForce(force Vector2.XY, position Vector2.XY)

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.

func (Expanded) ApplyCentralImpulse

func (self Expanded) ApplyCentralImpulse(impulse Vector2.XY)

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 Instance.ApplyImpulse at the body's center of mass.

func (Expanded) ApplyForce

func (self Expanded) ApplyForce(force Vector2.XY, position Vector2.XY)

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.

func (Expanded) ApplyImpulse

func (self Expanded) ApplyImpulse(impulse Vector2.XY, position Vector2.XY)

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.

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this [Extension]See Interface for methods that can be overridden by T.

func (*Extension[T]) AsCanvasItem

func (self *Extension[T]) AsCanvasItem() CanvasItem.Instance

func (*Extension[T]) AsCollisionObject2D

func (self *Extension[T]) AsCollisionObject2D() CollisionObject2D.Instance

func (*Extension[T]) AsNode

func (self *Extension[T]) AsNode() Node.Instance

func (*Extension[T]) AsNode2D

func (self *Extension[T]) AsNode2D() Node2D.Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsPhysicsBody2D

func (self *Extension[T]) AsPhysicsBody2D() PhysicsBody2D.Instance

func (*Extension[T]) AsRigidBody2D

func (self *Extension[T]) AsRigidBody2D() Instance

type FreezeMode

type FreezeMode int //gd:RigidBody2D.FreezeMode
const (
	// 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.
	FreezeModeStatic FreezeMode = 0
	// Kinematic body freeze mode. Similar to [FreezeModeStatic], but collides with other bodies along its path when moved. Useful for a frozen body that needs to be animated.
	FreezeModeKinematic FreezeMode = 1
)

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Implementation

type Implementation = implementation

Implementation implements Interface with empty methods.

type Instance

type Instance [1]gdclass.RigidBody2D

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) AddConstantCentralForce

func (self Instance) AddConstantCentralForce(force Vector2.XY)

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 Instance.AddConstantForce at the body's center of mass.

func (Instance) AddConstantForce

func (self Instance) AddConstantForce(force Vector2.XY)

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.

func (Instance) AddConstantTorque

func (self Instance) AddConstantTorque(torque Float.X)

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

func (Instance) AngularDamp

func (self Instance) AngularDamp() Float.X

func (Instance) AngularDampMode

func (self Instance) AngularDampMode() DampMode

func (Instance) AngularVelocity

func (self Instance) AngularVelocity() Float.X

func (Instance) ApplyCentralForce

func (self Instance) ApplyCentralForce(force Vector2.XY)

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 Instance.ApplyForce at the body's center of mass.

func (Instance) ApplyCentralImpulse

func (self Instance) ApplyCentralImpulse()

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 Instance.ApplyImpulse at the body's center of mass.

func (Instance) ApplyForce

func (self Instance) ApplyForce(force Vector2.XY)

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.

func (Instance) ApplyImpulse

func (self Instance) ApplyImpulse(impulse Vector2.XY)

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.

func (Instance) ApplyTorque

func (self Instance) ApplyTorque(torque Float.X)

Applies a rotational force without affecting position. A force is time dependent and meant to be applied every physics update.

Note: Instance.Inertia is required for this to work. To have Instance.Inertia, an active graphics.gd/classdb/CollisionShape2D must be a child of the node, or you can manually set Instance.Inertia.

func (Instance) ApplyTorqueImpulse

func (self Instance) ApplyTorqueImpulse(torque Float.X)

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

func (Instance) AsCanvasItem

func (self Instance) AsCanvasItem() CanvasItem.Instance

func (Instance) AsCollisionObject2D

func (self Instance) AsCollisionObject2D() CollisionObject2D.Instance

func (Instance) AsNode

func (self Instance) AsNode() Node.Instance

func (Instance) AsNode2D

func (self Instance) AsNode2D() Node2D.Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsPhysicsBody2D

func (self Instance) AsPhysicsBody2D() PhysicsBody2D.Instance

func (Instance) AsRigidBody2D

func (self Instance) AsRigidBody2D() Instance

func (Instance) CanSleep

func (self Instance) CanSleep() bool

func (Instance) CenterOfMass

func (self Instance) CenterOfMass() Vector2.XY

func (Instance) CenterOfMassMode

func (self Instance) CenterOfMassMode() CenterOfMassMode

func (Instance) ConstantForce

func (self Instance) ConstantForce() Vector2.XY

func (Instance) ConstantTorque

func (self Instance) ConstantTorque() Float.X

func (Instance) ContactMonitor

func (self Instance) ContactMonitor() bool

func (Instance) ContinuousCd

func (self Instance) ContinuousCd() CCDMode

func (Instance) CustomIntegrator

func (self Instance) CustomIntegrator() bool

func (Instance) Freeze

func (self Instance) Freeze() bool

func (Instance) FreezeMode

func (self Instance) FreezeMode() FreezeMode

func (Instance) GetCollidingBodies

func (self Instance) GetCollidingBodies() []Node2D.Instance

Returns a list of the bodies colliding with this one. Requires Instance.ContactMonitor to be set to true and Instance.MaxContactsReported 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.

func (Instance) GetContactCount

func (self Instance) GetContactCount() int

Returns the number of contacts this body has with other bodies. By default, this returns 0 unless bodies are configured to monitor contacts (see Instance.ContactMonitor).

Note: To retrieve the colliding bodies, use Instance.GetCollidingBodies.

func (Instance) GravityScale

func (self Instance) GravityScale() Float.X

func (Instance) ID

func (self Instance) ID() ID

func (Instance) Inertia

func (self Instance) Inertia() Float.X

func (Instance) LinearDamp

func (self Instance) LinearDamp() Float.X

func (Instance) LinearDampMode

func (self Instance) LinearDampMode() DampMode

func (Instance) LinearVelocity

func (self Instance) LinearVelocity() Vector2.XY

func (Instance) LockRotation

func (self Instance) LockRotation() bool

func (Instance) Mass

func (self Instance) Mass() Float.X

func (Instance) MaxContactsReported

func (self Instance) MaxContactsReported() int

func (Instance) OnBodyEntered

func (self Instance) OnBodyEntered(cb func(body Node.Instance), flags ...Signal.Flags)

func (Instance) OnBodyExited

func (self Instance) OnBodyExited(cb func(body Node.Instance), flags ...Signal.Flags)

func (Instance) OnBodyShapeEntered

func (self Instance) OnBodyShapeEntered(cb func(body_rid RID.Any, body Node.Instance, body_shape_index int, local_shape_index int), flags ...Signal.Flags)

func (Instance) OnBodyShapeExited

func (self Instance) OnBodyShapeExited(cb func(body_rid RID.Any, body Node.Instance, body_shape_index int, local_shape_index int), flags ...Signal.Flags)

func (Instance) OnSleepingStateChanged

func (self Instance) OnSleepingStateChanged(cb func(), flags ...Signal.Flags)

func (Instance) PhysicsMaterialOverride

func (self Instance) PhysicsMaterialOverride() PhysicsMaterial.Instance

func (Instance) SetAngularDamp

func (self Instance) SetAngularDamp(value Float.X)

func (Instance) SetAngularDampMode

func (self Instance) SetAngularDampMode(value DampMode)

func (Instance) SetAngularVelocity

func (self Instance) SetAngularVelocity(value Float.X)

func (Instance) SetAxisVelocity

func (self Instance) SetAxisVelocity(axis_velocity Vector2.XY)

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.

func (Instance) SetCanSleep

func (self Instance) SetCanSleep(value bool)

func (Instance) SetCenterOfMass

func (self Instance) SetCenterOfMass(value Vector2.XY)

func (Instance) SetCenterOfMassMode

func (self Instance) SetCenterOfMassMode(value CenterOfMassMode)

func (Instance) SetConstantForce

func (self Instance) SetConstantForce(value Vector2.XY)

func (Instance) SetConstantTorque

func (self Instance) SetConstantTorque(value Float.X)

func (Instance) SetContactMonitor

func (self Instance) SetContactMonitor(value bool)

func (Instance) SetContinuousCd

func (self Instance) SetContinuousCd(value CCDMode)

func (Instance) SetCustomIntegrator

func (self Instance) SetCustomIntegrator(value bool)

func (Instance) SetFreeze

func (self Instance) SetFreeze(value bool)

func (Instance) SetFreezeMode

func (self Instance) SetFreezeMode(value FreezeMode)

func (Instance) SetGravityScale

func (self Instance) SetGravityScale(value Float.X)

func (Instance) SetInertia

func (self Instance) SetInertia(value Float.X)

func (Instance) SetLinearDamp

func (self Instance) SetLinearDamp(value Float.X)

func (Instance) SetLinearDampMode

func (self Instance) SetLinearDampMode(value DampMode)

func (Instance) SetLinearVelocity

func (self Instance) SetLinearVelocity(value Vector2.XY)

func (Instance) SetLockRotation

func (self Instance) SetLockRotation(value bool)

func (Instance) SetMass

func (self Instance) SetMass(value Float.X)

func (Instance) SetMaxContactsReported

func (self Instance) SetMaxContactsReported(value int)

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) SetPhysicsMaterialOverride

func (self Instance) SetPhysicsMaterialOverride(value PhysicsMaterial.Instance)

func (Instance) SetSleeping

func (self Instance) SetSleeping(value bool)

func (Instance) Sleeping

func (self Instance) Sleeping() bool

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

type Interface

type Interface interface {
	// 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 the [Instance.CustomIntegrator] property allows you to disable the standard force integration and do fully custom force integration for a body.
	IntegrateForces(state PhysicsDirectBodyState2D.Instance)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL