Rigid Body 2D
Simulates the node as a rigid body. Colliders on the node and its children form its shape.
component physics.Body2D
Try it
Gravity on a rigid body
The dynamic box starts with Gravity Scale 0 and stays suspended; the floor has a static collider.
Set Gravity Scale to 50 to exaggerate gravity and make the box fall. Reset restores the floating starting state.
Scene3 nodes
Fields
| Name | Type | Default | Description |
|---|---|---|---|
| Type | enum | dynamic | static never moves, kinematic moves by velocity, dynamic reacts to forces and contacts. one of: static, kinematic, dynamic |
| Mass Mode | enum | auto | auto takes the mass from collider density, override uses Mass. one of: auto, override |
| Mass | float | 0 | Mass in kilograms when Mass Mode is override. ≥ 0 hidden in the Inspector |
| Gravity Scale | float | 1 | Multiplier of the project gravity for this body; 0 makes it float. |
| Linear Damping | float | 0 | Slows the movement every step, like air resistance; 0 keeps the speed. ≥ 0 |
| Angular Damping | float | 0 | Slows the rotation every step; 0 keeps spinning. ≥ 0 |
| Fixed Rotation | bool | false | Keeps the body upright: contacts and torque never rotate it. |
| Bullet | bool | false | Continuous collision against other dynamic bodies, so a fast body cannot pass through them. Costs more. |
| Allow Sleep | bool | true | Lets the body sleep after it comes to rest, which saves time; contacts and scripts wake it. |
| Interpolate | bool | false | Draws the body between physics steps for smooth motion; scripts still see the exact pose. |
| Layer | layer | 0 | Collision layer, see Project Settings → Physics. 0 – 31 one of physics.layers |
| Mask | layer mask | -1 | Layers this body collides with. mask over physics.layers |
Runtime state
Values the component has while the game runs. They are not saved with the scene.
| Name | Type | Default | Description |
|---|---|---|---|
| Velocity | Vec2 | (0, 0) | Velocity in units per second. |
| Angular Velocity | float | 0 | Rotation speed in radians per second. |
| Awake | bool | false | Whether the body is simulated this step or sleeping. |
From a script
Run with the Move action, jump with an impulse and read the velocity back; the body's own fields stay editable from the script.
package player
import "aego/input"
script Runner {
var Speed float = 6
var Jump Vec2 = Vec2{0, -6}
func Update(dt float) {
body := node.Get<Body2D>()
dir := input.Axis("Move")
body.SetVelocity(Vec2{dir * Speed, body.Velocity.Y})
if input.Pressed("Jump") {
body.ApplyImpulse(Jump * body.Mass())
}
body.GravityScale = 1
if body.Velocity.Y > 0 {
body.GravityScale = 2
}
}
}
Methods
ApplyForce method
func (Body2D) ApplyForce(force Vec2)
Pushes the body's center of mass during the next physics step. Call it from FixedUpdate.
ApplyForceAt method
func (Body2D) ApplyForceAt(force Vec2, point Vec2)
Pushes the body at a world point, which also spins it.
ApplyImpulse method
func (Body2D) ApplyImpulse(impulse Vec2)
Changes the velocity at once, scaled by mass.
Example
body.ApplyImpulse(Vec2{Y: -jumpImpulse})
ApplyImpulseAt method
func (Body2D) ApplyImpulseAt(impulse Vec2, point Vec2)
Applies an impulse at a world point.
ApplyTorque method
func (Body2D) ApplyTorque(torque float)
Spins the body during the next physics step.
Mass() method
func (Body2D) Mass() float
Returns the mass computed from the colliders or the override.
MoveTo method
func (Body2D) MoveTo(position Vec2, angle float)
Drives a kinematic body to the pose by the end of the next physics step, pushing dynamic bodies on the way. Call it from FixedUpdate.
SetAngularVelocity method
func (Body2D) SetAngularVelocity(velocity float)
Sets the spin in radians per second.
SetVelocity method
func (Body2D) SetVelocity(velocity Vec2)
Sets the linear velocity in units per second.
Sleep method
func (Body2D) Sleep()
Stops the body until something touches it.
Teleport method
func (Body2D) Teleport(position Vec2, angle float)
Moves the body to a world pose without sweeping, keeping its velocity.
WakeUp method
func (Body2D) WakeUp()
Makes a sleeping body simulate again.
Script events
Scripts on a node with this component receive these events from the Physics 2D module.
OnCollisionEnter— Runs after the physics step in which this body started touching other.OnCollisionExit— Runs when the bodies stop touching, including when one of them is destroyed or disabled. Contact.Point is the last known point.OnCollisionStay— Runs after every later physics step while the bodies keep touching. It costs nothing when no script declares it.OnTriggerEnter— Runs when other's collider entered a trigger of this body, or this body entered other's trigger.OnTriggerExit— Runs when the overlap with a trigger ends.
Related guides
- Components These components make a node part of the simulation.
- Scripting Scripts move bodies through the Rigid Body 2D component and ask the world what is where through
aego/physics. - Character motion Motion 2D moves a character with a kinematic Rigid Body 2D.