Module body

A Body is the representation of an Entity in physics space.

As such, it is only relevant in Scenes driven by physics engines.

The Body is used to represent the Entity in the physical space. The Sprite represents it in the graphical space. The Body dimensions, rotation, and position determine how the Sprite is drawn.

If you need to set a collision area that is smaller than the Sprite's cell size, use set_hit_box.

Body extends BoundObject

Properties

angle The current rotation of the Body in radians
can_rotate (bool) Whether the Body can rotate on its own.
elasticity Elasticity factor, on a scale of 0.0 (Not bouncy) to 1.0 (Perfectly bouncy).
entity (read only) The Entity the Body is attached to.
force The current amount of force being applied to Body in Newtons.
friction Coefficient of friction, on a scale of 0.0 (Smooth) to 1.0 (Sticky).
is_rogue (bool) If true, then Body is not modified by the physics solver.
is_static (bool) If true, then Body does not collide with other objects.
mass Mass in kg
pos An {x, y} vector of the Body's center in the physics space (in meters).
velo An {x, y} vector of the Body's velocity (in meters per second).
collision_layers Set the collision bit-plane of the Body.
sensors The Collection of the Body's Sensors

Class Methods

Body:new (proto, width, height, mass, can_rotate) Create a new Body

Instance Methods

body:apply_force (force, offset) Apply a force on the next solve.
body:set_hit_box (width, height, offset) Set the Body's collision box in relation to the Body's dimensions.


Properties

Properties. Significant fields on an instance.
angle
The current rotation of the Body in radians
can_rotate
(bool) Whether the Body can rotate on its own.
elasticity
Elasticity factor, on a scale of 0.0 (Not bouncy) to 1.0 (Perfectly bouncy).
entity
(read only) The Entity the Body is attached to.
force
The current amount of force being applied to Body in Newtons.
friction
Coefficient of friction, on a scale of 0.0 (Smooth) to 1.0 (Sticky).
is_rogue
(bool) If true, then Body is not modified by the physics solver.

This could be useful for something like a gate or moving platform.

is_static
(bool) If true, then Body does not collide with other objects. Sensors attached to Body will still work.

This could be useful for something like a door or switch.

mass
Mass in kg
pos

An {x, y} vector of the Body's center in the physics space (in meters).

Warning: You must set the property explicitly for changes to take apply. Like this:

  body.pos = {10, 11}

Not this:

  body.pos[1] = 10
velo

An {x, y} vector of the Body's velocity (in meters per second).

Warning: You must set the property explicitly for changes to take apply. Like this:

  body.velo = {5, 0}

Not this:

  body.velo[1] = 5
collision_layers
Set the collision bit-plane of the Body.

Bodies only collide if they are in the same bit-planes. i.e. (a.collision_layers & b.collision_layers) != 0.

By default, a Body occupies all bit-planes.

sensors
The Collection of the Body's Sensors

Class Methods

Class Methods. Must be called on Class, with a capital leading character. e.g. Class:method("foo")
Body:new (proto, width, height, mass, can_rotate)
Create a new Body

Parameters:

  • proto string The Body prototype. Currently, the only valid
  • width number The width of the body in meters.
  • height number The height of the body in meters.
  • mass number The mass of the body in kg.
  • can_rotate boolean Whether the Body can rotate or not option is "chipmunk"

Returns:

    Body

Instance Methods

Instance Methods. Must be called on an instance of Class. e.g. instance:method("foo")
body:apply_force (force, offset)
Apply a force on the next solve.

Parameters:

  • force table An {x, y} vector of the force in Newtons (kg * m / s ^ 2).
  • offset table An {x, y} vector of the offset from center the force is applied to (in respect to rotation).

Returns:

    nil

Usage:

    body:apply_force({100, 0}, {0, 0}) -- Push right
body:set_hit_box (width, height, offset)
Set the Body's collision box in relation to the Body's dimensions.

Parameters:

  • width number The width of the hitbox, as a coefficient of the Body's width.
  • height number The height of the hitbox, as a coefficient of the Body's height.
  • offset table An {x, y} vector of the hitbox center in relation to the Body's center in meters.

Returns:

    nil

Usage:

    body:set_hit_box(0.5, 0.5, {0, 10}) -- A hitbox half the size
generated by LDoc 1.3.12