physics3d-scene

Garnet Physics 3D Scene

ECS integration for garnet-physics3d. It mirrors the 2D garnet-physics-scene plugin.

require "garnet-physics3d-scene"

alias P = Garnet::Physics3D
alias S = P::Scene
alias V = P::Vector

world = Garnet::World.new(fixed_seconds: 1_f32 / 60)
world.add_plugin(S::Plugin.new(units_per_meter: 1))

ball = world.spawn(
  S::BodyDescription.new(:dynamic, V.new(0_f32, 3_f32, 0_f32),
    [P::ColliderPart.new(:sphere, V.new(0.5_f32, 0_f32, 0_f32))]),
  model_drawables)

world.schedule.add(:update, name: "game.landing") do |w, _|
  w.events(S::ContactBegan) { |hit| thud if hit.involves?(ball) }
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 Submits a parent-local translation in scene units before each step. Pass an orientation as its second argument to drive the full pose; without one the solver keeps controlling the body's rotation.
Teleport, SetVelocity One-shot commands in parent-local scene units, applied when added (or at the next step if the body doesn't exist yet), then removed.
Space on an entity Its subtree simulates in a separate world (per room or level); inactive spaces are not stepped.
"physics3d.step" system, FixedUpdate Applies targets, steps once, writes poses, emits events.
Physics3D::World resource world.resource(P::World) for raycasts, spherecasts and character movement.
Collider (with optional RigidBody) A body that follows the entity; see "Colliders on entities".
"physics3d.colliders" system, Update order -1000 Moves static and kinematic collider bodies to their entities and rebuilds changed ones.

Each bound entity receives Garnet::Scene::Components::Pose3D. Local 3D composition is Pose3D * Transform3D, so the game's Transform3D is a visual offset from the simulated body and is never overwritten. Poses are parent-local scene units: keep physics entities under a stationary parent or at the root.

Teleport moves the body in place with Physics3D::World#teleport, for owned and borrowed bodies alike: the handle, rotation and velocity are kept.

After each fixed step the plugin emits ContactBegan, ContactEnded (first, second, contact) and TriggerEntered, TriggerExited (sensor, visitor, contact). Endpoints are Entity?; unbound bodies are nil. Use plugin.entity?(hit.body) to map query hits to entities.

Colliders on entities

Collider is the authored collision shape: box, sphere, capsule, cylinder, cone, convex hull or triangle mesh, with an offset, sensor?, layer category and mask, a free-form tag and a material. It is the engine's one collision component: simulated bodies, triggers, raycasts and the movement queries of characters, cameras and agents all see it.

world.spawn(Transform3D.new(position: V.new(4, 0, 0)), S::Collider.box(1, 2, 1))          # static wall
world.spawn(Transform3D.new, S::Collider.capsule(0.4, 1.8), S::RigidBody.new(:kinematic)) # moved by the game
world.spawn(Transform3D.new(position: V.new(0, 5, 0)), S::Collider.sphere(0.3), S::RigidBody.new(:dynamic))
Body Behaviour
No RigidBody Static. Created at the entity's world transform; moved (teleported) when the entity moves.
RigidBody.new(:kinematic) Driven toward the entity's transform each step, pushing dynamic bodies.
RigidBody.new(:dynamic) Simulated. The plugin writes Pose3D so the entity's world transform matches the body; Teleport and SetVelocity apply.

Sizes are in the entity's local scene units and scale with its world transform. Changing the Collider or RigidBody, or the entity's scale, rebuilds the body. Bodies are visible to queries as soon as they exist. A sensor reports TriggerEntered/TriggerExited with the sensor entity. An entity takes a Collider or a BodyDescription, not both.

The plugin also installs Obstacles as the World's Garnet::Scene::Obstacles3D, so Scene::CollisionWorld3D (used by CharacterController3DPlugin, CameraFollow3DPlugin and the garnet-behaviors agents) blocks movers with the physics colliders. Those queries are vertical columns (Physics3D::World#resolve_column) and ignore height by default; with a CollisionWorld3D#step_clearance, colliders below a mover's feet plus that clearance don't block it. Foliage avoids Collider footprints.

Scene documents

require "garnet-physics3d-scene/scene_data" (it needs garnet-scene-data) registers garnet.collider3d (version 2) and garnet.rigid_body3d. When it's loaded, SceneData::SchemaPlugin and the editor register them, and Garnet::DefaultPlugins installs Plugin. SchemaPlugin registers them in worlds without garnet-engine. Version 1 garnet.collider3d values, from the former Scene::Components::Collider3D, load unchanged: solid: false becomes sensor: true.

Several worlds

Rooms or levels that reuse the same coordinates need separate collision. Put a Space on each subtree root:

room = world.spawn(S::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(S::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). "physics3d.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.

Plugin.new(simulate: false) keeps bodies in step with their entities but never advances the worlds or writes poses. Editor previews of authored scenes use it, so queries and footprints work while nothing falls.

Without an argument the plugin creates a world with the schedule's fixed step and gravity: and closes it with the plugin. Pass an existing world to borrow it; its timestep must equal the schedule's.

Earlier APIs

Simulation (one container, explicit update/bind) and Scene.sync(container, world_or_frame) still work. Both now write Pose3D instead of overwriting Transform3D; read node[Pose3D] where code used to read the generated Transform3D. A Simulation also exposes impacts: the last update's contacts for bodies given a World#impact_threshold, with their first and second entities. Contact points and normals stay in physics meters.

After Simulation#close (see closed?), update and bind raise Physics3D::Error (a Garnet::Error) rather than ArgumentError.

Repository

physics3d-scene

Owner
Statistic
  • 0
  • 0
  • 0
  • 1
  • 3
  • about 1 hour ago
  • November 30, 2026
License

MIT License

Links
Synced at

Fri, 04 Dec 2026 01:01:48 GMT

Languages