Physics
Based on game build 714014 | Last updated: 2026-03-10
Overview
The physics.lua file provides low-level utilities for handling entity destruction, physics-based launching, and collision callbacks. It is not a traditional component added to entities, but rather a shared utility module containing helper functions (Launch, DestroyEntity, LaunchAndClearArea, etc.) and supporting classes (CollisionMaskBatcher) used across the codebase. It integrates with several components (workable, pickable, health, combat, inventoryitem, mine, locomotor) to determine how entities react when interacted with or destroyed by physics or player actions.
Usage example
-- Launch an entity away from a launcher
Launch(inst, launcher, 8)
-- Launch an entity with more precise control
Launch2(inst, launcher, 6, 2, 0.5, 1.0, 12, nil)
-- Destroy an entity based on its components and tags
DestroyEntity(tree, player, false, true)
-- Clear a radius and launch items up
LaunchAndClearArea(spawner, 5, 5, 1, 1, 0)
Dependencies & tags
Components used: workable, pickable, health, combat, inventoryitem, mine, locomotor, Transform, Physics
Tags added: None
Tags checked: no_collision_callback_for_other, stump, intense, _inventoryitem, locomotor, INLIMBO, and dynamic *_workable/NPC_workable tags
Properties
No public properties are defined in this module. It contains only functions and a helper class.
Main functions
OnPhysicsCollision(...)
- Description: Internal callback invoked by the engine when two physics-enabled entities collide. Routes to registered per-entity collision callbacks and respects the
no_collision_callback_for_othertag. - Parameters:
guid1,guid2(number) — Entity GUIDs.world_position_on_a_*,world_position_on_b_*(number) — World positions of collision on each entity.world_normal_on_b_*(number) — Surface normal on entity B.lifetime_in_frames(number) — Frames since collision began.
- Returns: Nothing.
- Error states: No-op if either entity is invalid or if the respective callback is
nil.
CollisionMaskBatcher(entormask)
- Description: Helper class for batching collision mask changes before applying them to an entity, minimizing calls to the underlying C++ physics layer.
- Parameters:
entormask— An entity instance or a raw collision mask bitmask. - Properties:
Property Type Default Description masknumber 0or entity's current maskThe accumulated bitmask for collision testing. - Methods:
ClearCollisionMask()— Resetsmaskto0.SetCollisionMask(...)— Setsmaskto the bitwise OR of provided masks.CollidesWith(mask)— Addsmasktomask(logical OR).ClearCollidesWith(mask)— Removesmaskfrommask(logical AND NOT).CommitTo(ent)— Writesmasktoent.Physics:SetCollisionMask.
Launch(inst, launcher, basespeed)
- Description: Launches
instaway fromlauncherat an angle with slight randomness, using a fixed vertical impulse (10). - Parameters:
inst(Entity) — Entity to launch.launcher(Entity) — Source of launch direction.basespeed(number, optional) — Base horizontal speed; defaults to5.
- Returns: Nothing.
- Error states: No-op if
inst.Physicsis inactive ornil, or iflauncherisnil.
Launch2(inst, launcher, basespeed, speedmult, startheight, startradius, vertical_speed, force_angle)
- Description: Launches
instwith full control over initial position, angle, and velocity. - Parameters:
inst(Entity) — Entity to launch.launcher(Entity) — Reference point for launch origin.basespeed(number) — Base speed multiplier.speedmult(number) — Random speed variance added.startheight(number) — Launch height above launcher.startradius(number) — Horizontal offset radius from launcher.vertical_speed(number, optional) — Initial vertical velocity.force_angle(number, optional) — Fixed angle in degrees; ifnil, direction is computed from relative positions.
- Returns:
number— The computed launch angle (in radians), or0on failure. - Error states: Returns
0ifinstorlauncheris invalid or physics inactive.
LaunchAt(inst, launcher, target, speedmult, startheight, startradius, randomangleoffset)
- Description: Launches
insttowardtarget(or away from camera direction iftargetisnil) with angular randomness. - Parameters:
inst(Entity) — Entity to launch.launcher(Entity) — Origin point.target(Entity, optional) — Target to face or aim near.speedmult(number, optional) — Speed multiplier; defaults to1.startheight,startradius— Launch offset parameters.randomangleoffset(number, optional) — Angular randomness in degrees; defaults to30.
- Returns: Nothing.
- Error states: No-op if
inst.Physicsis inactive orlauncherisnil.
LaunchToXZ(inst, tox, toz)
- Description: Launches
instdirectly toward(tox, toz)on the ground plane using physics-based speed tuned to friction. - Parameters:
inst(Entity) — Entity to launch.tox,toz(number) — Target world X and Z coordinates.
- Returns: Nothing.
- Error states: Teleports to height
.1and sets vertical velocity2iftox, tozequals current position.
DestroyEntity(ent, destroyer, kill_all_creatures, remove_entity_as_fallback)
- Description: Handles entity destruction logic based on component state and tags. Respects workables, pickables, health, and combat.
- Parameters:
ent(Entity) — Entity to destroy.destroyer(Entity) — Entity performing the destruction.kill_all_creatures(boolean) — If true, kills health-containing entities not otherwise handled.remove_entity_as_fallback(boolean) — If true, removes the entity if no other action is taken.
- Returns: Nothing.
- Error states: Skips processing if
entis invalid or if a proxy entity exists (and delegates to it).
LaunchAndClearArea(inst, radius, launch_basespeed, launch_speedmult, launch_startheight, launch_startradius)
- Description: Destroys or launches all entities within a radius of
inst, respecting different tags and components. - Parameters:
inst(Entity) — Center of area effect.radius(number) — Search radius.launch_basespeed,launch_speedmult,launch_startheight,launch_startradius— Parameters passed toLaunch2.
- Returns: Nothing.
- Behavior:
- Destroys collapsible entities (
*_workable,_combat,pickable) viaDestroyEntity. - Deactivates
minecomponents, then launches non-nobounceinventory items viaLaunch2.
- Destroys collapsible entities (
Events & listeners
- Listens to: None (this module does not register listeners directly).
- Pushes: None (this module does not fire events directly).