Body2D¶
Description¶
Body2D is the 2D rigid-body handle built on top of the Box2D v3 physics library. Obtain one by calling object.getBody2D(). After creating shapes and setting properties, call load() to register the body with the physics world. The body position is synchronized with the parent Object transform automatically each physics step.
Inherits: EntityHandle
Properties¶
| Type | Name | Default | Languages |
|---|---|---|---|
| BodyType | type | STATIC |
C++ | Lua |
| Vector2 | linearVelocity | (0,0) |
C++ | Lua |
| float | angularVelocity | 0 |
C++ | Lua |
| float | linearDamping | 0 |
C++ | Lua |
| float | angularDamping | 0 |
C++ | Lua |
| bool | enableSleep | true |
C++ | Lua |
| bool | awake | true |
C++ | Lua |
| bool | fixedRotation | false |
C++ | Lua |
| bool | bullet | false |
C++ | Lua |
| bool | enabled | true |
C++ | Lua |
| float | gravityScale | 1.0 |
C++ | Lua |
| float | mass | — | C++ | Lua |
| float | rotationalInertia | — | C++ | Lua |
Methods¶
| Returns | Name | Languages |
|---|---|---|
| void | load | C++ | Lua |
| int | createBoxShape | C++ | Lua |
| int | createCenteredBoxShape | C++ | Lua |
| int | createRoundedBoxShape | C++ | Lua |
| int | createPolygonShape | C++ | Lua |
| int | createCircleShape | C++ | Lua |
| int | createCapsuleShape | C++ | Lua |
| int | createSegmentShape | C++ | Lua |
| int | createChainShape | C++ | Lua |
| void | removeAllShapes | C++ | Lua |
| size_t | getNumShapes | C++ | Lua |
| Shape2DType | getShapeType | C++ | Lua |
| void | setShapeDensity | C++ | Lua |
| float | getShapeDensity | C++ | Lua |
| void | setShapeFriction | C++ | Lua |
| float | getShapeFriction | C++ | Lua |
| void | setShapeRestitution | C++ | Lua |
| float | getShapeRestitution | C++ | Lua |
| void | setShapeEnableHitEvents | C++ | Lua |
| void | setShapeContactEvents | C++ | Lua |
| void | setShapePreSolveEvents | C++ | Lua |
| void | setShapeSensorEvents | C++ | Lua |
| void | setBitsFilter | C++ | Lua |
| void | setCategoryBitsFilter | C++ | Lua |
| void | setMaskBitsFilter | C++ | Lua |
| void | setGroupIndexFilter | C++ | Lua |
| void | applyMassFromShapes | C++ | Lua |
| void | applyForce | C++ | Lua |
| void | applyForceToCenter | C++ | Lua |
| void | applyTorque | C++ | Lua |
| void | applyLinearImpulse | C++ | Lua |
| void | applyLinearImpulseToCenter | C++ | Lua |
| void | applyAngularImpulse | C++ | Lua |
| std::vector\<Contact2D> | getBodyContacts | C++ | Lua |
| std::vector\<Contact2D> | getShapeContacts | C++ | Lua |
| Object | getAttachedObject | C++ | Lua |
| float | getPointsToMeterScale | C++ | Lua |
Enumerations¶
BodyType¶
- STATIC — Does not move; infinite mass. Used for ground, walls, and platforms.
- KINEMATIC — Moved by setting velocity directly, not by forces. Collides with dynamic bodies.
- DYNAMIC — Fully simulated; responds to forces, torques, and gravity.
Shape2DType¶
- BOX — Rectangular polygon shape.
- CIRCLE — Circle shape defined by center and radius.
- CAPSULE — Two circles connected by a rectangle.
- POLYGON — Arbitrary convex polygon.
- SEGMENT — Line segment (edge shape, no volume).
- CHAIN — Connected chain of line segments; can be open or closed.
Property details¶
type¶
- Setter:
void setType(BodyType type) - Getter:
BodyType getType() const
Sets the body simulation mode. See BodyType. Must be set before calling load or changed at runtime by re-loading the body.
linearVelocity¶
- Setter:
void setLinearVelocity(Vector2 linearVelocity) - Getter:
Vector2 getLinearVelocity() const
Current velocity of the center of mass in world units per second.
angularVelocity¶
- Setter:
void setAngularVelocity(float angularVelocity) - Getter:
float getAngularVelocity() const
Current angular velocity in radians per second (regardless of Engine::useDegrees).
linearDamping / angularDamping¶
- Setters:
void setLinearDamping(float)/void setAngularDamping(float) - Getters:
float getLinearDamping() const/float getAngularDamping() const
Drag coefficients that slow down translation and rotation over time. Values in [0, ∞). Zero means no damping.
fixedRotation¶
- Setter:
void setFixedRotation(bool fixedRotation) - Getter:
bool isFixedRotation() const
When true, the body cannot rotate. Useful for top-down characters that should always stay upright.
bullet¶
- Setter:
void setBullet(bool bullet) - Getter:
bool isBullet() const
Enables continuous collision detection (CCD) for fast-moving objects that might tunnel through thin geometry in a single time step.
gravityScale¶
- Setter:
void setGravityScale(float gravityScale) - Getter:
float getGravityScale() const
Multiplier applied to global gravity for this body. 0 means the body is unaffected by gravity; negative values invert gravity.
mass / rotationalInertia¶
- Getter:
float getMass() const/float getRotationalInertia() const
Computed mass and moment of inertia. Updated automatically when applyMassFromShapes is called.
Method details¶
load¶
void load()
Registers all shapes with the Box2D world and activates the body. Must be called after creating shapes. Call again to rebuild the body after runtime shape changes.
createBoxShape¶
int createBoxShape(float width, float height)
Creates a rectangular polygon shape with corner at the entity's local origin. Returns the shape index (0-based).
createCenteredBoxShape¶
int createCenteredBoxShape(float width, float height)int createCenteredBoxShape(float width, float height, Vector2 center, float angle)
Creates a rectangular polygon centered at (width/2, height/2) relative to the entity origin. The second overload allows an explicit center offset and pre-rotation.
createRoundedBoxShape¶
int createRoundedBoxShape(float width, float height, float radius)
Creates a rounded rectangle (box with chamfered corners). radius specifies the corner rounding amount.
createPolygonShape¶
int createPolygonShape(std::vector<Vector2> vertices)
Creates a convex hull from the given vertices. Box2D requires the shape to be convex; provide vertices in counter-clockwise order.
createCircleShape¶
int createCircleShape(Vector2 center, float radius)
Creates a circle shape at center (in local space) with the given radius.
createCapsuleShape¶
int createCapsuleShape(Vector2 center1, Vector2 center2, float radius)
Creates a capsule defined by two center points and a radius. Good for characters and rounded projectiles.
createSegmentShape¶
int createSegmentShape(Vector2 point1, Vector2 point2)
Creates a one-sided edge segment. Only dynamic bodies moving from point1 to point2 (based on normal direction) are blocked. Useful for platforms.
createChainShape¶
int createChainShape(std::vector<Vector2> vertices, bool loop)
Creates a chain of connected edge segments. When loop is true, the last point connects back to the first forming a closed boundary.
removeAllShapes¶
void removeAllShapes()
Removes all shapes from the body. Call load again after adding new shapes.
setShapeDensity / setShapeFriction / setShapeRestitution¶
Per-shape or all-shapes material properties:
- density — Mass per unit area (affects computed mass).
- friction — Coulomb friction coefficient
[0, ∞).0= frictionless,1= high friction. - restitution — Bounciness
[0, 1].0= no bounce,1= perfectly elastic.
All three come in two overloads: setXxx(value) applies to all shapes; setXxx(index, value) applies to a single shape.
setShapeEnableHitEvents / setShapeContactEvents / setShapePreSolveEvents / setShapeSensorEvents¶
Enable specific collision event callbacks for a shape. See PhysicsSystem events and Events manual.
- HitEvents — Fired when two shapes collide (one-shot, not sustained).
- ContactEvents — Fired while shapes are in contact.
- PreSolveEvents — Fired before the physics impulse is resolved; allows modifying or cancelling the collision.
- SensorEvents — Marks the shape as a sensor (no physical response) but still fires overlap events.
setBitsFilter¶
void setBitsFilter(uint16_t categoryBits, uint16_t maskBits)void setBitsFilter(size_t shapeIndex, uint16_t categoryBits, uint16_t maskBits)
Configures collision filtering using bitmasks. A body collides with another only if (a.category & b.mask) != 0 and (b.category & a.mask) != 0.
const uint16_t LAYER_PLAYER = 0x0001;
const uint16_t LAYER_ENEMY = 0x0002;
const uint16_t LAYER_WALL = 0x0004;
// Player collides with enemies and walls
playerBody.setBitsFilter(LAYER_PLAYER, LAYER_ENEMY | LAYER_WALL);
// Enemy collides with player and walls
enemyBody.setBitsFilter(LAYER_ENEMY, LAYER_PLAYER | LAYER_WALL);
applyMassFromShapes¶
void applyMassFromShapes()
Recomputes mass and inertia from the attached shapes' density values. Call after changing setShapeDensity at runtime.
applyForce¶
void applyForce(const Vector2& force, const Vector2& point, bool wake)
Applies a force (in Newtons) at a given world-space point. Pass wake = true to activate a sleeping body.
applyForceToCenter¶
void applyForceToCenter(const Vector2& force, bool wake)
Applies a force at the body's center of mass; produces linear acceleration but no torque.
applyTorque¶
void applyTorque(float torque, bool wake)
Applies a rotational force. Positive values spin counter-clockwise.
applyLinearImpulse / applyLinearImpulseToCenter¶
Instantaneous velocity changes (Δv = impulse / mass). Prefer impulses over forces for one-shot events like jumps.
applyAngularImpulse¶
void applyAngularImpulse(float impulse, bool wake)
Instantaneous angular velocity change.
getBodyContacts / getShapeContacts¶
std::vector<Contact2D> getBodyContacts()std::vector<Contact2D> getShapeContacts(size_t index)
Returns currently active contact manifolds for the entire body or for a specific shape. Useful for manual overlap queries without subscribing to per-frame events.
getAttachedObject¶
Object getAttachedObject()
Returns the Object whose entity owns this body.
getPointsToMeterScale¶
float getPointsToMeterScale() const
Returns the pixels-to-meters conversion scale used by Box2D. The physics simulation uses SI units (meters/kg/seconds); divide pixel distances by this value before passing them to physics methods, or use the engine's automatic scaling.