@adatechnology/game-destruction-three 0.21.0 → 0.22.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 +8 -0
- package/dist/index.d.ts +15 -5
- package/dist/index.global.js +848 -88
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +763 -80
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -759,6 +759,110 @@ var GroundSpots = class {
|
|
|
759
759
|
}
|
|
760
760
|
};
|
|
761
761
|
|
|
762
|
+
// src/piece-contacts.ts
|
|
763
|
+
var SEGMENT_BOX_ROUNDS = 4;
|
|
764
|
+
var PARALLEL = 1e-4;
|
|
765
|
+
var sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
766
|
+
var dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
767
|
+
var along = (a, d, t) => [a[0] + d[0] * t, a[1] + d[1] * t, a[2] + d[2] * t];
|
|
768
|
+
var clamp01 = (t) => t < 0 ? 0 : t > 1 ? 1 : t;
|
|
769
|
+
var nearestOnSegment = (from, to, p) => {
|
|
770
|
+
const d = sub(to, from);
|
|
771
|
+
const len2 = dot(d, d);
|
|
772
|
+
return len2 < 1e-12 ? from : along(from, d, clamp01(dot(sub(p, from), d) / len2));
|
|
773
|
+
};
|
|
774
|
+
var nearestOfSegments = (p1, q1, p2, q2) => {
|
|
775
|
+
const d1 = sub(q1, p1);
|
|
776
|
+
const d2 = sub(q2, p2);
|
|
777
|
+
const r = sub(p1, p2);
|
|
778
|
+
const a = dot(d1, d1);
|
|
779
|
+
const e = dot(d2, d2);
|
|
780
|
+
const f = dot(d2, r);
|
|
781
|
+
let s = 0;
|
|
782
|
+
let t = 0;
|
|
783
|
+
if (a < 1e-12 && e < 1e-12) return [p1, p2];
|
|
784
|
+
if (a < 1e-12) t = clamp01(f / e);
|
|
785
|
+
else {
|
|
786
|
+
const c = dot(d1, r);
|
|
787
|
+
if (e < 1e-12) s = clamp01(-c / a);
|
|
788
|
+
else {
|
|
789
|
+
const b = dot(d1, d2);
|
|
790
|
+
const denom = a * e - b * b;
|
|
791
|
+
if (denom > PARALLEL * a * e) s = clamp01((b * f - c * e) / denom);
|
|
792
|
+
else {
|
|
793
|
+
const s0 = clamp01(-c / a);
|
|
794
|
+
const s1 = clamp01((b - c) / a);
|
|
795
|
+
s = (s0 + s1) / 2;
|
|
796
|
+
}
|
|
797
|
+
t = (b * s + f) / e;
|
|
798
|
+
if (t < 0) {
|
|
799
|
+
t = 0;
|
|
800
|
+
s = clamp01(-c / a);
|
|
801
|
+
} else if (t > 1) {
|
|
802
|
+
t = 1;
|
|
803
|
+
s = clamp01((b - c) / a);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
return [along(p1, d1, s), along(p2, d2, t)];
|
|
808
|
+
};
|
|
809
|
+
var toBox = (box, p) => {
|
|
810
|
+
const d = sub(p, box.c);
|
|
811
|
+
const e = box.e;
|
|
812
|
+
return [e[0] * d[0] + e[1] * d[1] + e[2] * d[2], e[4] * d[0] + e[5] * d[1] + e[6] * d[2], e[8] * d[0] + e[9] * d[1] + e[10] * d[2]];
|
|
813
|
+
};
|
|
814
|
+
var fromBox = (box, l) => {
|
|
815
|
+
const e = box.e;
|
|
816
|
+
return [box.c[0] + e[0] * l[0] + e[4] * l[1] + e[8] * l[2], box.c[1] + e[1] * l[0] + e[5] * l[1] + e[9] * l[2], box.c[2] + e[2] * l[0] + e[6] * l[1] + e[10] * l[2]];
|
|
817
|
+
};
|
|
818
|
+
var capsuleBox = (from, to, radius, box) => {
|
|
819
|
+
let p = nearestOnSegment(from, to, box.c);
|
|
820
|
+
let l = toBox(box, p);
|
|
821
|
+
for (let round = 0; round < SEGMENT_BOX_ROUNDS; round++) {
|
|
822
|
+
const clamped = [Math.max(-box.half[0], Math.min(box.half[0], l[0])), Math.max(-box.half[1], Math.min(box.half[1], l[1])), Math.max(-box.half[2], Math.min(box.half[2], l[2]))];
|
|
823
|
+
p = nearestOnSegment(from, to, fromBox(box, clamped));
|
|
824
|
+
l = toBox(box, p);
|
|
825
|
+
}
|
|
826
|
+
const q = [Math.max(-box.half[0], Math.min(box.half[0], l[0])), Math.max(-box.half[1], Math.min(box.half[1], l[1])), Math.max(-box.half[2], Math.min(box.half[2], l[2]))];
|
|
827
|
+
const gap = [l[0] - q[0], l[1] - q[1], l[2] - q[2]];
|
|
828
|
+
const distance = Math.hypot(gap[0], gap[1], gap[2]);
|
|
829
|
+
if (distance >= radius) return null;
|
|
830
|
+
const e = box.e;
|
|
831
|
+
if (distance > 1e-6) {
|
|
832
|
+
const n = [gap[0] / distance, gap[1] / distance, gap[2] / distance];
|
|
833
|
+
const normal = [e[0] * n[0] + e[4] * n[1] + e[8] * n[2], e[1] * n[0] + e[5] * n[1] + e[9] * n[2], e[2] * n[0] + e[6] * n[1] + e[10] * n[2]];
|
|
834
|
+
return { point: fromBox(box, q), normal, depth: radius - distance };
|
|
835
|
+
}
|
|
836
|
+
let axis = 0;
|
|
837
|
+
for (let k = 1; k < 3; k++) if (box.half[k] - Math.abs(l[k]) < box.half[axis] - Math.abs(l[axis])) axis = k;
|
|
838
|
+
const sign = l[axis] < 0 ? -1 : 1;
|
|
839
|
+
return { point: p, normal: [sign * e[axis * 4], sign * e[axis * 4 + 1], sign * e[axis * 4 + 2]], depth: radius + box.half[axis] - Math.abs(l[axis]) };
|
|
840
|
+
};
|
|
841
|
+
function shapeContact(a, b) {
|
|
842
|
+
if (a.kind === "capsule" && b.kind === "capsule") {
|
|
843
|
+
const [pa, pb] = nearestOfSegments(a.from, a.to, b.from, b.to);
|
|
844
|
+
const d = sub(pa, pb);
|
|
845
|
+
const distance = Math.hypot(d[0], d[1], d[2]);
|
|
846
|
+
const reach = a.radius + b.radius;
|
|
847
|
+
if (distance >= reach) return null;
|
|
848
|
+
const normal = distance > 1e-6 ? [d[0] / distance, d[1] / distance, d[2] / distance] : [1, 0, 0];
|
|
849
|
+
return { point: along(pb, normal, b.radius - (reach - distance) / 2), normal, depth: reach - distance };
|
|
850
|
+
}
|
|
851
|
+
if (a.kind === "capsule" && b.kind === "boxes") {
|
|
852
|
+
let best = null;
|
|
853
|
+
for (const box of b.boxes) {
|
|
854
|
+
const hit = capsuleBox(a.from, a.to, a.radius, box);
|
|
855
|
+
if (hit && (!best || hit.depth > best.depth)) best = hit;
|
|
856
|
+
}
|
|
857
|
+
return best;
|
|
858
|
+
}
|
|
859
|
+
if (a.kind === "boxes" && b.kind === "capsule") {
|
|
860
|
+
const hit = shapeContact(b, a);
|
|
861
|
+
return hit && { point: hit.point, normal: [-hit.normal[0], -hit.normal[1], -hit.normal[2]], depth: hit.depth };
|
|
862
|
+
}
|
|
863
|
+
return null;
|
|
864
|
+
}
|
|
865
|
+
|
|
762
866
|
// src/pieces.ts
|
|
763
867
|
var SLAM_MIN_SPEED = 2;
|
|
764
868
|
var SLAM_BOUNCE = 0.2;
|
|
@@ -767,6 +871,8 @@ var SUB_DT = 1 / 120;
|
|
|
767
871
|
var MU_STATIC = 0.7;
|
|
768
872
|
var MU_DYNAMIC = 0.6;
|
|
769
873
|
var SLIP_SPEED = 0.02;
|
|
874
|
+
var ROLLING_RESISTANCE = 0.015;
|
|
875
|
+
var ROUND_LYING = Math.SQRT1_2;
|
|
770
876
|
var SLEEP_AFTER = 0.5;
|
|
771
877
|
var CALM_DRIFT = 0.01;
|
|
772
878
|
var CALM_TURN = 0.05;
|
|
@@ -787,6 +893,13 @@ var SUPPORT_CHECKS2 = 16;
|
|
|
787
893
|
var SUPPORT_GAP2 = 0.02;
|
|
788
894
|
var WAKE_ABOVE_SPEED = 0.05;
|
|
789
895
|
var WAKE_ABOVE_SPIN = 0.1;
|
|
896
|
+
var SURFACE_CELL = 0.5;
|
|
897
|
+
var SURFACE_CELLS_ACROSS = 24;
|
|
898
|
+
var cellKey = (ix, iz) => (ix + 32768) * 65536 + (iz + 32768);
|
|
899
|
+
var MAX_COLLISION_STEPS = 8;
|
|
900
|
+
var MIN_ROUND_RADIUS = 5e-3;
|
|
901
|
+
var BOUNCE_MIN_SPEED = 0.1;
|
|
902
|
+
var KNOCK_FLAT = 0.5;
|
|
790
903
|
var topFace = 1;
|
|
791
904
|
var boxTopAt = (s, x, z) => {
|
|
792
905
|
const e = s.e;
|
|
@@ -819,8 +932,11 @@ var faceNormal = (s, axis) => {
|
|
|
819
932
|
};
|
|
820
933
|
var pairFriction = (a, b) => ({
|
|
821
934
|
static: Math.sqrt((a?.static ?? MU_STATIC) * (b?.static ?? MU_STATIC)),
|
|
822
|
-
dynamic: Math.sqrt((a?.dynamic ?? MU_DYNAMIC) * (b?.dynamic ?? MU_DYNAMIC))
|
|
935
|
+
dynamic: Math.sqrt((a?.dynamic ?? MU_DYNAMIC) * (b?.dynamic ?? MU_DYNAMIC)),
|
|
936
|
+
// the softer of the two sets how much a round thing rolling on the other sinks in and is held back
|
|
937
|
+
rolling: Math.max(a?.rolling ?? 0, b?.rolling ?? ROLLING_RESISTANCE)
|
|
823
938
|
});
|
|
939
|
+
var pairRestitution = (a, b) => Math.sqrt((a ?? SLAM_BOUNCE) * (b ?? SLAM_BOUNCE));
|
|
824
940
|
var PieceSystem = class {
|
|
825
941
|
constructor(THREE, o) {
|
|
826
942
|
__publicField(this, "THREE", THREE);
|
|
@@ -830,9 +946,20 @@ var PieceSystem = class {
|
|
|
830
946
|
__publicField(this, "p");
|
|
831
947
|
__publicField(this, "q");
|
|
832
948
|
__publicField(this, "dq");
|
|
949
|
+
__publicField(this, "nextQ");
|
|
833
950
|
__publicField(this, "m");
|
|
834
951
|
__publicField(this, "ax");
|
|
835
952
|
__publicField(this, "solids", []);
|
|
953
|
+
/** The boxes other pieces land on: a piece's own parts when it has them (`partBoxes`), else its box. */
|
|
954
|
+
__publicField(this, "surfaces", []);
|
|
955
|
+
/** The surfaces by the cells of a grid on the ground (`SURFACE_CELL`) their bounding circle covers: what is under a point is looked for in its cell only. */
|
|
956
|
+
__publicField(this, "surfaceGrid", /* @__PURE__ */ new Map());
|
|
957
|
+
/** The surfaces wider than `SURFACE_CELLS_ACROSS` cells (a slab come down whole): looked at for every point. */
|
|
958
|
+
__publicField(this, "wideSurfaces", []);
|
|
959
|
+
/** Each piece's box of the step (`solids`), by its body. */
|
|
960
|
+
__publicField(this, "solidOf", /* @__PURE__ */ new Map());
|
|
961
|
+
/** Each piece's surfaces of the step (its parts' boxes, or its box), by its body. */
|
|
962
|
+
__publicField(this, "surfacesOf", /* @__PURE__ */ new Map());
|
|
836
963
|
__publicField(this, "supportTurn", 0);
|
|
837
964
|
/** The surface normal and the friction of what the last `supportAt` found under the point (the ground and fixed tops are level: up, and no piece). */
|
|
838
965
|
__publicField(this, "supportNormal", UP);
|
|
@@ -840,6 +967,7 @@ var PieceSystem = class {
|
|
|
840
967
|
this.p = new THREE.Vector3();
|
|
841
968
|
this.q = new THREE.Quaternion();
|
|
842
969
|
this.dq = new THREE.Quaternion();
|
|
970
|
+
this.nextQ = new THREE.Quaternion();
|
|
843
971
|
this.m = new THREE.Matrix4();
|
|
844
972
|
this.ax = new THREE.Vector3();
|
|
845
973
|
}
|
|
@@ -890,10 +1018,33 @@ var PieceSystem = class {
|
|
|
890
1018
|
}
|
|
891
1019
|
remove(objectId) {
|
|
892
1020
|
const i = this.list.findIndex((b) => b.objectId === objectId);
|
|
893
|
-
if (i
|
|
1021
|
+
if (i < 0) return;
|
|
1022
|
+
const [gone] = this.list.splice(i, 1);
|
|
1023
|
+
if (gone) this.forgetSolids(gone);
|
|
894
1024
|
}
|
|
895
1025
|
clear() {
|
|
896
1026
|
this.list.length = 0;
|
|
1027
|
+
this.solids = [];
|
|
1028
|
+
this.surfaces = [];
|
|
1029
|
+
this.solidOf.clear();
|
|
1030
|
+
this.surfacesOf.clear();
|
|
1031
|
+
this.surfaceGrid.clear();
|
|
1032
|
+
this.wideSurfaces.length = 0;
|
|
1033
|
+
}
|
|
1034
|
+
/** Drops a piece from the boxes of the step (`solids`, `surfaces` and their grid). */
|
|
1035
|
+
forgetSolids(body) {
|
|
1036
|
+
this.solidOf.delete(body);
|
|
1037
|
+
this.surfacesOf.delete(body);
|
|
1038
|
+
this.solids = this.solids.filter((s) => s.body !== body);
|
|
1039
|
+
this.surfaces = this.surfaces.filter((s) => s.body !== body);
|
|
1040
|
+
for (const [key, cell] of this.surfaceGrid) {
|
|
1041
|
+
const kept = cell.filter((s) => s.body !== body);
|
|
1042
|
+
if (kept.length === 0) this.surfaceGrid.delete(key);
|
|
1043
|
+
else if (kept.length !== cell.length) this.surfaceGrid.set(key, kept);
|
|
1044
|
+
}
|
|
1045
|
+
const wide = this.wideSurfaces.filter((s) => s.body !== body);
|
|
1046
|
+
this.wideSurfaces.length = 0;
|
|
1047
|
+
this.wideSurfaces.push(...wide);
|
|
897
1048
|
}
|
|
898
1049
|
/** `clear` and the clock back to 0. */
|
|
899
1050
|
reset() {
|
|
@@ -934,9 +1085,17 @@ var PieceSystem = class {
|
|
|
934
1085
|
mulInvInertiaWith(b, e, x, y, z) {
|
|
935
1086
|
const [hx, hy, hz] = b.halfExtents;
|
|
936
1087
|
const k = 3 / b.mass;
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
1088
|
+
let ix = k / (hy * hy + hz * hz);
|
|
1089
|
+
let iy = k / (hx * hx + hz * hz);
|
|
1090
|
+
let iz = k / (hx * hx + hy * hy);
|
|
1091
|
+
const round = b.round;
|
|
1092
|
+
if (round) {
|
|
1093
|
+
const about = 1 / (round.inertia * b.mass * round.radius * round.radius);
|
|
1094
|
+
if (round.kind === "sphere") ix = iy = iz = about;
|
|
1095
|
+
else if (round.axis === 0) ix = about;
|
|
1096
|
+
else if (round.axis === 1) iy = about;
|
|
1097
|
+
else iz = about;
|
|
1098
|
+
}
|
|
940
1099
|
const lx = (e[0] * x + e[1] * y + e[2] * z) * ix;
|
|
941
1100
|
const ly = (e[4] * x + e[5] * y + e[6] * z) * iy;
|
|
942
1101
|
const lz = (e[8] * x + e[9] * y + e[10] * z) * iz;
|
|
@@ -991,12 +1150,89 @@ var PieceSystem = class {
|
|
|
991
1150
|
}
|
|
992
1151
|
}
|
|
993
1152
|
update(dt) {
|
|
1153
|
+
const steps = this.collisionSteps(dt);
|
|
1154
|
+
for (let i = 0; i < steps; i++) this.step(dt / steps);
|
|
1155
|
+
}
|
|
1156
|
+
/**
|
|
1157
|
+
* Steps of an update: one, or more while a round piece and a piece it could meet within the update close in fast enough to pass through each other between two (closing at
|
|
1158
|
+
* their two speeds they cover at most the round one's radius per step: two cans at 3.6 m/s close 12 cm in a frame, a can is 7 cm across; a plank thrown at a can still counts).
|
|
1159
|
+
* A round piece with nothing in reach takes one step, so its sweep against the world (the costly part) runs once.
|
|
1160
|
+
*/
|
|
1161
|
+
collisionSteps(dt) {
|
|
1162
|
+
let need = 1;
|
|
1163
|
+
const p = this.p;
|
|
1164
|
+
const speedOf = (b) => b.held ? 0 : Math.hypot(b.velocity[0], b.velocity[1], b.velocity[2]);
|
|
1165
|
+
for (const b of this.list) {
|
|
1166
|
+
if (!b.round || b.held) continue;
|
|
1167
|
+
const radius = Math.max(MIN_ROUND_RADIUS, b.round.radius);
|
|
1168
|
+
const speed = b.sleeping ? 0 : speedOf(b);
|
|
1169
|
+
const c = this.centerOf(b, p);
|
|
1170
|
+
const cx = c.x, cy = c.y, cz = c.z;
|
|
1171
|
+
const rb = Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]);
|
|
1172
|
+
for (const o of this.list) {
|
|
1173
|
+
if (o === b || b.sleeping && o.sleeping) continue;
|
|
1174
|
+
const closing = speed + (o.sleeping ? 0 : speedOf(o));
|
|
1175
|
+
if (closing * dt < radius * 0.5) continue;
|
|
1176
|
+
const reach = closing * dt + rb + Math.hypot(o.halfExtents[0], o.halfExtents[1], o.halfExtents[2]);
|
|
1177
|
+
const oc = this.centerOf(o, p);
|
|
1178
|
+
if (Math.abs(oc.x - cx) > reach || Math.abs(oc.y - cy) > reach || Math.abs(oc.z - cz) > reach) continue;
|
|
1179
|
+
need = Math.max(need, Math.ceil(closing * dt / radius));
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
return Math.max(1, Math.min(MAX_COLLISION_STEPS, need));
|
|
1183
|
+
}
|
|
1184
|
+
step(dt) {
|
|
994
1185
|
this.time += dt;
|
|
995
1186
|
this.solids = this.list.map((body) => {
|
|
996
1187
|
const c = this.centerOf(body, new this.THREE.Vector3());
|
|
997
1188
|
const h = body.halfExtents;
|
|
998
1189
|
return { body, c: [c.x, c.y, c.z], e: [...new this.THREE.Matrix4().makeRotationFromQuaternion(body.root.quaternion).elements], half: h, r: Math.hypot(h[0], h[1], h[2]) };
|
|
999
1190
|
});
|
|
1191
|
+
this.surfaces = this.solids.flatMap((whole) => {
|
|
1192
|
+
const parts = whole.body.partBoxes;
|
|
1193
|
+
if (!parts) return [whole];
|
|
1194
|
+
const e = whole.e;
|
|
1195
|
+
return parts.map(({ center, half }) => ({
|
|
1196
|
+
body: whole.body,
|
|
1197
|
+
c: [
|
|
1198
|
+
whole.c[0] + e[0] * center[0] + e[4] * center[1] + e[8] * center[2],
|
|
1199
|
+
whole.c[1] + e[1] * center[0] + e[5] * center[1] + e[9] * center[2],
|
|
1200
|
+
whole.c[2] + e[2] * center[0] + e[6] * center[1] + e[10] * center[2]
|
|
1201
|
+
],
|
|
1202
|
+
e,
|
|
1203
|
+
half,
|
|
1204
|
+
r: Math.hypot(half[0], half[1], half[2])
|
|
1205
|
+
}));
|
|
1206
|
+
});
|
|
1207
|
+
this.solidOf.clear();
|
|
1208
|
+
for (const one of this.solids) this.solidOf.set(one.body, one);
|
|
1209
|
+
this.surfacesOf.clear();
|
|
1210
|
+
for (const one of this.surfaces) {
|
|
1211
|
+
const list = this.surfacesOf.get(one.body);
|
|
1212
|
+
if (list) list.push(one);
|
|
1213
|
+
else this.surfacesOf.set(one.body, [one]);
|
|
1214
|
+
}
|
|
1215
|
+
this.surfaceGrid.clear();
|
|
1216
|
+
this.wideSurfaces.length = 0;
|
|
1217
|
+
for (const one of this.surfaces) {
|
|
1218
|
+
const x0 = Math.floor((one.c[0] - one.r) / SURFACE_CELL);
|
|
1219
|
+
const x1 = Math.floor((one.c[0] + one.r) / SURFACE_CELL);
|
|
1220
|
+
const z0 = Math.floor((one.c[2] - one.r) / SURFACE_CELL);
|
|
1221
|
+
const z1 = Math.floor((one.c[2] + one.r) / SURFACE_CELL);
|
|
1222
|
+
if (x1 - x0 > SURFACE_CELLS_ACROSS || z1 - z0 > SURFACE_CELLS_ACROSS) {
|
|
1223
|
+
this.wideSurfaces.push(one);
|
|
1224
|
+
continue;
|
|
1225
|
+
}
|
|
1226
|
+
for (let ix = x0; ix <= x1; ix++) {
|
|
1227
|
+
for (let iz = z0; iz <= z1; iz++) {
|
|
1228
|
+
const key = cellKey(ix, iz);
|
|
1229
|
+
const cell = this.surfaceGrid.get(key);
|
|
1230
|
+
if (cell) cell.push(one);
|
|
1231
|
+
else this.surfaceGrid.set(key, [one]);
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
if (this.wideSurfaces.length > 0) for (const cell of this.surfaceGrid.values()) cell.push(...this.wideSurfaces);
|
|
1000
1236
|
this.wakeUnsupported();
|
|
1001
1237
|
for (const b of this.list) {
|
|
1002
1238
|
if (b.held) {
|
|
@@ -1014,7 +1250,7 @@ var PieceSystem = class {
|
|
|
1014
1250
|
const w = b.angularVelocity;
|
|
1015
1251
|
if (Math.hypot(v[0], v[1], v[2]) > WAKE_ABOVE_SPEED || Math.hypot(w[0], w[1], w[2]) > WAKE_ABOVE_SPIN) this.wakeAbove(b);
|
|
1016
1252
|
}
|
|
1017
|
-
this.
|
|
1253
|
+
this.collide(dt);
|
|
1018
1254
|
}
|
|
1019
1255
|
/** A few sleeping pieces each update (`SUPPORT_CHECKS`, in turn) look under them again: one whose table broke, or whose shelf fell over, falls. */
|
|
1020
1256
|
wakeUnsupported() {
|
|
@@ -1043,11 +1279,15 @@ var PieceSystem = class {
|
|
|
1043
1279
|
}
|
|
1044
1280
|
return false;
|
|
1045
1281
|
}
|
|
1282
|
+
/** The surfaces whose cell holds `(x, z)`, the wide ones (`wideSurfaces`) among them. */
|
|
1283
|
+
surfacesAt(x, z) {
|
|
1284
|
+
return this.surfaceGrid.get(cellKey(Math.floor(x / SURFACE_CELL), Math.floor(z / SURFACE_CELL))) ?? this.wideSurfaces;
|
|
1285
|
+
}
|
|
1046
1286
|
/** The highest top of a piece (other than `self`) under `(x, z)` that a point at height `y` stands on (at most `step` above it), -Infinity when none; the boxes of the last update. */
|
|
1047
1287
|
topAt(x, z, y, step, self) {
|
|
1048
1288
|
let top = -Infinity;
|
|
1049
|
-
for (const s of this.
|
|
1050
|
-
if (s.body.objectId === self || s.body.held || Math.abs(x - s.c[0]) > s.r || Math.abs(z - s.c[2]) > s.r) continue;
|
|
1289
|
+
for (const s of this.surfacesAt(x, z)) {
|
|
1290
|
+
if (s.body.objectId === self || s.body.held && !s.body.carrying || Math.abs(x - s.c[0]) > s.r || Math.abs(z - s.c[2]) > s.r) continue;
|
|
1051
1291
|
const t = boxTopAt(s, x, z);
|
|
1052
1292
|
if (t > top && t <= y + step) top = t;
|
|
1053
1293
|
}
|
|
@@ -1055,25 +1295,50 @@ var PieceSystem = class {
|
|
|
1055
1295
|
}
|
|
1056
1296
|
/** What lay asleep on `b` (a box over its top) wakes at once when `b` moves: a can on a table that goes over goes with it. */
|
|
1057
1297
|
wakeAbove(b) {
|
|
1058
|
-
const below = this.
|
|
1298
|
+
const below = this.solidOf.get(b);
|
|
1059
1299
|
if (!below) return;
|
|
1060
1300
|
const top = below.c[1] + below.half[0] * Math.abs(below.e[1]) + below.half[1] * Math.abs(below.e[5]) + below.half[2] * Math.abs(below.e[9]);
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1301
|
+
const x0 = Math.floor((below.c[0] - below.r) / SURFACE_CELL);
|
|
1302
|
+
const x1 = Math.floor((below.c[0] + below.r) / SURFACE_CELL);
|
|
1303
|
+
const z0 = Math.floor((below.c[2] - below.r) / SURFACE_CELL);
|
|
1304
|
+
const z1 = Math.floor((below.c[2] + below.r) / SURFACE_CELL);
|
|
1305
|
+
const wide = x1 - x0 > SURFACE_CELLS_ACROSS || z1 - z0 > SURFACE_CELLS_ACROSS;
|
|
1306
|
+
const look = (part) => {
|
|
1307
|
+
const o = part.body;
|
|
1308
|
+
const s = this.solidOf.get(o);
|
|
1309
|
+
if (!s || !o.sleeping || o.held || o === b) return;
|
|
1310
|
+
if (s.c[1] < below.c[1] || s.c[1] - s.r > top + STEP || Math.hypot(s.c[0] - below.c[0], s.c[2] - below.c[2]) > s.r + below.r) return;
|
|
1311
|
+
if (boxTopAt(below, s.c[0], s.c[2]) === -Infinity && !this.restsOn(o, b, true)) return;
|
|
1065
1312
|
o.sleeping = false;
|
|
1066
1313
|
o.still = 0;
|
|
1067
1314
|
o.calmAt = null;
|
|
1315
|
+
};
|
|
1316
|
+
if (wide) for (const s of this.solids) look(s);
|
|
1317
|
+
else for (let ix = x0; ix <= x1; ix++) for (let iz = z0; iz <= z1; iz++) for (const part of this.surfaceGrid.get(cellKey(ix, iz)) ?? []) look(part);
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* Whether `o` rests on a top of `below` (its parts, or its box): a corner of `o` (its contacts or its box) is over that top and within `STEP` of it. With `lowest`, only its
|
|
1321
|
+
* lowest corners count (within `STEP` of its lowest): a can rolling by another is beside it, not on it; without, a board leaning on a seat by its high end rests on the seat.
|
|
1322
|
+
*/
|
|
1323
|
+
restsOn(o, below, lowest) {
|
|
1324
|
+
const c = this.centerOf(o, this.p);
|
|
1325
|
+
const e = this.m.makeRotationFromQuaternion(o.root.quaternion).elements;
|
|
1326
|
+
const points = this.samples(o, false).map(([sx, sy, sz]) => [c.x + e[0] * sx + e[4] * sy + e[8] * sz, c.y + e[1] * sx + e[5] * sy + e[9] * sz, c.z + e[2] * sx + e[6] * sy + e[10] * sz]);
|
|
1327
|
+
let low = Infinity;
|
|
1328
|
+
for (const p of points) low = Math.min(low, p[1]);
|
|
1329
|
+
for (const [x, y, z] of points) {
|
|
1330
|
+
if (lowest && y > low + STEP) continue;
|
|
1331
|
+
for (const s of this.surfacesAt(x, z)) if (s.body === below && Math.abs(y - boxTopAt(s, x, z)) <= STEP) return true;
|
|
1068
1332
|
}
|
|
1333
|
+
return false;
|
|
1069
1334
|
}
|
|
1070
1335
|
/** Where a corner of `self` at `(x, y, z)` lands: the ground, the top of another piece or of something fixed under it. */
|
|
1071
1336
|
supportAt(self, x, y, z, step) {
|
|
1072
1337
|
let top = this.o.groundAt(x, z);
|
|
1073
1338
|
let on = null;
|
|
1074
1339
|
let face = 1;
|
|
1075
|
-
for (const s of this.
|
|
1076
|
-
if (s.body === self || s.body.held || Math.abs(x - s.c[0]) > s.r || Math.abs(z - s.c[2]) > s.r) continue;
|
|
1340
|
+
for (const s of this.surfacesAt(x, z)) {
|
|
1341
|
+
if (s.body === self || s.body.held && !s.body.carrying || Math.abs(x - s.c[0]) > s.r || Math.abs(z - s.c[2]) > s.r) continue;
|
|
1077
1342
|
const t = boxTopAt(s, x, z);
|
|
1078
1343
|
if (t > top && t <= y + step) {
|
|
1079
1344
|
top = t;
|
|
@@ -1098,9 +1363,10 @@ var PieceSystem = class {
|
|
|
1098
1363
|
const into = -(v[0] * n[0] + v[1] * n[1] + v[2] * n[2]);
|
|
1099
1364
|
if (into <= 0) return;
|
|
1100
1365
|
const k = Math.min(1, hit.distance / travel);
|
|
1366
|
+
const bounce = pairRestitution(b.restitution, hit.restitution);
|
|
1101
1367
|
for (let i = 0; i < 3; i++) {
|
|
1102
1368
|
pos[i] = start[i] + (pos[i] - start[i]) * k;
|
|
1103
|
-
v[i] += (1 +
|
|
1369
|
+
v[i] += (1 + bounce) * into * n[i];
|
|
1104
1370
|
}
|
|
1105
1371
|
const w = b.angularVelocity;
|
|
1106
1372
|
w[0] *= 0.5;
|
|
@@ -1139,9 +1405,16 @@ var PieceSystem = class {
|
|
|
1139
1405
|
gLow = Math.min(gLow, g);
|
|
1140
1406
|
gHigh = Math.max(gHigh, g);
|
|
1141
1407
|
}
|
|
1142
|
-
|
|
1143
|
-
|
|
1408
|
+
let local = b.round ? this.roundSamples(b, q) : gHigh - gLow > STEP ? this.samples(b, true) : corners;
|
|
1409
|
+
let prev = local.map(() => [0, 0, 0]);
|
|
1144
1410
|
for (let s = 0; s < steps; s++) {
|
|
1411
|
+
if (b.round) {
|
|
1412
|
+
const wl0 = Math.hypot(w[0], w[1], w[2]);
|
|
1413
|
+
const next = this.nextQ.copy(q);
|
|
1414
|
+
if (wl0 > 1e-6) next.premultiply(this.dq.setFromAxisAngle(this.ax.set(w[0] / wl0, w[1] / wl0, w[2] / wl0), wl0 * h)).normalize();
|
|
1415
|
+
local = this.roundSamples(b, next);
|
|
1416
|
+
if (prev.length !== local.length) prev = local.map(() => [0, 0, 0]);
|
|
1417
|
+
}
|
|
1145
1418
|
this.m.makeRotationFromQuaternion(q);
|
|
1146
1419
|
const e0 = this.m.elements;
|
|
1147
1420
|
for (let i = 0; i < local.length; i++) {
|
|
@@ -1159,7 +1432,7 @@ var PieceSystem = class {
|
|
|
1159
1432
|
this.dq.setFromAxisAngle(this.ax.set(w[0] / wl, w[1] / wl, w[2] / wl), wl * h);
|
|
1160
1433
|
q.premultiply(this.dq).normalize();
|
|
1161
1434
|
}
|
|
1162
|
-
const damp = Math.exp(-0.15 * h);
|
|
1435
|
+
const damp = b.round ? 1 : Math.exp(-0.15 * h);
|
|
1163
1436
|
w[0] *= damp;
|
|
1164
1437
|
w[1] *= damp;
|
|
1165
1438
|
w[2] *= damp;
|
|
@@ -1227,6 +1500,12 @@ var PieceSystem = class {
|
|
|
1227
1500
|
}
|
|
1228
1501
|
}
|
|
1229
1502
|
if (lowest < Infinity) b.floor = lowest;
|
|
1503
|
+
if (b.round && ns.length > 0) {
|
|
1504
|
+
const sum = [0, 0, 0];
|
|
1505
|
+
for (const one of ns) for (let k = 0; k < 3; k++) sum[k] += one[k];
|
|
1506
|
+
const len = Math.hypot(sum[0], sum[1], sum[2]);
|
|
1507
|
+
b.surfaceNormal = len > 1e-6 ? [sum[0] / len, sum[1] / len, sum[2] / len] : UP;
|
|
1508
|
+
} else if (b.round && rs.length === 0) b.surfaceNormal = UP;
|
|
1230
1509
|
if (side) {
|
|
1231
1510
|
pos[0] -= side.dir[0] * side.back;
|
|
1232
1511
|
pos[2] -= side.dir[1] * side.back;
|
|
@@ -1255,14 +1534,15 @@ var PieceSystem = class {
|
|
|
1255
1534
|
landPoint = [lx, pos[1] - Math.max(...rs.map((r) => -r[1])), lz];
|
|
1256
1535
|
}
|
|
1257
1536
|
const pointV = (r) => [v[0] + w[1] * r[2] - w[2] * r[1], v[1] + w[2] * r[0] - w[0] * r[2], v[2] + w[0] * r[1] - w[1] * r[0]];
|
|
1258
|
-
const
|
|
1537
|
+
const dot2 = (a, c2) => a[0] * c2[0] + a[1] * c2[1] + a[2] * c2[2];
|
|
1259
1538
|
const goal = rs.map((r, k) => {
|
|
1260
|
-
const vn =
|
|
1539
|
+
const vn = dot2(pointV(r), ns[k]);
|
|
1261
1540
|
return vn < 0 ? -restitution * vn : 0;
|
|
1262
1541
|
});
|
|
1263
1542
|
const slipping = rs.map((r, k) => {
|
|
1264
1543
|
const pv = pointV(r);
|
|
1265
|
-
|
|
1544
|
+
pv[1] += this.o.gravity * h;
|
|
1545
|
+
const vn = dot2(pv, ns[k]);
|
|
1266
1546
|
return Math.hypot(pv[0] - vn * ns[k][0], pv[1] - vn * ns[k][1], pv[2] - vn * ns[k][2]) > SLIP_SPEED;
|
|
1267
1547
|
});
|
|
1268
1548
|
const vx0 = v[0];
|
|
@@ -1293,13 +1573,13 @@ var PieceSystem = class {
|
|
|
1293
1573
|
for (let k = 0; k < rs.length; k++) {
|
|
1294
1574
|
const r = rs[k];
|
|
1295
1575
|
const n = ns[k];
|
|
1296
|
-
const vn =
|
|
1576
|
+
const vn = dot2(pointV(r), n);
|
|
1297
1577
|
const total = Math.max(0, accN[k] + (goal[k] - vn) / reach(r, n));
|
|
1298
1578
|
const j = total - accN[k];
|
|
1299
1579
|
accN[k] = total;
|
|
1300
1580
|
if (j !== 0) push(r, [n[0] * j, n[1] * j, n[2] * j]);
|
|
1301
1581
|
const pv = pointV(r);
|
|
1302
|
-
const pn =
|
|
1582
|
+
const pn = dot2(pv, n);
|
|
1303
1583
|
const vt = [pv[0] - pn * n[0], pv[1] - pn * n[1], pv[2] - pn * n[2]];
|
|
1304
1584
|
const speed = Math.hypot(vt[0], vt[1], vt[2]);
|
|
1305
1585
|
const f = [accT[k][0], accT[k][1], accT[k][2]];
|
|
@@ -1344,7 +1624,7 @@ var PieceSystem = class {
|
|
|
1344
1624
|
const height = -low;
|
|
1345
1625
|
const wx = x1 - x0;
|
|
1346
1626
|
const wz = z1 - z0;
|
|
1347
|
-
unstable = Math.min(wx, wz) < NARROW * height || x0 > 0 || x1 < 0 || z0 > 0 || z1 < 0;
|
|
1627
|
+
unstable = !b.round && (Math.min(wx, wz) < NARROW * height || x0 > 0 || x1 < 0 || z0 > 0 || z1 < 0);
|
|
1348
1628
|
if (b.tipping) {
|
|
1349
1629
|
b.tipping.seconds -= h;
|
|
1350
1630
|
const [hx, hy, hz] = b.halfExtents;
|
|
@@ -1363,13 +1643,19 @@ var PieceSystem = class {
|
|
|
1363
1643
|
if (wx <= wz) w[2] -= dirX * k;
|
|
1364
1644
|
else w[0] += dirZ * k;
|
|
1365
1645
|
}
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1646
|
+
if (b.round) {
|
|
1647
|
+
let rolling = 0;
|
|
1648
|
+
for (const mu of mus) rolling = Math.max(rolling, mu.rolling ?? ROLLING_RESISTANCE);
|
|
1649
|
+
this.rollingResistance(b, q, w, h, rolling);
|
|
1650
|
+
} else {
|
|
1651
|
+
const rd = Math.exp((rs.length >= 3 ? -3 : -1.5) * h);
|
|
1652
|
+
w[0] *= rd;
|
|
1653
|
+
w[1] *= rd;
|
|
1654
|
+
w[2] *= rd;
|
|
1655
|
+
const fd = Math.exp(-0.3 * h);
|
|
1656
|
+
v[0] *= fd;
|
|
1657
|
+
v[2] *= fd;
|
|
1658
|
+
}
|
|
1373
1659
|
}
|
|
1374
1660
|
if (this.o.sweep && !b.held) this.sweepOn(b, start, pos);
|
|
1375
1661
|
b.root.quaternion.copy(q);
|
|
@@ -1405,62 +1691,239 @@ var PieceSystem = class {
|
|
|
1405
1691
|
}
|
|
1406
1692
|
/** Whether the centre `c` of `upper` is over the top of `lower`'s box (as it was at the start of the update). */
|
|
1407
1693
|
onTopOf(upper, lower, c) {
|
|
1408
|
-
const s = this.
|
|
1694
|
+
const s = this.solidOf.get(lower);
|
|
1409
1695
|
if (!s) return false;
|
|
1410
1696
|
const top = boxTopAt(s, c.x, c.z);
|
|
1411
1697
|
return top > -Infinity && c.y >= top - 0.01;
|
|
1412
1698
|
}
|
|
1699
|
+
/**
|
|
1700
|
+
* The points a round piece touches the ground by as it is turned now (`q`): its rims (`contacts`), which hold it standing on an end, and the exact lowest point of each
|
|
1701
|
+
* rim (of the ball), so lying on its side it rests on a line and rolls smoothly instead of over the corners of a polygon.
|
|
1702
|
+
*/
|
|
1703
|
+
roundSamples(b, q) {
|
|
1704
|
+
const round = b.round;
|
|
1705
|
+
const rims = b.contacts ?? [];
|
|
1706
|
+
if (!round) return rims;
|
|
1707
|
+
const e = this.m.makeRotationFromQuaternion(q).elements;
|
|
1708
|
+
const n = b.surfaceNormal ?? UP;
|
|
1709
|
+
const down = [-(e[0] * n[0] + e[1] * n[1] + e[2] * n[2]), -(e[4] * n[0] + e[5] * n[1] + e[6] * n[2]), -(e[8] * n[0] + e[9] * n[1] + e[10] * n[2])];
|
|
1710
|
+
if (round.kind === "sphere") return [...rims, [down[0] * round.radius, down[1] * round.radius, down[2] * round.radius]];
|
|
1711
|
+
const k = round.axis;
|
|
1712
|
+
const across = [down[0], down[1], down[2]];
|
|
1713
|
+
across[k] = 0;
|
|
1714
|
+
const len = Math.hypot(across[0], across[1], across[2]);
|
|
1715
|
+
if (len < 1e-3) return rims;
|
|
1716
|
+
const lowest = (at, radius) => {
|
|
1717
|
+
const p = [across[0] / len * radius, across[1] / len * radius, across[2] / len * radius];
|
|
1718
|
+
p[k] = at;
|
|
1719
|
+
return p;
|
|
1720
|
+
};
|
|
1721
|
+
const half = b.halfExtents[k];
|
|
1722
|
+
const low = (round.profile ?? [[-half, round.radius], [half, round.radius]]).map(([at, radius]) => lowest(at, radius));
|
|
1723
|
+
return len > ROUND_LYING ? low : [...rims, ...low];
|
|
1724
|
+
}
|
|
1725
|
+
/**
|
|
1726
|
+
* Slows the spin of a round piece on the ground by its rolling resistance: a torque C_rr N r about the axis it rolls on (N = m g n_y on a surface of normal n), so with
|
|
1727
|
+
* I = k m r^2 the spin loses C_rr g n_y / (k r) per second, and down a slope of angle t it rolls at g (sin t - C_rr cos t) / (1 + k). Standing on its rim, a can's spin
|
|
1728
|
+
* about the rest dies down; lying, it keeps turning about the line it touches by (a tapered bottle rounds its narrow end).
|
|
1729
|
+
*/
|
|
1730
|
+
rollingResistance(b, q, w, h, rolling) {
|
|
1731
|
+
const round = b.round;
|
|
1732
|
+
if (!round) return;
|
|
1733
|
+
const e = this.m.makeRotationFromQuaternion(q).elements;
|
|
1734
|
+
const cylinder = round.kind === "cylinder";
|
|
1735
|
+
const a = cylinder ? [e[round.axis * 4], e[round.axis * 4 + 1], e[round.axis * 4 + 2]] : [0, 0, 0];
|
|
1736
|
+
const along2 = cylinder ? w[0] * a[0] + w[1] * a[1] + w[2] * a[2] : 0;
|
|
1737
|
+
const roll = cylinder ? [a[0] * along2, a[1] * along2, a[2] * along2] : [w[0], w[1], w[2]];
|
|
1738
|
+
const rest = [w[0] - roll[0], w[1] - roll[1], w[2] - roll[2]];
|
|
1739
|
+
const spin = Math.hypot(roll[0], roll[1], roll[2]);
|
|
1740
|
+
const upright = b.surfaceNormal?.[1] ?? 1;
|
|
1741
|
+
const slow = spin > 0 ? Math.max(0, spin - rolling * this.o.gravity * upright * h / (round.inertia * round.radius)) / spin : 0;
|
|
1742
|
+
const lying = cylinder && Math.abs(a[1]) < ROUND_LYING;
|
|
1743
|
+
const damp = lying ? 1 : Math.exp(-3 * h);
|
|
1744
|
+
for (let i = 0; i < 3; i++) w[i] = roll[i] * slow + rest[i] * damp;
|
|
1745
|
+
}
|
|
1413
1746
|
/** Half the height of the box of a piece as it is turned now. */
|
|
1414
1747
|
halfHeight(b) {
|
|
1415
1748
|
const e = this.m.makeRotationFromQuaternion(b.root.quaternion).elements;
|
|
1416
1749
|
return Math.abs(e[1]) * b.halfExtents[0] + Math.abs(e[5]) * b.halfExtents[1] + Math.abs(e[9]) * b.halfExtents[2];
|
|
1417
1750
|
}
|
|
1418
|
-
/**
|
|
1419
|
-
|
|
1751
|
+
/**
|
|
1752
|
+
* Pieces against each other. Candidates by sweep and prune along x on their bounding spheres (sorted once per step: fifty cans cost a sort and a handful of pairs, not
|
|
1753
|
+
* 1225); a pair with a round piece in it meets by its shapes (`shapeContact`) and exchanges an impulse, two boxes are only pushed apart (`separatePair`).
|
|
1754
|
+
*/
|
|
1755
|
+
collide(dt) {
|
|
1420
1756
|
const n = this.list.length;
|
|
1421
1757
|
if (n < 2) return;
|
|
1422
|
-
const
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1758
|
+
const centers = this.list.map((b) => {
|
|
1759
|
+
const c = this.centerOf(b, this.p);
|
|
1760
|
+
return [c.x, c.y, c.z];
|
|
1761
|
+
});
|
|
1762
|
+
const radii = this.list.map((b) => Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]));
|
|
1763
|
+
const order = this.list.map((_, i) => i).sort((i, j) => centers[i][0] - radii[i] - (centers[j][0] - radii[j]));
|
|
1764
|
+
for (let oi = 0; oi < n; oi++) {
|
|
1765
|
+
const i = order[oi];
|
|
1766
|
+
const reach = centers[i][0] + radii[i];
|
|
1767
|
+
for (let oj = oi + 1; oj < n; oj++) {
|
|
1768
|
+
const j = order[oj];
|
|
1769
|
+
if (centers[j][0] - radii[j] > reach) break;
|
|
1770
|
+
const a = this.list[i];
|
|
1428
1771
|
const b = this.list[j];
|
|
1429
|
-
|
|
1430
|
-
const
|
|
1431
|
-
|
|
1432
|
-
const
|
|
1433
|
-
|
|
1434
|
-
this.
|
|
1435
|
-
this.
|
|
1436
|
-
if (
|
|
1437
|
-
if (
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1772
|
+
if ((a.held || a.sleeping) && (b.held || b.sleeping)) continue;
|
|
1773
|
+
const r = radii[i] + radii[j];
|
|
1774
|
+
const ca = centers[i];
|
|
1775
|
+
const cb = centers[j];
|
|
1776
|
+
if (Math.abs(ca[1] - cb[1]) > r || Math.abs(ca[2] - cb[2]) > r) continue;
|
|
1777
|
+
if (!a.round && !b.round && Math.abs(ca[1] - cb[1]) >= (this.halfHeight(a) + this.halfHeight(b)) * 0.9) continue;
|
|
1778
|
+
if (this.onTopOf(b, a, new this.THREE.Vector3(...cb)) || this.onTopOf(a, b, new this.THREE.Vector3(...ca))) continue;
|
|
1779
|
+
if (a.round || b.round) this.knock(a, b, ca, cb);
|
|
1780
|
+
else if (this.partsOverlap(a, b) && !this.restsOn(a, b, false) && !this.restsOn(b, a, false)) this.separatePair(a, b, ca, cb, dt);
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
/** The shape a piece meets others by (`ContactShape`), as it stands now: a round one a capsule round its axis, any other the boxes of its parts or its box. */
|
|
1785
|
+
contactShape(b, c) {
|
|
1786
|
+
const e = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
|
|
1787
|
+
const round = b.round;
|
|
1788
|
+
if (round) {
|
|
1789
|
+
if (round.kind === "sphere") return { kind: "capsule", from: c, to: c, radius: round.radius };
|
|
1790
|
+
const reach = Math.max(0, b.halfExtents[round.axis] - round.radius);
|
|
1791
|
+
const k = round.axis * 4;
|
|
1792
|
+
const d = [e[k] * reach, e[k + 1] * reach, e[k + 2] * reach];
|
|
1793
|
+
return { kind: "capsule", from: [c[0] - d[0], c[1] - d[1], c[2] - d[2]], to: [c[0] + d[0], c[1] + d[1], c[2] + d[2]], radius: round.radius };
|
|
1794
|
+
}
|
|
1795
|
+
const parts = b.partBoxes ?? [{ center: [0, 0, 0], half: b.halfExtents }];
|
|
1796
|
+
return {
|
|
1797
|
+
kind: "boxes",
|
|
1798
|
+
boxes: parts.map(({ center, half }) => ({
|
|
1799
|
+
c: [c[0] + e[0] * center[0] + e[4] * center[1] + e[8] * center[2], c[1] + e[1] * center[0] + e[5] * center[1] + e[9] * center[2], c[2] + e[2] * center[0] + e[6] * center[1] + e[10] * center[2]],
|
|
1800
|
+
e,
|
|
1801
|
+
half
|
|
1802
|
+
}))
|
|
1803
|
+
};
|
|
1804
|
+
}
|
|
1805
|
+
/**
|
|
1806
|
+
* Two pieces that met, one of them round: they are put apart along the normal (by their inverse masses) and, closing in, exchange an impulse
|
|
1807
|
+
* j = -(1 + e) v_n / (1/m_a + 1/m_b + the turning of each), with the restitution e of their two materials, and the friction of their surfaces across it. The impulses are
|
|
1808
|
+
* equal and opposite, so the momentum of the pair is kept. A held piece is not moved (the hand holds it); a sleeping one wakes when it is knocked.
|
|
1809
|
+
*/
|
|
1810
|
+
knock(a, b, ca, cb) {
|
|
1811
|
+
const hit = shapeContact(this.contactShape(a, ca), this.contactShape(b, cb));
|
|
1812
|
+
if (!hit) return;
|
|
1813
|
+
const n = hit.normal;
|
|
1814
|
+
if (Math.abs(n[1]) > SLAM_FLOOR_NORMAL) return;
|
|
1815
|
+
const ra = [hit.point[0] - ca[0], hit.point[1] - ca[1], hit.point[2] - ca[2]];
|
|
1816
|
+
const rb = [hit.point[0] - cb[0], hit.point[1] - cb[1], hit.point[2] - cb[2]];
|
|
1817
|
+
const pointV = (body, r) => {
|
|
1818
|
+
const v2 = body.velocity;
|
|
1819
|
+
const w = body.angularVelocity;
|
|
1820
|
+
return [v2[0] + w[1] * r[2] - w[2] * r[1], v2[1] + w[2] * r[0] - w[0] * r[2], v2[2] + w[0] * r[1] - w[1] * r[0]];
|
|
1821
|
+
};
|
|
1822
|
+
const relative = () => {
|
|
1823
|
+
const va = pointV(a, ra);
|
|
1824
|
+
const vb = pointV(b, rb);
|
|
1825
|
+
return [va[0] - vb[0], va[1] - vb[1], va[2] - vb[2]];
|
|
1826
|
+
};
|
|
1827
|
+
const vn0 = relative();
|
|
1828
|
+
const closing = vn0[0] * n[0] + vn0[1] * n[1] + vn0[2] * n[2];
|
|
1829
|
+
const wake = (body) => {
|
|
1830
|
+
body.sleeping = false;
|
|
1831
|
+
body.still = 0;
|
|
1832
|
+
body.calmAt = null;
|
|
1833
|
+
};
|
|
1834
|
+
if (closing < -WAKE_ABOVE_SPEED) {
|
|
1835
|
+
if (a.sleeping) wake(a);
|
|
1836
|
+
if (b.sleeping) wake(b);
|
|
1837
|
+
}
|
|
1838
|
+
const ima = a.held || a.sleeping ? 0 : 1 / a.mass;
|
|
1839
|
+
const imb = b.held || b.sleeping ? 0 : 1 / b.mass;
|
|
1840
|
+
if (ima + imb === 0) return;
|
|
1841
|
+
const ea = [...this.m.makeRotationFromQuaternion(a.root.quaternion).elements];
|
|
1842
|
+
const eb = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
|
|
1843
|
+
const flat = Math.hypot(n[0], n[2]);
|
|
1844
|
+
const out = flat > KNOCK_FLAT ? [n[0] / flat, 0, n[2] / flat] : n;
|
|
1845
|
+
const depth = Math.max(0, hit.depth - SLOP) / (flat > KNOCK_FLAT ? flat : 1);
|
|
1846
|
+
for (const [body, share, sign] of [[a, ima, 1], [b, imb, -1]]) {
|
|
1847
|
+
if (share === 0) continue;
|
|
1848
|
+
const move = depth * share * sign / (ima + imb);
|
|
1849
|
+
body.root.position.x += out[0] * move;
|
|
1850
|
+
body.root.position.y += out[1] * move;
|
|
1851
|
+
body.root.position.z += out[2] * move;
|
|
1852
|
+
}
|
|
1853
|
+
if (closing >= 0) return;
|
|
1854
|
+
const turning = (body, e, r, d) => {
|
|
1855
|
+
const t2 = this.mulInvInertiaWith(body, e, r[1] * d[2] - r[2] * d[1], r[2] * d[0] - r[0] * d[2], r[0] * d[1] - r[1] * d[0]);
|
|
1856
|
+
return (t2[1] * r[2] - t2[2] * r[1]) * d[0] + (t2[2] * r[0] - t2[0] * r[2]) * d[1] + (t2[0] * r[1] - t2[1] * r[0]) * d[2];
|
|
1857
|
+
};
|
|
1858
|
+
const resist = (d) => ima + imb + (ima > 0 ? turning(a, ea, ra, d) : 0) + (imb > 0 ? turning(b, eb, rb, d) : 0);
|
|
1859
|
+
const give = (j2) => {
|
|
1860
|
+
if (ima > 0) {
|
|
1861
|
+
for (let i = 0; i < 3; i++) a.velocity[i] += j2[i] * ima;
|
|
1862
|
+
const dw = this.mulInvInertiaWith(a, ea, ra[1] * j2[2] - ra[2] * j2[1], ra[2] * j2[0] - ra[0] * j2[2], ra[0] * j2[1] - ra[1] * j2[0]);
|
|
1863
|
+
for (let i = 0; i < 3; i++) a.angularVelocity[i] += dw[i];
|
|
1864
|
+
}
|
|
1865
|
+
if (imb > 0) {
|
|
1866
|
+
for (let i = 0; i < 3; i++) b.velocity[i] -= j2[i] * imb;
|
|
1867
|
+
const dw = this.mulInvInertiaWith(b, eb, rb[1] * j2[2] - rb[2] * j2[1], rb[2] * j2[0] - rb[0] * j2[2], rb[0] * j2[1] - rb[1] * j2[0]);
|
|
1868
|
+
for (let i = 0; i < 3; i++) b.angularVelocity[i] -= dw[i];
|
|
1869
|
+
}
|
|
1870
|
+
};
|
|
1871
|
+
const bounce = -closing > BOUNCE_MIN_SPEED ? pairRestitution(a.restitution, b.restitution) : 0;
|
|
1872
|
+
const j = -(1 + bounce) * closing / resist(n);
|
|
1873
|
+
give([n[0] * j, n[1] * j, n[2] * j]);
|
|
1874
|
+
const v = relative();
|
|
1875
|
+
const vn = v[0] * n[0] + v[1] * n[1] + v[2] * n[2];
|
|
1876
|
+
const vt = [v[0] - vn * n[0], v[1] - vn * n[1], v[2] - vn * n[2]];
|
|
1877
|
+
const slide = Math.hypot(vt[0], vt[1], vt[2]);
|
|
1878
|
+
if (slide < 1e-6) return;
|
|
1879
|
+
const t = [vt[0] / slide, vt[1] / slide, vt[2] / slide];
|
|
1880
|
+
const jt = Math.min(slide / resist(t), pairFriction(a.friction, b.friction ?? null).dynamic * j);
|
|
1881
|
+
give([-t[0] * jt, -t[1] * jt, -t[2] * jt]);
|
|
1882
|
+
if (-closing >= SLAM_MIN_SPEED && this.time - Math.max(a.lastHit, b.lastHit) > 0.15) {
|
|
1883
|
+
a.lastHit = b.lastHit = this.time;
|
|
1884
|
+
this.o.onKnock?.(a, b, hit.point, -closing);
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
/** Whether a part of `a` and a part of `b` (their boxes of the step, as world-aligned boxes around them) overlap. */
|
|
1888
|
+
partsOverlap(a, b) {
|
|
1889
|
+
const extent = (s, k) => Math.abs(s.e[k]) * s.half[0] + Math.abs(s.e[k + 4]) * s.half[1] + Math.abs(s.e[k + 8]) * s.half[2];
|
|
1890
|
+
for (const pa of this.surfacesOf.get(a) ?? []) {
|
|
1891
|
+
for (const pb of this.surfacesOf.get(b) ?? []) {
|
|
1892
|
+
let apart = false;
|
|
1893
|
+
for (let k = 0; k < 3 && !apart; k++) apart = Math.abs(pa.c[k] - pb.c[k]) > extent(pa, k) + extent(pb, k);
|
|
1894
|
+
if (!apart) return true;
|
|
1462
1895
|
}
|
|
1463
1896
|
}
|
|
1897
|
+
return false;
|
|
1898
|
+
}
|
|
1899
|
+
/** Two boxes overlapping side by side (horizontal bounding circles): pushed apart, gently, so they do not interpenetrate forever. */
|
|
1900
|
+
separatePair(a, b, ca, cb, dt) {
|
|
1901
|
+
const fixedA = a.held || a.sleeping;
|
|
1902
|
+
const fixedB = b.held || b.sleeping;
|
|
1903
|
+
const maxMove = 1.5 * dt;
|
|
1904
|
+
const rh = (Math.max(a.halfExtents[0], a.halfExtents[2]) + Math.max(b.halfExtents[0], b.halfExtents[2])) * 0.8;
|
|
1905
|
+
let dx = cb[0] - ca[0];
|
|
1906
|
+
let dz = cb[2] - ca[2];
|
|
1907
|
+
let d = Math.hypot(dx, dz);
|
|
1908
|
+
if (d >= rh) return;
|
|
1909
|
+
if (d < 1e-5) {
|
|
1910
|
+
dx = 1;
|
|
1911
|
+
dz = 0;
|
|
1912
|
+
d = 1;
|
|
1913
|
+
}
|
|
1914
|
+
const push = Math.min(maxMove, (rh - d) * 0.5);
|
|
1915
|
+
const wa = fixedA ? 0 : fixedB ? 1 : b.mass / (a.mass + b.mass);
|
|
1916
|
+
const wb = fixedB ? 0 : fixedA ? 1 : a.mass / (a.mass + b.mass);
|
|
1917
|
+
const ux = dx / (d || 1);
|
|
1918
|
+
const uz = dz / (d || 1);
|
|
1919
|
+
if (!fixedA) {
|
|
1920
|
+
a.root.position.x -= ux * push * 2 * wa;
|
|
1921
|
+
a.root.position.z -= uz * push * 2 * wa;
|
|
1922
|
+
}
|
|
1923
|
+
if (!fixedB) {
|
|
1924
|
+
b.root.position.x += ux * push * 2 * wb;
|
|
1925
|
+
b.root.position.z += uz * push * 2 * wb;
|
|
1926
|
+
}
|
|
1464
1927
|
}
|
|
1465
1928
|
};
|
|
1466
1929
|
|
|
@@ -1762,6 +2225,23 @@ function specFromObject3D(THREE, root, options = {}) {
|
|
|
1762
2225
|
return { spec, parts: meshesOf };
|
|
1763
2226
|
}
|
|
1764
2227
|
|
|
2228
|
+
// src/friction.ts
|
|
2229
|
+
var SURFACE_BY_KEY = [
|
|
2230
|
+
[/rubber|tire|tyre/, { static: 1, dynamic: 0.8, rolling: 0.03, restitution: 0.8 }],
|
|
2231
|
+
[/foam|sponge|fabric|cloth|leather|textile|carpet/, { static: 0.6, dynamic: 0.5, rolling: 0.08, restitution: 0.2 }],
|
|
2232
|
+
[/concrete|brick|stone|masonry|cement|asphalt|plaster/, { static: 0.7, dynamic: 0.6, rolling: 0.012, restitution: 0.4 }],
|
|
2233
|
+
[/steel|iron|metal|alumin|copper|brass|tin/, { static: 0.6, dynamic: 0.4, rolling: 2e-3, restitution: 0.6 }],
|
|
2234
|
+
[/glass/, { static: 0.4, dynamic: 0.35, rolling: 2e-3, restitution: 0.7 }],
|
|
2235
|
+
[/ceramic|porcelain|tile/, { static: 0.5, dynamic: 0.4, rolling: 5e-3, restitution: 0.5 }],
|
|
2236
|
+
[/wood|mdf|plywood|board|cork/, { static: 0.4, dynamic: 0.3, rolling: 0.01, restitution: 0.5 }],
|
|
2237
|
+
[/plastic|polycarbonate|acrylic|nylon|pvc/, { static: 0.4, dynamic: 0.3, rolling: 0.01, restitution: 0.5 }],
|
|
2238
|
+
[/paper|cardboard|carton/, { static: 0.4, dynamic: 0.3, rolling: 0.02, restitution: 0.2 }]
|
|
2239
|
+
];
|
|
2240
|
+
function surfaceOf(materialId, def) {
|
|
2241
|
+
const keys = [materialId, ...def?.tags ?? []].join(" ").toLowerCase();
|
|
2242
|
+
return SURFACE_BY_KEY.find(([key]) => key.test(keys))?.[1];
|
|
2243
|
+
}
|
|
2244
|
+
|
|
1765
2245
|
// src/renderer.ts
|
|
1766
2246
|
import { kitOriginOf } from "@adatechnology/game-contracts";
|
|
1767
2247
|
|
|
@@ -2870,6 +3350,63 @@ var FEET_BAND = 0.03;
|
|
|
2870
3350
|
var TIP_MIN_HEIGHT = 0.3;
|
|
2871
3351
|
var TIP_NARROW = 0.3;
|
|
2872
3352
|
var CONTACT_MESHES = 12;
|
|
3353
|
+
var PART_BOX_MESHES = 64;
|
|
3354
|
+
var ROUND_SAMPLES = 400;
|
|
3355
|
+
var ROUND_SIDES = 0.12;
|
|
3356
|
+
var ROUND_SLACK = 1.12;
|
|
3357
|
+
var RIM_POINTS = 12;
|
|
3358
|
+
var rimPoints = (round, half) => {
|
|
3359
|
+
const out = [];
|
|
3360
|
+
const circle = (axis, at, radius) => {
|
|
3361
|
+
const [i, j] = axis === 0 ? [1, 2] : axis === 1 ? [0, 2] : [0, 1];
|
|
3362
|
+
for (let k = 0; k < RIM_POINTS; k++) {
|
|
3363
|
+
const a = k / RIM_POINTS * Math.PI * 2;
|
|
3364
|
+
const p = [0, 0, 0];
|
|
3365
|
+
p[axis] = at;
|
|
3366
|
+
p[i] = Math.cos(a) * radius;
|
|
3367
|
+
p[j] = Math.sin(a) * radius;
|
|
3368
|
+
out.push(p);
|
|
3369
|
+
}
|
|
3370
|
+
};
|
|
3371
|
+
if (round.kind === "sphere") for (const axis of [0, 1, 2]) circle(axis, 0, round.radius);
|
|
3372
|
+
else {
|
|
3373
|
+
const ends = round.profile ?? [];
|
|
3374
|
+
circle(round.axis, -half[round.axis], ends[0]?.[1] ?? round.radius);
|
|
3375
|
+
circle(round.axis, half[round.axis], ends[ends.length - 1]?.[1] ?? round.radius);
|
|
3376
|
+
}
|
|
3377
|
+
return out;
|
|
3378
|
+
};
|
|
3379
|
+
var PROFILE_RINGS = 8;
|
|
3380
|
+
var SHELL_CYLINDER = 1;
|
|
3381
|
+
var SOLID_CYLINDER = 0.5;
|
|
3382
|
+
var SHELL_SPHERE = 2 / 3;
|
|
3383
|
+
var SOLID_SPHERE = 0.4;
|
|
3384
|
+
var LIQUID_KG_PER_L = 1;
|
|
3385
|
+
var profileOf = (points, axis, half, radius) => {
|
|
3386
|
+
const [i, j] = axis === 0 ? [1, 2] : axis === 1 ? [0, 2] : [0, 1];
|
|
3387
|
+
const widest = new Array(PROFILE_RINGS).fill(0);
|
|
3388
|
+
for (const p of points) {
|
|
3389
|
+
const ring = Math.min(PROFILE_RINGS - 1, Math.max(0, Math.floor((p[axis] + half) / (2 * half) * PROFILE_RINGS)));
|
|
3390
|
+
widest[ring] = Math.max(widest[ring], Math.hypot(p[i], p[j]));
|
|
3391
|
+
}
|
|
3392
|
+
const rings = [];
|
|
3393
|
+
for (let k = 0; k < PROFILE_RINGS; k++) {
|
|
3394
|
+
if (widest[k] <= 0) continue;
|
|
3395
|
+
const at = k === 0 ? -half : k === PROFILE_RINGS - 1 ? half : -half + (k + 0.5) / PROFILE_RINGS * 2 * half;
|
|
3396
|
+
rings.push([at, Math.min(radius, widest[k])]);
|
|
3397
|
+
}
|
|
3398
|
+
const hull = [];
|
|
3399
|
+
for (const ring of rings) {
|
|
3400
|
+
while (hull.length >= 2) {
|
|
3401
|
+
const [a0, r0] = hull[hull.length - 2];
|
|
3402
|
+
const [a1, r1] = hull[hull.length - 1];
|
|
3403
|
+
if ((r1 - r0) * (ring[0] - a0) <= (ring[1] - r0) * (a1 - a0)) hull.pop();
|
|
3404
|
+
else break;
|
|
3405
|
+
}
|
|
3406
|
+
hull.push(ring);
|
|
3407
|
+
}
|
|
3408
|
+
return hull;
|
|
3409
|
+
};
|
|
2873
3410
|
var BROKEN_GAP = 0.03;
|
|
2874
3411
|
var BROKEN_SAMPLES = [[0.5, 0.5], [0.1, 0.1], [0.9, 0.1], [0.1, 0.9], [0.9, 0.9]];
|
|
2875
3412
|
var UNCHECKED_KINDS = /* @__PURE__ */ new Set(["structure", "room", "ground"]);
|
|
@@ -3070,7 +3607,12 @@ var Renderer = class {
|
|
|
3070
3607
|
scene: o.scene,
|
|
3071
3608
|
onHardLanding: (p, point, speed) => this.hardLanding(p, point, speed),
|
|
3072
3609
|
sweep: (p, from, to) => this.sweepPiece(p, from, to),
|
|
3073
|
-
onSlam: (p, hit, speed) => this.slam(p, hit, speed)
|
|
3610
|
+
onSlam: (p, hit, speed) => this.slam(p, hit, speed),
|
|
3611
|
+
// two pieces knocked together: each sounds as its material does (a can on a can, a bottle on a table's leg)
|
|
3612
|
+
onKnock: (a, b, point, speed) => {
|
|
3613
|
+
this.landingSound(a.objectId, point, clamp(speed / 10, 0.2, 1));
|
|
3614
|
+
this.landingSound(b.objectId, point, clamp(speed / 10, 0.2, 1));
|
|
3615
|
+
}
|
|
3074
3616
|
});
|
|
3075
3617
|
this.unsubscribe = this.world.on("*", (ev) => {
|
|
3076
3618
|
if (ev.type === "explode") this.coverWhenWentOff.set(ev, this.coverAround(ev));
|
|
@@ -3249,8 +3791,129 @@ var Renderer = class {
|
|
|
3249
3791
|
const { half, center } = this.measure(b.root, this.meshesOf(b));
|
|
3250
3792
|
const piece = this.pieceSys.create(objectId, b.root, half, center, options.mass ?? this.massOf(objectId), options.velocity);
|
|
3251
3793
|
b.piece = piece;
|
|
3794
|
+
this.fitSurface(b, piece, options.inertia);
|
|
3795
|
+
this.fitPartBoxes(b, piece);
|
|
3252
3796
|
return piece;
|
|
3253
3797
|
}
|
|
3798
|
+
/**
|
|
3799
|
+
* What lands on or runs into a piece of several meshes meets its parts as drawn (`partBoxes`: a bench's slats, a table's top and legs), from the moment it is made a piece, not
|
|
3800
|
+
* the box around them all: a board laid on a bench rests on its seat, a can rolls under a table between its legs. Its own contacts with the ground are left as they were.
|
|
3801
|
+
*/
|
|
3802
|
+
fitPartBoxes(b, body) {
|
|
3803
|
+
if (body.round) return;
|
|
3804
|
+
const contacts = body.contacts;
|
|
3805
|
+
this.setContacts(b, body, this.meshesOf(b).filter((mv) => this.visuals.shown(mv)));
|
|
3806
|
+
body.contacts = contacts;
|
|
3807
|
+
}
|
|
3808
|
+
/** What the piece's surface is (the friction of the material it stands on) and whether it is round (`round`: it rolls on rings of contacts). */
|
|
3809
|
+
fitSurface(b, body, inertia) {
|
|
3810
|
+
const box = new this.THREE.Box3();
|
|
3811
|
+
let low = Infinity;
|
|
3812
|
+
let material = null;
|
|
3813
|
+
for (const [partId, pv] of b.parts) {
|
|
3814
|
+
if (pv.gone !== "none") continue;
|
|
3815
|
+
for (const mv of pv.meshes) {
|
|
3816
|
+
if (!this.visuals.shown(mv)) continue;
|
|
3817
|
+
this.visuals.worldBox(mv, box);
|
|
3818
|
+
if (box.isEmpty() || box.min.y >= low - 5e-3) continue;
|
|
3819
|
+
low = box.min.y;
|
|
3820
|
+
material = this.world.part(b.id, partId)?.material ?? pv.material ?? null;
|
|
3821
|
+
}
|
|
3822
|
+
}
|
|
3823
|
+
const surface = material ? surfaceOf(material, this.world.materials.get(material)) : void 0;
|
|
3824
|
+
body.friction = surface && { static: surface.static, dynamic: surface.dynamic, rolling: surface.rolling };
|
|
3825
|
+
body.restitution = surface?.restitution;
|
|
3826
|
+
const wasRound = body.round !== void 0;
|
|
3827
|
+
if (inertia !== void 0) body.inertiaGiven = inertia;
|
|
3828
|
+
const round = this.roundOf(b, body);
|
|
3829
|
+
body.round = round ?? void 0;
|
|
3830
|
+
if (round) {
|
|
3831
|
+
body.contacts = rimPoints(round, body.halfExtents);
|
|
3832
|
+
body.partBoxes = void 0;
|
|
3833
|
+
} else if (wasRound) body.contacts = void 0;
|
|
3834
|
+
}
|
|
3835
|
+
/**
|
|
3836
|
+
* k of a round piece's moment of inertia about its axis (I = k m r^2), from where its mass is. A piece that is not hollow is a solid (k 1/2 for a cylinder, 2/5 for a ball).
|
|
3837
|
+
* A hollow one (`userData.hollow`) with nothing inside (no `destructionContains` liquid, no `destructionPowder`) is all wall, a thin shell (k 1, 2/3): an empty can. One with
|
|
3838
|
+
* contents weighs its walls as their area times their thickness (`destructionThickness`) times density, or, without a thickness, as what is left of its mass besides the liquid
|
|
3839
|
+
* (`LIQUID_KG_PER_L`), and the rest as a solid: a full 350 ml can comes to about 0.53 and rolls down a slope at g sin t / 1.53, an empty one at g sin t / 2.
|
|
3840
|
+
*/
|
|
3841
|
+
inertiaOf(b, body, kind, axis, radius) {
|
|
3842
|
+
const shell = kind === "sphere" ? SHELL_SPHERE : SHELL_CYLINDER;
|
|
3843
|
+
const solid = kind === "sphere" ? SOLID_SPHERE : SOLID_CYLINDER;
|
|
3844
|
+
let hollow = false;
|
|
3845
|
+
let filled = false;
|
|
3846
|
+
let liters = 0;
|
|
3847
|
+
let thickness = 0;
|
|
3848
|
+
let material = null;
|
|
3849
|
+
for (const [partId, pv] of b.parts) {
|
|
3850
|
+
if (pv.gone !== "none") continue;
|
|
3851
|
+
for (const mv of pv.meshes) {
|
|
3852
|
+
const ud = mv.mesh.userData;
|
|
3853
|
+
if (ud.hollow !== true) continue;
|
|
3854
|
+
hollow = true;
|
|
3855
|
+
if (ud.destructionPowder) filled = true;
|
|
3856
|
+
if (ud.destructionContains) {
|
|
3857
|
+
filled = true;
|
|
3858
|
+
const left = this.world.part(b.id, partId)?.liters;
|
|
3859
|
+
liters += left ?? (typeof ud.destructionContains.liters === "number" ? ud.destructionContains.liters : 0);
|
|
3860
|
+
}
|
|
3861
|
+
const t = typeof ud.destructionThickness === "number" ? ud.destructionThickness : 0;
|
|
3862
|
+
if (t > thickness) {
|
|
3863
|
+
thickness = t;
|
|
3864
|
+
material = this.world.part(b.id, partId)?.material ?? pv.material;
|
|
3865
|
+
}
|
|
3866
|
+
}
|
|
3867
|
+
}
|
|
3868
|
+
if (!hollow) return solid;
|
|
3869
|
+
if (!filled) return shell;
|
|
3870
|
+
const density = material ? this.world.materials.get(material)?.density : void 0;
|
|
3871
|
+
const area = kind === "sphere" ? 4 * Math.PI * radius * radius : 2 * Math.PI * radius * 2 * body.halfExtents[axis] + 2 * Math.PI * radius * radius;
|
|
3872
|
+
const walls = density && thickness > 0 ? area * thickness * density : body.mass - liters * LIQUID_KG_PER_L;
|
|
3873
|
+
const share = clamp(walls / body.mass, 0, 1);
|
|
3874
|
+
return share * shell + (1 - share) * solid;
|
|
3875
|
+
}
|
|
3876
|
+
/**
|
|
3877
|
+
* Whether what is drawn of the piece is round about one of its axes (a can, a bottle, a plate) or all of them (a ball): its points, in its frame from the centre of
|
|
3878
|
+
* mass, all lie within the circle the box's two other sides draw (a box's corners would stick out of it by sqrt 2). Null for anything else.
|
|
3879
|
+
*/
|
|
3880
|
+
roundOf(b, body) {
|
|
3881
|
+
const half = body.halfExtents;
|
|
3882
|
+
const inv = specFrameInverse(this.THREE, b.root);
|
|
3883
|
+
const m4 = new this.THREE.Matrix4();
|
|
3884
|
+
const p = new this.THREE.Vector3();
|
|
3885
|
+
const [cx, cy, cz] = body.localCenter;
|
|
3886
|
+
const points = [];
|
|
3887
|
+
for (const mv of this.meshesOf(b)) {
|
|
3888
|
+
if (!this.visuals.shown(mv) || mv.host) continue;
|
|
3889
|
+
const pos = mv.mesh.geometry.getAttribute("position");
|
|
3890
|
+
if (!pos) continue;
|
|
3891
|
+
mv.mesh.updateWorldMatrix(true, false);
|
|
3892
|
+
m4.multiplyMatrices(inv, mv.mesh.matrixWorld);
|
|
3893
|
+
const stride = Math.max(1, Math.floor(pos.count / ROUND_SAMPLES));
|
|
3894
|
+
for (let i = 0; i < pos.count; i += stride) {
|
|
3895
|
+
p.fromBufferAttribute(pos, i).applyMatrix4(m4);
|
|
3896
|
+
points.push([p.x - cx, p.y - cy, p.z - cz]);
|
|
3897
|
+
}
|
|
3898
|
+
}
|
|
3899
|
+
if (points.length < 12) return null;
|
|
3900
|
+
const fits = (axis2) => {
|
|
3901
|
+
const [i, j] = axis2 === 0 ? [1, 2] : axis2 === 1 ? [0, 2] : [0, 1];
|
|
3902
|
+
if (Math.abs(half[i] - half[j]) > ROUND_SIDES * Math.max(half[i], half[j])) return null;
|
|
3903
|
+
const radius2 = (half[i] + half[j]) / 2;
|
|
3904
|
+
for (const q of points) if (Math.hypot(q[i], q[j]) > radius2 * ROUND_SLACK) return null;
|
|
3905
|
+
return radius2;
|
|
3906
|
+
};
|
|
3907
|
+
const radii = [0, 1, 2].map(fits);
|
|
3908
|
+
if (radii.every((r) => r !== null)) {
|
|
3909
|
+
const radius2 = (half[0] + half[1] + half[2]) / 3;
|
|
3910
|
+
return { kind: "sphere", axis: 1, radius: radius2, inertia: body.inertiaGiven ?? this.inertiaOf(b, body, "sphere", 1, radius2) };
|
|
3911
|
+
}
|
|
3912
|
+
const axis = [0, 1, 2].find((k) => radii[k] !== null);
|
|
3913
|
+
if (axis === void 0) return null;
|
|
3914
|
+
const radius = radii[axis] ?? 0;
|
|
3915
|
+
return { kind: "cylinder", axis, radius, inertia: body.inertiaGiven ?? this.inertiaOf(b, body, "cylinder", axis, radius), profile: profileOf(points, axis, half[axis], radius) };
|
|
3916
|
+
}
|
|
3254
3917
|
pieces() {
|
|
3255
3918
|
return [...this.pieceSys.list];
|
|
3256
3919
|
}
|
|
@@ -3274,7 +3937,11 @@ var Renderer = class {
|
|
|
3274
3937
|
const box = { min: [from[0] - core, from[1] - core, from[2] - core], max: [from[0] + core, from[1] + core, from[2] + core] };
|
|
3275
3938
|
const hit = this.world.sweepBox(box, d, travel, { ignore: p.objectId, spacing: Math.max(0.05, core) });
|
|
3276
3939
|
if (!hit) return null;
|
|
3277
|
-
|
|
3940
|
+
const other = this.pieceSys.get(hit.object);
|
|
3941
|
+
if (other && (p.round || other.round)) return null;
|
|
3942
|
+
const material = this.world.part(hit.object, hit.part)?.material;
|
|
3943
|
+
const restitution = material === void 0 ? void 0 : surfaceOf(material, this.world.materials.get(material))?.restitution;
|
|
3944
|
+
return { object: hit.object, part: hit.part, distance: hit.distance, point: hit.point, normal: hit.normal, ...restitution === void 0 ? {} : { restitution } };
|
|
3278
3945
|
}
|
|
3279
3946
|
/** A piece thrown into a wall: the wall takes the blow of its weight where it met it, the piece takes it too, and each sounds as its material does. */
|
|
3280
3947
|
slam(p, hit, speed) {
|
|
@@ -4047,6 +4714,10 @@ var Renderer = class {
|
|
|
4047
4714
|
old.halfExtents = half;
|
|
4048
4715
|
this.pieceSys.setCenter(old, center);
|
|
4049
4716
|
old.mass = Math.max(0.05, this.massOf(objectId));
|
|
4717
|
+
if (old.contacts || old.round) {
|
|
4718
|
+
this.fitSurface(b, old);
|
|
4719
|
+
if (!old.round) this.setContacts(b, old, this.meshesOf(b).filter((mv) => this.visuals.shown(mv)));
|
|
4720
|
+
} else if (old.partBoxes) this.fitPartBoxes(b, old);
|
|
4050
4721
|
old.sleeping = false;
|
|
4051
4722
|
} else if (kitOriginOf(b.root)?.kind === "furniture") {
|
|
4052
4723
|
const piece = this.makePiece(objectId);
|
|
@@ -4100,6 +4771,7 @@ var Renderer = class {
|
|
|
4100
4771
|
}
|
|
4101
4772
|
const piece = this.pieceSys.create(newId, group, half, center, this.massOf(newId), vel, ang);
|
|
4102
4773
|
nb.piece = piece;
|
|
4774
|
+
this.fitSurface(nb, piece);
|
|
4103
4775
|
if (kitOriginOf(from.root)?.kind === "furniture") this.tipIfTopHeavy(nb);
|
|
4104
4776
|
this.o.onPiece?.(piece);
|
|
4105
4777
|
}
|
|
@@ -4120,7 +4792,10 @@ var Renderer = class {
|
|
|
4120
4792
|
body.halfExtents = half;
|
|
4121
4793
|
this.pieceSys.setCenter(body, center);
|
|
4122
4794
|
body.mass = Math.max(0.05, this.massOf(objectId));
|
|
4123
|
-
if (!structural)
|
|
4795
|
+
if (!structural) {
|
|
4796
|
+
this.fitSurface(b, body);
|
|
4797
|
+
if (!body.round) this.setContacts(b, body, left);
|
|
4798
|
+
}
|
|
4124
4799
|
body.sleeping = false;
|
|
4125
4800
|
return;
|
|
4126
4801
|
}
|
|
@@ -4232,11 +4907,13 @@ var Renderer = class {
|
|
|
4232
4907
|
}
|
|
4233
4908
|
/**
|
|
4234
4909
|
* The piece touches the ground by the corners of each part left of it, not by the box around them all: on one leg, or on the legs of one side, its centre of mass is off
|
|
4235
|
-
* what holds it and it goes over until the top meets the floor. Past `CONTACT_MESHES` meshes it keeps the corners of its box
|
|
4910
|
+
* what holds it and it goes over until the top meets the floor. Past `CONTACT_MESHES` meshes it keeps the corners of its box; up to `PART_BOX_MESHES`, what lands on it lands
|
|
4911
|
+
* on its parts.
|
|
4236
4912
|
*/
|
|
4237
4913
|
setContacts(b, body, meshes) {
|
|
4238
|
-
if (meshes.length >
|
|
4914
|
+
if (meshes.length > PART_BOX_MESHES) {
|
|
4239
4915
|
body.contacts = void 0;
|
|
4916
|
+
body.partBoxes = void 0;
|
|
4240
4917
|
return;
|
|
4241
4918
|
}
|
|
4242
4919
|
const inv = specFrameInverse(this.THREE, b.root);
|
|
@@ -4244,14 +4921,20 @@ var Renderer = class {
|
|
|
4244
4921
|
const lb = new this.THREE.Box3();
|
|
4245
4922
|
const [cx, cy, cz] = body.localCenter;
|
|
4246
4923
|
const points = [];
|
|
4924
|
+
const boxes = [];
|
|
4247
4925
|
for (const mv of meshes) {
|
|
4248
4926
|
mv.mesh.updateWorldMatrix(true, false);
|
|
4249
4927
|
this.visuals.localBox(mv, lb);
|
|
4250
4928
|
if (lb.isEmpty()) continue;
|
|
4251
4929
|
lb.applyMatrix4(m4.multiplyMatrices(inv, mv.mesh.matrixWorld));
|
|
4252
4930
|
for (let i = 0; i < 8; i++) points.push([(i & 1 ? lb.max.x : lb.min.x) - cx, (i & 2 ? lb.max.y : lb.min.y) - cy, (i & 4 ? lb.max.z : lb.min.z) - cz]);
|
|
4931
|
+
boxes.push({
|
|
4932
|
+
center: [(lb.min.x + lb.max.x) / 2 - cx, (lb.min.y + lb.max.y) / 2 - cy, (lb.min.z + lb.max.z) / 2 - cz],
|
|
4933
|
+
half: [Math.max(5e-3, (lb.max.x - lb.min.x) / 2), Math.max(5e-3, (lb.max.y - lb.min.y) / 2), Math.max(5e-3, (lb.max.z - lb.min.z) / 2)]
|
|
4934
|
+
});
|
|
4253
4935
|
}
|
|
4254
|
-
body.contacts = points.length > 0 ? points : void 0;
|
|
4936
|
+
body.contacts = points.length > 0 && meshes.length <= CONTACT_MESHES ? points : void 0;
|
|
4937
|
+
body.partBoxes = boxes.length > 1 ? boxes : void 0;
|
|
4255
4938
|
}
|
|
4256
4939
|
/**
|
|
4257
4940
|
* A piece of furniture standing on feet too close for its height (a board on two legs in a line, a lone leg) does not stay up: the box of a piece hides that its weight is up
|