Defold Learn logo


Box2D b2Fixture documentation

Functions for interacting with fixtures attached to Box2D bodies. Fixtures are addressed functionally by (body, fixture_index) rather than persistent Lua handles.

Version: alpha

FUNCTIONS
b2d.fixture.get_aabb() Get fixture AABB for a child shape.
b2d.fixture.get_density() Get fixture density.
b2d.fixture.get_filter_data() Get fixture filter data for a child shape.
b2d.fixture.get_friction() Get fixture friction.
b2d.fixture.get_restitution() Get fixture restitution.
b2d.fixture.get_shape() Get the fixture shape as a functional shape table.
b2d.fixture.get_type() Get the fixture type.
b2d.fixture.is_sensor() Check if a fixture is a sensor.
b2d.fixture.refilter() Refilter a fixture.
b2d.fixture.set_density() Set fixture density.
b2d.fixture.set_filter_data() Set fixture filter data for a child shape.
b2d.fixture.set_friction() Set fixture friction.
b2d.fixture.set_restitution() Set fixture restitution.
b2d.fixture.set_sensor() Set sensor mode for a fixture.
b2d.fixture.set_shape() Set the fixture shape geometry.
b2d.fixture.test_point() Test a point against a fixture.

Functions

b2d.fixture.get_aabb()

b2d.fixture.get_aabb(body,fixture_index,child_index)

Get fixture AABB for a child shape.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures
child_index number
1-based child shape index

RETURNS

aabb table
table with lower and upper

b2d.fixture.get_density()

b2d.fixture.get_density(body,fixture_index)

Get fixture density.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures

RETURNS

density number
density in kg/m^2

b2d.fixture.get_filter_data()

b2d.fixture.get_filter_data(body,fixture_index,child_index)

Get fixture filter data for a child shape.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures
child_index number
1-based child shape index

RETURNS

filter table
table with category_bits, mask_bits, and group_index

b2d.fixture.get_friction()

b2d.fixture.get_friction(body,fixture_index)

Get fixture friction.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures

RETURNS

friction number

b2d.fixture.get_restitution()

b2d.fixture.get_restitution(body,fixture_index)

Get fixture restitution.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures

RETURNS

restitution number

b2d.fixture.get_shape()

b2d.fixture.get_shape(body,fixture_index)

Get the fixture shape as a functional shape table.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures

RETURNS

shape table
shape table with numeric type from b2d.shape.SHAPE_TYPE_*, suitable for reuse in b2d.body.create_fixture. Circle shapes use radius and center, edge shapes use v1, v2, optional v0, v3, polygon shapes use vertices, and chain shapes use vertices, loop, optional prev_vertex, and next_vertex. Any angle values are in radians.

b2d.fixture.get_type()

b2d.fixture.get_type(body,fixture_index)

Get the fixture type.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures

RETURNS

type number

b2d.fixture.is_sensor()

b2d.fixture.is_sensor(body,fixture_index)

Check if a fixture is a sensor.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures

RETURNS

enabled boolean

b2d.fixture.refilter()

b2d.fixture.refilter(body,fixture_index,touch_proxies)

Refilter a fixture.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures
touch_proxies boolean
if true, touch broad-phase proxies

b2d.fixture.set_density()

b2d.fixture.set_density(body,fixture_index,density,update_mass)

Set fixture density.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures
density number
density in kg/m^2
update_mass boolean
if true, reset body mass data after the change

b2d.fixture.set_filter_data()

b2d.fixture.set_filter_data(body,fixture_index,child_index,filter)

Set fixture filter data for a child shape.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures
child_index number
1-based child shape index
filter table
table with category_bits, mask_bits, and group_index

b2d.fixture.set_friction()

b2d.fixture.set_friction(body,fixture_index,friction)

Set fixture friction.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures
friction number

b2d.fixture.set_restitution()

b2d.fixture.set_restitution(body,fixture_index,restitution)

Set fixture restitution.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures
restitution number

b2d.fixture.set_sensor()

b2d.fixture.set_sensor(body,fixture_index,enabled)

Set sensor mode for a fixture.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures
enabled boolean

b2d.fixture.set_shape()

b2d.fixture.set_shape(body,fixture_index,shape,update_mass)

This updates the existing Box2D v2 shape using the same table format as b2d.body.create_fixture and b2d.fixture.get_shape. The shape type must match the current fixture shape type. Polygon updates must keep the same vertex count. Chain shape geometry cannot be updated in-place. The body mass is not updated unless update_mass is true.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures
shape table
shape table with numeric type from b2d.shape.SHAPE_TYPE_*
update_mass boolean
if true, reset body mass data after the change

EXAMPLES

local body = b2d.get_body("#collisionobject")

-- Move a circle shape relative to the body origin.
local circle = b2d.fixture.get_shape(body, 1)
circle.center = vmath.vector3(24, 0, 0)
b2d.fixture.set_shape(body, 1, circle, true)

-- Replace an edge shape's local endpoints.
b2d.fixture.set_shape(body, 2, {
    type = b2d.shape.SHAPE_TYPE_EDGE,
    v1 = vmath.vector3(-32, 0, 0),
    v2 = vmath.vector3( 32, 0, 0),
})

-- Update a box shape using the polygon box convenience format.
-- The existing polygon must already have four vertices.
b2d.fixture.set_shape(body, 3, {
    type = b2d.shape.SHAPE_TYPE_BOX,
    hx = 16,
    hy = 8,
    center = vmath.vector3(0, 20, 0),
    angle = math.rad(30),
}, true)

b2d.fixture.test_point()

b2d.fixture.test_point(body,fixture_index,point)

Test a point against a fixture.

PARAMETERS

body b2Body
body
fixture_index number
1-based fixture index from b2d.body.get_fixtures
point vector3
point in world coordinates

RETURNS

hit boolean