@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 CHANGED
@@ -22,7 +22,8 @@ export const valley = definePhysicsWorld({
22
22
  bodies: [
23
23
  { id: "ground", motionKind: "fixed", shapes: [{ shapeKind: "heightfield", dataPath: "levels/valley-heights.bin", rowCount: 1501, columnCount: 1501, cellSizeM: 1 }] },
24
24
  { id: "gate", positionM: { x: 40, y: 1, z: 0 }, massKg: 30, shapes: [{ shapeKind: "box", sizeM: { x: 2, y: 2, z: 0.1 } }] },
25
- { id: "lift", motionKind: "kinematic", positionM: { x: 0, y: 0, z: 20 }, shapes: [{ shapeKind: "box", sizeM: { x: 3, y: 0.2, z: 3 } }] },
25
+ { id: "lift", motionKind: "kinematic", positionM: { x: 0, y: 0, z: 20 }, shapes: [{ shapeKind: "box", sizeM: { x: 3, y: 0.2, z: 3 } }],
26
+ timetable: { stops: [{ waitDurationSec: 2 }, { positionM: { x: 0, y: 6, z: 20 }, travelDurationSec: 4, waitDurationSec: 2 }], repeatMode: "back_and_forth" } },
26
27
  { id: "car:1", bodyKindName: "car", positionM: { x: 10, y: 2, z: 0 } },
27
28
  ],
28
29
  bodyKinds: { car: { massKg: 900, shapes: [{ shapeKind: "box", sizeM: { x: 1.8, y: 0.6, z: 4 } }], collisionLayerName: "vehicle", wheels: [/* positionM, radiusM, isSteered, isDriven */], seats: [{ positionM: { x: -0.4, y: 0.3, z: 0 } }] } },
@@ -33,14 +34,16 @@ export const valley = definePhysicsWorld({
33
34
  ```
34
35
 
35
36
  - **Bodies** are `moving` (physics moves them), `kinematic` (the game moves them: lifts, doors on rails, platforms) or `fixed`, made of shapes: `box`, `ball`, `capsule`, `cylinder`, `hull`, and for the fixed world `mesh` and `heightfield`. Metres, kilograms, +Y up. `setBody(id, { motionKind })` parks one or lets it go.
37
+ - **Bodies on a timetable:** a kinematic body with a `timetable` goes between its stops by itself: a lift, a tram, a turning arm, a sweeper. 2 to 32 `stops`, each a place, a turn, how long the trip there takes and how long it waits; `repeatMode` `loop`, `back_and_forth` or `once`; `easeKind` `linear` or `smooth`. Every game and the game server work out where it is from the room's step alone, so nothing of it is sent and a step run again finds it where it was. It carries the characters and bodies standing on it and pushes what's in its way.
36
38
  - **Ground for a big map:** a heightfield or a mesh too big to write inline is read from a data file (`dataPath`, formats in `data.js`; name it `.bin`, a file type Zaisma's upload takes): `loadPhysicsData(world)` in a game, `readDataFile` where the world opens on a game server. Only the ground near something that moves is laid, so a 1,500 m valley costs what the few hundred metres around its players cost. `keepGroundNear(pointM)` keeps it laid where a camera or a query looks.
37
39
  - **Joints:** `fixed`, `ball`, `hinge`, `slider`, `spring` and `rope`, between two bodies, a body and a character, or a body and the world, with limits; declared, or added and let go while playing.
38
40
  - **Vehicles:** a body with `wheels` and `vehicle` drives on ray-cast wheels: suspension, engine, brakes, steering. Whoever sits in seat 0 drives it from their input (`vehicleControls`, by default the stick and `isBraking`); others ride along, seated at once. Wheels never stand on characters (and only on `wheelCollisionLayerNames` when given), a heavy vehicle's springs hold its weight (`maximumSuspensionForceNewtons`), and `drawnBody` answers each wheel's spring and spin to draw it by.
39
41
  - **Bodies while playing:** `setBody` sets a body's place, speed, motion kind, layer or `massKg` (what rides in a cart's bed weighs on it); `addBody` takes a `motionKind` (a building piece placed `fixed` at once); a body or kind may slow at its own `linearDampingNumber` and `angularDampingNumber`. A world played alone frees its engine with `dispose()`.
40
42
  - **Small quick things** (lighter than 8 kg: food, stones, bullets you can see) look ahead for what they'll meet, so they hold on heightfield ground and thin walls up to about 20 m/s at the default `substepCount`; anything faster wants more substeps.
41
43
  - **Collision layers:** named, each saying what it touches, and changed while playing (`setBody`, `setCharacter`): how a ghost passes walls, or a thing goes out of play.
42
- - **Queries:** `castRay`, `castBall`, `castCapsule`, `overlapBall` and `overlapCapsule`, answering whose body or character, where, and the surface's normal; a capsule's contacts say how deep it is in each, to push a game's own movement out of walls. A cast writes into an object the game keeps when given one (`castRay(query, hit)`), so thousands a second make nothing new. `castBall` and `castCapsule` with `isStoppedAtPenetration: false` move out of what they start touching (a camera pressed to a wall).
43
- - **A level's collision as it ships:** a mesh takes a `Float32Array` of points and a `Uint32Array` of indices as they are, up to 2,000,000 triangles, and `triangleGroupNumbers` puts triangles in groups the world switches on and off (`setTriangleGroup`: a door that opens, a bridge that falls).
44
+ - **Queries:** `castRay`, `castBall`, `castCapsule`, `overlapBall` and `overlapCapsule`, answering whose body or character, where, the surface's normal, which of a mesh's triangles (`triangleIndex`) and its surface (`surfaceName`); a capsule's contacts say how deep it is in each, to push a game's own movement out of walls, one a body or, with `isEachTriangle`, one for every triangle it touches. A cast writes into an object the game keeps when given one (`castRay(query, hit)`), and so do `overlapCapsule(query, contacts)` and a step's `moveAndSlide(character, movedM, moved)`, so thousands a second make nothing new.
45
+ - **Surfaces:** a world names its surfaces (`surfaceNames: ["grass", "metal_grate", "glass"]`), a shape says which it is (`surfaceName`) or a mesh says triangle by triangle (`triangleSurfaceIndices`), and every hit and contact answers the name: footsteps, bullet marks, tyre grip. `castBall` and `castCapsule` with `isStoppedAtPenetration: false` move out of what they start touching (a camera pressed to a wall).
46
+ - **A level's collision as it ships:** a mesh takes a `Float32Array` of points and a `Uint32Array` of indices as they are, up to 2,000,000 triangles, and `triangleGroupNumbers` puts triangles in up to 63 groups the world switches on and off (`setTriangleGroup`: a door that opens, a bridge that falls). A group may sit on a collision layer of its own (`triangleGroups: [{ groupNumber: 4, collisionLayerName: "glass" }]`: glass that stops soldiers and lets shots through). The engine holds a mesh once however many worlds are made from it: every room a game server copy runs, a menu's world and the match's.
44
47
  - **The engine itself:** `physicsEngine()` answers Rapier as the piece ships it; `rapier/rapier.d.ts` types all of it (what a game uses most closely, the rest loosely). A body a game makes straight in the engine (`stepContext.rapierWorld`) isn't the piece's: it's never sent, nor put back on a replay, so online it differs in every game. Declare it (or add it with `addBody`), or hold it to a character with a joint (`otherBodyId`), and it's shared like any other; keep the engine's own for what's only drawn.
45
48
 
46
49
  ## Characters
@@ -83,7 +86,7 @@ const hit = soldiers.castRay({ fromM, directionM, maximumDistanceM: 200 }, keptH
83
86
  // on the game server: soldiers.onServer(state, room).stateAt(targetId, viewTickNumber(shooterId)), setTriangleGroup(3, false)
84
87
  ```
85
88
 
86
- It's @zaisma/net's characters underneath, so everything they do is there (`audience: "own"`, `othersStepSizeNumber`, `roomFields`, `emit`). Nothing in the level moves and characters walk through each other; one copy of the level serves every room a game server runs, and each room switches its own triangle groups. The settings are declared alike in the game and on the game server.
89
+ It's @zaisma/net's characters underneath, so everything they do is there (`audience: "own"`, `othersStepSizeNumber`, `roomFields`, `emit`). The level moves only by timetable (a lift or a platform: a soldier rides it and is pushed by it, and `drawnBody` or `applyTo` draws it; a query finds it where the room's present step has it) and characters walk through each other; one copy of the level serves every room a game server runs, and each room switches its own triangle groups. The settings are declared alike in the game and on the game server.
87
90
 
88
91
  ## Alone
89
92
 
@@ -144,4 +147,27 @@ Opened like every piece's ([`@zaisma/piece`](../piece)), and changed while playi
144
147
 
145
148
  Online, whoever runs the world decides them. Hooks: `beforePutBack` (where something that fell out of the world comes back) and `bodyHit` (for a thud or dust). A character kind adds its own (`characterSettings`, `characterHooks`).
146
149
 
150
+ ## Measured
151
+
152
+ What the piece costs, measured on a MacBook (Apple M5, 10 cores) with other work running, on version 2.5.0 (December 4, 2026). `node --expose-gc scripts/physics-bench.mjs` in Zaisma's repository measures it again.
153
+
154
+ | What | Measured |
155
+ | --- | --- |
156
+ | A step of 999 crates falling, 2 substeps | 6.1 ms; the engine alone takes 5.8 (1.04 times); through a game server's room 8.0 |
157
+ | The same 999 at rest | 0.14 ms |
158
+ | Garbage a step leaves, 999 crates | 5 to 10 KB (2.4.1 left 7.5 MB) |
159
+ | 999 things spread over a 1 km heightfield, falling | 6.4 ms a step, worst 10.5; at rest 0.2 ms |
160
+ | 64 characters walking | 0.9 ms a step |
161
+ | 8 players and 300 crates kept moving, to one player | 9.7 KB a second down, 0.6 up |
162
+
163
+ A shooter's real levels, beside that shooter's own collision code written for nothing else (Shieldbreak: the same 20,000 rays and capsules through both):
164
+
165
+ | Level | Triangles | Memory | Opens in | A second room | A ray | A capsule's contacts |
166
+ | --- | --- | --- | --- | --- | --- | --- |
167
+ | Cascade | 72,442 | 6.5 MB (its own: 5.7) | 37 ms (35) | 0.04 MB, 5 ms | 1.1 µs (1.3) | 2.1 µs (2.3) |
168
+ | Liftoff | 48,696 | 4.4 MB (1.6) | 22 ms (22) | 0.04 MB, 3 ms | 1.2 µs (1.4) | 1.9 µs (1.9) |
169
+ | Caldera | 99,087 | 9.0 MB (7.9) | 45 ms (43) | 0.04 MB, 6 ms | 1.4 µs (1.3) | 1.8 µs (2.0) |
170
+
171
+ Every ray answered the same as the shooter's own, and a query leaves 100 to 300 bytes of garbage.
172
+
147
173
  `index.d.ts`, `server.d.ts` and `rapier/rapier.d.ts` (the engine, through `physicsEngine()`) are the reference. How pieces work: [docs/PIECES.md](https://github.com/zaisma/zaisma-sdk/blob/main/docs/PIECES.md). PolyForm Shield 1.0.0 licensed (`LICENSE.txt`): use it in any game, sold or free, on Zaisma, on your own server or anywhere else; only a product that competes with it, or with what Zaisma makes with it, is ruled out. Versions before 2.4.0 were MIT licensed and stay so; Rapier is Apache-2.0 licensed (`rapier/LICENSE.txt`).
package/batch.js ADDED
@@ -0,0 +1,155 @@
1
+ // The piece's side of the engine's batch calls (scripts/physics-engine/zaisma.rs). Rapier's own JavaScript answers one number
2
+ // at a time, each through an object made for the answer; these calls take and answer plain numbers, written into two
3
+ // buffers read straight from the engine's memory. So a step that settles a thousand bodies, and a ray, a sweep or a
4
+ // character's move, leave nothing behind for the garbage collector.
5
+ //
6
+ // The buffers' first part is where a query, a read or a move answers (overwritten by the next call); past it are the
7
+ // bodies a step left awake, kept until the next step.
8
+
9
+ /** Where a call answers: the floats and whole numbers before the awake bodies. */
10
+ export const ANSWER_FLOAT_COUNT = 4096;
11
+ export const ANSWER_NUMBER_COUNT = 1024;
12
+ /** Floats for one body: its place (3), rotation (4), speed (3) and spin (3). */
13
+ export const BODY_FLOAT_COUNT = 13;
14
+ /** Floats for one contact: its point (3), normal (3), depth (1) and its surface's own normal (3). */
15
+ export const CONTACT_FLOAT_COUNT = 10;
16
+ /** Whole numbers for one contact: its owner's tag, which triangle (or NO_TRIANGLE_INDEX) and which collider. */
17
+ export const CONTACT_NUMBER_COUNT = 3;
18
+
19
+ /** A tag's low bits: whose a body or a collider is. Its number is above them, from bit 8. */
20
+ export const BODY_TAG_NUMBER = 1;
21
+ export const CHARACTER_TAG_NUMBER = 2;
22
+ export const TAG_KIND_MASK_NUMBER = 3;
23
+ /** A body's tag: small and maybe quick, so it looks ahead for what it'll meet. */
24
+ export const SMALL_TAG_NUMBER = 4;
25
+
26
+ export const SKIPS_CHARACTERS_FLAG_NUMBER = 1;
27
+ export const EXCLUDES_BODY_FLAG_NUMBER = 2;
28
+ export const EACH_TRIANGLE_FLAG_NUMBER = 4;
29
+ export const ONE_SIDED_FLAG_NUMBER = 8;
30
+
31
+ export const WRITE_MOVING_NUMBER = 0;
32
+ export const WRITE_RESTING_NUMBER = 1;
33
+ export const WRITE_PLACE_ONLY_NUMBER = 2;
34
+
35
+ /** A hit or a contact that isn't on a triangle. */
36
+ export const NO_TRIANGLE_INDEX = 0xffffffff;
37
+
38
+ const EMPTY_FLOATS = new Float32Array(0);
39
+ const EMPTY_NUMBERS = new Uint32Array(0);
40
+
41
+ /** The batch calls of one engine world, with the engine's own sets they work on. */
42
+ export function createBatch(engine, rapierWorld) {
43
+ let awakeBodyRoomCount = 64;
44
+ const raw = new engine.RawBatch(ANSWER_FLOAT_COUNT + awakeBodyRoomCount * BODY_FLOAT_COUNT, ANSWER_NUMBER_COUNT + awakeBodyRoomCount);
45
+ // The engine's memory is another buffer after it grows, and a view of the old one is then empty.
46
+ let floats = EMPTY_FLOATS;
47
+ let numbers = EMPTY_NUMBERS;
48
+ return {
49
+ raw,
50
+ bodies: rapierWorld.bodies.raw,
51
+ colliders: rapierWorld.colliders.raw,
52
+ broadPhase: rapierWorld.broadPhase.raw,
53
+ narrowPhase: rapierWorld.narrowPhase.raw,
54
+ islands: rapierWorld.islands.raw,
55
+ pipeline: rapierWorld.physicsPipeline.raw,
56
+ integrationParameters: rapierWorld.integrationParameters.raw,
57
+ impulseJoints: rapierWorld.impulseJoints.raw,
58
+ multibodyJoints: rapierWorld.multibodyJoints.raw,
59
+ ccdSolver: rapierWorld.ccdSolver.raw,
60
+ /** The floats the last call answered into. Read at once: the next call may move them. */
61
+ floats() {
62
+ if (floats.length === 0) floats = new Float32Array(engine.engineMemory().buffer, raw.floatsPointer(), raw.floatCount());
63
+ return floats;
64
+ },
65
+ /** The whole numbers the last call answered into. Read at once: the next call may move them. */
66
+ numbers() {
67
+ if (numbers.length === 0) numbers = new Uint32Array(engine.engineMemory().buffer, raw.numbersPointer(), raw.numberCount());
68
+ return numbers;
69
+ },
70
+ /** Makes room for this many awake bodies. */
71
+ reserveAwake(bodyCount) {
72
+ if (bodyCount <= awakeBodyRoomCount) return;
73
+ awakeBodyRoomCount = Math.max(bodyCount, awakeBodyRoomCount * 2);
74
+ raw.reserve(ANSWER_FLOAT_COUNT + awakeBodyRoomCount * BODY_FLOAT_COUNT, ANSWER_NUMBER_COUNT + awakeBodyRoomCount);
75
+ floats = EMPTY_FLOATS;
76
+ numbers = EMPTY_NUMBERS;
77
+ },
78
+ free() {
79
+ raw.free();
80
+ },
81
+ };
82
+ }
83
+
84
+ // ---- A level's mesh, held once ----
85
+ //
86
+ // A mesh's triangles, and what the engine builds to search them, are the same in every world made from the same
87
+ // level: every room a game server copy runs, a menu's world and the match's. So the engine holds each once, and
88
+ // every collider made from it shares it. It's kept by the list of triangles it was made from (a world's own
89
+ // declaration, or a data file read once), and let go when that list is.
90
+
91
+ const heldByTriangleIndices = new WeakMap();
92
+ const letGo = typeof FinalizationRegistry === "undefined" ? { register() {} } : new FinalizationRegistry((raws) => raws.forEach((raw) => raw.free()));
93
+
94
+ /**
95
+ * A mesh as a collider is made from it (new engine.ColliderDesc(mesh.shape)), built the first time it's asked for
96
+ * and shared after. With groupNumbers (a group for each triangle), only groupNumber's triangles and the points they
97
+ * use. Answers { shape, triangleIndexes }: triangleIndexes says which of the whole mesh's triangles each of the
98
+ * shape's is, or null when they're the same. Null when the triangles make no mesh.
99
+ */
100
+ export function heldMesh(engine, pointsM, triangleIndices, groupNumbers = null, groupNumber = 0) {
101
+ let held = heldByTriangleIndices.get(triangleIndices);
102
+ if (!held || held.pointsM !== pointsM || held.groupNumbers !== groupNumbers) {
103
+ // The same triangles with other points or groups: a colliders' own hold keeps what it was made from.
104
+ held?.raws.splice(0).forEach((raw) => raw.free());
105
+ held = { pointsM, groupNumbers, raws: [], meshByGroupNumber: new Map() };
106
+ heldByTriangleIndices.set(triangleIndices, held);
107
+ letGo.register(triangleIndices, held.raws);
108
+ }
109
+ // -1 for the whole mesh, not a group of it.
110
+ const heldGroupNumber = groupNumbers ? groupNumber : -1;
111
+ if (held.meshByGroupNumber.has(heldGroupNumber)) return held.meshByGroupNumber.get(heldGroupNumber);
112
+ let meshPointsM = pointsM;
113
+ let meshTriangleIndices = triangleIndices;
114
+ let triangleIndexes = null;
115
+ if (groupNumbers) {
116
+ let triangleCount = 0;
117
+ for (let triangleIndex = 0; triangleIndex < groupNumbers.length; triangleIndex += 1) if (groupNumbers[triangleIndex] === groupNumber) triangleCount += 1;
118
+ triangleIndexes = new Uint32Array(triangleCount);
119
+ meshTriangleIndices = new Uint32Array(triangleCount * 3);
120
+ // Only the points the group's triangles use, numbered again in the order they're met.
121
+ const newPointIndexes = new Int32Array(pointsM.length / 3).fill(-1);
122
+ let pointCount = 0;
123
+ let writtenCount = 0;
124
+ for (let triangleIndex = 0; triangleIndex < groupNumbers.length; triangleIndex += 1) {
125
+ if (groupNumbers[triangleIndex] !== groupNumber) continue;
126
+ triangleIndexes[writtenCount] = triangleIndex;
127
+ for (let cornerIndex = 0; cornerIndex < 3; cornerIndex += 1) {
128
+ const pointIndex = triangleIndices[triangleIndex * 3 + cornerIndex];
129
+ if (newPointIndexes[pointIndex] === -1) {
130
+ newPointIndexes[pointIndex] = pointCount;
131
+ pointCount += 1;
132
+ }
133
+ meshTriangleIndices[writtenCount * 3 + cornerIndex] = newPointIndexes[pointIndex];
134
+ }
135
+ writtenCount += 1;
136
+ }
137
+ meshPointsM = new Float32Array(pointCount * 3);
138
+ for (let pointIndex = 0; pointIndex < newPointIndexes.length; pointIndex += 1) {
139
+ const newPointIndex = newPointIndexes[pointIndex];
140
+ if (newPointIndex === -1) continue;
141
+ meshPointsM[newPointIndex * 3] = pointsM[pointIndex * 3];
142
+ meshPointsM[newPointIndex * 3 + 1] = pointsM[pointIndex * 3 + 1];
143
+ meshPointsM[newPointIndex * 3 + 2] = pointsM[pointIndex * 3 + 2];
144
+ }
145
+ }
146
+ const raw = meshTriangleIndices.length ? new engine.TriMesh(meshPointsM, meshTriangleIndices).intoRaw() : undefined;
147
+ let mesh = null;
148
+ if (raw) {
149
+ held.raws.push(raw);
150
+ // A collider takes its own hold on the shape and frees it once it's made: the one kept here stays.
151
+ mesh = { shape: { type: engine.ShapeType.TriMesh, intoRaw: () => engine.RawBatch.sharedShape(raw) }, triangleIndexes };
152
+ }
153
+ held.meshByGroupNumber.set(heldGroupNumber, mesh);
154
+ return mesh;
155
+ }
package/characters.js CHANGED
@@ -44,7 +44,7 @@ export function stickVehicleControls(input) {
44
44
  * - inputFields: what moves it, as @zaisma/net's input fields.
45
45
  * - step(character, input, stepContext): one fixed step of one character. character has id, own (the kind's fields,
46
46
  * change them in place), velocity { x, y, z } (a kinematic one's), facingYawRad, isGrounded, airTickCount, and
47
- * rigidBody and collider (the engine's). stepContext has moveAndSlide(character, movedM), castRay, castBall,
47
+ * rigidBody and collider (the engine's). stepContext has moveAndSlide(character, movedM, into), castRay, castBall,
48
48
  * overlapBall, bodyState(bodyId), characterState(characterId), setJoint(jointId, { isEnabled }),
49
49
  * setLayer(character, collisionLayerName), tickDurationSec, tuning (the world's numbers), isInCharge, isReplay and emit.
50
50
  * A field with isShared: false is the kind's own memory in each game, never sent nor compared: set by each step
@@ -118,9 +118,9 @@ const RUN_SPEED_PCT = 180;
118
118
 
119
119
  /**
120
120
  * Where the player pushes, in the world, from a stick and where they look: moveY forward along the view, moveX to
121
- * the view's right, never more than 1 long.
121
+ * the view's right, never more than 1 long. With into, written there instead of a new object.
122
122
  */
123
- export function wishOf(input) {
123
+ export function wishOf(input, into = null) {
124
124
  const forwardX = Math.sin(input.viewYawRad ?? 0);
125
125
  const forwardZ = Math.cos(input.viewYawRad ?? 0);
126
126
  let wishX = -forwardZ * (input.moveX ?? 0) + forwardX * (input.moveY ?? 0);
@@ -130,9 +130,18 @@ export function wishOf(input) {
130
130
  wishX /= wishNumber;
131
131
  wishZ /= wishNumber;
132
132
  }
133
- return { x: wishX, z: wishZ, lengthNumber: Math.min(wishNumber, 1) };
133
+ const wish = into ?? { x: 0, z: 0, lengthNumber: 0 };
134
+ wish.x = wishX;
135
+ wish.z = wishZ;
136
+ wish.lengthNumber = Math.min(wishNumber, 1);
137
+ return wish;
134
138
  }
135
139
 
140
+ // One step's own notes, used at once and kept by nothing: a room of walkers leaves nothing for the garbage collector.
141
+ const stepWish = { x: 0, z: 0, lengthNumber: 0 };
142
+ const stepDesiredM = { x: 0, y: 0, z: 0 };
143
+ const stepMoved = { movedM: { x: 0, y: 0, z: 0 }, isGrounded: false };
144
+
136
145
  /**
137
146
  * The piece's own character kind: it walks (and runs) where the stick points along the view, jumps, steps up stairs,
138
147
  * climbs slopes up to the world's steepest, slides along walls, pushes what's in its way, rides what it stands on,
@@ -152,7 +161,7 @@ export function walkingCharacters() {
152
161
  /** One step of walking: the speed the stick asks, gravity, a jump, the move, and where it faces. */
153
162
  export function walkStep(character, input, stepContext) {
154
163
  const { tickDurationSec, tuning } = stepContext;
155
- const wish = wishOf(input);
164
+ const wish = wishOf(input, stepWish);
156
165
  const speedMPerSec = tuning.walkSpeedMPerSec * (input.isRunning ? RUN_SPEED_PCT / 100 : 1);
157
166
  const velocity = character.velocity;
158
167
  const accelerationMPerSecPerSec = character.isGrounded ? GROUND_ACCELERATION_M_PER_SEC_PER_SEC : AIR_ACCELERATION_M_PER_SEC_PER_SEC;
@@ -169,7 +178,10 @@ export function walkStep(character, input, stepContext) {
169
178
  velocity.y = tuning.jumpSpeedMPerSec;
170
179
  character.airTickCount = COYOTE_TICK_COUNT + 1;
171
180
  } else velocity.y -= tuning.gravityMPerSecPerSec * tickDurationSec;
172
- const moved = stepContext.moveAndSlide(character, { x: velocity.x * tickDurationSec, y: velocity.y * tickDurationSec, z: velocity.z * tickDurationSec });
181
+ stepDesiredM.x = velocity.x * tickDurationSec;
182
+ stepDesiredM.y = velocity.y * tickDurationSec;
183
+ stepDesiredM.z = velocity.z * tickDurationSec;
184
+ const moved = stepContext.moveAndSlide(character, stepDesiredM, stepMoved);
173
185
  // What a wall or the ground took away is gone from the speed too.
174
186
  velocity.x = Math.abs(moved.movedM.x) < 1e-6 ? 0 : moved.movedM.x / tickDurationSec;
175
187
  velocity.z = Math.abs(moved.movedM.z) < 1e-6 ? 0 : moved.movedM.z / tickDurationSec;
@@ -179,7 +191,9 @@ export function walkStep(character, input, stepContext) {
179
191
  character.isGrounded = moved.isGrounded && velocity.y <= 0;
180
192
  character.airTickCount = character.isGrounded ? 0 : Math.min(character.airTickCount + 1, 250);
181
193
  // A speed too small to see is none, so a walker standing still comes to rest and costs nothing.
182
- for (const axis of ["x", "y", "z"]) if (Math.abs(velocity[axis]) < 1e-3) velocity[axis] = 0;
194
+ if (Math.abs(velocity.x) < 1e-3) velocity.x = 0;
195
+ if (Math.abs(velocity.y) < 1e-3) velocity.y = 0;
196
+ if (Math.abs(velocity.z) < 1e-3) velocity.z = 0;
183
197
  if (wish.lengthNumber > 0.1) {
184
198
  const turnRad = Math.atan2(Math.sin(Math.atan2(wish.x, wish.z) - character.facingYawRad), Math.cos(Math.atan2(wish.x, wish.z) - character.facingYawRad));
185
199
  const mostTurnRad = TURN_SPEED_RAD_PER_SEC * tickDurationSec;