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 An opaque handle to the native Box2D body of a collision-object component. Obtain it with b2d.get_body and pass it to functions in b2d.body. The collision object owns the body, so use b2d.body.is_valid before retaining a handle across deletion or collection changes.
Parameters
value (userdata) - Box2D body handleExamples
local body = b2d.get_body("#collisionobject")
if body and b2d.body.is_valid(body) then
b2d.body.apply_force_to_center(body, vmath.vector3(0, 100, 0))
end
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: ENUM Body types
Members
b2d.body.B2_DYNAMIC_BODY - Dynamic bodyb2d.body.B2_KINEMATIC_BODY - Kinematic bodyb2d.body.B2_STATIC_BODY - Static (immovable) bodyType: FUNCTION Compute the world AABB of all body shapes.
Parameters
body (b2Body) - bodyReturns
aabb (b2d.aabb) - body boundsType: 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.
Parameters
body (b2Body) - bodydefinition (b2d.chain_definition) - the chain definitionReturns
chain (b2Chain) - created chain handlesegments (b2d.shape_info[]) - created 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 (b2d.fixture_definition) - fixture definitionReturns
fixture (b2d.fixture_info) - fixture informationExamples
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.
Parameters
body (b2Body) - bodydefinition (b2d.shape_create_definition) - the shape definition.Type: FUNCTION Destroy a fixture from a body.
Parameters
body (b2Body) - bodyfixture_index (integer) - 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 (integer) - 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 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 (b2d.contact_data[]) - array of contact tablesType: FUNCTION Get the fixtures attached to this body.
Parameters
body (b2Body) - bodyReturns
fixtures (b2d.fixture_info[]) - the attached fixturesType: 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 (b2Joint[]) - joint handles created by b2d.jointType: FUNCTION Get the joints attached to this body.
Parameters
body (b2Body) - bodyReturns
joints (b2Joint[]) - joint 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 (b2d.mass_data) - the mass dataType: 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 |
nil) - body name, or nil if no name is set |
Type: FUNCTION Get the next body in the world’s body list.
Parameters
body (b2Body) - bodyReturns
body (b2Body |
nil) - the next body, or nil if this is the last body |
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 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 (b2d.shape_info[]) - attached shapesType: 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 (b2d.transform) - the body transformType: 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 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 (b2d.mass_data) - the mass dataType: 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 An opaque handle to the Box2D physics world owned by a collection. Obtain the current collection’s world with b2d.get_world, or the world that owns a body with b2d.body.get_world. Pass the handle to functions in b2d.world; it cannot be constructed directly from Lua.
Parameters
value (userdata) - Box2D world handleExamples
local world = b2d.get_world()
if world then
print(b2d.world.get_gravity(world))
end