@adatechnology/game-interactables 0.5.2 → 0.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 +7 -0
- package/dist/index.d.ts +105 -1
- package/dist/index.global.js +444 -13
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +444 -13
- package/dist/index.js.map +1 -1
- package/package.json +5 -3
package/dist/index.js
CHANGED
|
@@ -208,25 +208,25 @@ var DEFAULT_AXIS = [0, 1, 0];
|
|
|
208
208
|
function poseOf(motion, t) {
|
|
209
209
|
const tt = Math.max(0, Math.min(1, t));
|
|
210
210
|
const axis = motion.axis ?? DEFAULT_AXIS;
|
|
211
|
-
const
|
|
211
|
+
const pose3 = { position: [0, 0, 0], rotation: { axis: [axis[0], axis[1], axis[2]], angle: 0 }, scale: [1, 1, 1] };
|
|
212
212
|
switch (motion.kind) {
|
|
213
213
|
case "hinge":
|
|
214
214
|
case "rocker":
|
|
215
215
|
case "knob":
|
|
216
|
-
|
|
216
|
+
pose3.rotation.angle = (motion.angle ?? Math.PI / 2) * tt;
|
|
217
217
|
break;
|
|
218
218
|
case "slide": {
|
|
219
219
|
const d = (motion.distance ?? 0.4) * tt;
|
|
220
|
-
|
|
220
|
+
pose3.position = [axis[0] * d, axis[1] * d, axis[2] * d];
|
|
221
221
|
break;
|
|
222
222
|
}
|
|
223
223
|
case "scale": {
|
|
224
224
|
const k = 1 + ((motion.scale ?? 0.2) - 1) * tt;
|
|
225
|
-
|
|
225
|
+
pose3.scale = [axis[0] !== 0 ? k : 1, axis[1] !== 0 ? k : 1, axis[2] !== 0 ? k : 1];
|
|
226
226
|
break;
|
|
227
227
|
}
|
|
228
228
|
}
|
|
229
|
-
return
|
|
229
|
+
return pose3;
|
|
230
230
|
}
|
|
231
231
|
function rotY(v, ry) {
|
|
232
232
|
const c = Math.cos(ry);
|
|
@@ -246,8 +246,8 @@ function rotateAxis(v, axis, angle) {
|
|
|
246
246
|
const c = Math.cos(angle);
|
|
247
247
|
const s = Math.sin(angle);
|
|
248
248
|
const dot = k[0] * v[0] + k[1] * v[1] + k[2] * v[2];
|
|
249
|
-
const
|
|
250
|
-
return [v[0] * c +
|
|
249
|
+
const cross3 = [k[1] * v[2] - k[2] * v[1], k[2] * v[0] - k[0] * v[2], k[0] * v[1] - k[1] * v[0]];
|
|
250
|
+
return [v[0] * c + cross3[0] * s + k[0] * dot * (1 - c), v[1] * c + cross3[1] * s + k[1] * dot * (1 - c), v[2] * c + cross3[2] * s + k[2] * dot * (1 - c)];
|
|
251
251
|
}
|
|
252
252
|
function clamp(v, lo, hi) {
|
|
253
253
|
return v < lo ? lo : v > hi ? hi : v;
|
|
@@ -346,7 +346,11 @@ function createWorld(options = {}) {
|
|
|
346
346
|
const next = clamp(target2, 0, p.maxT);
|
|
347
347
|
const changed = next !== p.target;
|
|
348
348
|
p.target = next;
|
|
349
|
-
if (instant)
|
|
349
|
+
if (instant) {
|
|
350
|
+
p.driver?.snap(next);
|
|
351
|
+
p.commanded = next;
|
|
352
|
+
writeT(o, p, next);
|
|
353
|
+
}
|
|
350
354
|
if (changed || instant) emit({ type: "part", object: o.id, part: p.spec.id, t: p.t, target: p.target });
|
|
351
355
|
}
|
|
352
356
|
function setPartTarget(o, partId, target2, instant = false) {
|
|
@@ -927,6 +931,11 @@ function createWorld(options = {}) {
|
|
|
927
931
|
if (Math.hypot(dx, dz) > radius) continue;
|
|
928
932
|
const top = tr.y + s.spec.anchor[1];
|
|
929
933
|
if (pos[1] + ACTOR_HEIGHT < top - h || pos[1] > top) continue;
|
|
934
|
+
if (s.driver) {
|
|
935
|
+
s.driver.push(lv);
|
|
936
|
+
if (s.spec.kind === "panel" && s.spec.part && lv[2] < -0.05 && lp[2] > s.spec.anchor[2]) setPartTarget(o, s.spec.part, 1);
|
|
937
|
+
continue;
|
|
938
|
+
}
|
|
930
939
|
const cap = 8;
|
|
931
940
|
s.vax = clamp(s.vax - lv[2] * PUSH_GAIN, -cap, cap);
|
|
932
941
|
s.vaz = clamp(s.vaz + lv[0] * PUSH_GAIN, -cap, cap);
|
|
@@ -934,9 +943,10 @@ function createWorld(options = {}) {
|
|
|
934
943
|
}
|
|
935
944
|
}
|
|
936
945
|
}
|
|
946
|
+
const swingOut = [0, 0];
|
|
937
947
|
function stepSoft(dt) {
|
|
938
|
-
const
|
|
939
|
-
const h = dt /
|
|
948
|
+
const sub2 = Math.max(1, Math.ceil(dt / (1 / 120)));
|
|
949
|
+
const h = dt / sub2;
|
|
940
950
|
for (const o of objs.values()) {
|
|
941
951
|
if (!o.soft.size) continue;
|
|
942
952
|
const wl = rotY([wind[0], 0, wind[1]], -o.public.transform.ry);
|
|
@@ -944,11 +954,17 @@ function createWorld(options = {}) {
|
|
|
944
954
|
const k = s.spec.stiffness ?? 30;
|
|
945
955
|
const c = s.spec.damping ?? 4;
|
|
946
956
|
const max = s.spec.maxAngle ?? 0.7;
|
|
957
|
+
if (s.driver) {
|
|
958
|
+
s.driver.swing(swingOut);
|
|
959
|
+
s.ax = swingOut[0];
|
|
960
|
+
s.az = swingOut[1];
|
|
961
|
+
continue;
|
|
962
|
+
}
|
|
947
963
|
const gain = s.spec.wind ?? (s.spec.kind === "panel" ? 0 : 1);
|
|
948
964
|
const gust = gain * (0.6 + 0.4 * Math.sin(time * 1.7 + s.phase)) * 0.1;
|
|
949
965
|
const fx = -wl[2] * gust;
|
|
950
966
|
const fz = wl[0] * gust;
|
|
951
|
-
for (let i = 0; i <
|
|
967
|
+
for (let i = 0; i < sub2; i++) {
|
|
952
968
|
s.vax += (-k * s.ax - c * s.vax + fx) * h;
|
|
953
969
|
s.vaz += (-k * s.az - c * s.vaz + fz) * h;
|
|
954
970
|
s.ax += s.vax * h;
|
|
@@ -1459,11 +1475,37 @@ function createWorld(options = {}) {
|
|
|
1459
1475
|
if (opts.merge) return [...mergeColliders(fixed, { gap: opts.gap, slack: opts.slack }), ...out];
|
|
1460
1476
|
return [...fixed, ...out];
|
|
1461
1477
|
}
|
|
1478
|
+
function updateDriven(o, p, dt) {
|
|
1479
|
+
const driver = p.driver;
|
|
1480
|
+
if (p.broken) return;
|
|
1481
|
+
if (p.commanded !== p.target && !(gated(o, p) && p.target > p.t)) {
|
|
1482
|
+
driver.aim(p.target);
|
|
1483
|
+
p.commanded = p.target;
|
|
1484
|
+
p.moving = true;
|
|
1485
|
+
}
|
|
1486
|
+
driver.update(dt);
|
|
1487
|
+
if (driver.broken) {
|
|
1488
|
+
p.broken = true;
|
|
1489
|
+
emit({ type: "broke", object: o.id, part: p.spec.id });
|
|
1490
|
+
return;
|
|
1491
|
+
}
|
|
1492
|
+
const next = clamp(driver.read(), 0, p.maxT);
|
|
1493
|
+
if (next !== p.t) writeT(o, p, next);
|
|
1494
|
+
if (!driver.settled) return;
|
|
1495
|
+
p.target = next;
|
|
1496
|
+
p.commanded = next;
|
|
1497
|
+
if (p.moving) emit({ type: "part", object: o.id, part: p.spec.id, t: next, target: next });
|
|
1498
|
+
p.moving = false;
|
|
1499
|
+
}
|
|
1462
1500
|
function update(dt) {
|
|
1463
1501
|
if (!(dt > 0)) return;
|
|
1464
1502
|
time += dt;
|
|
1465
1503
|
for (const o of objs.values()) {
|
|
1466
1504
|
for (const p of o.parts.values()) {
|
|
1505
|
+
if (p.driver) {
|
|
1506
|
+
updateDriven(o, p, dt);
|
|
1507
|
+
continue;
|
|
1508
|
+
}
|
|
1467
1509
|
if (p.t === p.target) continue;
|
|
1468
1510
|
if (gated(o, p) && p.target > p.t) continue;
|
|
1469
1511
|
const step = (p.spec.motion.speed ?? 2.5) * dt;
|
|
@@ -1540,7 +1582,9 @@ function createWorld(options = {}) {
|
|
|
1540
1582
|
};
|
|
1541
1583
|
for (const p of spec.parts ?? []) {
|
|
1542
1584
|
const t0 = clamp(p.t0 ?? 0, 0, 1);
|
|
1543
|
-
|
|
1585
|
+
const driver = options.partPhysics?.attachPart(id, p, pub.transform);
|
|
1586
|
+
o.parts.set(p.id, { spec: p, t: t0, target: t0, maxT: 1, ...driver ? { driver, commanded: t0 } : {} });
|
|
1587
|
+
driver?.snap(t0);
|
|
1544
1588
|
pub.parts[p.id] = t0;
|
|
1545
1589
|
}
|
|
1546
1590
|
for (const c of spec.containers ?? []) {
|
|
@@ -1566,7 +1610,8 @@ function createWorld(options = {}) {
|
|
|
1566
1610
|
for (const f of spec.fixtures ?? []) o.fixtures.set(f.id, { spec: f, on: false, flowing: false, frac: 0, timer: 0 });
|
|
1567
1611
|
let n = 0;
|
|
1568
1612
|
for (const s of spec.soft ?? []) {
|
|
1569
|
-
|
|
1613
|
+
const driver = options.partPhysics?.attachSoft?.(id, s, pub.transform);
|
|
1614
|
+
o.soft.set(s.id, { spec: s, ax: 0, az: 0, vax: 0, vaz: 0, phase: n++ * 2.399963 % (Math.PI * 2), ...driver ? { driver } : {} });
|
|
1570
1615
|
}
|
|
1571
1616
|
for (const d of spec.damage ?? []) o.hp.set(d.target, { max: d.hp, hp: d.hp });
|
|
1572
1617
|
objs.set(id, o);
|
|
@@ -1578,6 +1623,8 @@ function createWorld(options = {}) {
|
|
|
1578
1623
|
const o = objs.get(id);
|
|
1579
1624
|
if (!o) return;
|
|
1580
1625
|
for (const [actor, s] of [...seats]) if (s.object === id) seats.delete(actor);
|
|
1626
|
+
for (const p of o.parts.values()) p.driver?.dispose();
|
|
1627
|
+
for (const s of o.soft.values()) s.driver?.dispose();
|
|
1581
1628
|
objs.delete(id);
|
|
1582
1629
|
if (defaultTank && defaultTank.startsWith(id + ":")) defaultTank = null;
|
|
1583
1630
|
rebuildPlugs();
|
|
@@ -1613,6 +1660,8 @@ function createWorld(options = {}) {
|
|
|
1613
1660
|
const p = o.parts.get(k);
|
|
1614
1661
|
if (p) {
|
|
1615
1662
|
p.target = clamp(v, 0, p.maxT);
|
|
1663
|
+
p.driver?.snap(p.target);
|
|
1664
|
+
p.commanded = p.target;
|
|
1616
1665
|
writeT(o, p, p.target);
|
|
1617
1666
|
}
|
|
1618
1667
|
}
|
|
@@ -1774,8 +1823,390 @@ function createWorld(options = {}) {
|
|
|
1774
1823
|
};
|
|
1775
1824
|
return world;
|
|
1776
1825
|
}
|
|
1826
|
+
|
|
1827
|
+
// src/engine-chain.ts
|
|
1828
|
+
var UP = [0, 1, 0];
|
|
1829
|
+
var sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
1830
|
+
var length = (v) => Math.hypot(v[0], v[1], v[2]);
|
|
1831
|
+
var cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
1832
|
+
function alignY(direction) {
|
|
1833
|
+
const axis = cross(UP, direction);
|
|
1834
|
+
const sine = length(axis);
|
|
1835
|
+
if (sine < 1e-9) return direction[1] >= 0 ? [0, 0, 0, 1] : [1, 0, 0, 0];
|
|
1836
|
+
const angle = Math.atan2(sine, direction[1]);
|
|
1837
|
+
const half = Math.sin(angle / 2) / sine;
|
|
1838
|
+
return [axis[0] * half, axis[1] * half, axis[2] * half, Math.cos(angle / 2)];
|
|
1839
|
+
}
|
|
1840
|
+
var perpendicular = (direction) => {
|
|
1841
|
+
const side = Math.abs(direction[0]) < 0.9 ? cross(direction, [1, 0, 0]) : cross(direction, [0, 0, 1]);
|
|
1842
|
+
const size = length(side);
|
|
1843
|
+
return [side[0] / size, side[1] / size, side[2] / size];
|
|
1844
|
+
};
|
|
1845
|
+
function buildChain(world, spec) {
|
|
1846
|
+
const count = spec.points.length - 1;
|
|
1847
|
+
const lengths = Array.from({ length: count }, (_, i) => length(sub(spec.points[i + 1], spec.points[i])));
|
|
1848
|
+
const total = lengths.reduce((sum, each) => sum + each, 0);
|
|
1849
|
+
const pairs = [];
|
|
1850
|
+
for (let a = 0; a < count; a++) for (let b = a + 1; b < count; b++) pairs.push([a, b]);
|
|
1851
|
+
const group = world.createGroup(count, pairs);
|
|
1852
|
+
const bodies = [];
|
|
1853
|
+
const joints = [];
|
|
1854
|
+
for (let i = 0; i < count; i++) {
|
|
1855
|
+
const from = spec.points[i];
|
|
1856
|
+
const to = spec.points[i + 1];
|
|
1857
|
+
const span = lengths[i];
|
|
1858
|
+
const direction = [(to[0] - from[0]) / span, (to[1] - from[1]) / span, (to[2] - from[2]) / span];
|
|
1859
|
+
const link = world.createBody({
|
|
1860
|
+
shape: { kind: "box", half: [spec.halfWidth, span / 2, spec.halfDepth] },
|
|
1861
|
+
motion: "dynamic",
|
|
1862
|
+
mass: spec.totalMass * (span / total),
|
|
1863
|
+
position: [(from[0] + to[0]) / 2, (from[1] + to[1]) / 2, (from[2] + to[2]) / 2],
|
|
1864
|
+
rotation: alignY(direction),
|
|
1865
|
+
layer: "debris",
|
|
1866
|
+
group: { id: group, sub: i },
|
|
1867
|
+
linearDamping: spec.damping,
|
|
1868
|
+
angularDamping: spec.damping,
|
|
1869
|
+
user: -4
|
|
1870
|
+
});
|
|
1871
|
+
const previous = i === 0 ? spec.startBody : bodies[i - 1];
|
|
1872
|
+
if (i === 0) world.ignorePair(spec.startBody, link, true);
|
|
1873
|
+
joints.push(world.createJoint(swingTwist(spec, previous, link, from, direction)));
|
|
1874
|
+
bodies.push(link);
|
|
1875
|
+
}
|
|
1876
|
+
if (spec.endBody !== void 0) {
|
|
1877
|
+
const last = spec.points[count];
|
|
1878
|
+
const direction = sub(last, spec.points[count - 1]);
|
|
1879
|
+
const size = length(direction);
|
|
1880
|
+
world.ignorePair(spec.endBody, bodies[count - 1], true);
|
|
1881
|
+
joints.push(world.createJoint(swingTwist(spec, bodies[count - 1], spec.endBody, last, [direction[0] / size, direction[1] / size, direction[2] / size])));
|
|
1882
|
+
}
|
|
1883
|
+
return { bodies, joints, halfLengths: lengths.map((each) => each / 2), group };
|
|
1884
|
+
}
|
|
1885
|
+
function disposeChain(world, chain) {
|
|
1886
|
+
for (const joint of chain.joints) world.removeJoint(joint);
|
|
1887
|
+
for (const body of chain.bodies) world.removeBody(body);
|
|
1888
|
+
world.releaseGroup(chain.group);
|
|
1889
|
+
}
|
|
1890
|
+
var swingTwist = (spec, a, b, point, direction) => ({
|
|
1891
|
+
kind: "swingTwist",
|
|
1892
|
+
a,
|
|
1893
|
+
b,
|
|
1894
|
+
point,
|
|
1895
|
+
twistAxis: direction,
|
|
1896
|
+
planeAxis: perpendicular(direction),
|
|
1897
|
+
normalHalfCone: spec.halfCone,
|
|
1898
|
+
planeHalfCone: spec.halfCone,
|
|
1899
|
+
twistMin: -spec.twist,
|
|
1900
|
+
twistMax: spec.twist,
|
|
1901
|
+
friction: spec.jointFriction
|
|
1902
|
+
});
|
|
1903
|
+
var pose = new Float64Array(7);
|
|
1904
|
+
var writeLinkEnd = (world, chain, index, sign, out, offset) => {
|
|
1905
|
+
world.readPose(chain.bodies[index], pose, 0);
|
|
1906
|
+
const x = pose[3];
|
|
1907
|
+
const y = pose[4];
|
|
1908
|
+
const z = pose[5];
|
|
1909
|
+
const w = pose[6];
|
|
1910
|
+
const half = chain.halfLengths[index] * sign;
|
|
1911
|
+
out[offset] = pose[0] + half * 2 * (x * y - w * z);
|
|
1912
|
+
out[offset + 1] = pose[1] + half * (1 - 2 * (x * x + z * z));
|
|
1913
|
+
out[offset + 2] = pose[2] + half * 2 * (y * z + w * x);
|
|
1914
|
+
};
|
|
1915
|
+
function chainPoints(world, chain, out) {
|
|
1916
|
+
const count = chain.bodies.length;
|
|
1917
|
+
writeLinkEnd(world, chain, 0, -1, out, 0);
|
|
1918
|
+
for (let i = 0; i < count; i++) writeLinkEnd(world, chain, i, 1, out, (i + 1) * 3);
|
|
1919
|
+
}
|
|
1920
|
+
function chainEnd(world, chain, out) {
|
|
1921
|
+
writeLinkEnd(world, chain, chain.bodies.length - 1, 1, out, 0);
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
// src/engine-soft.ts
|
|
1925
|
+
var STRIP_WIDTH = 0.12;
|
|
1926
|
+
var LINK_LENGTH = 0.15;
|
|
1927
|
+
var MAX_LINKS = 8;
|
|
1928
|
+
var FABRIC_KG_PER_M2 = 0.3;
|
|
1929
|
+
var FABRIC_HALF_DEPTH = 0.01;
|
|
1930
|
+
var TWIST_RANGE = 0.3;
|
|
1931
|
+
var DEFAULT_MAX_ANGLE = 0.7;
|
|
1932
|
+
var DEFAULT_DAMPING = 4;
|
|
1933
|
+
var JOINT_HOLD_NM = 2e-3;
|
|
1934
|
+
var PUSH_GAIN2 = 0.4;
|
|
1935
|
+
function attachStrips(world, soft, transform) {
|
|
1936
|
+
const [width, height, depth] = soft.size;
|
|
1937
|
+
const strips = Math.max(1, Math.round(width / STRIP_WIDTH));
|
|
1938
|
+
const links = Math.min(MAX_LINKS, Math.max(2, Math.round(height / LINK_LENGTH)));
|
|
1939
|
+
const rail = world.createBody({ shape: { kind: "box", half: [width / 2, 0.01, Math.max(depth, 0.02) / 2] }, motion: "static", position: localToWorld(transform, soft.anchor), layer: "static", user: -3 });
|
|
1940
|
+
const damping = soft.damping ?? DEFAULT_DAMPING;
|
|
1941
|
+
const chains = [];
|
|
1942
|
+
const tops = [];
|
|
1943
|
+
for (let i = 0; i < strips; i++) {
|
|
1944
|
+
const x = soft.anchor[0] - width / 2 + width * (i + 0.5) / strips;
|
|
1945
|
+
const top = localToWorld(transform, [x, soft.anchor[1], soft.anchor[2]]);
|
|
1946
|
+
tops.push(top);
|
|
1947
|
+
chains.push(
|
|
1948
|
+
buildChain(world, {
|
|
1949
|
+
points: Array.from({ length: links + 1 }, (_, k) => [top[0], top[1] - height * k / links, top[2]]),
|
|
1950
|
+
halfWidth: width / strips * 0.45,
|
|
1951
|
+
halfDepth: FABRIC_HALF_DEPTH,
|
|
1952
|
+
totalMass: width / strips * height * FABRIC_KG_PER_M2,
|
|
1953
|
+
startBody: rail,
|
|
1954
|
+
halfCone: soft.maxAngle ?? DEFAULT_MAX_ANGLE,
|
|
1955
|
+
twist: TWIST_RANGE,
|
|
1956
|
+
jointFriction: JOINT_HOLD_NM,
|
|
1957
|
+
damping: damping / 4
|
|
1958
|
+
})
|
|
1959
|
+
);
|
|
1960
|
+
}
|
|
1961
|
+
const tip = new Float64Array(3);
|
|
1962
|
+
const lastLinks = chains.map((chain) => chain.bodies[chain.bodies.length - 1]);
|
|
1963
|
+
const cosYaw = Math.cos(-transform.ry);
|
|
1964
|
+
const sinYaw = Math.sin(-transform.ry);
|
|
1965
|
+
let restedAx = 0;
|
|
1966
|
+
let restedAz = 0;
|
|
1967
|
+
let isRested = false;
|
|
1968
|
+
let disposed = false;
|
|
1969
|
+
const everyLinkAsleep = () => {
|
|
1970
|
+
for (let i = 0; i < lastLinks.length; i++) if (!world.isSleeping(lastLinks[i])) return false;
|
|
1971
|
+
return true;
|
|
1972
|
+
};
|
|
1973
|
+
return {
|
|
1974
|
+
swing(out) {
|
|
1975
|
+
const asleep = everyLinkAsleep();
|
|
1976
|
+
if (asleep && isRested) {
|
|
1977
|
+
out[0] = restedAx;
|
|
1978
|
+
out[1] = restedAz;
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
let dx = 0;
|
|
1982
|
+
let dz = 0;
|
|
1983
|
+
for (let i = 0; i < chains.length; i++) {
|
|
1984
|
+
chainEnd(world, chains[i], tip);
|
|
1985
|
+
const top = tops[i];
|
|
1986
|
+
const offsetX = tip[0] - top[0];
|
|
1987
|
+
const offsetZ = tip[2] - top[2];
|
|
1988
|
+
dx += offsetX * cosYaw + offsetZ * sinYaw;
|
|
1989
|
+
dz += -offsetX * sinYaw + offsetZ * cosYaw;
|
|
1990
|
+
}
|
|
1991
|
+
out[0] = -Math.asin(Math.max(-1, Math.min(1, dz / strips / height)));
|
|
1992
|
+
out[1] = Math.asin(Math.max(-1, Math.min(1, dx / strips / height)));
|
|
1993
|
+
restedAx = out[0];
|
|
1994
|
+
restedAz = out[1];
|
|
1995
|
+
isRested = asleep;
|
|
1996
|
+
},
|
|
1997
|
+
push(velocity) {
|
|
1998
|
+
const world3 = rotY(velocity, transform.ry);
|
|
1999
|
+
for (const chain of chains) {
|
|
2000
|
+
for (const body of chain.bodies) world.addImpulse(body, [world3[0] * PUSH_GAIN2 * linkMass(soft, strips, links), world3[1] * PUSH_GAIN2 * linkMass(soft, strips, links), world3[2] * PUSH_GAIN2 * linkMass(soft, strips, links)]);
|
|
2001
|
+
}
|
|
2002
|
+
},
|
|
2003
|
+
dispose() {
|
|
2004
|
+
if (disposed) return;
|
|
2005
|
+
disposed = true;
|
|
2006
|
+
for (const chain of chains) disposeChain(world, chain);
|
|
2007
|
+
world.removeBody(rail);
|
|
2008
|
+
}
|
|
2009
|
+
};
|
|
2010
|
+
}
|
|
2011
|
+
var linkMass = (soft, strips, links) => soft.size[0] / strips * (soft.size[1] / links) * FABRIC_KG_PER_M2;
|
|
2012
|
+
|
|
2013
|
+
// src/engine-leaf.ts
|
|
2014
|
+
var AIM_GAIN = 8;
|
|
2015
|
+
var ARRIVED_AT = 4e-3;
|
|
2016
|
+
var axisAngle = (axis, angle) => {
|
|
2017
|
+
const size = Math.hypot(axis[0], axis[1], axis[2]) || 1;
|
|
2018
|
+
const k = Math.sin(angle / 2) / size;
|
|
2019
|
+
return [axis[0] * k, axis[1] * k, axis[2] * k, Math.cos(angle / 2)];
|
|
2020
|
+
};
|
|
2021
|
+
var multiply = (a, b) => [
|
|
2022
|
+
a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
|
|
2023
|
+
a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
|
|
2024
|
+
a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
|
|
2025
|
+
a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2]
|
|
2026
|
+
];
|
|
2027
|
+
var pose2 = new Float64Array(7);
|
|
2028
|
+
var linearScratch = [0, 0, 0];
|
|
2029
|
+
var angularScratch = [0, 0, 0];
|
|
2030
|
+
var NO_SPIN = [0, 0, 0];
|
|
2031
|
+
function createLeafDriver(spec) {
|
|
2032
|
+
const { world, leaf, frame, joint, drive } = spec;
|
|
2033
|
+
const { axis, pivot, centre, range } = drive;
|
|
2034
|
+
let target = 0;
|
|
2035
|
+
let settled = true;
|
|
2036
|
+
let broken = false;
|
|
2037
|
+
let disposed = false;
|
|
2038
|
+
let lastT = 0;
|
|
2039
|
+
let restedT;
|
|
2040
|
+
const measure = () => {
|
|
2041
|
+
if (drive.isHinge) return world.jointAngles(joint).hinge / range;
|
|
2042
|
+
world.readPose(leaf, pose2, 0);
|
|
2043
|
+
return ((pose2[0] - centre[0]) * axis[0] + (pose2[1] - centre[1]) * axis[1] + (pose2[2] - centre[2]) * axis[2]) / range;
|
|
2044
|
+
};
|
|
2045
|
+
const read = () => {
|
|
2046
|
+
if (broken) return lastT;
|
|
2047
|
+
if (settled && world.isSleeping(leaf)) {
|
|
2048
|
+
restedT ?? (restedT = measure());
|
|
2049
|
+
lastT = restedT;
|
|
2050
|
+
return lastT;
|
|
2051
|
+
}
|
|
2052
|
+
restedT = void 0;
|
|
2053
|
+
lastT = measure();
|
|
2054
|
+
return lastT;
|
|
2055
|
+
};
|
|
2056
|
+
const stop = () => {
|
|
2057
|
+
world.setVelocity(leaf, NO_SPIN, NO_SPIN);
|
|
2058
|
+
settled = true;
|
|
2059
|
+
};
|
|
2060
|
+
const move = (rate) => {
|
|
2061
|
+
world.wake(leaf);
|
|
2062
|
+
const speed = rate * range;
|
|
2063
|
+
angularScratch[0] = axis[0] * speed;
|
|
2064
|
+
angularScratch[1] = axis[1] * speed;
|
|
2065
|
+
angularScratch[2] = axis[2] * speed;
|
|
2066
|
+
if (!drive.isHinge) return world.setVelocity(leaf, angularScratch, NO_SPIN);
|
|
2067
|
+
world.readPose(leaf, pose2, 0);
|
|
2068
|
+
const armX = pose2[0] - pivot[0];
|
|
2069
|
+
const armY = pose2[1] - pivot[1];
|
|
2070
|
+
const armZ = pose2[2] - pivot[2];
|
|
2071
|
+
linearScratch[0] = angularScratch[1] * armZ - angularScratch[2] * armY;
|
|
2072
|
+
linearScratch[1] = angularScratch[2] * armX - angularScratch[0] * armZ;
|
|
2073
|
+
linearScratch[2] = angularScratch[0] * armY - angularScratch[1] * armX;
|
|
2074
|
+
world.setVelocity(leaf, linearScratch, angularScratch);
|
|
2075
|
+
};
|
|
2076
|
+
const place = (t) => {
|
|
2077
|
+
const amount = t * drive.range;
|
|
2078
|
+
if (drive.isHinge) {
|
|
2079
|
+
const centre2 = rotateAxis([drive.centre[0] - drive.pivot[0], drive.centre[1] - drive.pivot[1], drive.centre[2] - drive.pivot[2]], drive.axis, amount);
|
|
2080
|
+
world.setPose(leaf, [drive.pivot[0] + centre2[0], drive.pivot[1] + centre2[1], drive.pivot[2] + centre2[2]], multiply(axisAngle(drive.axis, amount), drive.base), true);
|
|
2081
|
+
} else {
|
|
2082
|
+
world.setPose(leaf, [drive.centre[0] + drive.axis[0] * amount, drive.centre[1] + drive.axis[1] * amount, drive.centre[2] + drive.axis[2] * amount], drive.base, true);
|
|
2083
|
+
}
|
|
2084
|
+
world.setVelocity(leaf, [0, 0, 0], [0, 0, 0]);
|
|
2085
|
+
};
|
|
2086
|
+
return {
|
|
2087
|
+
leaf,
|
|
2088
|
+
aim(next) {
|
|
2089
|
+
if (broken) return;
|
|
2090
|
+
target = next;
|
|
2091
|
+
settled = false;
|
|
2092
|
+
restedT = void 0;
|
|
2093
|
+
},
|
|
2094
|
+
snap(t) {
|
|
2095
|
+
target = t;
|
|
2096
|
+
settled = true;
|
|
2097
|
+
restedT = void 0;
|
|
2098
|
+
if (!broken) place(t);
|
|
2099
|
+
},
|
|
2100
|
+
update() {
|
|
2101
|
+
if (broken) return;
|
|
2102
|
+
if (!world.jointExists(joint)) {
|
|
2103
|
+
broken = true;
|
|
2104
|
+
settled = true;
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
if (settled) return;
|
|
2108
|
+
const error = target - read();
|
|
2109
|
+
if (Math.abs(error) < ARRIVED_AT) return stop();
|
|
2110
|
+
move(clamp(error * AIM_GAIN, -drive.speed, drive.speed));
|
|
2111
|
+
},
|
|
2112
|
+
read,
|
|
2113
|
+
get settled() {
|
|
2114
|
+
return settled;
|
|
2115
|
+
},
|
|
2116
|
+
get broken() {
|
|
2117
|
+
return broken;
|
|
2118
|
+
},
|
|
2119
|
+
dispose() {
|
|
2120
|
+
if (disposed) return;
|
|
2121
|
+
disposed = true;
|
|
2122
|
+
spec.onDispose();
|
|
2123
|
+
if (!broken) world.removeJoint(joint);
|
|
2124
|
+
world.removeBody(leaf);
|
|
2125
|
+
if (spec.ownsFrame) world.removeBody(frame);
|
|
2126
|
+
}
|
|
2127
|
+
};
|
|
2128
|
+
}
|
|
2129
|
+
|
|
2130
|
+
// src/engine-parts.ts
|
|
2131
|
+
var HOLLOW_DOOR_DENSITY = 150;
|
|
2132
|
+
var MIN_LEAF_MASS = 1;
|
|
2133
|
+
var SLIDE_HOLD_N = 4;
|
|
2134
|
+
var HINGE_HOLD_NM = 0.4;
|
|
2135
|
+
var SOFT_KINDS = /* @__PURE__ */ new Set(["cloth", "curtain", "strip"]);
|
|
2136
|
+
var quatY = (ry) => [0, Math.sin(ry / 2), 0, Math.cos(ry / 2)];
|
|
2137
|
+
var cross2 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
2138
|
+
var perpendicularTo = (axis) => {
|
|
2139
|
+
const side = Math.abs(axis[0]) < 0.9 ? cross2(axis, [1, 0, 0]) : cross2(axis, [0, 0, 1]);
|
|
2140
|
+
const size = Math.hypot(side[0], side[1], side[2]);
|
|
2141
|
+
return [side[0] / size, side[1] / size, side[2] / size];
|
|
2142
|
+
};
|
|
2143
|
+
function createEnginePartPhysics(world, options = {}) {
|
|
2144
|
+
const disposers = /* @__PURE__ */ new Set();
|
|
2145
|
+
const leaves = /* @__PURE__ */ new Map();
|
|
2146
|
+
return {
|
|
2147
|
+
attachPart(object, part, transform) {
|
|
2148
|
+
const key = `${object}:${part.id}`;
|
|
2149
|
+
const retire = () => {
|
|
2150
|
+
leaves.delete(key);
|
|
2151
|
+
disposers.delete(dispose);
|
|
2152
|
+
};
|
|
2153
|
+
const dispose = () => driver?.dispose();
|
|
2154
|
+
const driver = attachLeaf(world, options, { object, part, transform, onDispose: retire });
|
|
2155
|
+
if (!driver) return void 0;
|
|
2156
|
+
leaves.set(key, driver.leaf);
|
|
2157
|
+
disposers.add(dispose);
|
|
2158
|
+
return driver;
|
|
2159
|
+
},
|
|
2160
|
+
leafOf: (object, part) => leaves.get(`${object}:${part}`),
|
|
2161
|
+
attachSoft(_object, soft, transform) {
|
|
2162
|
+
const driver = SOFT_KINDS.has(soft.kind) ? attachStrips(world, soft, transform) : void 0;
|
|
2163
|
+
if (!driver) return void 0;
|
|
2164
|
+
const dispose = () => driver.dispose();
|
|
2165
|
+
disposers.add(dispose);
|
|
2166
|
+
return { swing: driver.swing, push: driver.push, dispose: () => {
|
|
2167
|
+
disposers.delete(dispose);
|
|
2168
|
+
driver.dispose();
|
|
2169
|
+
} };
|
|
2170
|
+
},
|
|
2171
|
+
dispose() {
|
|
2172
|
+
for (const dispose of [...disposers]) dispose();
|
|
2173
|
+
disposers.clear();
|
|
2174
|
+
}
|
|
2175
|
+
};
|
|
2176
|
+
}
|
|
2177
|
+
function attachLeaf(world, options, request) {
|
|
2178
|
+
const { object, part, transform } = request;
|
|
2179
|
+
const motion = part.motion;
|
|
2180
|
+
const bounds = part.bounds;
|
|
2181
|
+
if (!bounds || motion.kind !== "hinge" && motion.kind !== "slide") return void 0;
|
|
2182
|
+
const isHinge = motion.kind === "hinge";
|
|
2183
|
+
const axisLocal = rotY(motion.axis ?? [0, 1, 0], part.ry ?? 0);
|
|
2184
|
+
const axis = rotY(axisLocal, transform.ry);
|
|
2185
|
+
const pivot = localToWorld(transform, part.pivot);
|
|
2186
|
+
const half = [(bounds[3] - bounds[0]) / 2, (bounds[4] - bounds[1]) / 2, (bounds[5] - bounds[2]) / 2];
|
|
2187
|
+
const centre = localToWorld(transform, [(bounds[0] + bounds[3]) / 2, (bounds[1] + bounds[4]) / 2, (bounds[2] + bounds[5]) / 2]);
|
|
2188
|
+
const rotation = quatY(transform.ry);
|
|
2189
|
+
const range = isHinge ? motion.angle ?? Math.PI / 2 : motion.distance ?? 0.4;
|
|
2190
|
+
const speed = motion.speed ?? 2.5;
|
|
2191
|
+
const frame = options.frameOf?.(object) ?? world.createBody({ shape: { kind: "box", half: [0.01, 0.01, 0.01] }, motion: "static", position: pivot, layer: "static", user: -3 });
|
|
2192
|
+
const ownsFrame = options.frameOf?.(object) === void 0;
|
|
2193
|
+
const mass = options.leafMass?.(object, part) ?? Math.max(MIN_LEAF_MASS, 8 * half[0] * half[1] * half[2] * HOLLOW_DOOR_DENSITY);
|
|
2194
|
+
const leaf = world.createBody({ shape: { kind: "box", half }, motion: "dynamic", mass, position: centre, rotation, layer: "prop", allowSleep: true, user: -3 });
|
|
2195
|
+
world.ignorePair(frame, leaf, true);
|
|
2196
|
+
const limits = { min: Math.min(0, range), max: Math.max(0, range) };
|
|
2197
|
+
const breakImpulse = options.breakImpulse?.(object, part);
|
|
2198
|
+
const joint = world.createJoint(
|
|
2199
|
+
isHinge ? { kind: "hinge", a: frame, b: leaf, point: pivot, axis, normal: perpendicularTo(axis), ...limits, friction: HINGE_HOLD_NM, ...breakImpulse === void 0 ? {} : { breakImpulse } } : { kind: "slider", a: frame, b: leaf, point: pivot, axis, ...limits, friction: SLIDE_HOLD_N, ...breakImpulse === void 0 ? {} : { breakImpulse } }
|
|
2200
|
+
);
|
|
2201
|
+
return createLeafDriver({ world, leaf, frame, ownsFrame, joint, onDispose: request.onDispose, drive: { isHinge, axis, pivot, centre, range, speed, base: rotation } });
|
|
2202
|
+
}
|
|
1777
2203
|
export {
|
|
2204
|
+
alignY,
|
|
2205
|
+
buildChain,
|
|
2206
|
+
chainPoints,
|
|
2207
|
+
createEnginePartPhysics,
|
|
1778
2208
|
createWorld,
|
|
2209
|
+
disposeChain,
|
|
1779
2210
|
fitsSize,
|
|
1780
2211
|
fitsSlot,
|
|
1781
2212
|
format,
|