Wiki

Clone wiki

Core / body

Back to Beyond the Codea in-app reference


FunctionIconType-Small.png physics.body() function

Introduction

The 'Physics' chapter of Codea's in-app reference documents most of the physics.body() function. The function returns a userdata value that represents a dynamic rigid 2D body.

myBody = physics.body(shapeType, ...)

shapeType can be one of the following. The points are vec2() values, loop is a boolean (true or false).

shapeTypeGlobal variableCommentArguments
0CIRCLECircleradius
1EDGEStraight edgepoint1, point2
2POLYGONPolygonpoint1, point2, point3, ...
3CHAINChain of edgesloop, point1, point2, point3, ...

If the body is a dead object and it is collected by Lua's automatic or manual garbage collection, then it will cease to exist and interact with other bodies.

myBody.density

The myBody.density syntax is used to set or get the density of the body, in units of kg per square metre. By default density is 1.

The number of pixels per metre (default 32) is set or got by physics.pixelToMeterRatio().

For example:

myBody = physics.body(CIRCLE, 32)
print(myBody.density)      -- Output: 1 
print(myBody.mass/math.pi) -- Output: 1

Setting the density of a body sets its mass accordingly. Setting the mass of a body does not reset its density.

myBody.linearDamping

The myBody.linearDamping syntax is used to set or get the linear damping property of the body. By default linearDamping is 0 (no linear damping).

Positive linear damping has the effect of reducing a body's linear velocity. The damping parameter can be larger than 1, but the damping effect becomes sensitive to the time step when the damping parameter is large.

myBody.angularDamping

The myBody.angularDamping syntax is used to set or get the angular damping property of the body. By default angularDamping is 0 (no angular damping).

Positive angular damping has the effect of reducing a body's angular velocity. The damping parameter can be larger than 1, but the damping effect becomes sensitive to the time step when the damping parameter is large.

myBody:destroy()

The 'Physics' chapter of Codea's in-app reference documents most of the myBody:destroy() function. It causes the body referred to by myBody (in this example) to be removed from the physics simulation immediately. It is important not to destroy bodies in the collide(contact) function, because collide() is called during the simulation of all bodies during a particular frame. Destroying a body in this way can cause Codea to crash.

Updated