Ss14 physics system core

From space-sunrise/project-fire · View interactive version

Ss14 physics system core is an AI agent skill from the GitHub repository space-sunrise/project-fire. Deep practical guide to PhysicsSystem in Space Station 14: simulation device (broadphase, contacts, islands, solver), connection with Transform/Container/Anchoring, client prediction part and working application patterns. Use it when you need to understand how physics actually works and where it is

How to use this skill

Skill file

PhysicsSystem: Core

This skill covers the architecture and mental model of PhysicsSystem :)

For the full directory of the public API, see the separate skill SS14 Physics System API.

Mental model

Physics in SS14 is convenient to think of as a pipeline of 6 layers:

  1. PhysicsComponent
  1. FixturesComponent
  1. Broadphase
  1. Contacts
  1. Islands + Solver
  1. Sync with Transform

Key idea:

How the tick of physics really works

Typical step (including substeps):

  1. PhysicsUpdateBeforeSolveEvent.
  2. Search for new potential pairs (FindNewContacts).
  3. Update contacts (CollideContacts) and events Start/EndCollide.
  4. Solve islands (Step -> integration of speeds -> constraints -> positions).
  5. PhysicsUpdateAfterSolveEvent.
  6. Finalization/lerp data on the last substep.

Practical conclusion:

Connection with Transform, Anchoring and Containers

PhysicsSystem is tightly connected to the transform hierarchy:

Consequence:

Client and server

General:

Differences:

Short decision tree

  1. Need a one-time push/jerk?
  1. Do you need constant thrust/torque?
  1. Do you need to switch the physical mode of an object?
  1. Do you need to change collision behavior without changing your body type?
  1. Do you need to reassemble the current contacts after a sudden change of collision/fixtures?
  1. Do you need geometry and distances based on real hitboxes?

Optimization when using PhysicsSystem :)

1) Component optimizations (the cheapest and most effective)

# Underground/pipe entity: collision is automatically synchronized with anchoring.
components:
- type: Physics
  canCollide: false
- type: CollideOnAnchor
# Massive small items: collision wake saves updates on the grid.
components:
- type: Physics
- type: CollisionWake

2) Sleep/wake and frequent speed updates

// The controller updates velocity every tick, but an extra wake at every step is not needed.
PhysicsSystem.SetAngularVelocity(uid, angularVelocity);
PhysicsSystem.SetLinearVelocity(uid, velocity, wakeBody: false);

3) Cheap query/contact patterns

// Quick broadphase screening: where appropriate, leave approximate = true.
var touching = _physics.GetEntitiesIntersectingBody(uid, collisionMask, approximate: true);

4) Make changes to fixtures and collisions in batches

// After a series of edits to the collision profile, we perform one recalculation of contacts.
_physics.SetCollisionMask(uid, id, fixture, newMask, manager: fixtures, body: body);
_physics.SetCollisionLayer(uid, id, fixture, newLayer, manager: fixtures, body: body);
_broadphase.RegenerateContacts((uid, body, fixtures, xform));

5) Use `hard: false` for trigger mechanics

# Trigger fixture: contact is present, there is no hard collision.
fixtures:
  sensor:
    hard: false
    mask: [ItemMask]
    layer: [SlipLayer]

Optimization patterns

Anti-optimization patterns

Code examples

Example 1: physical step (before solve -> contacts -> solve -> after solve)

var updateBeforeSolve = new PhysicsUpdateBeforeSolveEvent(prediction, frameTime);
RaiseLocalEvent(ref updateBeforeSolve);

_broadphase.FindNewContacts();
CollideContacts();
Step(frameTime, prediction);

var updateAfterSolve = new PhysicsUpdateAfterSolveEvent(prediction, frameTime);
RaiseLocalEvent(ref updateAfterSolve);

Example 2: correct transfer of an entity to container mode

// When inserted into a container, we dampen the dynamics and turn off collisions.
_physics.SetLinearVelocity(entity, Vector2.Zero, false, body: physics);
_physics.SetAngularVelocity(entity, 0, false, body: physics);
_physics.SetCanCollide(entity, false, false, body: physics);

Example 3: conveyor controller via contact iterator

var contacts = PhysicsSystem.GetContacts(conveyorUid);
while (contacts.MoveNext(out var contact))
{
    var other = contact.OtherEnt(conveyorUid);
    if (_conveyedQuery.HasComp(other))
        PhysicsSystem.WakeBody(other);
}

Example 4: predictable kinematics without unnecessary “waking up”

// For conveyor movement, it is not always necessary to wake up the body at every update.
PhysicsSystem.SetAngularVelocity(uid, angularVelocity);
PhysicsSystem.SetLinearVelocity(uid, velocity, wakeBody: false);

Example 5: re-sync contacts after changing physical mode

_physics.SetBodyType(uid, BodyType.Dynamic, fixtures, body, xform);
_physics.SetCanCollide(uid, true, manager: fixtures, body: body);
_broadphase.RegenerateContacts((uid, body, fixtures, xform));

Example 6: range-check on real hitboxes

var xfA = new Transform(worldPosA, worldRotA);
var xfB = new Transform(targetPos.Position, targetRot);

if (_physics.TryGetNearest(origin, other, out _, out _, out var distance, xfA, xfB, fixtureA, fixtureB))
{
    if (distance <= range)
        return true;
}

Example 7: runtime switching between "flying/not flying" via status+sleep

_physics.SetBodyStatus(target, targetPhysics, BodyStatus.InAir, false);
_physics.SetSleepingAllowed(target, targetPhysics, false);
_physics.WakeBody(target, body: targetPhysics);

Patterns 🙂

Anti-patterns

Mini-checklist before changes

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.