@zaisma/physics 2.4.0 → 2.5.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 +30 -4
- package/batch.js +155 -0
- package/characters.js +21 -7
- package/declare.js +208 -12
- package/index.d.ts +77 -13
- package/level-characters.js +46 -12
- package/package.json +7 -2
- package/piece.json +6 -2
- package/piece.json.js +6 -2
- package/rapier/CHANGES.md +16 -0
- package/rapier/rapier.js +4609 -2762
- package/rapier/rapier.wasm +0 -0
- package/simulation.js +867 -251
- package/sync.js +155 -55
package/level-characters.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
// Characters over a level: a shooter's soldiers, a racer's runners. Each moves on its own every step by its kind's
|
|
2
2
|
// rules against the world's fixed and kinematic bodies (the level), and the online core shares them as characters,
|
|
3
3
|
// not as a world: each player's game predicts only its own character, everyone else is drawn a moment behind, and a
|
|
4
|
-
// hit is judged by what the shooter saw (stateAt and viewTickNumber).
|
|
4
|
+
// hit is judged by what the shooter saw (stateAt and viewTickNumber). The level moves only by timetable (a lift, a
|
|
5
|
+
// platform: where each is at a step is the same on every side, so nothing of it is sent), and characters walk
|
|
5
6
|
// through each other: where another player is differs in every game, so it can't be stood against. One copy of the
|
|
6
7
|
// level serves every room a game server runs.
|
|
7
8
|
import { openPiece } from "../piece/index.js";
|
|
8
9
|
import statement from "./piece.json.js";
|
|
9
|
-
import { MAXIMUM_TRIANGLE_GROUP_NUMBER, refuse, worldOf } from "./declare.js";
|
|
10
|
+
import { MAXIMUM_TRIANGLE_GROUP_MASK_NUMBER, MAXIMUM_TRIANGLE_GROUP_NUMBER, refuse, timetablePlaceInto, triangleGroupBitNumberOf, triangleGroupMaskIndexOf, worldOf } from "./declare.js";
|
|
10
11
|
import { physicsEngine } from "./engine.js";
|
|
11
12
|
import { defineCharacterKind, walkingCharacters } from "./characters.js";
|
|
12
13
|
import { characterFieldsOf, characterViewOf, levelTuningOf, queriesOver } from "./sync.js";
|
|
@@ -17,8 +18,8 @@ const GIVEN_KEYS = ["name", "characters", "settings", "playerCharacterMode", "ev
|
|
|
17
18
|
const RIDING_FIELD_NAMES = ["rideBodyNumber", "seatIndex"];
|
|
18
19
|
/** How long a character nobody has stepped stays in the level's memory, in seconds: one that left, or another room's that ended. */
|
|
19
20
|
const FORGOTTEN_AFTER_SEC = 3;
|
|
20
|
-
/**
|
|
21
|
-
const
|
|
21
|
+
/** Which triangle groups are off, in the room's own fields: three whole numbers with a bit for each group. */
|
|
22
|
+
const GROUPS_FIELD_NAMES = ["offTriangleGroupMaskNumber", "offTriangleGroupSecondMaskNumber", "offTriangleGroupThirdMaskNumber"];
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* Answers levelCharactersKind for one side's online core (its charactersKind, from index.js in a game and server.js
|
|
@@ -35,10 +36,22 @@ export const levelCharactersKindFrom = (charactersKind) => (givenWorld, given =
|
|
|
35
36
|
const sharedFields = characterFieldsOf(kind).filter((field) => !RIDING_FIELD_NAMES.includes(field.name));
|
|
36
37
|
// A level whose meshes have triangle groups (doors, bridges) switches them room by room: which are off is the room's own.
|
|
37
38
|
const hasGroups = level.bodies.some((body) => body.shapes.some((shape) => shape.triangleGroupNumbers));
|
|
38
|
-
const roomFields = [...(given.roomFields ?? []), ...(hasGroups ?
|
|
39
|
+
const roomFields = [...(given.roomFields ?? []), ...(hasGroups ? GROUPS_FIELD_NAMES.map((name) => ({ name, valueKind: "integer", minimumNumber: 0, maximumNumber: MAXIMUM_TRIANGLE_GROUP_MASK_NUMBER })) : [])];
|
|
39
40
|
const sharedNames = sharedFields.map((field) => field.name);
|
|
41
|
+
const levelBodyById = new Map(level.bodies.map((body) => [body.id, body]));
|
|
40
42
|
let startCount = 0;
|
|
41
43
|
let simulation = null;
|
|
44
|
+
/** A step's length, known from the first step run: a timetable's clock is the step's index times it. */
|
|
45
|
+
let levelTickDurationSec = 0;
|
|
46
|
+
const timetabledPlace = { x: 0, y: 0, z: 0, rotationX: 0, rotationY: 0, rotationZ: 0, rotationW: 1 };
|
|
47
|
+
/** Where one of the level's bodies is at a step (a whole one, or between two): { positionM, rotation }, or null for one that isn't the level's. */
|
|
48
|
+
const bodyPlaceAt = (bodyId, tickNumber) => {
|
|
49
|
+
const body = levelBodyById.get(bodyId);
|
|
50
|
+
if (!body) return null;
|
|
51
|
+
if (!body.timetable) return { positionM: { ...body.positionM }, rotation: { ...body.rotation } };
|
|
52
|
+
timetablePlaceInto(body.timetable, tickNumber * levelTickDurationSec, timetabledPlace);
|
|
53
|
+
return { positionM: { x: timetabledPlace.x, y: timetabledPlace.y, z: timetabledPlace.z }, rotation: { x: timetabledPlace.rotationX, y: timetabledPlace.rotationY, z: timetabledPlace.rotationZ, w: timetabledPlace.rotationW } };
|
|
54
|
+
};
|
|
42
55
|
/** Each room's number, by the core's own part of its state: one level holds every room's characters, apart. */
|
|
43
56
|
const roomNumbers = new WeakMap();
|
|
44
57
|
let nextRoomNumber = 1;
|
|
@@ -47,6 +60,7 @@ export const levelCharactersKindFrom = (charactersKind) => (givenWorld, given =
|
|
|
47
60
|
let lastSweptTickIndex = 0;
|
|
48
61
|
|
|
49
62
|
const simulationFor = (tickDurationSec) => {
|
|
63
|
+
levelTickDurationSec = tickDurationSec;
|
|
50
64
|
simulation ??= createSimulation(physicsEngine(), level, { tickDurationSec, characterKind: kind, tuning: levelTuningOf(kind, settings), isEachCharacterAlone: true });
|
|
51
65
|
return simulation;
|
|
52
66
|
};
|
|
@@ -87,7 +101,7 @@ export const levelCharactersKindFrom = (charactersKind) => (givenWorld, given =
|
|
|
87
101
|
let characterIndex = level.characterIndexById.get(key);
|
|
88
102
|
if (characterIndex === undefined) characterIndex = level.addCharacter(key, { x: state.x, y: state.y - world.character.heightM / 2, z: state.z });
|
|
89
103
|
steppedAtTickIndex.set(key, stepContext.tickIndex);
|
|
90
|
-
if (hasGroups) level.setOffTriangleGroups(stepContext.room?.[
|
|
104
|
+
if (hasGroups) level.setOffTriangleGroups(stepContext.room?.[GROUPS_FIELD_NAMES[0]] ?? 0, stepContext.room?.[GROUPS_FIELD_NAMES[1]] ?? 0, stepContext.room?.[GROUPS_FIELD_NAMES[2]] ?? 0);
|
|
91
105
|
level.writeCharacter(characterIndex, state);
|
|
92
106
|
level.stepCharacterAlone(characterIndex, input, { ...level.stepTools, ...stepContext });
|
|
93
107
|
const read = level.readCharacter(characterIndex);
|
|
@@ -101,9 +115,24 @@ export const levelCharactersKindFrom = (charactersKind) => (givenWorld, given =
|
|
|
101
115
|
const clientApi = netKind.clientApi;
|
|
102
116
|
netKind.clientApi = (runtime, kindContext) => {
|
|
103
117
|
const characters = clientApi(runtime, kindContext);
|
|
118
|
+
levelTickDurationSec ||= 1 / kindContext.ticksPerSec;
|
|
119
|
+
/** The step after this game's last: the level as it stands where its own character is now. */
|
|
120
|
+
characters.levelTickIndex = () => (kindContext.tickIndex() ?? -1) + 1;
|
|
104
121
|
return Object.assign(characters, {
|
|
105
122
|
world,
|
|
106
123
|
settings,
|
|
124
|
+
/** Where to draw one of the level's bodies this frame: { positionM, rotation }. One on a timetable is where this frame's moment has it; null for a body that isn't the level's. */
|
|
125
|
+
drawnBody: (bodyId) => bodyPlaceAt(bodyId, (kindContext.tickIndex() ?? 0) + kindContext.fractionNumber()),
|
|
126
|
+
/** Puts a three.js object (anything with position.set and quaternion.set) where one of the level's bodies is drawn this frame. */
|
|
127
|
+
applyTo(object3d, bodyId) {
|
|
128
|
+
const drawn = bodyPlaceAt(bodyId, (kindContext.tickIndex() ?? 0) + kindContext.fractionNumber());
|
|
129
|
+
if (!drawn) return false;
|
|
130
|
+
object3d.position.set(drawn.positionM.x, drawn.positionM.y, drawn.positionM.z);
|
|
131
|
+
object3d.quaternion.set(drawn.rotation.x, drawn.rotation.y, drawn.rotation.z, drawn.rotation.w);
|
|
132
|
+
return true;
|
|
133
|
+
},
|
|
134
|
+
/** One of the level's bodies as this game has it now: { positionM, rotation }, or null. */
|
|
135
|
+
bodyState: (bodyId) => bodyPlaceAt(bodyId, (kindContext.tickIndex() ?? -1) + 1),
|
|
107
136
|
/** Where and how to draw a character this frame: its feet, facing, speed, footing and its kind's own fields; null for one not here. */
|
|
108
137
|
drawnCharacter: (characterId) => viewOf(characters.drawn(characterId), characterId),
|
|
109
138
|
/** A character as this game has it now. */
|
|
@@ -112,26 +141,31 @@ export const levelCharactersKindFrom = (charactersKind) => (givenWorld, given =
|
|
|
112
141
|
...queriesOver(() => withRoomGroups(characters)),
|
|
113
142
|
});
|
|
114
143
|
};
|
|
115
|
-
/** The level
|
|
144
|
+
/** The level as a room has it now, before it's asked anything: its triangle groups switched, its bodies on timetables where that room's step has them. */
|
|
116
145
|
const withRoomGroups = (characters) => {
|
|
117
|
-
if (simulation && hasGroups) simulation.setOffTriangleGroups(characters.room()?.[
|
|
146
|
+
if (simulation && hasGroups) simulation.setOffTriangleGroups(characters.room()?.[GROUPS_FIELD_NAMES[0]] ?? 0, characters.room()?.[GROUPS_FIELD_NAMES[1]] ?? 0, characters.room()?.[GROUPS_FIELD_NAMES[2]] ?? 0);
|
|
147
|
+
simulation?.placeTimetables(characters.levelTickIndex());
|
|
118
148
|
simulation?.refreshScene();
|
|
119
149
|
return simulation;
|
|
120
150
|
};
|
|
121
151
|
const onServer = netKind.onServer;
|
|
122
152
|
netKind.onServer = (state, room) => {
|
|
123
153
|
const characters = onServer(state, room);
|
|
154
|
+
characters.levelTickIndex = () => (characters.tickIndex ?? -1) + 1;
|
|
124
155
|
return Object.assign(characters, {
|
|
125
156
|
world,
|
|
157
|
+
/** One of the level's bodies as this room has it now, after its last step: { positionM, rotation }, or null. */
|
|
158
|
+
bodyState: (bodyId) => bodyPlaceAt(bodyId, (characters.tickIndex ?? -1) + 1),
|
|
126
159
|
characterState: (characterId) => viewOf(characters.has(characterId) ? characters.state(characterId) : null, characterId),
|
|
127
160
|
...queriesOver(() => withRoomGroups(characters)),
|
|
128
|
-
/** Switches one of the level's triangle groups on or off in this room (a door, a bridge), 1 to
|
|
161
|
+
/** Switches one of the level's triangle groups on or off in this room (a door, a bridge), 1 to 63, for every player from the next step. */
|
|
129
162
|
setTriangleGroup(groupNumber, isEnabled) {
|
|
130
163
|
if (!hasGroups) refuse("no_triangle_groups", "This level's meshes have no triangleGroupNumbers.", "groupNumber", "Give a mesh triangleGroupNumbers in the world's declaration, in the game and on its game server.");
|
|
131
164
|
if (!Number.isInteger(groupNumber) || groupNumber < 1 || groupNumber > MAXIMUM_TRIANGLE_GROUP_NUMBER || typeof isEnabled !== "boolean") refuse("bad_argument", `setTriangleGroup was given ${JSON.stringify(groupNumber)}, ${JSON.stringify(isEnabled)}.`, "groupNumber", `Give a group from 1 to ${MAXIMUM_TRIANGLE_GROUP_NUMBER}, then true to have it there or false to take it away.`);
|
|
132
|
-
const
|
|
133
|
-
const
|
|
134
|
-
|
|
165
|
+
const fieldName = GROUPS_FIELD_NAMES[triangleGroupMaskIndexOf(groupNumber)];
|
|
166
|
+
const maskNumber = characters.room()?.[fieldName] ?? 0;
|
|
167
|
+
const bitNumber = triangleGroupBitNumberOf(groupNumber);
|
|
168
|
+
return characters.setRoom({ [fieldName]: isEnabled ? maskNumber & ~bitNumber : maskNumber | bitNumber });
|
|
135
169
|
},
|
|
136
170
|
});
|
|
137
171
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zaisma/physics",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "Physics that works as it's added, alone and online, for every kind of game: one world of bodies (moving, kinematic or fixed), ground for big outdoor maps, joints, wheeled vehicles, seats, collision layers and queries, with characters that move by the game's own rules. Rapier is the engine, shipped inside.",
|
|
5
5
|
"license": "PolyForm-Shield-1.0.0",
|
|
6
6
|
"homepage": "https://zaisma.com",
|
|
@@ -42,15 +42,20 @@
|
|
|
42
42
|
"piece.json",
|
|
43
43
|
"piece.json.js",
|
|
44
44
|
"rapier/LICENSE.txt",
|
|
45
|
+
"rapier/CHANGES.md",
|
|
45
46
|
"rapier/rapier.d.ts",
|
|
46
47
|
"rapier/rapier.js",
|
|
47
48
|
"rapier/rapier.wasm",
|
|
48
49
|
"server.d.ts",
|
|
49
50
|
"server.js",
|
|
51
|
+
"batch.js",
|
|
50
52
|
"simulation.js",
|
|
51
53
|
"sync.js",
|
|
52
54
|
"level-characters.js",
|
|
53
55
|
"world.js",
|
|
54
56
|
"world.d.ts"
|
|
55
|
-
]
|
|
57
|
+
],
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
}
|
|
56
61
|
}
|
package/piece.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"statement_version_number": 1,
|
|
3
3
|
"name": "@zaisma/physics",
|
|
4
|
-
"piece_version": "2.
|
|
4
|
+
"piece_version": "2.5.0",
|
|
5
5
|
"kind": "foundation",
|
|
6
6
|
"standard": "@zaisma/standard",
|
|
7
7
|
"summary": "Physics that works as it's added, alone and online, for every kind of game: one world of bodies (moving, kinematic or fixed), ground for big outdoor maps, joints, wheeled vehicles, seats, collision layers and queries, with characters that move by the game's own rules. Rapier is the engine, shipped inside.",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"characters.js",
|
|
19
19
|
"data.js",
|
|
20
20
|
"rapier/rapier.d.ts",
|
|
21
|
+
"batch.js",
|
|
21
22
|
"simulation.js",
|
|
22
23
|
"sync.js",
|
|
23
24
|
"level-characters.js",
|
|
@@ -25,6 +26,7 @@
|
|
|
25
26
|
"rapier/rapier.js",
|
|
26
27
|
"rapier/rapier.wasm",
|
|
27
28
|
"rapier/LICENSE.txt",
|
|
29
|
+
"rapier/CHANGES.md",
|
|
28
30
|
"README.md",
|
|
29
31
|
"LICENSE.txt"
|
|
30
32
|
],
|
|
@@ -39,13 +41,15 @@
|
|
|
39
41
|
"engine.js",
|
|
40
42
|
"characters.js",
|
|
41
43
|
"data.js",
|
|
44
|
+
"batch.js",
|
|
42
45
|
"simulation.js",
|
|
43
46
|
"sync.js",
|
|
44
47
|
"level-characters.js",
|
|
45
48
|
"piece.json.js",
|
|
46
49
|
"rapier/rapier.js",
|
|
47
50
|
"rapier/rapier.wasm",
|
|
48
|
-
"rapier/LICENSE.txt"
|
|
51
|
+
"rapier/LICENSE.txt",
|
|
52
|
+
"rapier/CHANGES.md"
|
|
49
53
|
],
|
|
50
54
|
"needs": [
|
|
51
55
|
{
|
package/piece.json.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export default {
|
|
3
3
|
"statement_version_number": 1,
|
|
4
4
|
"name": "@zaisma/physics",
|
|
5
|
-
"piece_version": "2.
|
|
5
|
+
"piece_version": "2.5.0",
|
|
6
6
|
"kind": "foundation",
|
|
7
7
|
"standard": "@zaisma/standard",
|
|
8
8
|
"summary": "Physics that works as it's added, alone and online, for every kind of game: one world of bodies (moving, kinematic or fixed), ground for big outdoor maps, joints, wheeled vehicles, seats, collision layers and queries, with characters that move by the game's own rules. Rapier is the engine, shipped inside.",
|
|
@@ -19,6 +19,7 @@ export default {
|
|
|
19
19
|
"characters.js",
|
|
20
20
|
"data.js",
|
|
21
21
|
"rapier/rapier.d.ts",
|
|
22
|
+
"batch.js",
|
|
22
23
|
"simulation.js",
|
|
23
24
|
"sync.js",
|
|
24
25
|
"level-characters.js",
|
|
@@ -26,6 +27,7 @@ export default {
|
|
|
26
27
|
"rapier/rapier.js",
|
|
27
28
|
"rapier/rapier.wasm",
|
|
28
29
|
"rapier/LICENSE.txt",
|
|
30
|
+
"rapier/CHANGES.md",
|
|
29
31
|
"README.md",
|
|
30
32
|
"LICENSE.txt"
|
|
31
33
|
],
|
|
@@ -40,13 +42,15 @@ export default {
|
|
|
40
42
|
"engine.js",
|
|
41
43
|
"characters.js",
|
|
42
44
|
"data.js",
|
|
45
|
+
"batch.js",
|
|
43
46
|
"simulation.js",
|
|
44
47
|
"sync.js",
|
|
45
48
|
"level-characters.js",
|
|
46
49
|
"piece.json.js",
|
|
47
50
|
"rapier/rapier.js",
|
|
48
51
|
"rapier/rapier.wasm",
|
|
49
|
-
"rapier/LICENSE.txt"
|
|
52
|
+
"rapier/LICENSE.txt",
|
|
53
|
+
"rapier/CHANGES.md"
|
|
50
54
|
],
|
|
51
55
|
"needs": [
|
|
52
56
|
{
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# The engine in this folder
|
|
2
|
+
|
|
3
|
+
`rapier.js` and `rapier.wasm` are [Rapier](https://rapier.rs), the physics engine by Dimforge, under the Apache License 2.0 (`LICENSE.txt`). They are built by Zaisma from Rapier's own JavaScript bindings, [dimforge/rapier.js](https://github.com/dimforge/rapier.js) at tag `v0.19.3` (commit `0fd32c1cbbc7018af36f09b190c16ce72fbb9301`), with these changes. `ENGINE_VERSION` says which build it is: `0.19.3-zaisma.1`.
|
|
4
|
+
|
|
5
|
+
## What Zaisma changed
|
|
6
|
+
|
|
7
|
+
Nothing of Rapier's own is removed or behaves differently: its whole JavaScript API is there as in the stock package, and `rapier.d.ts` is the stock one. Added:
|
|
8
|
+
|
|
9
|
+
- **One Rust module, `RawBatch`,** compiled into the bindings. It does in one call, with plain numbers written into two buffers read straight from the engine's memory, what the stock JavaScript API does a number at a time through an object made for each answer: one substep of the world with what was hit, every awake body's place, rotation, speed and spin after a step (put on the steps the piece sends them at), a body's read and write, rays, a ball's and a capsule's sweep, overlaps, a capsule's contacts triangle by triangle, a character's move and a vehicle's update.
|
|
10
|
+
- **A count of the memory the engine holds,** kept by its allocator and read with `RawBatch.heldByteCount()`, `RawBatch.mostHeldByteCount()` and `RawBatch.restartMostHeldByteCount()`: what a level costs is measured exactly instead of guessed from how far the WebAssembly memory has grown.
|
|
11
|
+
- **Shapes shared between colliders,** with `RawBatch.sharedShape(shape)`: a second collider made from the same triangle mesh holds the mesh itself, not a copy, in any world.
|
|
12
|
+
- **Three fields made visible inside the crate** (`pub(crate)`), so the module can reach them: the character controller's `controller`, `result` and `events` (`src/control/character_controller.rs`), and the vehicle controller's `controller` (`src/control/ray_cast_vehicle_controller.rs`).
|
|
13
|
+
- **The loader**: one module that starts the engine from WebAssembly the piece is given (`startEngine`, `startEngineLater`) instead of fetching its own, and that also exports `RawBatch`, `ENGINE_VERSION` and `engineMemory()`.
|
|
14
|
+
- **The crate versions** are locked by Zaisma's own `Cargo.lock`: the tag's lock file was a version behind its `Cargo.toml`.
|
|
15
|
+
|
|
16
|
+
The build is Rapier's own `dim3`, `non-deterministic` configuration (no SIMD), then `wasm-opt -O4`, with `wasm-bindgen` 0.2.100.
|