Namespace: bullet3d.world
Language: Lua
Type: Defold Lua
File: script_bullet3d_world.cpp
Source: engine/gamesys/src/gamesys/scripts/bullet3d/script_bullet3d_world.cpp
Read and tune the Bullet dynamics world owned by the current collection. Defold remains responsible for world lifetime, stepping, collision objects, callbacks, and debug drawing.
World and collision-object values returned by this API are borrowed, generational handles. They become invalid when their collection or owning game object is deleted and must not be retained as native pointers.
All positions, distances, translations, dimensions and contact distances use
Defold world units. The binding converts them using physics.scale. Rotations
and unit normals are not scaled. Query functions refresh Bullet broadphase
AABBs before execution, so collision-object transform changes are visible.
Query filters are optional tables with these fields:
category_bits65535mask_bits65535include_triggerstrueignore| [type:btCollisionObject | btCollisionObject[]] one collision-object handle or an array of handles to exclude |
report_initial_overlapsfalsereport_initial_overlaps is a bullet3d.world query option only. It does
not change physics.raycast() or either Box2D backend.
Category and mask checks are reciprocal: the query category must match the object’s mask and the object’s category must match the query mask.
Temporary query shapes use the same geometry fields as
[ref:bullet3d.shape.get_shape]:
a sphere has type and diameter, a box has type and dimensions, a
Y-axis capsule has type, diameter and height, and a convex hull has
type and a vertices array with at least four vector3 values. The type
is one of the bullet3d.shape.SHAPE_TYPE_* constants. Every shape can specify
position and rotation; their defaults are zero and the identity rotation.
Cast shapes can also specify target_rotation, which defaults to rotation.
Capsule height is the length of the cylindrical middle section; total
end-to-end height is height + diameter. Query sizes are always expressed
in Defold world units. Except for triangle meshes, a table returned by
bullet3d.shape.get_shape can be reused directly after adding the desired
query transform fields. Queries accept only sphere, box, capsule, and hull.
Hull vertices describe a convex hull; concave input is convexified by Bullet.
All query vectors and scalar sizes must be finite. Diameters, dimensions and
capsule heights must be greater than zero; hulls require at least four finite
vertices. Cast translations must be finite and non-zero. Query rotations
must be finite, non-zero quaternions and are normalized by the binding. AABB
lower bounds must not exceed their corresponding upper bounds.
Overlap and enumeration results are arrays of btCollisionObject handles.
Cast results are tables containing object, point, normal, fraction,
initial_overlap, and inside. shape_index is present when Bullet reports
a compound child and is one-based. Cast arrays are sorted by ascending
fraction. fraction is in [0, 1] along the supplied translation. For native
hits, normal is the hit object’s outward unit surface normal; synthesized
initial-overlap hits use a zero normal. inside is true for a synthesized
ray-origin hit when Bullet reports signed contact distance less than or equal
to zero. It denotes initial contact or penetration rather than strict
geometric containment, and exact-surface cases follow Bullet’s contact
tolerance. Shape-cast initial overlaps set only initial_overlap.
Contact results contain object_a, object_b, position_a, position_b,
normal_on_b, and signed distance. Positions are points on their named
objects, and normal_on_b points from object B toward object A. A negative
distance is penetration and a small positive distance is Bullet’s contact
margin. Object order is always normalized to the order supplied by the caller.
max_results is optional. Zero or omission means unlimited results. A
negative value is an error. Broadphase overlaps, native world enumeration,
contacts, and equal-fraction cast hits have unspecified order. A capped query
can therefore return a different equal-priority subset after world changes.
Synchronous queries execute immediately and do not advance simulation.
Async casts are deferred until after the next physics step. They execute on
the main thread rather than a worker thread, and all queued casts for one
world share one broadphase AABB refresh. Every cast in the batch completes
before any callback runs, so callback mutations cannot affect other query
computations in that batch. Deferral avoids blocking the Lua call site but
does not remove the cast work from the frame.
Native fraction-zero cast callbacks are suppressed. Starting overlaps are
omitted by default, or reported through the exact, deduplicated synthesis
enabled by report_initial_overlaps; this avoids direction-dependent Bullet
results for casts that start touching or penetrating another object.
Type: STRUCT Bullet world axis-aligned bounding box
Members
lower (vector3) - lower world-space bound in Defold unitsupper (vector3) - upper world-space bound in Defold unitsType: FUNCTION Casts immediately from origin to origin + translation and returns all matching hits sorted by fraction. Translation must be non-zero. Bullet’s convex ray test normally does not report a ray whose start and end are both inside the same convex hull. Set filter.report_initial_overlaps = true to perform an exact point-overlap test at the origin and synthesize one deduplicated hit per initially touching or overlapping object with fraction = 0, zero normal, point = origin, initial_overlap = true, and inside = true. The point is the query origin, not a surface contact. This explicitly supports the inside-hull behavior requested by issue #5348. Fraction-zero native callbacks and starting overlaps are suppressed when the option is false.
Parameters
world (btDiscreteDynamicsWorld) - world handleorigin (vector3) - ray origin in world spacetranslation (vector3) - non-zero ray displacement in world unitsfilter (bullet3d.world.query_filter) (optional) - query filtermax_results (integer) (optional) - maximum sorted hits, or zero for allReturns
hits (bullet3d.world.cast_result[]) - cast results sorted by ascending fractionType: FUNCTION Queues the same ray query as bullet3d.world.cast_ray and returns without executing it. After the next physics step, callback(self, hits) receives the cast-result array sorted by fraction. The query observes post-step world state. It is deferred on the main thread, not executed concurrently; use it to move work out of the current Lua call and to query the stepped state, not as a guarantee of lower total CPU time. Queued casts for the same world share one broadphase AABB refresh and all finish before their callbacks begin.
Parameters
world (btDiscreteDynamicsWorld) - world handleorigin (vector3) - ray origin in world spacetranslation (vector3) - non-zero ray displacement in world unitscallback (fun(self:script_instance, hits:bullet3d.world.cast_result[])) - function called as callback(self, hits)filter (bullet3d.world.query_filter) (optional) - query filtermax_results (integer) (optional) - maximum sorted hits, or zero for allExamples
Queue a downward cast and inspect only the closest non-trigger hit:
bullet3d.world.cast_ray_async(
bullet3d.get_world(),
go.get_world_position(),
vmath.vector3(0, -100, 0),
function(self, hits)
if hits[1] then
print("hit", hits[1].object)
end
end,
{ include_triggers = false },
1)
Type: FUNCTION Equivalent to bullet3d.world.cast_ray with one result, but returns the hit table directly or nil on a miss.
Parameters
world (btDiscreteDynamicsWorld) - world handleorigin (vector3) - ray origin in world spacetranslation (vector3) - non-zero ray displacement in world unitsfilter (bullet3d.world.query_filter) (optional) - query filterReturns
hit (bullet3d.world.cast_result |
nil) - closest cast result, or nil on a miss |
Examples
Cast downward and report the closest non-trigger hit:
function init(self)
local world = bullet3d.get_world()
local origin = go.get_world_position()
local translation = vmath.vector3(0, -100, 0)
local filter = { include_triggers = false }
local hit = bullet3d.world.cast_ray_closest(
world, origin, translation, filter)
if hit then
local distance = vmath.length(translation) * hit.fraction
print("hit", hit.object, "after", distance, "units")
end
end
Type: STRUCT Bullet world cast result
Members
object (btCollisionObject) - hit collision objectpoint (vector3) - hit point in world space and Defold unitsnormal (vector3) - outward unit surface normalfraction (number) - fraction along the supplied translation in [0, 1]shape_index? (integer) - one-based compound child indexinitial_overlap (boolean) - whether the hit was synthesized from an initial overlapinside (boolean) - whether a synthesized ray-origin hit starts inside the objectType: FUNCTION Sweeps the temporary shape from shape.position by translation, while interpolating from shape.rotation to shape.target_rotation. Translation must be non-zero. The query executes immediately and returns all matching hits sorted by fraction. Bullet’s convex sweep supports only convex query shapes. When filter.report_initial_overlaps is true, an exact contact test at the starting transform synthesizes one deduplicated hit per overlapping object with fraction = 0, point = shape.position, zero normal, initial_overlap = true, and inside = false. The point is the query-shape origin, not a surface contact, and the result does not report penetration depth.
Parameters
world (btDiscreteDynamicsWorld) - world handleshape (bullet3d.shape.definition) - convex query shape with optional target rotationtranslation (vector3) - non-zero sweep displacement in world unitsfilter (bullet3d.world.query_filter) (optional) - query filtermax_results (integer) (optional) - maximum sorted hits, or zero for allReturns
hits (bullet3d.world.cast_result[]) - cast results sorted by ascending fractionType: FUNCTION Queues the same convex sweep as bullet3d.world.cast_shape and returns without executing it. After the next physics step, callback(self, hits) receives the sorted cast-result array from the post-step world state. The operation is deferred on the main thread rather than run concurrently. All queued casts for one world share one broadphase AABB refresh and all finish before their callbacks begin.
Parameters
world (btDiscreteDynamicsWorld) - world handleshape (bullet3d.shape.definition) - convex query shape with optional target rotationtranslation (vector3) - non-zero sweep displacement in world unitscallback (fun(self:script_instance, hits:bullet3d.world.cast_result[])) - function called as callback(self, hits)filter (bullet3d.world.query_filter) (optional) - query filtermax_results (integer) (optional) - maximum sorted hits, or zero for allType: FUNCTION Equivalent to bullet3d.world.cast_shape with one result, but returns the hit table directly or nil on a miss.
Parameters
world (btDiscreteDynamicsWorld) - world handleshape (bullet3d.shape.definition) - convex query shape with optional target rotationtranslation (vector3) - non-zero sweep displacement in world unitsfilter (bullet3d.world.query_filter) (optional) - query filterReturns
hit (bullet3d.world.cast_result |
nil) - closest cast result, or nil on a miss |
Type: FUNCTION Runs Bullet’s discrete pair contact algorithm without changing the simulation. Both borrowed handles must belong to world and must identify different objects. The output preserves the caller’s A/B order even when Bullet’s internal manifold order is reversed. Collision filters are not applied to an explicitly selected pair.
Parameters
world (btDiscreteDynamicsWorld) - world handleobject_a (btCollisionObject) - first collision object in the worldobject_b (btCollisionObject) - different second collision object in the worldmax_results (integer) (optional) - maximum number of contact points, or zero for allReturns
contacts (bullet3d.world.contact_result[]) - normalized contact resultsType: STRUCT Bullet world contact result
Members
object_a (btCollisionObject) - first collision objectobject_b (btCollisionObject) - second collision objectposition_a (vector3) - contact point on object A in world space and Defold unitsposition_b (vector3) - contact point on object B in world space and Defold unitsnormal_on_b (vector3) - unit normal pointing from object B toward object Adistance (number) - signed contact distance in Defold unitsType: FUNCTION Runs Bullet’s discrete contact test between object and matching objects in the same world. The supplied object is always object_a in returned contacts. The borrowed collision-object handle must belong to world. Bullet may return several contact points for one object pair and may include small positive contact-margin distances.
Parameters
world (btDiscreteDynamicsWorld) - world handleobject (btCollisionObject) - collision object belonging to the worldfilter (bullet3d.world.query_filter) (optional) - filter applied to candidate object_b valuesmax_results (integer) (optional) - maximum number of contact points, or zero for allReturns
contacts (bullet3d.world.contact_result[]) - normalized contact resultsExamples
Inspect current contacts for this collision object:
function update(self, dt)
local world = bullet3d.get_world()
local object = bullet3d.get_collision_object("#collisionobject")
local filter = { include_triggers = false }
local contacts = bullet3d.world.contact_test(world, object, filter)
for _, contact in ipairs(contacts) do
if contact.distance < 0 then
print("penetration", -contact.distance, "against", contact.object_b)
end
end
end
Type: FUNCTION Get the number of collision objects in the world
Parameters
world (btDiscreteDynamicsWorld) - world handleReturns
count (integer) - number of collision objectsType: FUNCTION Returns the Defold-owned collision objects currently registered in the world. Internal or unmanaged Bullet objects without Defold ownership metadata are not exposed.
Parameters
world (btDiscreteDynamicsWorld) - world handlemax_results (integer) (optional) - maximum number of results, or zero for allReturns
objects (btCollisionObject[]) - array of collision-object handlesType: FUNCTION Get world gravity
Parameters
world (btDiscreteDynamicsWorld) - world handleReturns
gravity (vector3) - gravity in Defold units per second squaredType: FUNCTION Test whether a world handle is valid
Parameters
world (btDiscreteDynamicsWorld) - world handleReturns
valid (boolean) - true if the native world still existsType: FUNCTION Finds collision objects whose Bullet broadphase bounds overlap the supplied world-space AABB. This is intentionally a broadphase query and can include objects whose actual collision geometry does not intersect the box. Use bullet3d.world.overlap_point or bullet3d.world.overlap_shape for exact narrow-phase overlap tests.
Parameters
world (btDiscreteDynamicsWorld) - world handleaabb (bullet3d.world.aabb) - world-space boundsfilter (bullet3d.world.query_filter) (optional) - query filtermax_results (integer) (optional) - maximum number of results, or zero for allReturns
objects (btCollisionObject[]) - array of overlapping collision-object handlesType: FUNCTION Performs an exact narrow-phase test using a temporary zero-radius Bullet sphere at the world-space point. A result is returned only for a contact with signed distance less than or equal to zero, so broadphase-only false positives are removed. Results on an exact surface follow Bullet’s contact tolerance.
Parameters
world (btDiscreteDynamicsWorld) - world handlepoint (vector3) - point in world spacefilter (bullet3d.world.query_filter) (optional) - query filtermax_results (integer) (optional) - maximum number of results, or zero for allReturns
objects (btCollisionObject[]) - array of overlapping collision-object handlesType: FUNCTION Performs an exact Bullet contact test for a temporary sphere, box, Y-axis capsule, or convex hull. Multiple native contact points for the same target object are deduplicated in the returned overlap array.
Parameters
world (btDiscreteDynamicsWorld) - world handleshape (bullet3d.shape.definition) - convex query shapefilter (bullet3d.world.query_filter) (optional) - query filtermax_results (integer) (optional) - maximum number of results, or zero for allReturns
objects (btCollisionObject[]) - array of overlapping collision-object handlesExamples
Find non-trigger objects overlapping a two-unit sphere around this game object:
function init(self)
local world = bullet3d.get_world()
local shape = {
type = bullet3d.shape.SHAPE_TYPE_SPHERE,
diameter = 2,
position = go.get_world_position(),
}
local filter = { include_triggers = false }
local overlaps = bullet3d.world.overlap_shape(world, shape, filter)
for _, object in ipairs(overlaps) do
print("overlap", object)
end
end
Type: STRUCT Bullet world query filter
Members
category_bits? (integer) - unsigned 16-bit category bits; defaults to 65535mask_bits? (integer) - unsigned 16-bit mask bits; defaults to 65535include_triggers? (boolean) - whether to include objects without contact response; defaults to trueignore? (btCollisionObject |
btCollisionObject[]) - one collision object or an array of collision objects to exclude |
report_initial_overlaps? (boolean) - whether casts synthesize fraction-zero hits for initial overlaps; defaults to falseType: FUNCTION Bullet propagates the new value to active dynamic bodies unless they have bullet3d.rigid_body.BT_DISABLE_WORLD_GRAVITY set. Such bodies retain their custom body gravity.
Parameters
world (btDiscreteDynamicsWorld) - world handlegravity (vector3) - finite gravity in Defold units per second squaredExamples
Set gravity for the current collection’s physics world:
function init(self)
local world = bullet3d.get_world()
if world then
bullet3d.world.set_gravity(world, vmath.vector3(0, -9.81, 0))
end
end