Defold Learn logo


Box2D b2World documentation

Query and cast functions for the Defold-owned Box2D v2 world.

Version: beta

FUNCTIONS
b2d.world.cast_mover() Cast a mover capsule.
b2d.world.cast_ray() Cast a ray.
b2d.world.cast_ray() Cast a ray and collect hits.
b2d.world.cast_ray_closest() Cast a ray and return the closest hit.
b2d.world.cast_ray_closest() Cast a ray and return the closest hit.
b2d.world.cast_shape() Cast a shape.
b2d.world.cast_shape() Cast a shape and collect hits.
b2d.world.collide_mover() Collide a mover capsule against the world.
b2d.world.enable_continuous() Enable or disable continuous collision.
b2d.world.enable_sleeping() Enable or disable world sleeping.
b2d.world.enable_speculative() Enable or disable speculative collision.
b2d.world.enable_warm_starting() Enable or disable warm starting.
b2d.world.explode() Apply an explosion impulse.
b2d.world.get_awake_body_count() Get the number of awake bodies.
b2d.world.get_counters() Get world counters.
b2d.world.get_gravity() Get world gravity.
b2d.world.get_hit_event_threshold() Get the hit event threshold.
b2d.world.get_maximum_linear_speed() Get the maximum linear speed.
b2d.world.get_profile() Get world profiling data.
b2d.world.get_restitution_threshold() Get the restitution threshold.
b2d.world.is_continuous_enabled() Get whether continuous collision is enabled.
b2d.world.is_locked() Check whether the world is locked.
b2d.world.is_sleeping_enabled() Get whether world sleeping is enabled.
b2d.world.is_valid() Check whether a world handle is valid.
b2d.world.is_warm_starting_enabled() Get whether warm starting is enabled.
b2d.world.overlap_aabb() Overlap an AABB.
b2d.world.overlap_aabb() Find shapes overlapping an AABB.
b2d.world.overlap_shape() Overlap a shape.
b2d.world.overlap_shape() Find shapes overlapping a shape proxy.
b2d.world.rebuild_static_tree() Rebuild the static broad-phase tree.
b2d.world.set_contact_tuning() Set contact solver tuning.
b2d.world.set_gravity() Set world gravity.
b2d.world.set_hit_event_threshold() Set the hit event threshold.
b2d.world.set_joint_tuning() Set joint solver tuning.
b2d.world.set_maximum_linear_speed() Set the maximum linear speed.
b2d.world.set_restitution_threshold() Set the restitution threshold.

Functions

b2d.world.cast_mover()

b2d.world.cast_mover(world,capsule,translation,filter)

The capsule table has center1, center2, and radius fields. The return value is the fraction of translation that can be traveled before collision, or 1 if there is no hit.

PARAMETERS

world b2World
world
capsule table
capsule table with center1, center2, and radius
translation vector3
capsule displacement
[filter] table
optional query filter with category_bits and mask_bits

RETURNS

fraction number
travel fraction before collision

b2d.world.cast_ray()

b2d.world.cast_ray(world,origin,translation,filter,max_results)

Cast a ray.

PARAMETERS

world b2World
world from b2d.get_world or b2d.body.get_world
origin vector3
world ray origin
translation vector3
world ray translation
filter table
optional query filter with category_bits, mask_bits, and optional group_index
max_results number
optional maximum result count

RETURNS

hits table
array of hit tables with fixture, shape, point, normal, and fraction
stats table
table with node_visits and leaf_visits

b2d.world.cast_ray()

b2d.world.cast_ray(world,origin,translation,filter,max_results)

The translation is the ray displacement from origin. Result order is not guaranteed by Box2D.

PARAMETERS

world b2World
world
origin vector3
ray start position
translation vector3
ray displacement
[filter] table
optional query filter with category_bits and mask_bits
[max_results] number
optional maximum result count. Omit or pass 0 for unlimited results.

RETURNS

hits table
array of cast hit tables
stats table
tree stats table

b2d.world.cast_ray_closest()

b2d.world.cast_ray_closest(world,origin,translation,filter)

Cast a ray and return the closest hit.

PARAMETERS

world b2World
world from b2d.get_world or b2d.body.get_world
origin vector3
world ray origin
translation vector3
world ray translation
filter table
optional query filter with category_bits, mask_bits, and optional group_index

RETURNS

hit table
hit table with fixture, shape, point, normal, fraction, node_visits, and leaf_visits, or nil

b2d.world.cast_ray_closest()

b2d.world.cast_ray_closest(world,origin,translation,filter)

The translation is the ray displacement from origin.

PARAMETERS

world b2World
world
origin vector3
ray start position
translation vector3
ray displacement
[filter] table
optional query filter with category_bits and mask_bits

RETURNS

hit table
nil
closest cast hit table with node_visits and leaf_visits, or nil on miss

b2d.world.cast_shape()

b2d.world.cast_shape(world,shape,translation,filter,max_results)

Uses Box2D v2 time-of-impact for fixture child shapes that support distance proxies. Grid fixture children are skipped.

PARAMETERS

world b2World
world from b2d.get_world or b2d.body.get_world
shape table
shape table using the same format as the shape field in b2d.body.create_fixture
translation vector3
world shape translation
filter table
optional query filter with category_bits, mask_bits, and optional group_index
max_results number
optional maximum result count

RETURNS

hits table
array of hit tables with fixture, shape, point, normal, and fraction
stats table
table with node_visits and leaf_visits

b2d.world.cast_shape()

b2d.world.cast_shape(world,shape,translation,filter,max_results)

The shape table uses the same circle, capsule, segment, polygon, and box formats as b2d.body.create_shape. The translation is the shape displacement.

PARAMETERS

world b2World
world
shape table
shape table
translation vector3
shape displacement
[filter] table
optional query filter with category_bits and mask_bits
[max_results] number
optional maximum result count. Omit or pass 0 for unlimited results.

RETURNS

hits table
array of cast hit tables
stats table
tree stats table

b2d.world.collide_mover()

b2d.world.collide_mover(world,capsule,filter,max_results)

The capsule table has center1, center2, and radius fields. Plane result tables include shape, normal, offset, and hit.

PARAMETERS

world b2World
world
capsule table
capsule table with center1, center2, and radius
[filter] table
optional query filter with category_bits and mask_bits
[max_results] number
optional maximum result count. Omit or pass 0 for unlimited results.

RETURNS

planes table
array of plane result tables

b2d.world.enable_continuous()

b2d.world.enable_continuous(world,enable)

Enable or disable continuous collision.

PARAMETERS

world b2World
world
enable boolean
true to enable continuous collision

b2d.world.enable_sleeping()

b2d.world.enable_sleeping(world,enable)

Enable or disable world sleeping.

PARAMETERS

world b2World
world
enable boolean
true to allow sleeping

b2d.world.enable_speculative()

b2d.world.enable_speculative(world,enable)

Enable or disable speculative collision.

PARAMETERS

world b2World
world
enable boolean
true to enable speculative collision

b2d.world.enable_warm_starting()

b2d.world.enable_warm_starting(world,enable)

Enable or disable warm starting.

PARAMETERS

world b2World
world
enable boolean
true to enable warm starting

b2d.world.explode()

b2d.world.explode(world,definition)

The definition table requires position, radius, falloff, and impulse_per_length. It may also include mask_bits.

PARAMETERS

world b2World
world
definition table
explosion definition

b2d.world.get_awake_body_count()

b2d.world.get_awake_body_count(world)

Get the number of awake bodies.

PARAMETERS

world b2World
world

RETURNS

count number
awake body count

b2d.world.get_counters()

b2d.world.get_counters(world)

The returned table contains body_count, shape_count, contact_count, joint_count, island_count, stack_used, static_tree_height, tree_height, byte_count, task_count, and color_counts.

PARAMETERS

world b2World
world

RETURNS

counters table
world counters

b2d.world.get_gravity()

b2d.world.get_gravity(world)

Get world gravity.

PARAMETERS

world b2World
world

RETURNS

gravity vector3
gravity vector

b2d.world.get_hit_event_threshold()

b2d.world.get_hit_event_threshold(world)

Get the hit event threshold.

PARAMETERS

world b2World
world

RETURNS

threshold number
hit event threshold in project units per second

b2d.world.get_maximum_linear_speed()

b2d.world.get_maximum_linear_speed(world)

Get the maximum linear speed.

PARAMETERS

world b2World
world

RETURNS

speed number
maximum linear speed in project units per second

b2d.world.get_profile()

b2d.world.get_profile(world)

The returned table contains Box2D timing fields including step, pairs, collide, solve, merge_islands, prepare_stages, solve_constraints, prepare_constraints, integrate_velocities, warm_start, solve_impulses, integrate_positions, relax_impulses, apply_restitution, store_impulses, split_islands, transforms, hit_events, refit, bullets, sleep_islands, and sensors.

PARAMETERS

world b2World
world

RETURNS

profile table
world profiling data

b2d.world.get_restitution_threshold()

b2d.world.get_restitution_threshold(world)

Get the restitution threshold.

PARAMETERS

world b2World
world

RETURNS

threshold number
restitution threshold in project units per second

b2d.world.is_continuous_enabled()

b2d.world.is_continuous_enabled(world)

Get whether continuous collision is enabled.

PARAMETERS

world b2World
world

RETURNS

enabled boolean
true if continuous collision is enabled

b2d.world.is_locked()

b2d.world.is_locked(world)

The world is locked during callbacks and some simulation phases. Functions marked as locked during callbacks cannot be called while this returns true.

PARAMETERS

world b2World
world

RETURNS

locked boolean
true if the world is locked

b2d.world.is_sleeping_enabled()

b2d.world.is_sleeping_enabled(world)

Get whether world sleeping is enabled.

PARAMETERS

world b2World
world

RETURNS

enabled boolean
true if sleeping is enabled

b2d.world.is_valid()

b2d.world.is_valid(world)

Check whether a world handle is valid.

PARAMETERS

world b2World
world

RETURNS

valid boolean
true if the world handle is valid

b2d.world.is_warm_starting_enabled()

b2d.world.is_warm_starting_enabled(world)

Get whether warm starting is enabled.

PARAMETERS

world b2World
world

RETURNS

enabled boolean
true if warm starting is enabled

b2d.world.overlap_aabb()

b2d.world.overlap_aabb(world,aabb,filter,max_results)

Overlap an AABB.

PARAMETERS

world b2World
world from b2d.get_world or b2d.body.get_world
aabb table
table with lower and upper vector3 fields
filter table
optional query filter with category_bits, mask_bits, and optional group_index
max_results number
optional maximum result count

RETURNS

fixtures table
array of fixture info tables
stats table
table with node_visits and leaf_visits

b2d.world.overlap_aabb()

b2d.world.overlap_aabb(world,aabb,filter,max_results)

The AABB table has lower and upper vector3 fields.

PARAMETERS

world b2World
world
aabb table
AABB table with lower and upper
[filter] table
optional query filter with category_bits and mask_bits
[max_results] number
optional maximum result count. Omit or pass 0 for unlimited results.

RETURNS

hits table
array of shape info tables
stats table
tree stats table

b2d.world.overlap_shape()

b2d.world.overlap_shape(world,shape,filter,max_results)

Overlap a shape.

PARAMETERS

world b2World
world from b2d.get_world or b2d.body.get_world
shape table
shape table using the same format as the shape field in b2d.body.create_fixture
filter table
optional query filter with category_bits, mask_bits, and optional group_index
max_results number
optional maximum result count

RETURNS

fixtures table
array of fixture info tables
stats table
table with node_visits and leaf_visits

b2d.world.overlap_shape()

b2d.world.overlap_shape(world,shape,filter,max_results)

The shape table uses the same circle, capsule, segment, polygon, and box formats as b2d.body.create_shape.

PARAMETERS

world b2World
world
shape table
shape table
[filter] table
optional query filter with category_bits and mask_bits
[max_results] number
optional maximum result count. Omit or pass 0 for unlimited results.

RETURNS

hits table
array of shape info tables
stats table
tree stats table

b2d.world.rebuild_static_tree()

b2d.world.rebuild_static_tree(world)

Rebuild the static broad-phase tree.

PARAMETERS

world b2World
world

b2d.world.set_contact_tuning()

b2d.world.set_contact_tuning(world,hertz,damping_ratio,pushout)

Set contact solver tuning.

PARAMETERS

world b2World
world
hertz number
contact stiffness frequency in hertz
damping_ratio number
contact damping ratio
pushout number
pushout velocity in project units per second

b2d.world.set_gravity()

b2d.world.set_gravity(world,gravity)

Set world gravity.

PARAMETERS

world b2World
world
gravity vector3
gravity vector

b2d.world.set_hit_event_threshold()

b2d.world.set_hit_event_threshold(world,threshold)

Set the hit event threshold.

PARAMETERS

world b2World
world
threshold number
hit event threshold in project units per second

b2d.world.set_joint_tuning()

b2d.world.set_joint_tuning(world,hertz,damping_ratio)

Set joint solver tuning.

PARAMETERS

world b2World
world
hertz number
joint stiffness frequency in hertz
damping_ratio number
joint damping ratio

b2d.world.set_maximum_linear_speed()

b2d.world.set_maximum_linear_speed(world,speed)

Set the maximum linear speed.

PARAMETERS

world b2World
world
speed number
maximum linear speed in project units per second

b2d.world.set_restitution_threshold()

b2d.world.set_restitution_threshold(world,threshold)

Collisions below this relative speed use inelastic collision response.

PARAMETERS

world b2World
world
threshold number
restitution threshold in project units per second