Skip to main content

body2d

2d · physics · 17 properties · 2D

Makes the node a 2D rigid body rapier simulates, in the xy plane: dynamic falls and responds to forces, static never moves, kinematic is moved by script or animation and pushes what it meets. Add a collider2d for it to collide with anything.

In a scene, body2d is the node key that applies it. A script reaches the same properties through node.body2d.get() and node.body2d.set(table).

Properties

propertytypedefaultdescription
angular_dampingfloat0Drag on spin, in the same terms as linear_damping At least 0.
can_sleepbooltrueLet the body stop being simulated once it has held still
ccdboolfalseSweep the body's whole path each step so a fast one cannot pass through a wall
center_of_massvec2[0,0]Where the extra mass sits, in the node's own space; only read when mass is set
dominancefloat0A body in a higher group is unpushable by a lower one; every non-dynamic body outranks them all Range -127–127.
enabledbooltrueSimulate this body at all; a disabled body keeps its state and costs nothing
fast_rotationboolfalseAllow a spin fast enough that rapier would otherwise clamp it
gravity_scalefloat1Multiplier on world gravity for this body: 0 hangs in the air, negative floats up
inertiafloat0Resistance to spin; 0 lets rapier derive it from the mass At least 0.
kindenumdynamicHow 2D physics drives the node: simulated, immovable, moved by script, or moved by a velocity you set One of dynamic, static, kinematic, kinematic_velocity. Scene shorthand: kind's value can be given as the component's whole value.
linear_dampingfloat0Drag on travel: how fast the body loses speed with nothing touching it At least 0.
lock_rotationboolfalseStop the body turning; how a 2D character stays upright
lock_translationflags[]Axes the body may not move along One of x, y.
massfloat0Extra mass on top of what the colliders' density gives; 0 leaves the body at its collider mass At least 0.
sleep_timefloat0.5Seconds of stillness before the body sleeps At least 0.
soft_ccdfloat0Distance ahead the body predicts contacts, in units; cheaper than ccd for merely fast bodies At least 0.
solver_iterationsfloat0Extra solver iterations for this body alone, for the one stack that jitters At least 0.

Script functions

Methods of node.body2d, the handle a node carrying this component exposes. Each is also a free function on its module, taking the node as its first argument. Every handle also has get(), set(table), has() and remove().

From physics2d:

methodwhat it does
add_body(string)Give the node a 2D rigid body of the given kind (BODY_DYNAMIC, BODY_STATIC, BODY_KINEMATIC).
add_force(float, float)Push the body until the force is reset; unlike an impulse this is spread over time.
add_force_at_point(float, float, float, float)Push at a world point, which also turns the body.
add_torque(float)Turn the body until the torque is reset.
angular_velocity() -> floatHow fast the body is spinning, in radians per second.
apply_impulse(float, float)Add an instant change in momentum, as if the body were struck.
apply_impulse_at_point(float, float, float, float)Strike the body at a world point, which spins it as well as moves it.
apply_torque_impulse(float)Add an instant change in angular momentum, as if the body were spun.
body_kind() -> stringWhether the body is dynamic, static, kinematic or kinematic_velocity.
damping() -> float, floatThis body's linear and angular damping.
dominance() -> floatThis body's dominance group.
gravity_scale() -> floatThis body's gravity multiplier.
is_ccd() -> boolWhether continuous collision detection is on for this body.
is_enabled() -> boolWhether the body is being simulated.
is_sleeping() -> boolWhether the body is asleep and being skipped.
kinetic_energy() -> floatThe body's kinetic energy, for a rest test the solver agrees with.
linear_velocity() -> float, floatHow fast the body is travelling, in units per second.
locked_axes() -> bool, bool, boolWhether x, y and rotation are frozen.
mass() -> floatThe body's total mass, colliders included.
max_contact_impulse() -> floatThe hardest contact this body took in the last step, zero when nothing touched it.
next_position() -> float, floatThe position a kinematic body has been told to move to.
predict_position(float) -> float, floatWhere the body will be after dt seconds at its current velocity.
reset_forces()Drop every force added since the last step.
reset_torques()Drop every torque added since the last step.
set_angular_velocity(float)Set how fast the body spins, in radians per second.
set_body_kind(string)Change the body between dynamic, static and kinematic in place, keeping its velocity.
set_ccd(bool)Sweep this body's whole path each step so it cannot pass through a wall.
set_damping(float, float)Set linear and angular damping together.
set_dominance(float)Set the group that decides which of two bodies can push the other.
set_enabled(bool)Simulate this body or leave it out entirely, keeping its state.
set_gravity_scale(float)Scale world gravity for this body alone.
set_linear_velocity(float, float)Set how fast the body travels, in units per second.
set_lock_rotation(bool)Freeze the body's spin: how a 2D character stays upright.
set_lock_translation(bool, bool)Freeze the body's movement along x and y.
sleep()Put the body to sleep now.
teleport(float, float)Move the body to a world position at once, clearing its velocity: what assigning the node's position cannot do, because the step writes that back every tick.
user_force() -> float, floatThe force the next step will integrate.
user_torque() -> floatThe torque the next step will integrate.
velocity_at_point(float, float) -> float, floatHow fast a world point on the body is moving, spin included.
wake_up()Wake the body, so the next step moves it.