Syncromesh/Simulation
Entity spawning, destruction, spatial queries, physics constraints, and engine lifecycle control.
Controls the simulation lifecycle and manages the entity pool. Entities are created with spawn and removed with destroy. The simulation owns the Box2D physics world, provides AABB-based spatial queries, and can create Box2D constraints (joints) between entity bodies.
Import #
import * as Simulation from 'Syncromesh/Simulation';Functions #
spawn
Creates a new entity and returns its ID.
- See Spawn Options for the full shape of the options object.
- The returned ID is used with all
Syncromesh/Entityfunctions.
spawn(options: SpawnOptions): Promise<number>destroy
Marks an entity for destruction.
destroy(id: number): Promise<void>query
Returns entity IDs whose physics bodies overlap the given AABB.
- Only entities with a physics body are included.
- AABB shape:
{ min: { x, y }, max: { x, y } }.
query(area: AABB): Promise<number[]>createConstraint
Creates a physics constraint (joint) and returns its constraint ID.
- Supported types:
distance,weld,prismatic,revolute. - Both entities must be active and have physics bodies.
createConstraint(options: ConstraintDef): Promise<number>destroyConstraint
Destroys a previously created constraint.
destroyConstraint(constraintId: number): Promise<boolean>isConstraintActive
Returns whether a constraint ID is currently active.
isConstraintActive(constraintId: number): Promise<boolean>getEntityConstraints
Lists active constraint IDs attached to an entity.
getEntityConstraints(id: number): Promise<number[]>getPhysicsSettings
Returns the current physics solver settings.
getPhysicsSettings(): Promise<PhysicsSettings>setPhysicsSettings
Updates physics solver settings used for future simulation steps.
velocityIterationsandpositionIterationsare clamped to the range1..100.- Higher iteration counts can improve fast-body and constraint stability at higher CPU cost.
setPhysicsSettings(settings: Partial<PhysicsSettings>): Promise<void>setRunState(state: number): Promise<void>quit
Signals the engine to quit.
quit(): Promise<void>Spawn options #
SpawnOptions
Configuration object passed to
spawn().positionPoint optional — Initial world position{ x, y }.bodyBodyOptions optional — Physics body configuration. Omit for a non-physical entity.renderableRenderDescriptor[] optional — Array of renderable descriptors to attach at spawn time. Includes GPU particle emitters; see GPU Particles.
BodyOptions
Physics body definition.
typestring —"static","dynamic", or"kinematic". Defaults to static.linearDampingnumber optional — Linear velocity damping factor.bulletboolean optional — Enables Box2D bullet mode for fast dynamic bodies to reduce tunneling through other moving bodies. Use sparingly.fixtureFixtureDef[] optional — Array of fixture definitions to attach to the body.
PhysicsSettings
Physics solver settings used when stepping the Box2D world.
velocityIterationsnumber — Box2D velocity solver iterations. Defaults to2.positionIterationsnumber — Box2D position solver iterations. Defaults to2.
Constraint options #
ConstraintDef
Base shape for all
createConstraint() options.typestring —"distance","weld","prismatic", or"revolute".entityAnumber — First entity ID.entityBnumber — Second entity ID.collideConnectedboolean optional — If true, attached bodies can still collide.
DistanceConstraintDef
Distance (spring/rope-like) constraint in world space.
typestring —"distance"anchorAPoint optional — World-space anchor onentityA(defaults to body center).anchorBPoint optional — World-space anchor onentityB(defaults to body center).lengthnumber optional — Rest length.minLengthnumber optional — Minimum allowed length.maxLengthnumber optional — Maximum allowed length.stiffnessnumber optional — Linear stiffness.dampingnumber optional — Linear damping.
WeldConstraintDef
Weld (rigid attach) constraint.
typestring —"weld"anchorPoint optional — World-space weld anchor (defaults to body center ofentityA).referenceAnglenumber optional — Reference bodyB-bodyA angle in radians.stiffnessnumber optional — Rotational stiffness.dampingnumber optional — Rotational damping.
PrismaticConstraintDef
Prismatic (slider) constraint.
typestring —"prismatic"anchorPoint optional — World-space anchor (defaults to body center ofentityA).axisPoint optional — World-space axis direction (defaults to{ x: 1, y: 0 }).referenceAnglenumber optional — Reference bodyB-bodyA angle in radians.enableLimitboolean optional — Enable translation limits.lowerTranslationnumber optional — Lower translation limit.upperTranslationnumber optional — Upper translation limit.enableMotorboolean optional — Enable motor.motorSpeednumber optional — Motor speed.maxMotorForcenumber optional — Maximum motor force.
RevoluteConstraintDef
Revolute (hinge) constraint.
typestring —"revolute"anchorPoint optional — World-space hinge anchor (defaults to body center ofentityA).referenceAnglenumber optional — Reference bodyB-bodyA angle in radians.enableLimitboolean optional — Enable angular limits.lowerAnglenumber optional — Lower angle limit in radians.upperAnglenumber optional — Upper angle limit in radians.enableMotorboolean optional — Enable motor.motorSpeednumber optional — Motor speed in radians/sec.maxMotorTorquenumber optional — Maximum motor torque.
RunState #
| Value | Name |
|---|---|
| 0 | STOPPED |
| 1 | HEADLESS |
| 2 | START |
| 3 | RUNNING |
| 4 | PAUSED |
| 5 | STOP |
| 6 | QUIT |
| 7 | RELOAD |
Examples #
import * as Simulation from 'Syncromesh/Simulation';
import * as Entity from 'Syncromesh/Entity';
// Spawn two dynamic entities
const a = await Simulation.spawn({
position: { x: 100, y: 50 },
body: { type: 'dynamic', fixture: [{ type: 'box', size: { x: 1, y: 1 }, density: 1.0 }] }
});
const b = await Simulation.spawn({
position: { x: 104, y: 50 },
body: { type: 'dynamic', fixture: [{ type: 'box', size: { x: 1, y: 1 }, density: 1.0 }] }
});
// Connect them with a distance constraint
const constraintId = await Simulation.createConstraint({
type: 'distance',
entityA: a,
entityB: b,
length: 4.0,
stiffness: 4.0,
damping: 0.8
});
// Query nearby entities
const nearby = await Simulation.query({
min: { x: 90, y: 40 },
max: { x: 110, y: 60 }
});
// Cleanup
await Simulation.destroyConstraint(constraintId);
await Simulation.destroy(a);
await Simulation.destroy(b);