@zaisma/physics 2.5.0 → 2.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 +62 -14
- package/batch.js +105 -49
- package/bodies.js +72 -0
- package/characters.js +29 -9
- package/declare.js +144 -14
- package/index.d.ts +229 -43
- package/index.js +2 -2
- package/level-characters.js +2 -1
- package/package.json +2 -1
- package/piece.json +18 -1
- package/piece.json.js +18 -1
- package/rapier/CHANGES.md +5 -1
- package/rapier/rapier.js +309 -6
- package/rapier/rapier.wasm +0 -0
- package/simulation.js +626 -73
- package/sync.js +301 -60
package/declare.js
CHANGED
|
@@ -16,10 +16,14 @@ export const JOINT_KINDS = ["fixed", "ball", "hinge", "slider", "spring", "rope"
|
|
|
16
16
|
export const PLAYER_CHARACTER_MODES = ["on_join", "by_game"];
|
|
17
17
|
const ID_PATTERN = /^[A-Za-z0-9_.:-]{1,64}$/;
|
|
18
18
|
|
|
19
|
-
/** Bodies
|
|
20
|
-
export const MAXIMUM_BODY_COUNT =
|
|
19
|
+
/** Bodies that move or may (moving and kinematic ones, and every body added while playing) one world holds at most. */
|
|
20
|
+
export const MAXIMUM_BODY_COUNT = 2000;
|
|
21
|
+
/** Bodies declared fixed one world holds at most: a level's pieces, which never move and are never sent. */
|
|
22
|
+
export const MAXIMUM_FIXED_BODY_COUNT = 20_000;
|
|
21
23
|
/** Characters one world holds at most: players' and bots'. */
|
|
22
|
-
export const MAXIMUM_CHARACTER_COUNT =
|
|
24
|
+
export const MAXIMUM_CHARACTER_COUNT = 250;
|
|
25
|
+
/** Sizes of character a world names at most, beside its own character's. */
|
|
26
|
+
export const MAXIMUM_CHARACTER_SIZE_COUNT = 31;
|
|
23
27
|
const MAXIMUM_SHAPE_COUNT = 16;
|
|
24
28
|
const MAXIMUM_HULL_POINT_COUNT = 255;
|
|
25
29
|
const MAXIMUM_MESH_TRIANGLE_COUNT = 50_000;
|
|
@@ -31,6 +35,8 @@ const MAXIMUM_HEIGHTFIELD_SIDE_COUNT = 4096;
|
|
|
31
35
|
export const MAXIMUM_COLLISION_LAYER_COUNT = 16;
|
|
32
36
|
/** Joints one world holds at most, declared and added while playing together. */
|
|
33
37
|
export const MAXIMUM_JOINT_COUNT = 1000;
|
|
38
|
+
/** Bodies that aren't fixed and joints a world declares together at most: what the online core takes from the start. More come while playing, with addBody and addJoint. */
|
|
39
|
+
export const MAXIMUM_DECLARED_THING_COUNT = 2048;
|
|
34
40
|
const MAXIMUM_WHEEL_COUNT = 8;
|
|
35
41
|
const MAXIMUM_SEAT_COUNT = 8;
|
|
36
42
|
/** The layer a character riding in a seat is on: it touches nothing, not its seat nor the body under it. */
|
|
@@ -42,7 +48,7 @@ export const WORLD_REACH_M = 10_000;
|
|
|
42
48
|
/** How heavy a cubic metre of a body is when it doesn't say its mass: furniture is mostly air. */
|
|
43
49
|
export const DEFAULT_DENSITY_KG_PER_M3_NUMBER = 250;
|
|
44
50
|
|
|
45
|
-
const WORLD_KEYS = ["bodies", "bodyKinds", "character", "playerCharacterMode", "startPositionsM", "fallOutHeightM", "collisionLayers", "surfaceNames", "joints", "substepCount", "groundReachM"];
|
|
51
|
+
const WORLD_KEYS = ["bodies", "bodyKinds", "character", "characterSizes", "playerCharacterMode", "startPositionsM", "fallOutHeightM", "collisionLayers", "surfaceNames", "joints", "substepCount", "groundReachM", "zones"];
|
|
46
52
|
const BODY_KEYS = ["id", "bodyKindName", "motionKind", "positionM", "rotationDeg", "massKg", "bouncePct", "gripPct", "linearDampingNumber", "angularDampingNumber", "canBeGrabbed", "shapes", "collisionLayerName", "wheels", "vehicle", "seats", "timetable"];
|
|
47
53
|
const BODY_KIND_KEYS = ["massKg", "bouncePct", "gripPct", "linearDampingNumber", "angularDampingNumber", "canBeGrabbed", "shapes", "collisionLayerName", "wheels", "vehicle", "seats"];
|
|
48
54
|
const WHEEL_KEYS = ["positionM", "radiusM", "suspensionRestM", "suspensionTravelM", "isSteered", "isDriven"];
|
|
@@ -63,7 +69,23 @@ export const MAXIMUM_TIMETABLE_STOP_COUNT = 32;
|
|
|
63
69
|
const TIMETABLE_STEPS_PER_M = 1000;
|
|
64
70
|
/** The longest a trip between two stops, or a wait at one, lasts, in seconds. */
|
|
65
71
|
export const MAXIMUM_TIMETABLE_DURATION_SEC = 3600;
|
|
66
|
-
const JOINT_KEYS = ["id", "jointKind", "bodyId", "otherBodyId", "anchorM", "otherAnchorM", "axis", "minimumNumber", "maximumNumber", "restLengthM", "stiffnessNumber", "dampingNumber"];
|
|
72
|
+
const JOINT_KEYS = ["id", "jointKind", "bodyId", "otherBodyId", "anchorM", "otherAnchorM", "axis", "minimumNumber", "maximumNumber", "restLengthM", "stiffnessNumber", "dampingNumber", "motor", "breakForceNewtons"];
|
|
73
|
+
const MOTOR_KEYS = ["motorKind", "targetNumber", "maximumForceNumber", "stiffnessNumber", "dampingNumber"];
|
|
74
|
+
/** What a joint's motor holds: a speed (a wheel that turns, a lift's winch) or a position (a door that closes itself, a crane's arm). */
|
|
75
|
+
export const MOTOR_KINDS = ["speed", "position"];
|
|
76
|
+
/** The furthest a motor's target goes either way: radians (a second) about a hinge, metres (a second) along a slider. */
|
|
77
|
+
export const MAXIMUM_MOTOR_TARGET_NUMBER = 10_000;
|
|
78
|
+
const ZONE_KEYS = ["id", "shapes", "positionM", "rotationDeg", "bodyId", "collisionLayerNames", "gravityPct", "pushMPerSecPerSec", "dampingNumber", "isEnabled"];
|
|
79
|
+
const ZONE_SHAPE_KEYS = ["shapeKind", "sizeM", "radiusM", "heightM", "positionM", "rotationDeg"];
|
|
80
|
+
/** The shapes a zone is made of, in the order the engine numbers them. */
|
|
81
|
+
export const ZONE_SHAPE_KINDS = ["ball", "capsule", "box", "cylinder"];
|
|
82
|
+
/** A world holds this many zones, each of up to 8 shapes. */
|
|
83
|
+
export const MAXIMUM_ZONE_COUNT = 250;
|
|
84
|
+
const MAXIMUM_ZONE_SHAPE_COUNT = 8;
|
|
85
|
+
/** The most a zone turns gravity (in percent of it, either way), pushes (metres a second each second) and slows. */
|
|
86
|
+
const MAXIMUM_ZONE_GRAVITY_PCT = 1000;
|
|
87
|
+
const MAXIMUM_ZONE_PUSH_M_PER_SEC_PER_SEC = 1000;
|
|
88
|
+
const MAXIMUM_ZONE_DAMPING_NUMBER = 100;
|
|
67
89
|
const COLLISION_LAYER_KEYS = ["touches"];
|
|
68
90
|
const DATA_PATH_PATTERN = /^[A-Za-z0-9_][A-Za-z0-9_./-]{0,199}$/;
|
|
69
91
|
const BODY_KIND_NAME_PATTERN = /^[a-z][a-z0-9_]{0,39}$/;
|
|
@@ -87,6 +109,7 @@ export const triangleGroupBitNumberOf = (groupNumber) => 1 << ((groupNumber - 1)
|
|
|
87
109
|
/** Surfaces one world names at most (what a hit says it met: concrete, metal, water). */
|
|
88
110
|
export const MAXIMUM_SURFACE_COUNT = 255;
|
|
89
111
|
const CHARACTER_KEYS = ["heightM", "radiusM", "stepHeightM", "maximumSlopeDeg"];
|
|
112
|
+
const CHARACTER_SIZE_KEYS = ["name", ...CHARACTER_KEYS];
|
|
90
113
|
|
|
91
114
|
const isPlainObject = (given) => given !== null && typeof given === "object" && !Array.isArray(given);
|
|
92
115
|
const isFiniteNumber = (given) => typeof given === "number" && Number.isFinite(given);
|
|
@@ -590,6 +613,83 @@ export function checkJoint(given, what, bodyIdOf = () => true) {
|
|
|
590
613
|
restLengthM: numberOf("restLengthM", 0, 0, WORLD_REACH_M),
|
|
591
614
|
stiffnessNumber: numberOf("stiffnessNumber", 2000, 0, 1e9),
|
|
592
615
|
dampingNumber: numberOf("dampingNumber", 100, 0, 1e9),
|
|
616
|
+
motor: motorOf(given.motor, jointKind, `${what}.motor`),
|
|
617
|
+
// How hard it pulls or pushes its ends before it breaks. Left out, it never breaks.
|
|
618
|
+
breakForceNewtons: given.breakForceNewtons === undefined ? null : numberOf("breakForceNewtons", 0, 0.001, 1e12),
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/** A joint's motor, checked: what turns a hinge or slides a slider by itself. Null for a joint without one. */
|
|
623
|
+
function motorOf(given, jointKind, what) {
|
|
624
|
+
if (given === undefined) return null;
|
|
625
|
+
if (jointKind !== "hinge" && jointKind !== "slider") refuse("bad_joint", `${what} is on a ${jointKind} joint.`, what, "Only a hinge (it turns about its axis) and a slider (it slides along its axis) take a motor.");
|
|
626
|
+
refuseUnknownKeys(given, MOTOR_KEYS, "bad_joint", what);
|
|
627
|
+
if (!MOTOR_KINDS.includes(given.motorKind)) refuse("bad_joint", `${what}.motorKind is ${JSON.stringify(given.motorKind)}.`, `${what}.motorKind`, `Use: ${MOTOR_KINDS.join(", ")}.`);
|
|
628
|
+
const numberOf = (key, fallbackNumber, minimumNumber, maximumNumber) => {
|
|
629
|
+
if (given[key] === undefined) return fallbackNumber;
|
|
630
|
+
if (!isFiniteNumber(given[key]) || given[key] < minimumNumber || given[key] > maximumNumber) refuse("bad_joint", `${what}.${key} is ${JSON.stringify(given[key])}.`, `${what}.${key}`, `Give a number from ${minimumNumber} to ${maximumNumber}, or leave it out.`);
|
|
631
|
+
return given[key];
|
|
632
|
+
};
|
|
633
|
+
const isSpeed = given.motorKind === "speed";
|
|
634
|
+
return {
|
|
635
|
+
motorKind: given.motorKind,
|
|
636
|
+
targetNumber: numberOf("targetNumber", 0, -MAXIMUM_MOTOR_TARGET_NUMBER, MAXIMUM_MOTOR_TARGET_NUMBER),
|
|
637
|
+
maximumForceNumber: given.maximumForceNumber === undefined ? null : numberOf("maximumForceNumber", 0, 0, 1e12),
|
|
638
|
+
// A speed is held by damping alone; a position by a spring and its damping.
|
|
639
|
+
stiffnessNumber: isSpeed ? 0 : numberOf("stiffnessNumber", 2000, 0, 1e9),
|
|
640
|
+
dampingNumber: numberOf("dampingNumber", isSpeed ? 10_000 : 200, 0, 1e9),
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** One shape of a zone, checked: as the engine takes it (its kind's number and three lengths), placed in the zone. */
|
|
645
|
+
function zoneShapeOf(given, what) {
|
|
646
|
+
refuseUnknownKeys(given, ZONE_SHAPE_KEYS, "bad_zone", what);
|
|
647
|
+
const { shapeKind } = given;
|
|
648
|
+
if (!ZONE_SHAPE_KINDS.includes(shapeKind)) refuse("bad_zone", `${what}.shapeKind is ${JSON.stringify(shapeKind)}.`, `${what}.shapeKind`, `Use: ${ZONE_SHAPE_KINDS.join(", ")}.`);
|
|
649
|
+
const takes = { box: ["sizeM"], ball: ["radiusM"], capsule: ["radiusM", "heightM"], cylinder: ["radiusM", "heightM"] }[shapeKind];
|
|
650
|
+
for (const key of ["sizeM", "radiusM", "heightM"]) if (takes.includes(key) !== (given[key] !== undefined)) refuse("bad_zone", takes.includes(key) ? `${what} is a ${shapeKind} with no ${key}.` : `${what} is a ${shapeKind}, which has no ${key}.`, `${what}.${key}`, `A ${shapeKind} takes: ${takes.join(", ")}.`);
|
|
651
|
+
let lengthsM;
|
|
652
|
+
if (shapeKind === "box") {
|
|
653
|
+
const sizeM = pointOf(given.sizeM, null, "bad_zone", `${what}.sizeM`);
|
|
654
|
+
if (!(sizeM.x > 0 && sizeM.y > 0 && sizeM.z > 0)) refuse("bad_zone", `${what}.sizeM is ${JSON.stringify(given.sizeM)}.`, `${what}.sizeM`, "Give how wide (x), tall (y) and deep (z) the box is, each above 0, in metres.");
|
|
655
|
+
lengthsM = [sizeM.x / 2, sizeM.y / 2, sizeM.z / 2];
|
|
656
|
+
} else {
|
|
657
|
+
const radiusM = lengthOf(given.radiusM, "bad_zone", `${what}.radiusM`);
|
|
658
|
+
if (shapeKind === "ball") lengthsM = [radiusM, 0, 0];
|
|
659
|
+
else {
|
|
660
|
+
const heightM = lengthOf(given.heightM, "bad_zone", `${what}.heightM`);
|
|
661
|
+
if (shapeKind === "capsule" && heightM < radiusM * 2) refuse("bad_zone", `${what}.heightM is ${heightM}, less than twice its radius.`, `${what}.heightM`, "A capsule's height is its whole height, round ends included: at least twice its radius.");
|
|
662
|
+
lengthsM = [radiusM, shapeKind === "capsule" ? heightM / 2 - radiusM : heightM / 2, 0];
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
return { shapeKind, shapeKindNumber: ZONE_SHAPE_KINDS.indexOf(shapeKind), lengthsM, positionM: pointOf(given.positionM, { x: 0, y: 0, z: 0 }, "bad_zone", `${what}.positionM`), rotation: rotationOf(given.rotationDeg, "bad_zone", `${what}.rotationDeg`) };
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/** A zone, checked: a place that tells who's in it, and may pull, push and slow what is. */
|
|
669
|
+
function checkZone(given, what, { bodyById, layerNames }) {
|
|
670
|
+
refuseUnknownKeys(given, ZONE_KEYS, "bad_zone", what);
|
|
671
|
+
const { id, shapes, bodyId = null, collisionLayerNames = null, gravityPct = 100, dampingNumber = 0, isEnabled = true } = given;
|
|
672
|
+
if (typeof id !== "string" || !ID_PATTERN.test(id)) refuse("bad_zone", `${what}.id is ${JSON.stringify(id)}.`, `${what}.id`, 'Give 1 to 64 letters, digits, "_", ".", ":" or "-", like "capture:a".');
|
|
673
|
+
if (!Array.isArray(shapes) || !shapes.length || shapes.length > MAXIMUM_ZONE_SHAPE_COUNT) refuse("bad_zone", `${what}.shapes isn't a list of 1 to ${MAXIMUM_ZONE_SHAPE_COUNT} shapes.`, `${what}.shapes`, 'Give the room it takes, like [{ shapeKind: "box", sizeM: { x: 6, y: 3, z: 6 } }].');
|
|
674
|
+
if (bodyId !== null && !bodyById.has(bodyId)) refuse("bad_zone", `${what}.bodyId is ${JSON.stringify(bodyId)}.`, `${what}.bodyId`, "Give the id of one of the world's bodies for the zone to ride (its place is then in that body's own space), or leave it out.");
|
|
675
|
+
if (collisionLayerNames !== null && (!Array.isArray(collisionLayerNames) || !collisionLayerNames.length || collisionLayerNames.some((name) => !layerNames.includes(name)))) refuse("bad_zone", `${what}.collisionLayerNames is ${JSON.stringify(collisionLayerNames)}.`, `${what}.collisionLayerNames`, `Give which of the world's collisionLayers it counts (${layerNames.join(", ")}), or leave it out for all of them.`);
|
|
676
|
+
if (!isFiniteNumber(gravityPct) || Math.abs(gravityPct) > MAXIMUM_ZONE_GRAVITY_PCT) refuse("bad_zone", `${what}.gravityPct is ${JSON.stringify(gravityPct)}.`, `${what}.gravityPct`, `Give how much of gravity pulls in the zone, from -${MAXIMUM_ZONE_GRAVITY_PCT} to ${MAXIMUM_ZONE_GRAVITY_PCT}: 100 (the default) is all of it, 0 none, below 0 lifts.`);
|
|
677
|
+
const pushMPerSecPerSec = pointOf(given.pushMPerSecPerSec, { x: 0, y: 0, z: 0 }, "bad_zone", `${what}.pushMPerSecPerSec`);
|
|
678
|
+
if (Math.hypot(pushMPerSecPerSec.x, pushMPerSecPerSec.y, pushMPerSecPerSec.z) > MAXIMUM_ZONE_PUSH_M_PER_SEC_PER_SEC) refuse("bad_zone", `${what}.pushMPerSecPerSec is ${JSON.stringify(given.pushMPerSecPerSec)}.`, `${what}.pushMPerSecPerSec`, `Give how it speeds things up, in metres a second each second, up to ${MAXIMUM_ZONE_PUSH_M_PER_SEC_PER_SEC} long.`);
|
|
679
|
+
if (!isFiniteNumber(dampingNumber) || dampingNumber < 0 || dampingNumber > MAXIMUM_ZONE_DAMPING_NUMBER) refuse("bad_zone", `${what}.dampingNumber is ${JSON.stringify(dampingNumber)}.`, `${what}.dampingNumber`, `Give how quickly speed and spin die away in the zone, 0 (not at all, the default) to ${MAXIMUM_ZONE_DAMPING_NUMBER}: water is about 2.`);
|
|
680
|
+
if (typeof isEnabled !== "boolean") refuse("bad_zone", `${what}.isEnabled is ${JSON.stringify(isEnabled)}.`, `${what}.isEnabled`, "Give false for a zone that starts switched off (setZone switches it), or leave it out.");
|
|
681
|
+
return {
|
|
682
|
+
id,
|
|
683
|
+
shapes: shapes.map((shape, shapeIndex) => zoneShapeOf(shape, `${what}.shapes[${shapeIndex}]`)),
|
|
684
|
+
positionM: pointOf(given.positionM, { x: 0, y: 0, z: 0 }, "bad_zone", `${what}.positionM`),
|
|
685
|
+
rotation: rotationOf(given.rotationDeg, "bad_zone", `${what}.rotationDeg`),
|
|
686
|
+
bodyId,
|
|
687
|
+
collisionLayerNames: collisionLayerNames === null ? null : [...collisionLayerNames],
|
|
688
|
+
gravityPct,
|
|
689
|
+
pushMPerSecPerSec,
|
|
690
|
+
dampingNumber,
|
|
691
|
+
hasEffects: gravityPct !== 100 || dampingNumber !== 0 || pushMPerSecPerSec.x !== 0 || pushMPerSecPerSec.y !== 0 || pushMPerSecPerSec.z !== 0,
|
|
692
|
+
isEnabled,
|
|
593
693
|
};
|
|
594
694
|
}
|
|
595
695
|
|
|
@@ -610,6 +710,17 @@ function checkBodyKinds(given) {
|
|
|
610
710
|
});
|
|
611
711
|
}
|
|
612
712
|
|
|
713
|
+
/** A character's size, checked: what it leaves out is the size it starts from (the piece's own for world.character, world.character for a named size). */
|
|
714
|
+
function checkCharacterSize(given, what, from, keys) {
|
|
715
|
+
refuseUnknownKeys(given, keys, "bad_world", what);
|
|
716
|
+
const radiusM = given.radiusM === undefined ? from.radiusM : lengthOf(given.radiusM, "bad_world", `${what}.radiusM`, { minimumM: 0.05, maximumM: 5 });
|
|
717
|
+
const heightM = given.heightM === undefined ? from.heightM : lengthOf(given.heightM, "bad_world", `${what}.heightM`, { minimumM: 0.2, maximumM: 20 });
|
|
718
|
+
if (heightM < radiusM * 2) refuse("bad_world", `${what} is shorter than it is wide.`, `${what}.heightM`, "Give a heightM of at least twice the radiusM: a character is a capsule standing up.");
|
|
719
|
+
const stepHeightM = given.stepHeightM === undefined ? from.stepHeightM : lengthOf(given.stepHeightM, "bad_world", `${what}.stepHeightM`, { minimumM: 0, maximumM: 5 });
|
|
720
|
+
if (given.maximumSlopeDeg !== undefined && (!isFiniteNumber(given.maximumSlopeDeg) || given.maximumSlopeDeg < 0 || given.maximumSlopeDeg > 89)) refuse("bad_world", `${what}.maximumSlopeDeg is ${JSON.stringify(given.maximumSlopeDeg)}.`, `${what}.maximumSlopeDeg`, "Give the steepest ground a character walks up, in degrees from 0 to 89.");
|
|
721
|
+
return { heightM, radiusM, stepHeightM, maximumSlopeDeg: given.maximumSlopeDeg ?? from.maximumSlopeDeg };
|
|
722
|
+
}
|
|
723
|
+
|
|
613
724
|
/**
|
|
614
725
|
* A physics world, checked: what createPhysics and physicsKind take. Write it in a file of its own that the game's
|
|
615
726
|
* page and, with a game server, the game server both import, so both build the same world. Throws PieceError
|
|
@@ -620,7 +731,7 @@ export function definePhysicsWorld(given = {}) {
|
|
|
620
731
|
const { bodies = [], character = {}, playerCharacterMode = "on_join", startPositionsM = [{ x: 0, y: 0, z: 0 }], fallOutHeightM = -30, joints = [], substepCount = 2, groundReachM = 24 } = given;
|
|
621
732
|
const collisionLayers = checkCollisionLayers(given.collisionLayers);
|
|
622
733
|
const bodyKinds = checkBodyKinds(given.bodyKinds);
|
|
623
|
-
if (!Array.isArray(bodies) || bodies.length > MAXIMUM_BODY_COUNT) refuse("bad_world",
|
|
734
|
+
if (!Array.isArray(bodies) || bodies.length > MAXIMUM_BODY_COUNT + MAXIMUM_FIXED_BODY_COUNT) refuse("bad_world", "world.bodies isn't a list of bodies.", "world.bodies", 'Give the world\'s bodies, like [{ id: "floor", motionKind: "fixed", shapes: [...] }].');
|
|
624
735
|
const ids = new Set();
|
|
625
736
|
const checkedBodies = bodies.map((body, bodyIndex) => {
|
|
626
737
|
const checked = checkBody(body, `world.bodies[${bodyIndex}]`, bodyKinds);
|
|
@@ -628,6 +739,9 @@ export function definePhysicsWorld(given = {}) {
|
|
|
628
739
|
ids.add(checked.id);
|
|
629
740
|
return checked;
|
|
630
741
|
});
|
|
742
|
+
const fixedBodyCount = checkedBodies.reduce((fixedCount, body) => fixedCount + (body.motionKind === "fixed" ? 1 : 0), 0);
|
|
743
|
+
if (fixedBodyCount > MAXIMUM_FIXED_BODY_COUNT) refuse("bad_world", `world.bodies has ${fixedBodyCount} fixed bodies, and a world holds ${MAXIMUM_FIXED_BODY_COUNT}.`, "world.bodies", "Make pieces that sit together one body of several shapes, or one mesh.");
|
|
744
|
+
if (checkedBodies.length - fixedBodyCount > MAXIMUM_BODY_COUNT) refuse("bad_world", `world.bodies has ${checkedBodies.length - fixedBodyCount} bodies that aren't fixed, and a world holds ${MAXIMUM_BODY_COUNT}.`, "world.bodies", 'Declare what never moves with motionKind: "fixed": those aren\'t counted here.');
|
|
631
745
|
const { surfaceNames = [] } = given;
|
|
632
746
|
if (!Array.isArray(surfaceNames) || surfaceNames.length > MAXIMUM_SURFACE_COUNT || surfaceNames.some((name) => typeof name !== "string" || !BODY_KIND_NAME_PATTERN.test(name)) || new Set(surfaceNames).size !== surfaceNames.length) refuse("bad_world", "world.surfaceNames isn't a list of surfaces, each named once.", "world.surfaceNames", `Give up to ${MAXIMUM_SURFACE_COUNT} names in lowercase words joined by underscores, like ["concrete", "metal", "wet_grass"]: what a shape's surfaceName and a mesh's triangleSurfaceIndices pick from, and what a hit says it met.`);
|
|
633
747
|
const named = { collisionLayers, surfaceNames };
|
|
@@ -644,6 +758,8 @@ export function definePhysicsWorld(given = {}) {
|
|
|
644
758
|
for (const name of wheelLayerNames ?? []) if (!layerNames.includes(name)) refuse("bad_world", `${what}.vehicle.wheelCollisionLayerNames names "${name}".`, `${what}.vehicle.wheelCollisionLayerNames`, `Use the world's collisionLayers: ${layerNames.join(", ")}.`);
|
|
645
759
|
}
|
|
646
760
|
if (!Array.isArray(joints) || joints.length > MAXIMUM_JOINT_COUNT) refuse("bad_world", `world.joints isn't a list of up to ${MAXIMUM_JOINT_COUNT} joints.`, "world.joints", 'Give joints like [{ id: "door:hinge", jointKind: "hinge", bodyId: "door", anchorM: { x: -0.45, y: 0, z: 0 } }].');
|
|
761
|
+
const declaredThingCount = checkedBodies.length - fixedBodyCount + joints.length;
|
|
762
|
+
if (declaredThingCount > MAXIMUM_DECLARED_THING_COUNT) refuse("bad_world", `world declares ${checkedBodies.length - fixedBodyCount} bodies that aren't fixed and ${joints.length} joints, and a world starts with ${MAXIMUM_DECLARED_THING_COUNT} of them together.`, "world.joints", "Declare fewer, and add the rest while playing with addBody and addJoint.");
|
|
647
763
|
const jointIds = new Set();
|
|
648
764
|
const checkedJoints = joints.map((joint, jointIndex) => {
|
|
649
765
|
const checked = checkJoint(joint, `world.joints[${jointIndex}]`, (bodyId) => ids.has(bodyId));
|
|
@@ -653,20 +769,34 @@ export function definePhysicsWorld(given = {}) {
|
|
|
653
769
|
});
|
|
654
770
|
if (!Number.isInteger(substepCount) || substepCount < 1 || substepCount > 8) refuse("bad_world", `world.substepCount is ${JSON.stringify(substepCount)}.`, "world.substepCount", "Give how many times the engine steps in each of the room's fixed steps, from 1 to 8: 2 (the default) steps a 30-a-second room 60 times a second, which cars and quick things want.");
|
|
655
771
|
const groundReach = lengthOf(groundReachM, "bad_world", "world.groundReachM", { minimumM: 2, maximumM: 1000 });
|
|
656
|
-
|
|
657
|
-
const
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
772
|
+
const ownCharacter = checkCharacterSize(character, "world.character", { heightM: 1.5, radiusM: 0.28, stepHeightM: 0.35, maximumSlopeDeg: 50 }, CHARACTER_KEYS);
|
|
773
|
+
const { characterSizes = [] } = given;
|
|
774
|
+
if (!Array.isArray(characterSizes) || characterSizes.length > MAXIMUM_CHARACTER_SIZE_COUNT) refuse("bad_world", `world.characterSizes isn't a list of up to ${MAXIMUM_CHARACTER_SIZE_COUNT} sizes.`, "world.characterSizes", 'Give the other sizes a character takes, like [{ name: "crouched", heightM: 1 }]: what a size leaves out is world.character\'s.');
|
|
775
|
+
const checkedCharacterSizes = characterSizes.map((characterSize, sizeIndex) => {
|
|
776
|
+
const what = `world.characterSizes[${sizeIndex}]`;
|
|
777
|
+
if (!isPlainObject(characterSize) || typeof characterSize.name !== "string" || !BODY_KIND_NAME_PATTERN.test(characterSize.name)) refuse("bad_world", `${what} has no name.`, `${what}.name`, 'Give each characterSize a name in lowercase words joined by underscores, like "crouched".');
|
|
778
|
+
if (characterSizes.findIndex((other) => other?.name === characterSize.name) !== sizeIndex) refuse("bad_world", `world.characterSizes names "${characterSize.name}" twice.`, `${what}.name`, "Give each characterSize its own name.");
|
|
779
|
+
return { name: characterSize.name, ...checkCharacterSize(characterSize, what, ownCharacter, CHARACTER_SIZE_KEYS) };
|
|
780
|
+
});
|
|
781
|
+
const { zones = [] } = given;
|
|
782
|
+
if (!Array.isArray(zones) || zones.length > MAXIMUM_ZONE_COUNT) refuse("bad_world", `world.zones isn't a list of up to ${MAXIMUM_ZONE_COUNT} zones.`, "world.zones", 'Give zones like [{ id: "capture:a", positionM: { x: 0, y: 1.5, z: 0 }, shapes: [{ shapeKind: "box", sizeM: { x: 6, y: 3, z: 6 } }] }].');
|
|
783
|
+
const bodyById = new Map(checkedBodies.map((body) => [body.id, body]));
|
|
784
|
+
const checkedZones = zones.map((zone, zoneIndex) => {
|
|
785
|
+
const checked = checkZone(zone, `world.zones[${zoneIndex}]`, { bodyById, layerNames });
|
|
786
|
+
if (zones.findIndex((other) => other?.id === checked.id) !== zoneIndex) refuse("bad_world", `world.zones names "${checked.id}" twice.`, `world.zones[${zoneIndex}].id`, "Give each zone its own id.");
|
|
787
|
+
return checked;
|
|
788
|
+
});
|
|
663
789
|
if (!PLAYER_CHARACTER_MODES.includes(playerCharacterMode)) refuse("bad_world", `world.playerCharacterMode is ${JSON.stringify(playerCharacterMode)}.`, "world.playerCharacterMode", `Use: ${PLAYER_CHARACTER_MODES.join(", ")}.`);
|
|
664
790
|
if (!Array.isArray(startPositionsM) || !startPositionsM.length || startPositionsM.length > MAXIMUM_CHARACTER_COUNT) refuse("bad_world", "world.startPositionsM isn't a list of places.", "world.startPositionsM", "Give where characters start, their feet on the ground, like [{ x: 0, y: 0, z: 0 }, { x: 2, y: 0, z: 0 }]: each new character takes the next.");
|
|
665
791
|
if (!isFiniteNumber(fallOutHeightM) || Math.abs(fallOutHeightM) > WORLD_REACH_M) refuse("bad_world", `world.fallOutHeightM is ${JSON.stringify(fallOutHeightM)}.`, "world.fallOutHeightM", "Give the height in metres below which anything that falls is put back where it started, like -30.");
|
|
666
792
|
return {
|
|
667
793
|
bodies: checkedBodies,
|
|
794
|
+
bodyById,
|
|
795
|
+
zones: checkedZones,
|
|
796
|
+
fixedBodyIds: checkedBodies.filter((body) => body.motionKind === "fixed").map((body) => body.id),
|
|
668
797
|
bodyKinds,
|
|
669
|
-
character:
|
|
798
|
+
character: ownCharacter,
|
|
799
|
+
characterSizes: checkedCharacterSizes,
|
|
670
800
|
collisionLayers,
|
|
671
801
|
surfaceNames: [...surfaceNames],
|
|
672
802
|
joints: checkedJoints,
|