Aego Русский

Rigid Body 2D

Simulates the node as a rigid body. Colliders on the node and its children form its shape.

component physics.Body2D
On this page

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.

Starts the scene; open the example parameters to experiment.
Open in AegoDownload example project
Scene3 nodes
Box
Transform
position[0, -3]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint1tintColor#f59e0bfforiginVec2[0.5, 0.5]sizeVec2[1, 1]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumdynamicmassModeenumautomassfloat0gravityScalefloat0linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0.25densityfloat1sizeVec2[1, 1]
Floor
Transform
position[0, 4]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#4b5563fforiginVec2[0.5, 0.5]sizeVec2[18, 0.6]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[18, 0.6]
Camera
Transform
position[0, 0]rotation0scale[1, 1]
Camera 2D
zoomfloat40viewportRect[0, 0, 1, 1]maskint-1orderint0clearbooltrueclearColorColor#111827ffscaleenumfitVirtualvirtualVec2[960, 540]activebooltrue

Fields

NameTypeDefaultDescription
Typeenumdynamic

static never moves, kinematic moves by velocity, dynamic reacts to forces and contacts.

one of: static, kinematic, dynamic
Mass Modeenumauto

auto takes the mass from collider density, override uses Mass.

one of: auto, override
Massfloat0

Mass in kilograms when Mass Mode is override.

≥ 0 hidden in the Inspector
Gravity Scalefloat1

Multiplier of the project gravity for this body; 0 makes it float.

Linear Dampingfloat0

Slows the movement every step, like air resistance; 0 keeps the speed.

≥ 0
Angular Dampingfloat0

Slows the rotation every step; 0 keeps spinning.

≥ 0
Fixed Rotationboolfalse

Keeps the body upright: contacts and torque never rotate it.

Bulletboolfalse

Continuous collision against other dynamic bodies, so a fast body cannot pass through them. Costs more.

Allow Sleepbooltrue

Lets the body sleep after it comes to rest, which saves time; contacts and scripts wake it.

Interpolateboolfalse

Draws the body between physics steps for smooth motion; scripts still see the exact pose.

Layerlayer0

Collision layer, see Project Settings → Physics.

0 – 31 one of physics.layers
Masklayer 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.

NameTypeDefaultDescription
VelocityVec2(0, 0)

Velocity in units per second.

Angular Velocityfloat0

Rotation speed in radians per second.

Awakeboolfalse

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.