Namespace: b2d.body
Language: Lua
Type: Defold Lua
File: script_box2d_body_v2.cpp
Source: engine/gamesys/src/gamesys/scripts/box2d/v2/script_box2d_body_v2.cpp
Functions for interacting with Box2D bodies.
Type: TYPEDEF Box2D body
Parameters
value (userdata)Type: FUNCTION Apply an angular impulse.
Parameters
body (b2Body) - bodyimpulse (number) - impulse the angular impulse in units of kgmm/sType: FUNCTION Apply an angular impulse.
Parameters
body (b2Body) - bodyimpulse (number) - impulse the angular impulse in units of kgmm/sType: FUNCTION Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity. This wakes up the body.
Parameters
body (b2Body) - bodyforce (vector3) - the world force vector, usually in Newtons (N).point (vector3) - the world position of the point of application.Type: FUNCTION Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity. This wakes up the body.
Parameters
body (b2Body) - bodyforce (vector3) - the world force vector, usually in Newtons (N).point (vector3) - the world position of the point of application.Type: FUNCTION Apply a force to the center of mass. This wakes up the body.
Parameters
body (b2Body) - bodyforce (vector3) - the world force vector, usually in Newtons (N).Type: FUNCTION Apply a force to the center of mass. This wakes up the body.
Parameters
body (b2Body) - bodyforce (vector3) - the world force vector, usually in Newtons (N).Type: FUNCTION Apply an impulse at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body.
Parameters
body (b2Body) - bodyimpulse (vector3) - the world impulse vector, usually in N-seconds or kg-m/s.point (vector3) - the world position of the point of application.Type: FUNCTION Apply an impulse at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body.
Parameters
body (b2Body) - bodyimpulse (vector3) - the world impulse vector, usually in N-seconds or kg-m/s.point (vector3) - the world position of the point of application.Type: FUNCTION Apply a linear impulse to the center of mass.
Parameters
body (b2Body) - bodyimpulse (vector3) - world impulse vectorType: FUNCTION Apply a torque. This affects the angular velocity without affecting the linear velocity of the center of mass. This wakes up the body.
Parameters
body (b2Body) - bodytorque (number) - torque about the z-axis (out of the screen), usually in N-m.Type: FUNCTION Apply a torque. This affects the angular velocity without affecting the linear velocity of the center of mass. This wakes up the body.
Parameters
body (b2Body) - bodytorque (number) - torque about the z-axis (out of the screen), usually in N-m.Type: CONSTANT Dynamic body
Type: CONSTANT Dynamic body
Type: CONSTANT Kinematic body
Type: CONSTANT Kinematic body
Type: CONSTANT Static (immovable) body
Type: CONSTANT Static (immovable) body
Type: FUNCTION Compute the world AABB of all body shapes.
Parameters
body (b2Body) - bodyReturns
aabb (table) - table with lower and upper vector3 fieldsType: FUNCTION Chains are one-sided connected segments with optional ghost vertices at the ends of open chains. Ghost vertices are creation-time chain data only and cannot be added to arbitrary shapes, bodies, or joints after creation.
definition.vertices table array of local vector3 vertices. Open chains require at least 2 vertices. Loop chains require at least 4 vertices. definition.loop boolean true to create a closed loop chain. definition.prev_vertex vector3 optional ghost vertex before the first vertex for open chains. definition.next_vertex vector3 optional ghost vertex after the last vertex for open chains. definition.friction number optional friction. definition.restitution number optional restitution. definition.material number optional material id. definition.filter table optional filter with category_bits, mask_bits, and group_index. definition.enable_sensor_events boolean true to enable sensor events for chain segments.
Parameters
body (b2Body) - bodydefinition (table) - the chain definitionReturns
chain (b2Chain) - created chain handlesegments (table) - array of shape info tables for the chain segmentsExamples
local chain, segments = b2d.body.create_chain(body, {
vertices = {
vmath.vector3(-64, 0, 0),
vmath.vector3(0, 16, 0),
vmath.vector3(64, 0, 0),
},
prev_vertex = vmath.vector3(-96, 0, 0),
next_vertex = vmath.vector3(96, 0, 0),
friction = 0.6,
})
Type: FUNCTION Creates a fixture and attach it to this body. Use this function if you need to set some fixture parameters, like friction. Otherwise you can create the fixture directly from a shape. If the density is non-zero, this function automatically updates the mass of the body. Contacts are not created until the next time step.
Parameters
body (b2Body) - bodydefinition (table) - fixture definition table with:
shape = shape table, friction = number, restitution = number,
density = number, sensor = boolean, and optional filter table.
Supported shape tables are:
circle = { type = b2d.shape.SHAPE_TYPE_CIRCLE, radius = number, center = vector3_or_nil }
edge = { type = b2d.shape.SHAPE_TYPE_EDGE, v1 = vector3, v2 = vector3, v0 = vector3_or_nil, v3 = vector3_or_nil }
polygon = { type = b2d.shape.SHAPE_TYPE_POLYGON, vertices = { vector3, ... } }
box = { type = b2d.shape.SHAPE_TYPE_BOX, hx = number, hy = number, center = vector3_or_nil, angle = radians_or_nil }
chain = { type = b2d.shape.SHAPE_TYPE_CHAIN, vertices = { vector3, ... }, loop = boolean_or_nil, prev_vertex = vector3_or_nil, next_vertex = vector3_or_nil }Returns
fixture (table) - fixture info table with index, type, sensor, density, friction, restitution, and child_countExamples
local body = b2d.get_body("#collisionobject")
local triangle = b2d.body.create_fixture(body, {
density = 1.0,
friction = 0.3,
shape = {
type = b2d.shape.SHAPE_TYPE_POLYGON,
vertices = {
vmath.vector3(-16, -16, 0),
vmath.vector3( 16, -16, 0),
vmath.vector3( 0, 16, 0),
},
},
})
Type: FUNCTION Creates a fixture from a shape and attach it to this body. This is a convenience function. Use b2FixtureDef if you need to set parameters like friction, restitution, user data, or filtering. If the density is non-zero, this function automatically updates the mass of the body.
Parameters
body (b2Body) - bodyshape (b2Shape) - the shape to be cloned.density (number) - the shape density (set to zero for static bodies).Type: FUNCTION Creates a shape and attaches it to this body. If the density is non-zero, this function automatically updates the mass of the body. Contacts are not created until the next time step. The definition may include density, friction, restitution, material, sensor or is_sensor, filter, and the shape table itself. The shape table can be in definition.shape or directly in definition.
Parameters
body (b2Body) - bodydefinition (table) - the shape definition.Type: FUNCTION Destroy a fixture from a body.
Parameters
body (b2Body) - bodyfixture_index (number) - 1-based fixture index from b2d.body.get_fixturesType: FUNCTION Destroy a shape. This removes the shape from the broad-phase and destroys all contacts associated with this shape. This will automatically adjust the mass of the body if the body is dynamic and the shape has positive density. All shapes attached to a body are implicitly destroyed when the body is destroyed.
Parameters
body (b2Body) - bodyshape_index (number) - 1-based shape index from b2d.body.get_shapesType: FUNCTION Print the body representation to the log output
Parameters
body (b2Body) - bodyType: FUNCTION Enable or disable contact events on all body shapes.
Parameters
body (b2Body) - bodyenable (boolean) - true to enable contact eventsType: FUNCTION Enable or disable hit events on all body shapes.
Parameters
body (b2Body) - bodyenable (boolean) - true to enable hit eventsType: FUNCTION You can disable sleeping on this body. If you disable sleeping, the body will be woken.
Parameters
body (b2Body) - bodyenable (boolean) - if false, the body will never sleep, and consume more CPUType: FUNCTION Get the angle in radians.
Parameters
body (b2Body) - bodyReturns
angle (number) - the current world rotation angle in radians.Type: FUNCTION Get the angular damping of the body.
Parameters
body (b2Body) - bodyReturns
damping (number) - the dampingType: FUNCTION Get the angular damping of the body.
Parameters
body (b2Body) - bodyReturns
damping (number) - the dampingType: FUNCTION Get the angular velocity.
Parameters
body (b2Body) - bodyReturns
velocity (number) - the angular velocity in radians/second.Type: FUNCTION Get the angular velocity.
Parameters
body (b2Body) - bodyReturns
velocity (number) - the angular velocity in radians/second.Type: FUNCTION Get touching contact data for a body.
Parameters
body (b2Body) - bodyReturns
contacts (table) - array of contact tablesType: FUNCTION Get the list of all contacts attached to this body.
Parameters
body (b2Body) - bodyReturns
edge (b2ContactEdge) - the first edgeType: FUNCTION Get the list of all contacts attached to this body.
Parameters
body (b2Body) - bodyReturns
edge (b2ContactEdge) - the first edgeType: FUNCTION Get the fixtures attached to this body.
Parameters
body (b2Body) - bodyReturns
fixtures (table) - array of fixture info tables with index, type, sensor, density, friction, restitution, and child_countType: FUNCTION Get the total force currently applied on this object
Notes
Parameters
body (b2Body) - bodyReturns
force (vector3)Type: FUNCTION Get the total force currently applied on this object
Notes
Parameters
body (b2Body) - bodyReturns
force (vector3)Type: FUNCTION Get the gravity scale of the body.
Parameters
body (b2Body) - bodyReturns
scale (number) - the scaleType: FUNCTION Get the gravity scale of the body.
Parameters
body (b2Body) - bodyReturns
scale (number) - the scaleType: FUNCTION Get the rotational inertia of the body about the local origin.
Parameters
body (b2Body) - bodyReturns
inertia (number) - the rotational inertia, usually in kg-m^2.Type: FUNCTION Get the joints attached to this body.
Parameters
body (b2Body) - bodyReturns
joints (table) - array of b2Joint handles created by b2d.jointType: FUNCTION Get the joints attached to this body.
Parameters
body (b2Body) - bodyReturns
joints (table) - array of b2Joint handles created by b2d.jointType: FUNCTION Get the linear damping of the body.
Parameters
body (b2Body) - bodyReturns
damping (number) - the dampingType: FUNCTION Get the linear damping of the body.
Parameters
body (b2Body) - bodyReturns
damping (number) - the dampingType: FUNCTION Get the linear velocity of the center of mass.
Parameters
body (b2Body) - bodyReturns
velocity (vector3) - the linear velocity of the center of mass.Type: FUNCTION Get the linear velocity of the center of mass.
Parameters
body (b2Body) - bodyReturns
velocity (vector3) - the linear velocity of the center of mass.Type: FUNCTION Get the world velocity of a local point.
Parameters
body (b2Body) - bodylocal_point (vector3) - a point in local coordinates.Returns
velocity (vector3) - the world velocity of a point.Type: FUNCTION Get the world velocity of a local point.
Parameters
body (b2Body) - bodylocal_point (vector3) - a point in local coordinates.Returns
velocity (vector3) - the world velocity of a point.Type: FUNCTION Get the world linear velocity of a world point attached to this body.
Parameters
body (b2Body) - bodyworld_point (vector3) - a point in world coordinates.Returns
velocity (vector3) - the world velocity of a point.Type: FUNCTION Get the world linear velocity of a world point attached to this body.
Parameters
body (b2Body) - bodyworld_point (vector3) - a point in world coordinates.Returns
velocity (vector3) - the world velocity of a point.Type: FUNCTION Get the local position of the center of mass.
Parameters
body (b2Body) - bodyReturns
center (vector3) - Get the local position of the center of mass.Type: FUNCTION Get the local position of the center of mass.
Parameters
body (b2Body) - bodyReturns
center (vector3) - Get the local position of the center of mass.Type: FUNCTION Gets a local point relative to the body’s origin given a world point.
Parameters
body (b2Body) - bodyworld_point (vector3) - a point in world coordinates.Returns
vector (vector3) - the corresponding local point relative to the body’s origin.Type: FUNCTION Gets a local point relative to the body’s origin given a world point.
Parameters
body (b2Body) - bodyworld_point (vector3) - a point in world coordinates.Returns
vector (vector3) - the corresponding local point relative to the body’s origin.Type: FUNCTION Gets a local vector given a world vector.
Parameters
body (b2Body) - bodyworld_vector (vector3) - a vector in world coordinates.Returns
vector (vector3) - the corresponding local vector.Type: FUNCTION Gets a local vector given a world vector.
Parameters
body (b2Body) - bodyworld_vector (vector3) - a vector in world coordinates.Returns
vector (vector3) - the corresponding local vector.Type: FUNCTION Get the total mass of the body.
Parameters
body (b2Body) - bodyReturns
mass (number) - the mass, usually in kilograms (kg).Type: FUNCTION Get the total mass of the body.
Parameters
body (b2Body) - bodyReturns
mass (number) - the mass, usually in kilograms (kg).Type: FUNCTION Get the mass data of the body.
Parameters
body (b2Body) - bodyReturns
data (table) - table with mass, center in local coordinates, and inertia.Type: FUNCTION Get the mass data of the body.
Parameters
body (b2Body) - bodyReturns
data (b2MassData) - a struct containing the mass, inertia and center of the body.Type: FUNCTION Get the body name.
Parameters
body (b2Body) - bodyReturns
name (string) - body name, or nil if no name is setType: FUNCTION Get the next body in the world’s body list.
Parameters
body (b2Body) - bodyReturns
body (b2Body) - the next bodyType: FUNCTION Get the world body origin position.
Parameters
body (b2Body) - bodyReturns
position (vector3) - the world position of the body’s origin.Type: FUNCTION Get the world body origin position.
Parameters
body (b2Body) - bodyReturns
position (vector3) - the world position of the body’s origin.Type: FUNCTION Get the rotational inertia of the body about the local origin.
Parameters
body (b2Body) - bodyReturns
inertia (number) - the rotational inertia, usually in kg-m^2.Type: FUNCTION Get the list of all shapes attached to this body.
Parameters
body (b2Body) - bodyReturns
shapes (table) - a table of shape info entries. Each entry includes shape_id for use with b2d.shape functions.Type: FUNCTION Get the sleep velocity threshold.
Parameters
body (b2Body) - bodyReturns
threshold (number) - velocity threshold in Defold units per secondType: FUNCTION Get the body transform for the body’s origin.
Parameters
body (b2Body) - bodyReturns
transform (table) - table with position and angle in radians.Type: FUNCTION Get the body transform for the body’s origin.
Parameters
body (b2Body) - bodyReturns
transform (b2Transform) - the world position of the body’s origin.Type: FUNCTION Get the type of this body.
Parameters
body (b2Body) - bodyReturns
type (b2BodyType) - the body typeType: FUNCTION Get the type of this body.
Parameters
body (b2Body) - bodyReturns
type (b2BodyType) - the body typeType: FUNCTION Get the user data pointer that was provided in the body definition.
Parameters
body (b2Body) - bodyReturns
id (hash) - the game object id this body is connected toType: FUNCTION Get the parent world of this body.
Parameters
body (b2Body) - bodyReturns
world (b2World)Type: FUNCTION Get the parent world of this body.
Parameters
body (b2Body) - bodyReturns
world (b2World)Type: FUNCTION Get the angle in radians.
Parameters
body (b2Body) - bodyReturns
angle (number) - the current world rotation angle in radians.Type: FUNCTION Get the world position of the center of mass.
Parameters
body (b2Body) - bodyReturns
center (vector3) - Get the world position of the center of mass.Type: FUNCTION Get the world position of the center of mass.
Parameters
body (b2Body) - bodyReturns
center (vector3) - Get the world position of the center of mass.Type: FUNCTION Get the world coordinates of a point given the local coordinates.
Parameters
body (b2Body) - bodylocal_vector (vector3) - localPoint a point on the body measured relative the the body’s origin.Returns
vector (vector3) - the same point expressed in world coordinates.Type: FUNCTION Get the world coordinates of a point given the local coordinates.
Parameters
body (b2Body) - bodylocal_vector (vector3) - localPoint a point on the body measured relative the the body’s origin.Returns
vector (vector3) - the same point expressed in world coordinates.Type: FUNCTION Get the world coordinates of a vector given the local coordinates.
Parameters
body (b2Body) - bodylocal_vector (vector3) - a vector fixed in the body.Returns
vector (vector3) - the same vector expressed in world coordinates.Type: FUNCTION Get the world coordinates of a vector given the local coordinates.
Parameters
body (b2Body) - bodylocal_vector (vector3) - a vector fixed in the body.Returns
vector (vector3) - the same vector expressed in world coordinates.Type: FUNCTION Get the active state of the body.
Parameters
body (b2Body) - bodyReturns
enabled (boolean) - is the body activeType: FUNCTION Get the active state of the body.
Parameters
body (b2Body) - bodyReturns
enabled (boolean) - is the body activeType: FUNCTION Get the sleeping state of this body.
Parameters
body (b2Body) - bodyReturns
enabled (boolean) - true if the body is awake, false if it’s sleeping.Type: FUNCTION Get the sleeping state of this body.
Parameters
body (b2Body) - bodyReturns
enabled (boolean) - true if the body is awake, false if it’s sleeping.Type: FUNCTION Is this body in bullet mode
Parameters
body (b2Body) - bodyReturns
enabled (boolean) - true if the body is in bullet modeType: FUNCTION Is this body in bullet mode
Parameters
body (b2Body) - bodyReturns
enabled (boolean) - true if the body is in bullet modeType: FUNCTION Does this body have fixed rotation?
Parameters
body (b2Body) - bodyReturns
enabled (boolean) - is the rotation fixedType: FUNCTION Does this body have fixed rotation?
Parameters
body (b2Body) - bodyReturns
enabled (boolean) - is the rotation fixedType: FUNCTION Is this body allowed to sleep
Parameters
body (b2Body) - bodyReturns
enabled (boolean) - true if the body is allowed to sleepType: FUNCTION Is this body allowed to sleep
Parameters
body (b2Body) - bodyReturns
enabled (boolean) - true if the body is allowed to sleepType: FUNCTION Validate a body handle.
Parameters
body (b2Body) - bodyReturns
valid (boolean) - true if the body handle still refers to a live Box2D bodyType: FUNCTION This resets the mass properties to the sum of the mass properties of the fixtures. This normally does not need to be called unless you called SetMassData to override
Parameters
body (b2Body) - bodyType: FUNCTION This resets the mass properties to the sum of the mass properties of the shapes. This normally does not need to be called unless you called SetMassData to override
Parameters
body (b2Body) - bodyType: FUNCTION Set the active state of the body. An inactive body is not simulated and cannot be collided with or woken up. If you pass a flag of true, all fixtures will be added to the broad-phase. If you pass a flag of false, all fixtures will be removed from the broad-phase and all contacts will be destroyed. Fixtures and joints are otherwise unaffected. You may continue to create/destroy fixtures and joints on inactive bodies. Fixtures on an inactive body are implicitly inactive and will not participate in collisions, ray-casts, or queries. Joints connected to an inactive body are implicitly inactive. An inactive body is still owned by a b2World object and remains in the body list.
Parameters
body (b2Body) - bodyenable (boolean) - true if the body should be activeType: FUNCTION Set the active state of the body. An inactive body is not simulated and cannot be collided with or woken up. If you pass a flag of true, all shapes will be added to the broad-phase. If you pass a flag of false, all shapes will be removed from the broad-phase and all contacts will be destroyed. Shapes and joints are otherwise unaffected. You may continue to create/destroy shapes and joints on inactive bodies. Shapes on an inactive body are implicitly inactive and will not participate in collisions, ray-casts, or queries. Joints connected to an inactive body are implicitly inactive. An inactive body is still owned by a b2World object and remains in the body list.
Parameters
body (b2Body) - bodyenable (boolean) - true if the body should be activeType: FUNCTION Set the angular damping of the body.
Parameters
body (b2Body) - bodydamping (number) - the dampingType: FUNCTION Set the angular damping of the body.
Parameters
body (b2Body) - bodydamping (number) - the dampingType: FUNCTION Set the angular velocity.
Parameters
body (b2Body) - bodyomega (number) - the new angular velocity in radians/second.Type: FUNCTION Set the angular velocity.
Parameters
body (b2Body) - bodyomega (number) - the new angular velocity in radians/second.Type: FUNCTION Set the sleep state of the body. A sleeping body has very low CPU cost.
Parameters
body (b2Body) - bodyenable (boolean) - flag set to false to put body to sleep, true to wake it.Type: FUNCTION Set the sleep state of the body. A sleeping body has very low CPU cost.
Parameters
body (b2Body) - bodyenable (boolean) - flag set to false to put body to sleep, true to wake it.Type: FUNCTION Should this body be treated like a bullet for continuous collision detection?
Parameters
body (b2Body) - bodyenable (boolean) - if true, the body will be in bullet modeType: FUNCTION Should this body be treated like a bullet for continuous collision detection?
Parameters
body (b2Body) - bodyenable (boolean) - if true, the body will be in bullet modeType: FUNCTION Set this body to have fixed rotation. This causes the mass to be reset.
Parameters
body (b2Body) - bodyenable (boolean) - true if the rotation should be fixedType: FUNCTION Set this body to have fixed rotation. This causes the mass to be reset.
Parameters
body (b2Body) - bodyenable (boolean) - true if the rotation should be fixedType: FUNCTION Set the gravity scale of the body.
Parameters
body (b2Body) - bodyscale (number) - the scaleType: FUNCTION Set the gravity scale of the body.
Parameters
body (b2Body) - bodyscale (number) - the scaleType: FUNCTION Set the linear damping of the body.
Parameters
body (b2Body) - bodydamping (number) - the dampingType: FUNCTION Set the linear damping of the body.
Parameters
body (b2Body) - bodydamping (number) - the dampingType: FUNCTION Set the linear velocity of the center of mass.
Parameters
body (b2Body) - bodyvelocity (vector3) - the new linear velocity of the center of mass.Type: FUNCTION Set the linear velocity of the center of mass.
Parameters
body (b2Body) - bodyvelocity (vector3) - the new linear velocity of the center of mass.Type: FUNCTION Set the mass properties to override the mass properties of the fixtures.
Notes
Parameters
body (b2Body) - bodydata (table) - table with mass, center in local coordinates, and inertia.Type: FUNCTION Set the mass properties to override the mass properties of the shapes.
Notes
Parameters
body (b2Body) - bodydata (b2MassData) - the mass properties.Type: FUNCTION Set the body name.
Parameters
body (b2Body) - bodyname (string) - body nameType: FUNCTION Set the sleep velocity threshold.
Parameters
body (b2Body) - bodythreshold (number) - velocity threshold in Defold units per secondType: FUNCTION You can disable sleeping on this body. If you disable sleeping, the body will be woken.
Parameters
body (b2Body) - bodyenable (boolean) - if false, the body will never sleep, and consume more CPUType: FUNCTION Set velocity to reach a target transform.
Parameters
body (b2Body) - bodyposition (vector3) - target world positionangle (number) - target world angle in radianstime_step (number) - time step used to compute velocityType: FUNCTION Set the position of the body’s origin and rotation. This breaks any contacts and wakes the other bodies. Manipulating a body’s transform may cause non-physical behavior.
Parameters
body (b2Body) - bodyposition (vector3) - the world position of the body’s local origin.angle (number) - the world position of the body’s local origin.Type: FUNCTION Set the position of the body’s origin and rotation. This breaks any contacts and wakes the other bodies. Manipulating a body’s transform may cause non-physical behavior.
Parameters
body (b2Body) - bodyposition (vector3) - the world position of the body’s local origin.angle (number) - the world position of the body’s local origin.Type: FUNCTION Set the type of this body. This may alter the mass and velocity.
Parameters
body (b2Body) - bodytype (b2BodyType) - the body typeType: FUNCTION Set the type of this body. This may alter the mass and velocity.
Parameters
body (b2Body) - bodytype (b2BodyType) - the body typeType: FUNCTION Set the user data. Use this to store your application specific data.
Parameters
body (b2Body) - bodyid (hash) - the game object idType: TYPEDEF Box2D world
Parameters
value (userdata)