@daneren2005/shared-memory-physics 1.6.3 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,7 +21,7 @@ npm install @daneren2005/shared-memory-physics @daneren2005/shared-memory-ecs @d
21
21
  | `dynamics` | - | `accelerationX?`, `accelerationY?`, `mass?` | `Float32Array` - acceleration and inverse mass |
22
22
  | `transform` | `width`, `height`, `angle?` / `radius` | `x`, `y`, `angle` | `Float32Array` – where, how big, facing |
23
23
  | `velocity` | – | `velocityX?`, `velocityY?`, `damping?` | `Float32Array` – world units per second |
24
- | `body` | `shape?`, `collideCategory?`, `collideMask?`, `sensor?`, `continuousCollisionDetection?` | – | `Uint32Array` – what shape, what collides with |
24
+ | `body` | `shape?`, `collideCategory?`, `collideMask?`, `sensor?`, `continuousCollisionDetection?`, `blocksPath?` | – | `Uint32Array` – what shape, what collides with |
25
25
  | `interpolation` | `interpolate` | – | `Float32Array` – where to *draw* it |
26
26
 
27
27
  Entity configs are flat and shared across every component, so velocity is keyed as `velocityX` / `velocityY`
@@ -79,6 +79,11 @@ defining config out of your own template, which is why `save` returns nothing fo
79
79
  its shape and categories back from the template rather than from the save. See
80
80
  [Body shapes](#body-shapes) for `shape` and [Collision filtering](#collision-filtering) for the other two.
81
81
 
82
+ `blocksPath` marks a body as something pathfinding should route around - a wall or a building rather than another
83
+ mover. The physics never reads it; it is a flag in the same packed word for a game's own navigation to query
84
+ (`body.blocksPath`, or `blocksPath(block)` off a raw block). It defaults to `false`, and a body carrying it is
85
+ assumed never to move, so a navigation structure can be updated only when such bodies are added or removed.
86
+
82
87
  ## Body shapes
83
88
 
84
89
  An entity is one of four shapes. The transform still owns its position, size and angle; a polygon additionally
@@ -322,10 +327,10 @@ Without a `getWorker` (or where Web Workers / `SharedArrayBuffer` are unavailabl
322
327
 
323
328
  A 50ms physics step against a 16ms frame means the transform only changes on one frame in three, and an entity
324
329
  drawn straight off it visibly stutters. `InterpolationSystem` fills in a **render position** that changes every
325
- frame instead, by blending between the two positions physics published either side of its last step:
330
+ frame instead, by consuming published simulation time while advancing toward the last coherent target:
326
331
 
327
332
  ```
328
- render = prev + (current - prev) * alpha
333
+ render += (target - render) * (frameDuration / remainingDuration)
329
334
  ```
330
335
 
331
336
  Normally every position it writes lies on the latest simulation segment. If worker scheduling lets several
@@ -343,10 +348,15 @@ world.addSystem(new InterpolationSystem<Components>(world));
343
348
  world.loadEntity({ x: 0, y: 0, width: 10, height: 10, velocityX: 100, interpolate: true });
344
349
  ```
345
350
 
346
- It takes no configuration and is not wired to the physics system. Everything it needs - the two positions, how
347
- much simulated time lies between them, and whether that pair is new - is published into the block by whichever
348
- run wrote it. Physics marks that publication dirty before changing either endpoint and stamps it complete only
349
- after both are ready, so rendering holds its previous position for a frame if it catches the worker mid-write.
351
+ It takes no configuration. Physics publishes the transform, step duration, cumulative simulated time, and a tick
352
+ stamp. Rendering caches each coherent target and its timing. If it catches physics mid-write, it continues
353
+ consuming the cached segment until another complete snapshot is available.
354
+
355
+ A worker publishes a run entity by entity while the main thread renders, so one frame can find some entities of a
356
+ run at its new step and the rest at the old one - two ships flying in formation would then drift apart on screen.
357
+ Each entity therefore keeps the publication it replaced, and `InterpolationSystem` snapshots every
358
+ `PhysicsSystem`'s committed tick once per frame: a publication newer than its run's commit is drawn from the
359
+ previous one until the whole run has landed. Both systems must share the same world object for this to apply.
350
360
  A game that retunes `deltaBetweenRuns` mid-flight is followed with nothing told to this system.
351
361
 
352
362
  Then draw from `interpolation` instead of `transform`, keeping the size and facing where they have always been:
@@ -482,16 +492,17 @@ spot, as above.
482
492
 
483
493
  ### The cost
484
494
 
485
- 44 bytes per interpolated entity, four extra writes per entity per physics step, and a per-frame pass of one
495
+ The renderer retains the last coherent target while physics publishes a new step, so an overlapping read can keep advancing without freezing a frame.
496
+
497
+ 68 bytes per interpolated entity, ten publication writes per entity per physics step, and a per-frame pass of one
486
498
  lerp per entity on the main thread - order 0.2ms at 10,000 entities. An entity without the component pays
487
499
  nothing at all. `forceMainThread: false` plus a `getWorker` moves the pass to a worker for worlds large enough
488
500
  that it shows up in a profile, at the price of the render position being one frame stale; because consumers only
489
501
  ever read `interpolation.x`, that is a constructor flag rather than a migration.
490
502
 
491
- Physics writes `prev` and then the transform on the worker thread while the main thread reads both, so the block
492
- carries a tick stamp that is written **last**, with a release store. The update reads it on either side of its
493
- own reads and drops the frame if it changed, rather than blending a `prev` from one step against a transform
494
- from another - which is the one mismatch that would draw an entity moving backwards.
503
+ Physics marks the tick dirty before changing timing and position, then publishes the completed tick with
504
+ a release store. The reader accepts a new snapshot only when its tick reads match and its run has committed;
505
+ otherwise it reads the previous publication, and failing that continues toward the cached target.
495
506
 
496
507
  ## Collisions
497
508
 
@@ -7,6 +7,7 @@ export interface BodyComponent {
7
7
  collideMask: number;
8
8
  sensor: boolean;
9
9
  continuousCollisionDetection: boolean;
10
+ blocksPath: boolean;
10
11
  }
11
12
  export declare const SHAPE_RECTANGLE = 1;
12
13
  export declare const SHAPE_CIRCLE = 2;
@@ -25,6 +26,7 @@ export interface BodyConfig {
25
26
  collideMask?: number;
26
27
  sensor?: boolean;
27
28
  continuousCollisionDetection?: boolean;
29
+ blocksPath?: boolean;
28
30
  }
29
31
  export declare const BODY_FLAGS_INDEX = 0;
30
32
  export declare const BODY_CATEGORY_INDEX = 1;
@@ -34,9 +36,11 @@ export declare const BODY_SHAPE_MASK = 7;
34
36
  export declare const BODY_SENSOR_FLAG = 8;
35
37
  export declare const BODY_CCD_FLAG = 16;
36
38
  export declare const BODY_DYING_FLAG = 32;
39
+ export declare const BODY_BLOCKS_PATH_FLAG = 64;
37
40
  export declare function bodyShape(body: Uint32Array): number;
38
41
  export declare function isSensor(body: Uint32Array): boolean;
39
42
  export declare function isContinuous(body: Uint32Array): boolean;
43
+ export declare function blocksPath(body: Uint32Array): boolean;
40
44
  export declare function isDying(body: Uint32Array): boolean;
41
45
  export declare function markDying(body: Uint32Array): void;
42
46
  export declare const bodyDefinition: ComponentDefinition<BodyComponent, Uint32Array, BodyConfig>;
@@ -5,15 +5,17 @@ export interface InterpolationComponent {
5
5
  index: number;
6
6
  x: number;
7
7
  y: number;
8
- prevX: number;
9
- prevY: number;
10
- progress: number;
11
8
  syncedTick: number;
12
9
  duration: number;
13
10
  tick: number;
14
11
  totalDuration: number;
15
12
  syncedDuration: number;
16
13
  remainingDuration: number;
14
+ targetX: number;
15
+ targetY: number;
16
+ syncedStepDuration: number;
17
+ previousX: number;
18
+ previousY: number;
17
19
  }
18
20
  export interface InterpolationConfig {
19
21
  interpolate?: boolean;
@@ -22,16 +24,22 @@ export interface InterpolationConfig {
22
24
  }
23
25
  export declare const INTERPOLATION_X_INDEX = 0;
24
26
  export declare const INTERPOLATION_Y_INDEX = 1;
25
- export declare const INTERPOLATION_PREV_X_INDEX = 2;
26
- export declare const INTERPOLATION_PREV_Y_INDEX = 3;
27
- export declare const INTERPOLATION_PROGRESS_INDEX = 4;
28
- export declare const INTERPOLATION_SYNCED_TICK_INDEX = 5;
29
- export declare const INTERPOLATION_DURATION_INDEX = 6;
30
- export declare const INTERPOLATION_TICK_INDEX = 7;
31
- export declare const INTERPOLATION_TOTAL_DURATION_INDEX = 8;
32
- export declare const INTERPOLATION_SYNCED_DURATION_INDEX = 9;
33
- export declare const INTERPOLATION_REMAINING_DURATION_INDEX = 10;
34
- export declare const INTERPOLATION_SIZE = 11;
27
+ export declare const INTERPOLATION_SYNCED_TICK_INDEX = 2;
28
+ export declare const INTERPOLATION_DURATION_INDEX = 3;
29
+ export declare const INTERPOLATION_TICK_INDEX = 4;
30
+ export declare const INTERPOLATION_TOTAL_DURATION_INDEX = 5;
31
+ export declare const INTERPOLATION_SYNCED_DURATION_INDEX = 6;
32
+ export declare const INTERPOLATION_REMAINING_DURATION_INDEX = 7;
33
+ export declare const INTERPOLATION_TARGET_X_INDEX = 8;
34
+ export declare const INTERPOLATION_TARGET_Y_INDEX = 9;
35
+ export declare const INTERPOLATION_SYNCED_STEP_DURATION_INDEX = 10;
36
+ export declare const INTERPOLATION_CHANNEL_INDEX = 11;
37
+ export declare const INTERPOLATION_PREVIOUS_X_INDEX = 12;
38
+ export declare const INTERPOLATION_PREVIOUS_Y_INDEX = 13;
39
+ export declare const INTERPOLATION_PREVIOUS_DURATION_INDEX = 14;
40
+ export declare const INTERPOLATION_PREVIOUS_TOTAL_DURATION_INDEX = 15;
41
+ export declare const INTERPOLATION_PREVIOUS_TICK_INDEX = 16;
42
+ export declare const INTERPOLATION_SIZE = 17;
35
43
  export declare const interpolationDefinition: ComponentDefinition<InterpolationComponent, Float32Array, InterpolationConfig>;
36
44
  export interface SnappableEntity {
37
45
  components: {
package/dist/index.d.ts CHANGED
@@ -7,8 +7,8 @@ export type { VelocityComponent, VelocityConfig, VelocitySerialization, } from '
7
7
  export { default as dynamicsDefinition } from './components/dynamics-component';
8
8
  export { DYNAMICS_ACCELERATION_X_INDEX, DYNAMICS_ACCELERATION_Y_INDEX, DYNAMICS_INVERSE_MASS_INDEX, DYNAMICS_SIZE, } from './components/dynamics-component';
9
9
  export type { DynamicsComponent, DynamicsConfig, DynamicsSerialization, } from './components/dynamics-component';
10
- export { default as bodyDefinition, canCollide, isSensor, isContinuous, isDying, markDying, bodyShape } from './components/body-component';
11
- export { SHAPE_RECTANGLE, SHAPE_CIRCLE, SHAPE_CAPSULE, SHAPE_POLYGON, DEFAULT_COLLIDE_CATEGORY, DEFAULT_COLLIDE_MASK, BODY_FLAGS_INDEX, BODY_SHAPE_MASK, BODY_SENSOR_FLAG, BODY_CCD_FLAG, BODY_DYING_FLAG, BODY_CATEGORY_INDEX, BODY_MASK_INDEX, BODY_SIZE, } from './components/body-component';
10
+ export { default as bodyDefinition, canCollide, isSensor, isContinuous, blocksPath, isDying, markDying, bodyShape } from './components/body-component';
11
+ export { SHAPE_RECTANGLE, SHAPE_CIRCLE, SHAPE_CAPSULE, SHAPE_POLYGON, DEFAULT_COLLIDE_CATEGORY, DEFAULT_COLLIDE_MASK, BODY_FLAGS_INDEX, BODY_SHAPE_MASK, BODY_SENSOR_FLAG, BODY_CCD_FLAG, BODY_DYING_FLAG, BODY_BLOCKS_PATH_FLAG, BODY_CATEGORY_INDEX, BODY_MASK_INDEX, BODY_SIZE, } from './components/body-component';
12
12
  export type { BodyComponent, BodyConfig, } from './components/body-component';
13
13
  export { default as polygonDefinition, preparePolygon } from './components/polygon-component';
14
14
  export { MAX_POLYGON_VERTICES, POLYGON_VERTEX_COUNT_INDEX, POLYGON_VERTICES_INDEX, POLYGON_SIZE, } from './components/polygon-component';
@@ -17,7 +17,7 @@ export { default as bouncinessDefinition } from './components/bounciness-compone
17
17
  export { BOUNCINESS_INDEX, BOUNCINESS_SIZE, } from './components/bounciness-component';
18
18
  export type { BouncinessComponent, BouncinessConfig, } from './components/bounciness-component';
19
19
  export { default as interpolationDefinition, snapEntity, startSpawnInterpolation } from './components/interpolation-component';
20
- export { INTERPOLATION_X_INDEX, INTERPOLATION_Y_INDEX, INTERPOLATION_PREV_X_INDEX, INTERPOLATION_PREV_Y_INDEX, INTERPOLATION_PROGRESS_INDEX, INTERPOLATION_SYNCED_TICK_INDEX, INTERPOLATION_DURATION_INDEX, INTERPOLATION_TICK_INDEX, INTERPOLATION_TOTAL_DURATION_INDEX, INTERPOLATION_SYNCED_DURATION_INDEX, INTERPOLATION_REMAINING_DURATION_INDEX, INTERPOLATION_SIZE, } from './components/interpolation-component';
20
+ export { INTERPOLATION_X_INDEX, INTERPOLATION_Y_INDEX, INTERPOLATION_SYNCED_TICK_INDEX, INTERPOLATION_DURATION_INDEX, INTERPOLATION_TICK_INDEX, INTERPOLATION_TOTAL_DURATION_INDEX, INTERPOLATION_SYNCED_DURATION_INDEX, INTERPOLATION_REMAINING_DURATION_INDEX, INTERPOLATION_TARGET_X_INDEX, INTERPOLATION_TARGET_Y_INDEX, INTERPOLATION_SYNCED_STEP_DURATION_INDEX, INTERPOLATION_CHANNEL_INDEX, INTERPOLATION_PREVIOUS_X_INDEX, INTERPOLATION_PREVIOUS_Y_INDEX, INTERPOLATION_PREVIOUS_DURATION_INDEX, INTERPOLATION_PREVIOUS_TOTAL_DURATION_INDEX, INTERPOLATION_PREVIOUS_TICK_INDEX, INTERPOLATION_SIZE, } from './components/interpolation-component';
21
21
  export type { InterpolationComponent, InterpolationConfig, SnappableEntity, SpawnableEntity, SpawnInterpolationOptions, } from './components/interpolation-component';
22
22
  export { physicsRegistry } from './components/registry';
23
23
  export type { PhysicsComponents, PhysicsUpdateComponents, InterpolationComponents, InterpolationUpdateComponents, } from './components/registry';
@@ -37,5 +37,6 @@ export type { PhysicsSystemConfig, VelocityAssignment } from './systems/physics-
37
37
  export { default as integrateDynamics } from './systems/dynamics';
38
38
  export type { DynamicsCommand, DynamicsCommandBuffer, DynamicsCommandQueue, DynamicsCommands, DynamicsWorld } from './systems/dynamics';
39
39
  export { default as interpolationUpdate } from './systems/interpolation-update';
40
+ export type { InterpolationWorld } from './systems/interpolation-update';
40
41
  export { default as InterpolationSystem } from './systems/interpolation-system';
41
42
  export type { InterpolationSystemConfig } from './systems/interpolation-system';