@zaisma/physics 2.5.0 → 2.6.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 +45 -11
- package/batch.js +100 -49
- package/characters.js +11 -5
- package/declare.js +41 -13
- package/index.d.ts +34 -6
- package/level-characters.js +2 -1
- package/package.json +1 -1
- package/piece.json +1 -1
- package/piece.json.js +1 -1
- package/rapier/CHANGES.md +3 -1
- package/rapier/rapier.js +13 -2
- package/rapier/rapier.wasm +0 -0
- package/simulation.js +172 -45
- package/sync.js +26 -13
package/rapier/rapier.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Rapier 0.19.3 (https://rapier.rs), the physics engine @zaisma/physics runs on, as Zaisma builds it (0.19.3-zaisma.
|
|
1
|
+
// Rapier 0.19.3 (https://rapier.rs), the physics engine @zaisma/physics runs on, as Zaisma builds it (0.19.3-zaisma.2): Rapier's
|
|
2
2
|
// JavaScript bindings at https://github.com/dimforge/rapier.js v0.19.3, with Zaisma's batch calls added and nothing of Rapier's
|
|
3
3
|
// taken out, written as one module by Zaisma's engine build. Apache-2.0 licensed: LICENSE.txt beside this file; what
|
|
4
4
|
// Zaisma changed is listed in CHANGES.md beside it. Not edited by hand. startEngine and startEngineLater at
|
|
@@ -736,6 +736,17 @@ var RawBatch = class {
|
|
|
736
736
|
const ret = wasm.rawbatch_heldByteCount();
|
|
737
737
|
return ret >>> 0;
|
|
738
738
|
}
|
|
739
|
+
/**
|
|
740
|
+
* Marks a collider as a piece of the level: one on a body that is fixed for good. The broad phase, as Zaisma
|
|
741
|
+
* builds it, never pairs two of them, so a level made of tens of thousands of pieces that touch each other
|
|
742
|
+
* costs a step nothing. (It reads bit 0 of a collider's user data, which nothing else in the bindings sets.)
|
|
743
|
+
* @param {RawColliderSet} colliders
|
|
744
|
+
* @param {number} collider
|
|
745
|
+
*/
|
|
746
|
+
static markLevelCollider(colliders, collider) {
|
|
747
|
+
_assertClass(colliders, RawColliderSet);
|
|
748
|
+
wasm.rawbatch_markLevelCollider(colliders.__wbg_ptr, collider);
|
|
749
|
+
}
|
|
739
750
|
/**
|
|
740
751
|
* The most bytes they've held at once since the count was last started again.
|
|
741
752
|
* @returns {number}
|
|
@@ -14398,7 +14409,7 @@ function reserveMemory(extraBytesCount) {
|
|
|
14398
14409
|
}
|
|
14399
14410
|
|
|
14400
14411
|
// ../../../../.cache/zaisma/physics-engine/package/engine-entry.js
|
|
14401
|
-
var ENGINE_VERSION = "0.19.3-zaisma.
|
|
14412
|
+
var ENGINE_VERSION = "0.19.3-zaisma.2";
|
|
14402
14413
|
var importObject = () => ({ "./rapier_wasm3d_bg.js": rapier_wasm3d_bg_exports });
|
|
14403
14414
|
var engineExports = null;
|
|
14404
14415
|
function started(exports) {
|
package/rapier/rapier.wasm
CHANGED
|
Binary file
|
package/simulation.js
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
// whatever was written in since.
|
|
16
16
|
import { MOTION_KINDS, RIDING_LAYER_NAME, refuse, timetablePlaceInto, triangleGroupBitNumberOf, triangleGroupMaskIndexOf } from "./declare.js";
|
|
17
17
|
import { dataOf } from "./data.js";
|
|
18
|
-
import { ANSWER_FLOAT_COUNT, ANSWER_NUMBER_COUNT, BODY_FLOAT_COUNT, BODY_TAG_NUMBER, CHARACTER_TAG_NUMBER, CONTACT_FLOAT_COUNT, CONTACT_NUMBER_COUNT, EACH_TRIANGLE_FLAG_NUMBER, EXCLUDES_BODY_FLAG_NUMBER, NO_TRIANGLE_INDEX, ONE_SIDED_FLAG_NUMBER, SKIPS_CHARACTERS_FLAG_NUMBER, SMALL_TAG_NUMBER, TAG_KIND_MASK_NUMBER, WRITE_MOVING_NUMBER, WRITE_PLACE_ONLY_NUMBER, WRITE_RESTING_NUMBER, createBatch,
|
|
18
|
+
import { ANSWER_FLOAT_COUNT, ANSWER_NUMBER_COUNT, BODY_FLOAT_COUNT, BODY_TAG_NUMBER, CHARACTER_TAG_NUMBER, CONTACT_FLOAT_COUNT, CONTACT_NUMBER_COUNT, EACH_TRIANGLE_FLAG_NUMBER, EXCLUDES_BODY_FLAG_NUMBER, NO_TRIANGLE_INDEX, ONE_SIDED_FLAG_NUMBER, SKIPS_CHARACTERS_FLAG_NUMBER, SMALL_TAG_NUMBER, TAG_KIND_MASK_NUMBER, WRITE_MOVING_NUMBER, WRITE_PLACE_ONLY_NUMBER, WRITE_RESTING_NUMBER, createBatch, heldMeshParts, isBigMesh } from "./batch.js";
|
|
19
19
|
|
|
20
20
|
/** A hit slower than this isn't told. */
|
|
21
21
|
const MINIMUM_TOLD_HIT_SPEED_M_PER_SEC = 1.2;
|
|
@@ -103,6 +103,9 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
103
103
|
let sameWriteCount = 0;
|
|
104
104
|
let changedWriteCount = 0;
|
|
105
105
|
let movableBodyCount = 0;
|
|
106
|
+
/** The bodies that aren't fixed for good, by index: what a step goes through, however many pieces the level is made of. */
|
|
107
|
+
const unfixedBodies = [];
|
|
108
|
+
let lowestFreeBodyIndex = 0;
|
|
106
109
|
const bodies = [];
|
|
107
110
|
const bodyIndexById = new Map();
|
|
108
111
|
/** The bodies on a timetable, and the step their places were last worked out for. */
|
|
@@ -154,7 +157,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
154
157
|
const surfaceIndexByName = new Map(world.surfaceNames.map((surfaceName, surfaceIndex) => [surfaceName, surfaceIndex]));
|
|
155
158
|
/** By collider number: { isMesh, triangleIndexes, surfaceIndices, surfaceName }, for a mesh's colliders and any with a surface. */
|
|
156
159
|
const hitInfoByColliderNumber = new Map();
|
|
157
|
-
/** Notes whose a collider is and, from its shape, what a hit on it says. mesh is
|
|
160
|
+
/** Notes whose a collider is and, from its shape, what a hit on it says. mesh is one of heldMeshParts' parts for a mesh's collider. */
|
|
158
161
|
function ownCollider(collider, owner, shape = null, mesh = null) {
|
|
159
162
|
ownerByColliderHandle.set(collider.handle, owner);
|
|
160
163
|
const surfaceName = shape?.surfaceName ?? null;
|
|
@@ -182,7 +185,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
182
185
|
else if (shape.shapeKind === "hull") description = engine.ColliderDesc.convexHull(new Float32Array(shape.pointsM));
|
|
183
186
|
else {
|
|
184
187
|
const meshData = dataOf(shape, readDataFile);
|
|
185
|
-
describedMesh =
|
|
188
|
+
describedMesh = heldMeshParts(engine, meshData.pointsM, meshData.triangleIndices)[0] ?? null;
|
|
186
189
|
description = describedMesh ? new engine.ColliderDesc(describedMesh.shape) : null;
|
|
187
190
|
}
|
|
188
191
|
// A hull of points all in one plane has no inside: a small ball stands in, so the body is still there.
|
|
@@ -281,8 +284,8 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
281
284
|
const untilStepCount = stepCount + GROUND_KEPT_STEP_COUNT;
|
|
282
285
|
for (const ground of grounds) if (ground.body.motionIndex !== 2) ground.isPlaced = false;
|
|
283
286
|
const isSettled = settledStepCount === stepCount;
|
|
284
|
-
for (const body of
|
|
285
|
-
if (
|
|
287
|
+
for (const body of unfixedBodies) {
|
|
288
|
+
if (body.motionIndex === 2) continue;
|
|
286
289
|
if (isSettled && body.settledStepCount === stepCount) {
|
|
287
290
|
const floats = batch.floats();
|
|
288
291
|
const firstIndex = ANSWER_FLOAT_COUNT + body.settledIndex * BODY_FLOAT_COUNT;
|
|
@@ -332,12 +335,16 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
332
335
|
const bodyTypeOf = (motionIndex) => (motionIndex === 0 ? engine.RigidBodyType.Dynamic : motionIndex === 1 ? engine.RigidBodyType.KinematicVelocityBased : engine.RigidBodyType.Fixed);
|
|
333
336
|
|
|
334
337
|
/** Adds a body (checkBody's answer) at an index: the next free one, or the one given. Answers the index. */
|
|
338
|
+
/** The lowest index no body has. */
|
|
339
|
+
function freeBodyIndex() {
|
|
340
|
+
while (lowestFreeBodyIndex < bodies.length && bodies[lowestFreeBodyIndex] !== null) lowestFreeBodyIndex += 1;
|
|
341
|
+
return lowestFreeBodyIndex;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** How many times what never moves has changed: a fixed body gone or set moving, a triangle group switched. What rests on it is judged again. */
|
|
345
|
+
let levelChangeCount = 0;
|
|
335
346
|
function addBody(declaration, givenIndex = null) {
|
|
336
|
-
|
|
337
|
-
if (index === null) {
|
|
338
|
-
index = bodies.findIndex((held) => held === null);
|
|
339
|
-
if (index === -1) index = bodies.length;
|
|
340
|
-
}
|
|
347
|
+
const index = givenIndex ?? freeBodyIndex();
|
|
341
348
|
while (bodies.length <= index) bodies.push(null);
|
|
342
349
|
const motionIndex = MOTION_KINDS.indexOf(declaration.motionKind);
|
|
343
350
|
const description = (motionIndex === 0 ? engine.RigidBodyDesc.dynamic() : motionIndex === 1 ? engine.RigidBodyDesc.kinematicVelocityBased() : engine.RigidBodyDesc.fixed())
|
|
@@ -349,12 +356,18 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
349
356
|
const layerIndex = layerIndexByName.get(declaration.collisionLayerName);
|
|
350
357
|
const body = { index, declaration, rigidBody, colliders: [], motionIndex, declaredMotionIndex: motionIndex, layerIndex, vehicle: null, settledIndex: 0, settledStepCount: -1, groundKeptStepCount: -Infinity, steppedFields: null };
|
|
351
358
|
bodies[index] = body;
|
|
359
|
+
if (motionIndex !== 2) {
|
|
360
|
+
// Kept in the order of their indexes, so every side goes through them alike.
|
|
361
|
+
let placeIndex = unfixedBodies.length;
|
|
362
|
+
while (placeIndex > 0 && unfixedBodies[placeIndex - 1].index > index) placeIndex -= 1;
|
|
363
|
+
unfixedBodies.splice(placeIndex, 0, body);
|
|
364
|
+
}
|
|
352
365
|
for (const shape of declaration.shapes) {
|
|
353
366
|
if (shape.shapeKind === "heightfield") {
|
|
354
367
|
addGround(body, shape);
|
|
355
368
|
continue;
|
|
356
369
|
}
|
|
357
|
-
if (shape.shapeKind === "mesh" && shape.triangleGroupNumbers) {
|
|
370
|
+
if (shape.shapeKind === "mesh" && (shape.triangleGroupNumbers || isBigMesh(dataOf(shape, readDataFile).triangleIndices))) {
|
|
358
371
|
addGroupedMesh(body, shape);
|
|
359
372
|
continue;
|
|
360
373
|
}
|
|
@@ -363,6 +376,8 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
363
376
|
raw.setColliderTag(collider.handle, bodyTagOf(index));
|
|
364
377
|
body.colliders.push(collider);
|
|
365
378
|
}
|
|
379
|
+
// A body fixed for good is a piece of the level: the engine never pairs two of them, however many touch.
|
|
380
|
+
if (motionIndex === 2) for (const collider of body.colliders) engine.RawBatch.markLevelCollider(batch.colliders, collider.handle);
|
|
366
381
|
if (motionIndex !== 2) {
|
|
367
382
|
movableBodyCount += 1;
|
|
368
383
|
batch.reserveAwake(movableBodyCount);
|
|
@@ -386,15 +401,17 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
386
401
|
const groupColliders = new Map();
|
|
387
402
|
const offTriangleGroupMaskNumbers = [0, 0, 0];
|
|
388
403
|
const isTriangleGroupOff = (groupNumber) => Boolean(offTriangleGroupMaskNumbers[triangleGroupMaskIndexOf(groupNumber)] & triangleGroupBitNumberOf(groupNumber));
|
|
389
|
-
/**
|
|
404
|
+
/**
|
|
405
|
+
* A mesh of several colliders: one for each group its triangles come in, so a door or a bridge can come and go,
|
|
406
|
+
* and a big mesh's in tiles.
|
|
407
|
+
*/
|
|
390
408
|
function addGroupedMesh(body, shape) {
|
|
391
409
|
const { pointsM, triangleIndices } = dataOf(shape, readDataFile);
|
|
392
410
|
const groupNumbers = shape.triangleGroupNumbers;
|
|
393
|
-
if (groupNumbers.length !== triangleIndices.length / 3) refuse("bad_body", `${body.declaration.id}'s mesh has ${triangleIndices.length / 3} triangles and ${groupNumbers.length} triangleGroupNumbers.`, "triangleGroupNumbers", "Give one group number for each of the mesh's triangles.");
|
|
394
|
-
if (shape.triangleSurfaceIndices && shape.triangleSurfaceIndices.length !==
|
|
395
|
-
for (const
|
|
396
|
-
const
|
|
397
|
-
if (!mesh) continue;
|
|
411
|
+
if (groupNumbers && groupNumbers.length !== triangleIndices.length / 3) refuse("bad_body", `${body.declaration.id}'s mesh has ${triangleIndices.length / 3} triangles and ${groupNumbers.length} triangleGroupNumbers.`, "triangleGroupNumbers", "Give one group number for each of the mesh's triangles.");
|
|
412
|
+
if (shape.triangleSurfaceIndices && shape.triangleSurfaceIndices.length !== triangleIndices.length / 3) refuse("bad_body", `${body.declaration.id}'s mesh has ${triangleIndices.length / 3} triangles and ${shape.triangleSurfaceIndices.length} triangleSurfaceIndices.`, "triangleSurfaceIndices", "Give one surface for each of the mesh's triangles.");
|
|
413
|
+
for (const mesh of heldMeshParts(engine, pointsM, triangleIndices, groupNumbers)) {
|
|
414
|
+
const { groupNumber } = mesh;
|
|
398
415
|
// A group may be on a layer of its own: glass that stops players and not shots, a rail only vehicles meet.
|
|
399
416
|
const ownLayerName = shape.triangleGroups?.find((group) => group.groupNumber === groupNumber)?.collisionLayerName;
|
|
400
417
|
const description = new engine.ColliderDesc(mesh.shape).setTranslation(shape.positionM.x, shape.positionM.y, shape.positionM.z).setRotation(shape.rotation);
|
|
@@ -415,6 +432,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
415
432
|
offTriangleGroupMaskNumbers[0] = firstMaskNumber;
|
|
416
433
|
offTriangleGroupMaskNumbers[1] = secondMaskNumber;
|
|
417
434
|
offTriangleGroupMaskNumbers[2] = thirdMaskNumber;
|
|
435
|
+
levelChangeCount += 1;
|
|
418
436
|
for (const [groupNumber, colliders] of groupColliders) for (const collider of colliders) collider.setEnabled(!isTriangleGroupOff(groupNumber));
|
|
419
437
|
isSceneOwed = true;
|
|
420
438
|
}
|
|
@@ -422,6 +440,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
422
440
|
function removeBody(index) {
|
|
423
441
|
const body = bodies[index];
|
|
424
442
|
if (!body) return false;
|
|
443
|
+
levelChangeCount += 1;
|
|
425
444
|
characterKind.onBodyRemoved?.(index, stepTools);
|
|
426
445
|
for (const [jointId, joint] of joints) if (joint.bodyIndexes.includes(index)) removeJoint(jointId);
|
|
427
446
|
for (const character of characters) if (character?.rideBodyIndex === index) unseat(character);
|
|
@@ -429,7 +448,11 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
429
448
|
for (const collider of body.colliders) disownCollider(collider);
|
|
430
449
|
for (const ground of grounds.filter((each) => each.body === body)) for (const tile of ground.tiles.values()) disownCollider(tile.collider);
|
|
431
450
|
for (let groundIndex = grounds.length - 1; groundIndex >= 0; groundIndex -= 1) if (grounds[groundIndex].body === body) grounds.splice(groundIndex, 1);
|
|
432
|
-
if (body.declaredMotionIndex !== 2)
|
|
451
|
+
if (body.declaredMotionIndex !== 2) {
|
|
452
|
+
movableBodyCount -= 1;
|
|
453
|
+
unfixedBodies.splice(unfixedBodies.indexOf(body), 1);
|
|
454
|
+
}
|
|
455
|
+
lowestFreeBodyIndex = Math.min(lowestFreeBodyIndex, index);
|
|
433
456
|
if (body.timetable) timetableBodies.splice(timetableBodies.indexOf(body), 1);
|
|
434
457
|
rapierWorld.removeRigidBody(body.rigidBody);
|
|
435
458
|
bodyIndexById.delete(body.declaration.id);
|
|
@@ -447,6 +470,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
447
470
|
|
|
448
471
|
/** Changes how a body moves while playing: a parked cart made fixed, a lift made kinematic, and back. */
|
|
449
472
|
function setBodyMotion(body, motionIndex) {
|
|
473
|
+
levelChangeCount += 1;
|
|
450
474
|
if (body.motionIndex === motionIndex || body.declaredMotionIndex === 2 || body.timetable) return;
|
|
451
475
|
body.motionIndex = motionIndex;
|
|
452
476
|
body.rigidBody.setBodyType(bodyTypeOf(motionIndex), true);
|
|
@@ -490,8 +514,8 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
490
514
|
/** Sets each vehicle's controls from its driver's input, or lets it roll with none. */
|
|
491
515
|
function driveVehicles(worldStepContext) {
|
|
492
516
|
const { inputOf } = worldStepContext;
|
|
493
|
-
for (const body of
|
|
494
|
-
if (!body
|
|
517
|
+
for (const body of unfixedBodies) {
|
|
518
|
+
if (!body.vehicle) continue;
|
|
495
519
|
const driver = characters.find((character) => character && character.rideBodyIndex === body.index && character.seatIndex === 0);
|
|
496
520
|
const controls = driver ? characterKind.vehicleControls(inputOf(driver.id) ?? characterKind.idleInput, { bodyId: body.declaration.id, ...stepTools.bodyState(body.declaration.id) }, worldStepContext) : { engineNumber: 0, steerNumber: 0, brakeNumber: 0 };
|
|
497
521
|
const { vehicle } = body.declaration;
|
|
@@ -512,8 +536,8 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
512
536
|
}
|
|
513
537
|
|
|
514
538
|
function updateVehicles() {
|
|
515
|
-
for (const body of
|
|
516
|
-
if (!body
|
|
539
|
+
for (const body of unfixedBodies) {
|
|
540
|
+
if (!body.vehicle || body.motionIndex !== 0 || body.rigidBody.isSleeping()) continue;
|
|
517
541
|
// Its wheels stand on what its layers say, never on a character and never on the vehicle itself.
|
|
518
542
|
raw.updateVehicle(body.vehicle.controller.raw, batch.broadPhase, batch.narrowPhase, batch.bodies, batch.colliders, substepDurationSec, body.vehicle.wheelGroupsNumber);
|
|
519
543
|
}
|
|
@@ -574,25 +598,44 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
574
598
|
|
|
575
599
|
// ---- Characters ----
|
|
576
600
|
|
|
577
|
-
const characterHalfSegmentM = world.character.heightM / 2 - world.character.radiusM;
|
|
578
601
|
const isKinematicKind = characterKind.bodyKind === "kinematic";
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
controller
|
|
586
|
-
controller
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
602
|
+
/**
|
|
603
|
+
* The sizes a character takes: the world's own first, then the ones it names. A character's body sits where its
|
|
604
|
+
* feet are and half the world's own height more, whatever its size, so a place on the wire means the same for all;
|
|
605
|
+
* its capsule is raised or lowered on it to stand on the same feet.
|
|
606
|
+
*/
|
|
607
|
+
const characterSizes = [{ name: null, ...world.character }, ...world.characterSizes].map((size) => {
|
|
608
|
+
const controller = isKinematicKind ? rapierWorld.createCharacterController(CHARACTER_SKIN_M) : null;
|
|
609
|
+
if (controller) {
|
|
610
|
+
controller.setUp({ x: 0, y: 1, z: 0 });
|
|
611
|
+
controller.setSlideEnabled(true);
|
|
612
|
+
if (size.stepHeightM > 0) controller.enableAutostep(size.stepHeightM, size.radiusM * 0.5, false);
|
|
613
|
+
controller.setMaxSlopeClimbAngle((size.maximumSlopeDeg * Math.PI) / 180);
|
|
614
|
+
controller.setMinSlopeSlideAngle?.(((size.maximumSlopeDeg + 5) * Math.PI) / 180);
|
|
615
|
+
controller.enableSnapToGround(size.stepHeightM > 0 ? size.stepHeightM : 0.1);
|
|
616
|
+
controller.setApplyImpulsesToDynamicBodies(characterKind.canPushBodies !== false);
|
|
617
|
+
controller.setCharacterMass(currentTuning.characterMassKg ?? 70);
|
|
618
|
+
}
|
|
619
|
+
return {
|
|
620
|
+
...size,
|
|
621
|
+
halfSegmentM: size.heightM / 2 - size.radiusM,
|
|
622
|
+
/** How far above the body's place the capsule's middle is. */
|
|
623
|
+
riseM: (size.heightM - world.character.heightM) / 2,
|
|
624
|
+
controller,
|
|
625
|
+
/** How far up a surface under the character faces, at least, for it to stand there: the steepest slope it climbs. */
|
|
626
|
+
minimumStandNormalNumber: Math.cos((size.maximumSlopeDeg * Math.PI) / 180),
|
|
627
|
+
};
|
|
628
|
+
});
|
|
629
|
+
const characterHalfSegmentM = characterSizes[0].halfSegmentM;
|
|
630
|
+
const tallestCharacterHeightM = Math.max(...characterSizes.map((size) => size.heightM));
|
|
631
|
+
const characterSizeIndexByName = new Map(characterSizes.map((size, sizeIndex) => [size.name, sizeIndex]));
|
|
590
632
|
|
|
591
633
|
/**
|
|
592
634
|
* Adds a character with its feet at a place, at an index: the next free one, or the one given. Answers the index.
|
|
593
635
|
* With isWaiting it's in the world's lists but not in the world, touching nothing, until writeCharacter says where
|
|
594
636
|
* it is: for a game that knows a character has joined before it knows where.
|
|
595
637
|
*/
|
|
638
|
+
const colliderRiseM = isKinematicKind ? CHARACTER_SKIN_M : 0;
|
|
596
639
|
function addCharacter(characterId, feetPositionM, givenIndex = null, isWaiting = false) {
|
|
597
640
|
let index = givenIndex;
|
|
598
641
|
if (index === null) {
|
|
@@ -605,7 +648,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
605
648
|
// No grip of its own, so a character never sticks to a wall it walks along: its feet's hold is in how it's moved.
|
|
606
649
|
// A kinematic character's capsule sits its controller's gap above the body's place: the controller keeps that gap
|
|
607
650
|
// from the ground, so the body stands exactly on it (and one placed exactly on the ground isn't already too close).
|
|
608
|
-
const colliderDescription = engine.ColliderDesc.capsule(characterHalfSegmentM, world.character.radiusM).setTranslation(0,
|
|
651
|
+
const colliderDescription = engine.ColliderDesc.capsule(characterHalfSegmentM, world.character.radiusM).setTranslation(0, colliderRiseM, 0).setFriction(0).setFrictionCombineRule(engine.CoefficientCombineRule.Min).setRestitution(0).setCollisionGroups(groupsOf(characterLayerIndex)).setActiveEvents(engine.ActiveEvents.COLLISION_EVENTS);
|
|
609
652
|
if (!isKinematicKind) colliderDescription.setMass(currentTuning.characterMassKg ?? 70);
|
|
610
653
|
const collider = rapierWorld.createCollider(colliderDescription, rigidBody);
|
|
611
654
|
ownerByColliderHandle.set(collider.handle, { isCharacter: true, index });
|
|
@@ -618,6 +661,10 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
618
661
|
isWaiting,
|
|
619
662
|
rigidBody,
|
|
620
663
|
collider,
|
|
664
|
+
size: characterSizes[0],
|
|
665
|
+
sizeIndex: 0,
|
|
666
|
+
/** Which of the world's characterSizes it is now, or null for the world's own character's. */
|
|
667
|
+
characterSizeName: null,
|
|
621
668
|
/** A kinematic character's speed: the engine doesn't keep one for it. */
|
|
622
669
|
velocity: { x: 0, y: 0, z: 0 },
|
|
623
670
|
facingYawRad: 0,
|
|
@@ -648,6 +695,73 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
648
695
|
return true;
|
|
649
696
|
}
|
|
650
697
|
|
|
698
|
+
/** How near a character's capsule the ground it rests on is, at most: its controller's gap and a little more. */
|
|
699
|
+
const REST_REACH_M = CHARACTER_SKIN_M + 0.03;
|
|
700
|
+
/**
|
|
701
|
+
* Whether a character stands on what never moves and touches nothing else: then, with nothing asked of it and no
|
|
702
|
+
* speed, a step would leave it exactly where it is, and its kind may skip the move. Judged once where it stands,
|
|
703
|
+
* and again only when it has moved, changed size or layer, or the level under it has changed.
|
|
704
|
+
*/
|
|
705
|
+
function isRestingOnLevel(character) {
|
|
706
|
+
if (character.rideBodyIndex >= 0) return false;
|
|
707
|
+
raw.readBody(batch.bodies, character.rigidBody.handle);
|
|
708
|
+
let floats = batch.floats();
|
|
709
|
+
const x = floats[0];
|
|
710
|
+
const y = floats[1];
|
|
711
|
+
const z = floats[2];
|
|
712
|
+
const rest = (character.rest ??= { x: NaN, y: NaN, z: NaN, sizeIndex: -1, layerIndex: -1, levelChangeCount: -1, isResting: false });
|
|
713
|
+
if (rest.x === x && rest.y === y && rest.z === z && rest.sizeIndex === character.sizeIndex && rest.layerIndex === character.layerIndex && rest.levelChangeCount === levelChangeCount) return rest.isResting;
|
|
714
|
+
rest.x = x;
|
|
715
|
+
rest.y = y;
|
|
716
|
+
rest.z = z;
|
|
717
|
+
rest.sizeIndex = character.sizeIndex;
|
|
718
|
+
rest.layerIndex = character.layerIndex;
|
|
719
|
+
rest.levelChangeCount = levelChangeCount;
|
|
720
|
+
rest.isResting = false;
|
|
721
|
+
const contactCount = characterContactsOf(character, x, y, z, REST_REACH_M);
|
|
722
|
+
floats = batch.floats();
|
|
723
|
+
const numbers = batch.numbers();
|
|
724
|
+
let isStanding = false;
|
|
725
|
+
for (let contactIndex = 0; contactIndex < contactCount; contactIndex += 1) {
|
|
726
|
+
const tagNumber = numbers[contactIndex * CONTACT_NUMBER_COUNT];
|
|
727
|
+
const body = (tagNumber & TAG_KIND_MASK_NUMBER) === BODY_TAG_NUMBER ? bodies[tagNumber >>> 8] : null;
|
|
728
|
+
if (!body || body.motionIndex !== 2) return false;
|
|
729
|
+
if (floats[contactIndex * CONTACT_FLOAT_COUNT + 4] >= character.size.minimumStandNormalNumber) isStanding = true;
|
|
730
|
+
}
|
|
731
|
+
rest.isResting = isStanding;
|
|
732
|
+
return isStanding;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/** How deep something may reach into a size a character asks to take before the size doesn't fit. */
|
|
736
|
+
const SIZE_FIT_DEPTH_M = 0.02;
|
|
737
|
+
/**
|
|
738
|
+
* Gives a character another of the world's sizes, its feet where they are. With isFitChecked, only where the size
|
|
739
|
+
* has room: answers false, and changes nothing, when something other than the ground it stands on is in its way.
|
|
740
|
+
*/
|
|
741
|
+
function setCharacterSize(character, sizeIndex, isFitChecked = false) {
|
|
742
|
+
if (character.sizeIndex === sizeIndex) return true;
|
|
743
|
+
const size = characterSizes[sizeIndex];
|
|
744
|
+
if (!size) return false;
|
|
745
|
+
if (isFitChecked && !character.isWaiting) {
|
|
746
|
+
raw.readBody(batch.bodies, character.rigidBody.handle);
|
|
747
|
+
let floats = batch.floats();
|
|
748
|
+
const contactCount = raw.capsuleContacts(batch.broadPhase, batch.narrowPhase, batch.bodies, batch.colliders, floats[0], floats[1] + CHARACTER_SKIN_M + size.riseM, floats[2], 0, 0, 0, 1, size.radiusM, size.halfSegmentM, 0, groupsOf(character.layerIndex), character.rigidBody.handle, (isEachCharacterAlone ? SKIPS_CHARACTERS_FLAG_NUMBER : 0) | EXCLUDES_BODY_FLAG_NUMBER | EACH_TRIANGLE_FLAG_NUMBER);
|
|
749
|
+
floats = batch.floats();
|
|
750
|
+
for (let contactIndex = 0; contactIndex < contactCount; contactIndex += 1) {
|
|
751
|
+
const firstIndex = contactIndex * CONTACT_FLOAT_COUNT;
|
|
752
|
+
if (floats[firstIndex + 6] > SIZE_FIT_DEPTH_M && floats[firstIndex + 4] < size.minimumStandNormalNumber) return false;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
character.size = size;
|
|
756
|
+
character.sizeIndex = sizeIndex;
|
|
757
|
+
character.characterSizeName = size.name;
|
|
758
|
+
character.collider.setShape(new engine.Capsule(size.halfSegmentM, size.radiusM));
|
|
759
|
+
character.collider.setTranslationWrtParent({ x: 0, y: colliderRiseM + size.riseM, z: 0 });
|
|
760
|
+
isPropagationOwed = true;
|
|
761
|
+
isSceneOwed = true;
|
|
762
|
+
return true;
|
|
763
|
+
}
|
|
764
|
+
|
|
651
765
|
function setCharacterLayer(character, layerIndex) {
|
|
652
766
|
if (character.layerIndex === layerIndex) return;
|
|
653
767
|
character.layerIndex = layerIndex;
|
|
@@ -847,6 +961,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
847
961
|
*/
|
|
848
962
|
function moveAndSlide(character, desiredM, into = null) {
|
|
849
963
|
const moved = into ?? { movedM: { x: 0, y: 0, z: 0 }, isGrounded: false };
|
|
964
|
+
const { controller } = character.size;
|
|
850
965
|
if (!controller) {
|
|
851
966
|
moved.movedM.x = 0;
|
|
852
967
|
moved.movedM.y = 0;
|
|
@@ -877,6 +992,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
877
992
|
},
|
|
878
993
|
...queries,
|
|
879
994
|
moveAndSlide,
|
|
995
|
+
isRestingOnLevel,
|
|
880
996
|
bodies,
|
|
881
997
|
characters,
|
|
882
998
|
bodyIndexById,
|
|
@@ -902,7 +1018,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
902
1018
|
raw.readBody(batch.bodies, character.rigidBody.handle);
|
|
903
1019
|
const floats = batch.floats();
|
|
904
1020
|
const velocityMPerSec = isKinematicKind ? { x: character.velocity.x, y: character.velocity.y, z: character.velocity.z } : { x: floats[7], y: floats[8], z: floats[9] };
|
|
905
|
-
return { positionM: { x: floats[0], y: floats[1] - world.character.heightM / 2, z: floats[2] }, velocityMPerSec, facingYawRad: character.facingYawRad, isGrounded: character.isGrounded, rideBodyId: bodies[character.rideBodyIndex]?.declaration.id ?? null, collisionLayerName: world.collisionLayers[character.ownLayerIndex].name, own: character.own };
|
|
1021
|
+
return { positionM: { x: floats[0], y: floats[1] - world.character.heightM / 2, z: floats[2] }, velocityMPerSec, facingYawRad: character.facingYawRad, isGrounded: character.isGrounded, rideBodyId: bodies[character.rideBodyIndex]?.declaration.id ?? null, collisionLayerName: world.collisionLayers[character.ownLayerIndex].name, characterSizeName: character.characterSizeName, own: character.own };
|
|
906
1022
|
},
|
|
907
1023
|
/**
|
|
908
1024
|
* Ties or lets go a joint from a step (a press that takes a cart's handles), so it's run again exactly on a
|
|
@@ -914,6 +1030,16 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
914
1030
|
return true;
|
|
915
1031
|
},
|
|
916
1032
|
/** Puts a character on one of the world's collision layers from its step (walking through his own cart while he holds it). */
|
|
1033
|
+
/**
|
|
1034
|
+
* Gives a character another of the world's characterSizes from its step (a crouch, standing up again), or null
|
|
1035
|
+
* for the world's own character's: its feet stay where they are. Answers false, and changes nothing, where the
|
|
1036
|
+
* size has no room (a low roof over a crouched head).
|
|
1037
|
+
*/
|
|
1038
|
+
setSize(character, characterSizeName) {
|
|
1039
|
+
const sizeIndex = characterSizeIndexByName.get(characterSizeName);
|
|
1040
|
+
if (sizeIndex === undefined) refuse("bad_argument", `setSize was given ${JSON.stringify(characterSizeName)}.`, "characterSizeName", `Use one of the world's characterSizes (${world.characterSizes.map((size) => size.name).join(", ") || "it names none"}), or null for the world's own character.`);
|
|
1041
|
+
return setCharacterSize(character, sizeIndex, true);
|
|
1042
|
+
},
|
|
917
1043
|
setLayer(character, collisionLayerName) {
|
|
918
1044
|
const layerIndex = layerIndexByName.get(collisionLayerName);
|
|
919
1045
|
if (layerIndex === undefined || collisionLayerName === RIDING_LAYER_NAME) refuse("bad_argument", `setLayer was given ${JSON.stringify(collisionLayerName)}.`, "collisionLayerName", `Use one of the world's collisionLayers: ${world.collisionLayers.filter((layer) => layer.name !== RIDING_LAYER_NAME).map((layer) => layer.name).join(", ")}.`);
|
|
@@ -1092,7 +1218,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
1092
1218
|
/** Whether a point is within reach of a body on a timetable: only then is a character asked what it touches. */
|
|
1093
1219
|
function isNearTimetable(x, y, z) {
|
|
1094
1220
|
for (const body of timetableBodies) {
|
|
1095
|
-
const reachM = body.reachM +
|
|
1221
|
+
const reachM = body.reachM + tallestCharacterHeightM;
|
|
1096
1222
|
const awayX = x - body.nowPlace.x;
|
|
1097
1223
|
const awayY = y - body.nowPlace.y;
|
|
1098
1224
|
const awayZ = z - body.nowPlace.z;
|
|
@@ -1103,9 +1229,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
1103
1229
|
|
|
1104
1230
|
/** What a character's capsule touches, within marginM of it: the contact count, read from the batch's answer. */
|
|
1105
1231
|
const characterContactsOf = (character, x, y, z, marginM) =>
|
|
1106
|
-
raw.capsuleContacts(batch.broadPhase, batch.narrowPhase, batch.bodies, batch.colliders, x, y + CHARACTER_SKIN_M, z, 0, 0, 0, 1,
|
|
1107
|
-
/** How far up a surface under a character faces, at least, for the character to stand on it: the world's steepest slope a character climbs. */
|
|
1108
|
-
const minimumStandNormalNumber = Math.cos((world.character.maximumSlopeDeg * Math.PI) / 180);
|
|
1232
|
+
raw.capsuleContacts(batch.broadPhase, batch.narrowPhase, batch.bodies, batch.colliders, x, y + CHARACTER_SKIN_M + character.size.riseM, z, 0, 0, 0, 1, character.size.radiusM, character.size.halfSegmentM, marginM, groupsOf(character.layerIndex), character.rigidBody.handle, SKIPS_CHARACTERS_FLAG_NUMBER | EXCLUDES_BODY_FLAG_NUMBER);
|
|
1109
1233
|
/** The body on a timetable a contact's tag is of, or null. */
|
|
1110
1234
|
const timetableBodyOfTag = (tagNumber) => ((tagNumber & TAG_KIND_MASK_NUMBER) === BODY_TAG_NUMBER ? (bodies[tagNumber >>> 8]?.timetable ? bodies[tagNumber >>> 8] : null) : null);
|
|
1111
1235
|
|
|
@@ -1140,7 +1264,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
1140
1264
|
function shiftCharacter(character, x, y, z, shiftX, shiftY, shiftZ, mover) {
|
|
1141
1265
|
const shiftM = Math.hypot(shiftX, shiftY, shiftZ);
|
|
1142
1266
|
if (shiftM < 1e-7) return;
|
|
1143
|
-
const isBlocked = raw.castShape(batch.broadPhase, batch.narrowPhase, batch.bodies, batch.colliders, 1,
|
|
1267
|
+
const isBlocked = raw.castShape(batch.broadPhase, batch.narrowPhase, batch.bodies, batch.colliders, 1, character.size.radiusM, character.size.halfSegmentM, 0, 0, 0, 1, x, y + CHARACTER_SKIN_M + character.size.riseM, z, shiftX / shiftM, shiftY / shiftM, shiftZ / shiftM, shiftM, false, groupsOf(character.layerIndex), mover.rigidBody.handle, SKIPS_CHARACTERS_FLAG_NUMBER | EXCLUDES_BODY_FLAG_NUMBER);
|
|
1144
1268
|
const freeNumber = isBlocked ? Math.max(batch.floats()[0] - CHARACTER_SKIN_M, 0) / shiftM : 1;
|
|
1145
1269
|
if (freeNumber <= 0) return;
|
|
1146
1270
|
batch.bodies.rbSetTranslation(character.rigidBody.handle, x + shiftX * freeNumber, y + shiftY * freeNumber, z + shiftZ * freeNumber, false);
|
|
@@ -1172,7 +1296,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
1172
1296
|
const firstIndex = contactIndex * CONTACT_FLOAT_COUNT;
|
|
1173
1297
|
const depthM = floats[firstIndex + 6];
|
|
1174
1298
|
// Under its feet: a surface that faces up enough to stand on.
|
|
1175
|
-
if (floats[firstIndex + 4] >= minimumStandNormalNumber) {
|
|
1299
|
+
if (floats[firstIndex + 4] >= character.size.minimumStandNormalNumber) {
|
|
1176
1300
|
if (depthM > closestM) {
|
|
1177
1301
|
closestM = depthM;
|
|
1178
1302
|
ridden = body;
|
|
@@ -1216,7 +1340,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
1216
1340
|
let normalZ = 0;
|
|
1217
1341
|
for (let contactIndex = 0; contactIndex < contactCount; contactIndex += 1) {
|
|
1218
1342
|
const firstIndex = contactIndex * CONTACT_FLOAT_COUNT;
|
|
1219
|
-
if (floats[firstIndex + 4] < minimumStandNormalNumber || floats[firstIndex + 6] <= closestM) continue;
|
|
1343
|
+
if (floats[firstIndex + 4] < character.size.minimumStandNormalNumber || floats[firstIndex + 6] <= closestM) continue;
|
|
1220
1344
|
const body = timetableBodyOfTag(numbers[contactIndex * CONTACT_NUMBER_COUNT]);
|
|
1221
1345
|
if (!body) continue;
|
|
1222
1346
|
closestM = floats[firstIndex + 6];
|
|
@@ -1550,6 +1674,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
1550
1674
|
fields.rideBodyNumber = character.rideBodyIndex + 1;
|
|
1551
1675
|
fields.seatIndex = character.seatIndex;
|
|
1552
1676
|
fields.layerNumber = character.ownLayerIndex;
|
|
1677
|
+
fields.sizeNumber = character.sizeIndex;
|
|
1553
1678
|
return Object.assign(fields, characterKind.readOwn(character, stepTools));
|
|
1554
1679
|
}
|
|
1555
1680
|
/** readCharacter for whoever reads the fields at once and keeps nothing (the wire, after a step): the character's own object filled again. */
|
|
@@ -1568,6 +1693,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
1568
1693
|
rigidBody.setEnabled(true);
|
|
1569
1694
|
}
|
|
1570
1695
|
if (fields.layerNumber !== undefined && fields.layerNumber < world.collisionLayers.length && fields.layerNumber !== ridingLayerIndex) character.ownLayerIndex = fields.layerNumber;
|
|
1696
|
+
if (fields.sizeNumber !== undefined && fields.sizeNumber !== character.sizeIndex) setCharacterSize(character, fields.sizeNumber);
|
|
1571
1697
|
batch.bodies.rbSetTranslation(rigidBody.handle, fields.x, fields.y, fields.z, false);
|
|
1572
1698
|
isPropagationOwed = true;
|
|
1573
1699
|
character.facingYawRad = fields.facingYawRad;
|
|
@@ -1613,7 +1739,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
1613
1739
|
body.rigidBody.setAngularDamping(body.declaration.angularDampingNumber ?? nextTuning.angularDampingNumber);
|
|
1614
1740
|
body.rigidBody.wakeUp();
|
|
1615
1741
|
}
|
|
1616
|
-
controller?.setCharacterMass(nextTuning.characterMassKg ?? 70);
|
|
1742
|
+
for (const size of characterSizes) size.controller?.setCharacterMass(nextTuning.characterMassKg ?? 70);
|
|
1617
1743
|
characterKind.onTuning?.(nextTuning, stepTools);
|
|
1618
1744
|
}
|
|
1619
1745
|
|
|
@@ -1638,6 +1764,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
1638
1764
|
tickDurationSec,
|
|
1639
1765
|
stepTools,
|
|
1640
1766
|
addBody,
|
|
1767
|
+
freeBodyIndex,
|
|
1641
1768
|
removeBody,
|
|
1642
1769
|
addCharacter,
|
|
1643
1770
|
removeCharacter,
|
|
@@ -1713,7 +1840,7 @@ export function createSimulation(engine, world, { tickDurationSec, characterKind
|
|
|
1713
1840
|
/** Calls a function with the index of every moving body that isn't at rest. */
|
|
1714
1841
|
forEachAwakeBody(listener) {
|
|
1715
1842
|
if (settledStepCount !== stepCount) {
|
|
1716
|
-
for (const body of
|
|
1843
|
+
for (const body of unfixedBodies) if (body.motionIndex === 0 && !body.rigidBody.isSleeping()) listener(body.index);
|
|
1717
1844
|
return;
|
|
1718
1845
|
}
|
|
1719
1846
|
for (let settledIndex = 0; settledIndex < settledCount; settledIndex += 1) {
|