physics-scene
Garnet Physics Scene
ECS integration for garnet-physics. Install the plugin, describe bodies with components, and read contact events.
require "garnet-physics-scene"
alias P = Garnet::Physics
alias PS = P::Scene
world = Garnet::World.new(fixed_seconds: 1_f32 / 60)
world.add_plugin(PS::Plugin.new(units_per_meter: 32))
crate = world.spawn(
PS::BodyDescription.new(:dynamic, P::Vec2.new(2_f32, 0_f32), [P::BoxShape.new(1, 1).as(P::Shape)]),
Garnet::Graphics::Components::Sprite.new(region),
parent: level)
world.schedule.add(:update, name: "game.impacts") do |w, _|
w.events(PS::ContactBegan) do |hit|
play_thud if hit.involves?(crate)
end
end
What the plugin does
| Piece | Behaviour |
|---|---|
BodyDescription added |
Creates a body (observer) and adds a BodyBinding the plugin owns. |
BodyDescription replaced with a different value |
Recreates the body on the next fixed step. |
BodyDescription removed or entity destroyed |
Removes the body. |
BodyBinding added by the game |
Borrows an existing body; the game keeps ownership. |
KinematicTarget |
Drives a kinematic body toward a parent-local position in scene units. |
Teleport, SetVelocity |
One-shot commands in parent-local scene units: move the body (and Pose) or set its linear velocity, then removed. |
Space on an entity |
Its subtree simulates in a separate world (per room or level); inactive spaces are not stepped. |
"physics.step" system, FixedUpdate |
Applies targets, steps once, writes poses, emits events. |
Physics::World resource |
world.resource(P::World) for raycasts, shapecasts and overlaps. |
Each bound entity receives Garnet::Scene::Components::Pose, the scene slot reserved for simulation bridges. Local composition is Placement * Pose * Socket * Affine * Transform, so an authored Transform stays a visual offset and Spine sockets (which write Socket) never collide with physics. Poses are parent-local scene units (meters * units_per_meter): keep physics entities under a stationary parent or at the root.
Events are emitted after each fixed step:
ContactBegan,ContactEndedwithfirst,secondandcontact;TriggerEntered,TriggerExitedwithsensor,visitorandcontact.
Endpoints are Entity?: a body with no entity (a tile collider, for example) is nil. Each event has involves?(entity) and other(entity). End events can name an entity destroyed during the previous step.
Move bodies and set velocities with command components instead of the Physics::World resource:
puck << PS::Teleport.new(Vec2.new(320, 128), angle: 0) # angle: nil keeps the current angle
puck << PS::SetVelocity.new(Vec2.new(64, -256)) # scene units per second
A command applies as soon as it is added to an entity with a body, so the entity's Pose (and a position read later in the frame) already reflects the teleport. Commands added before the body exists are applied by the next "physics.step" before kinematic targets. Either way the plugin removes the component afterwards.
Map query results back to entities with plugin.entity?(hit.body); get the plugin with world.plugin(PS::Plugin). plugin.body?(entity) returns the bound body.
Several worlds
Rooms or levels that reuse the same coordinates need separate collision. Put a Space on each subtree root:
room = world.spawn(PS::Space.new) # or Space.new(gravity: ..., active: false)
world.spawn(wall_description, parent: room) # created in the room's world
plugin.physics(room) # that world, for queries
room.mut(PS::Space).active = false # park it: not stepped
Each space's world is created on first use with the plugin's timestep and gravity (or the space's own gravity). "physics.step" advances the default world and every active space, writes poses and emits events from all of them. Bodies don't move between spaces: add the Space before or with the subtree's bodies, and replace a description to recreate a body after reparenting. Removing the Space or destroying its root removes its bodies and closes the world.
A Space was chosen over collision layers: layers would keep every room in one broadphase, spend a 32-bit category budget, and need masks threaded through every raycast and character move, while a separate world isolates a room completely and costs nothing while parked.
Without an argument the plugin creates a Physics::World using the schedule's fixed step, gravity: and substeps:, and closes it with the plugin. Pass an existing world to borrow it (its timestep must equal the schedule's). Removing the plugin removes the bodies it created.
A world the plugin creates runs Box2D's internal work on world.jobs when that pool is parallel (builds with -Dpreview_mt -Dexecution_context); the results are identical either way. Install a custom pool with JobsPlugin before this plugin, or pass parallel: false to step on the World thread only. A borrowed world keeps whatever jobs: it was created with.
Tile colliders
TileColliders.new(physics, map, units_per_meter: 16) installs static boxes for a tile map's solid cells and removes them on close.
Earlier APIs
Simulation (one container, explicit update/bind) still works and now writes Pose. Scene.sync(container, frame) is deprecated: it keeps writing the legacy Affine slot so existing code that seeds Affine keeps its behaviour. Migrate to the plugin, which writes Pose and leaves Affine free for imported transforms.
After Simulation#close (see closed?), update and bind raise Physics::Error (a Garnet::Error) rather than ArgumentError.
physics-scene
- 0
- 0
- 0
- 1
- 2
- 2 days ago
- November 30, 2026
MIT License
Thu, 03 Dec 2026 02:11:03 GMT