@daneren2005/shared-memory-physics 1.6.3 → 1.7.0
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 +18 -12
- package/dist/components/body-component.d.ts +4 -0
- package/dist/components/interpolation-component.d.ts +12 -12
- package/dist/index.d.ts +3 -3
- package/dist/index.js +351 -338
- package/dist/index.js.map +1 -1
- package/package.json +11 -10
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
|
|
330
|
+
frame instead, by consuming published simulation time while advancing toward the last coherent target:
|
|
326
331
|
|
|
327
332
|
```
|
|
328
|
-
render
|
|
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,10 @@ 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.
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
351
|
+
It takes no configuration and is not wired to the physics system. Physics publishes the transform, step
|
|
352
|
+
duration, cumulative simulated time, and a tick stamp. Rendering caches each coherent target and its timing.
|
|
353
|
+
If it catches physics mid-write, it continues consuming the cached segment until another complete snapshot
|
|
354
|
+
is available.
|
|
350
355
|
A game that retunes `deltaBetweenRuns` mid-flight is followed with nothing told to this system.
|
|
351
356
|
|
|
352
357
|
Then draw from `interpolation` instead of `transform`, keeping the size and facing where they have always been:
|
|
@@ -482,16 +487,17 @@ spot, as above.
|
|
|
482
487
|
|
|
483
488
|
### The cost
|
|
484
489
|
|
|
485
|
-
|
|
490
|
+
The renderer retains the last coherent target while physics publishes a new step, so an overlapping read can keep advancing without freezing a frame.
|
|
491
|
+
|
|
492
|
+
44 bytes per interpolated entity, four publication writes per entity per physics step, and a per-frame pass of one
|
|
486
493
|
lerp per entity on the main thread - order 0.2ms at 10,000 entities. An entity without the component pays
|
|
487
494
|
nothing at all. `forceMainThread: false` plus a `getWorker` moves the pass to a worker for worlds large enough
|
|
488
495
|
that it shows up in a profile, at the price of the render position being one frame stale; because consumers only
|
|
489
496
|
ever read `interpolation.x`, that is a constructor flag rather than a migration.
|
|
490
497
|
|
|
491
|
-
Physics
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
from another - which is the one mismatch that would draw an entity moving backwards.
|
|
498
|
+
Physics marks the tick dirty before changing timing and position, then publishes the completed tick with
|
|
499
|
+
a release store. The reader accepts a new snapshot only when its tick reads match; otherwise it continues
|
|
500
|
+
toward the cached target.
|
|
495
501
|
|
|
496
502
|
## Collisions
|
|
497
503
|
|
|
@@ -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,15 @@ 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
17
|
}
|
|
18
18
|
export interface InterpolationConfig {
|
|
19
19
|
interpolate?: boolean;
|
|
@@ -22,15 +22,15 @@ export interface InterpolationConfig {
|
|
|
22
22
|
}
|
|
23
23
|
export declare const INTERPOLATION_X_INDEX = 0;
|
|
24
24
|
export declare const INTERPOLATION_Y_INDEX = 1;
|
|
25
|
-
export declare const
|
|
26
|
-
export declare const
|
|
27
|
-
export declare const
|
|
28
|
-
export declare const
|
|
29
|
-
export declare const
|
|
30
|
-
export declare const
|
|
31
|
-
export declare const
|
|
32
|
-
export declare const
|
|
33
|
-
export declare const
|
|
25
|
+
export declare const INTERPOLATION_SYNCED_TICK_INDEX = 2;
|
|
26
|
+
export declare const INTERPOLATION_DURATION_INDEX = 3;
|
|
27
|
+
export declare const INTERPOLATION_TICK_INDEX = 4;
|
|
28
|
+
export declare const INTERPOLATION_TOTAL_DURATION_INDEX = 5;
|
|
29
|
+
export declare const INTERPOLATION_SYNCED_DURATION_INDEX = 6;
|
|
30
|
+
export declare const INTERPOLATION_REMAINING_DURATION_INDEX = 7;
|
|
31
|
+
export declare const INTERPOLATION_TARGET_X_INDEX = 8;
|
|
32
|
+
export declare const INTERPOLATION_TARGET_Y_INDEX = 9;
|
|
33
|
+
export declare const INTERPOLATION_SYNCED_STEP_DURATION_INDEX = 10;
|
|
34
34
|
export declare const INTERPOLATION_SIZE = 11;
|
|
35
35
|
export declare const interpolationDefinition: ComponentDefinition<InterpolationComponent, Float32Array, InterpolationConfig>;
|
|
36
36
|
export interface SnappableEntity {
|
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,
|
|
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_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';
|