@adatechnology/game-interactables 0.5.1 → 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.
@@ -21,7 +21,12 @@ var GameInteractables = (() => {
21
21
  // src/index.ts
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
+ alignY: () => alignY,
25
+ buildChain: () => buildChain,
26
+ chainPoints: () => chainPoints,
27
+ createEnginePartPhysics: () => createEnginePartPhysics,
24
28
  createWorld: () => createWorld,
29
+ disposeChain: () => disposeChain,
25
30
  fitsSize: () => fitsSize,
26
31
  fitsSlot: () => fitsSlot,
27
32
  format: () => format,
@@ -249,25 +254,25 @@ var GameInteractables = (() => {
249
254
  function poseOf(motion, t) {
250
255
  const tt = Math.max(0, Math.min(1, t));
251
256
  const axis = motion.axis ?? DEFAULT_AXIS;
252
- const pose = { position: [0, 0, 0], rotation: { axis: [axis[0], axis[1], axis[2]], angle: 0 }, scale: [1, 1, 1] };
257
+ const pose3 = { position: [0, 0, 0], rotation: { axis: [axis[0], axis[1], axis[2]], angle: 0 }, scale: [1, 1, 1] };
253
258
  switch (motion.kind) {
254
259
  case "hinge":
255
260
  case "rocker":
256
261
  case "knob":
257
- pose.rotation.angle = (motion.angle ?? Math.PI / 2) * tt;
262
+ pose3.rotation.angle = (motion.angle ?? Math.PI / 2) * tt;
258
263
  break;
259
264
  case "slide": {
260
265
  const d = (motion.distance ?? 0.4) * tt;
261
- pose.position = [axis[0] * d, axis[1] * d, axis[2] * d];
266
+ pose3.position = [axis[0] * d, axis[1] * d, axis[2] * d];
262
267
  break;
263
268
  }
264
269
  case "scale": {
265
270
  const k = 1 + ((motion.scale ?? 0.2) - 1) * tt;
266
- pose.scale = [axis[0] !== 0 ? k : 1, axis[1] !== 0 ? k : 1, axis[2] !== 0 ? k : 1];
271
+ pose3.scale = [axis[0] !== 0 ? k : 1, axis[1] !== 0 ? k : 1, axis[2] !== 0 ? k : 1];
267
272
  break;
268
273
  }
269
274
  }
270
- return pose;
275
+ return pose3;
271
276
  }
272
277
  function rotY(v, ry) {
273
278
  const c = Math.cos(ry);
@@ -287,8 +292,8 @@ var GameInteractables = (() => {
287
292
  const c = Math.cos(angle);
288
293
  const s = Math.sin(angle);
289
294
  const dot = k[0] * v[0] + k[1] * v[1] + k[2] * v[2];
290
- const cross = [k[1] * v[2] - k[2] * v[1], k[2] * v[0] - k[0] * v[2], k[0] * v[1] - k[1] * v[0]];
291
- return [v[0] * c + cross[0] * s + k[0] * dot * (1 - c), v[1] * c + cross[1] * s + k[1] * dot * (1 - c), v[2] * c + cross[2] * s + k[2] * dot * (1 - c)];
295
+ 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]];
296
+ 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)];
292
297
  }
293
298
  function clamp(v, lo, hi) {
294
299
  return v < lo ? lo : v > hi ? hi : v;
@@ -387,7 +392,11 @@ var GameInteractables = (() => {
387
392
  const next = clamp(target2, 0, p.maxT);
388
393
  const changed = next !== p.target;
389
394
  p.target = next;
390
- if (instant) writeT(o, p, next);
395
+ if (instant) {
396
+ p.driver?.snap(next);
397
+ p.commanded = next;
398
+ writeT(o, p, next);
399
+ }
391
400
  if (changed || instant) emit({ type: "part", object: o.id, part: p.spec.id, t: p.t, target: p.target });
392
401
  }
393
402
  function setPartTarget(o, partId, target2, instant = false) {
@@ -968,6 +977,11 @@ var GameInteractables = (() => {
968
977
  if (Math.hypot(dx, dz) > radius) continue;
969
978
  const top = tr.y + s.spec.anchor[1];
970
979
  if (pos[1] + ACTOR_HEIGHT < top - h || pos[1] > top) continue;
980
+ if (s.driver) {
981
+ s.driver.push(lv);
982
+ if (s.spec.kind === "panel" && s.spec.part && lv[2] < -0.05 && lp[2] > s.spec.anchor[2]) setPartTarget(o, s.spec.part, 1);
983
+ continue;
984
+ }
971
985
  const cap = 8;
972
986
  s.vax = clamp(s.vax - lv[2] * PUSH_GAIN, -cap, cap);
973
987
  s.vaz = clamp(s.vaz + lv[0] * PUSH_GAIN, -cap, cap);
@@ -975,9 +989,10 @@ var GameInteractables = (() => {
975
989
  }
976
990
  }
977
991
  }
992
+ const swingOut = [0, 0];
978
993
  function stepSoft(dt) {
979
- const sub = Math.max(1, Math.ceil(dt / (1 / 120)));
980
- const h = dt / sub;
994
+ const sub2 = Math.max(1, Math.ceil(dt / (1 / 120)));
995
+ const h = dt / sub2;
981
996
  for (const o of objs.values()) {
982
997
  if (!o.soft.size) continue;
983
998
  const wl = rotY([wind[0], 0, wind[1]], -o.public.transform.ry);
@@ -985,11 +1000,17 @@ var GameInteractables = (() => {
985
1000
  const k = s.spec.stiffness ?? 30;
986
1001
  const c = s.spec.damping ?? 4;
987
1002
  const max = s.spec.maxAngle ?? 0.7;
1003
+ if (s.driver) {
1004
+ s.driver.swing(swingOut);
1005
+ s.ax = swingOut[0];
1006
+ s.az = swingOut[1];
1007
+ continue;
1008
+ }
988
1009
  const gain = s.spec.wind ?? (s.spec.kind === "panel" ? 0 : 1);
989
1010
  const gust = gain * (0.6 + 0.4 * Math.sin(time * 1.7 + s.phase)) * 0.1;
990
1011
  const fx = -wl[2] * gust;
991
1012
  const fz = wl[0] * gust;
992
- for (let i = 0; i < sub; i++) {
1013
+ for (let i = 0; i < sub2; i++) {
993
1014
  s.vax += (-k * s.ax - c * s.vax + fx) * h;
994
1015
  s.vaz += (-k * s.az - c * s.vaz + fz) * h;
995
1016
  s.ax += s.vax * h;
@@ -1500,11 +1521,37 @@ var GameInteractables = (() => {
1500
1521
  if (opts.merge) return [...mergeColliders(fixed, { gap: opts.gap, slack: opts.slack }), ...out];
1501
1522
  return [...fixed, ...out];
1502
1523
  }
1524
+ function updateDriven(o, p, dt) {
1525
+ const driver = p.driver;
1526
+ if (p.broken) return;
1527
+ if (p.commanded !== p.target && !(gated(o, p) && p.target > p.t)) {
1528
+ driver.aim(p.target);
1529
+ p.commanded = p.target;
1530
+ p.moving = true;
1531
+ }
1532
+ driver.update(dt);
1533
+ if (driver.broken) {
1534
+ p.broken = true;
1535
+ emit({ type: "broke", object: o.id, part: p.spec.id });
1536
+ return;
1537
+ }
1538
+ const next = clamp(driver.read(), 0, p.maxT);
1539
+ if (next !== p.t) writeT(o, p, next);
1540
+ if (!driver.settled) return;
1541
+ p.target = next;
1542
+ p.commanded = next;
1543
+ if (p.moving) emit({ type: "part", object: o.id, part: p.spec.id, t: next, target: next });
1544
+ p.moving = false;
1545
+ }
1503
1546
  function update(dt) {
1504
1547
  if (!(dt > 0)) return;
1505
1548
  time += dt;
1506
1549
  for (const o of objs.values()) {
1507
1550
  for (const p of o.parts.values()) {
1551
+ if (p.driver) {
1552
+ updateDriven(o, p, dt);
1553
+ continue;
1554
+ }
1508
1555
  if (p.t === p.target) continue;
1509
1556
  if (gated(o, p) && p.target > p.t) continue;
1510
1557
  const step = (p.spec.motion.speed ?? 2.5) * dt;
@@ -1581,7 +1628,9 @@ var GameInteractables = (() => {
1581
1628
  };
1582
1629
  for (const p of spec.parts ?? []) {
1583
1630
  const t0 = clamp(p.t0 ?? 0, 0, 1);
1584
- o.parts.set(p.id, { spec: p, t: t0, target: t0, maxT: 1 });
1631
+ const driver = options.partPhysics?.attachPart(id, p, pub.transform);
1632
+ o.parts.set(p.id, { spec: p, t: t0, target: t0, maxT: 1, ...driver ? { driver, commanded: t0 } : {} });
1633
+ driver?.snap(t0);
1585
1634
  pub.parts[p.id] = t0;
1586
1635
  }
1587
1636
  for (const c of spec.containers ?? []) {
@@ -1607,7 +1656,8 @@ var GameInteractables = (() => {
1607
1656
  for (const f of spec.fixtures ?? []) o.fixtures.set(f.id, { spec: f, on: false, flowing: false, frac: 0, timer: 0 });
1608
1657
  let n = 0;
1609
1658
  for (const s of spec.soft ?? []) {
1610
- o.soft.set(s.id, { spec: s, ax: 0, az: 0, vax: 0, vaz: 0, phase: n++ * 2.399963 % (Math.PI * 2) });
1659
+ const driver = options.partPhysics?.attachSoft?.(id, s, pub.transform);
1660
+ o.soft.set(s.id, { spec: s, ax: 0, az: 0, vax: 0, vaz: 0, phase: n++ * 2.399963 % (Math.PI * 2), ...driver ? { driver } : {} });
1611
1661
  }
1612
1662
  for (const d of spec.damage ?? []) o.hp.set(d.target, { max: d.hp, hp: d.hp });
1613
1663
  objs.set(id, o);
@@ -1619,6 +1669,8 @@ var GameInteractables = (() => {
1619
1669
  const o = objs.get(id);
1620
1670
  if (!o) return;
1621
1671
  for (const [actor, s] of [...seats]) if (s.object === id) seats.delete(actor);
1672
+ for (const p of o.parts.values()) p.driver?.dispose();
1673
+ for (const s of o.soft.values()) s.driver?.dispose();
1622
1674
  objs.delete(id);
1623
1675
  if (defaultTank && defaultTank.startsWith(id + ":")) defaultTank = null;
1624
1676
  rebuildPlugs();
@@ -1654,6 +1706,8 @@ var GameInteractables = (() => {
1654
1706
  const p = o.parts.get(k);
1655
1707
  if (p) {
1656
1708
  p.target = clamp(v, 0, p.maxT);
1709
+ p.driver?.snap(p.target);
1710
+ p.commanded = p.target;
1657
1711
  writeT(o, p, p.target);
1658
1712
  }
1659
1713
  }
@@ -1815,6 +1869,383 @@ var GameInteractables = (() => {
1815
1869
  };
1816
1870
  return world;
1817
1871
  }
1872
+
1873
+ // src/engine-chain.ts
1874
+ var UP = [0, 1, 0];
1875
+ var sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
1876
+ var length = (v) => Math.hypot(v[0], v[1], v[2]);
1877
+ 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]];
1878
+ function alignY(direction) {
1879
+ const axis = cross(UP, direction);
1880
+ const sine = length(axis);
1881
+ if (sine < 1e-9) return direction[1] >= 0 ? [0, 0, 0, 1] : [1, 0, 0, 0];
1882
+ const angle = Math.atan2(sine, direction[1]);
1883
+ const half = Math.sin(angle / 2) / sine;
1884
+ return [axis[0] * half, axis[1] * half, axis[2] * half, Math.cos(angle / 2)];
1885
+ }
1886
+ var perpendicular = (direction) => {
1887
+ const side = Math.abs(direction[0]) < 0.9 ? cross(direction, [1, 0, 0]) : cross(direction, [0, 0, 1]);
1888
+ const size = length(side);
1889
+ return [side[0] / size, side[1] / size, side[2] / size];
1890
+ };
1891
+ function buildChain(world, spec) {
1892
+ const count = spec.points.length - 1;
1893
+ const lengths = Array.from({ length: count }, (_, i) => length(sub(spec.points[i + 1], spec.points[i])));
1894
+ const total = lengths.reduce((sum, each) => sum + each, 0);
1895
+ const pairs = [];
1896
+ for (let a = 0; a < count; a++) for (let b = a + 1; b < count; b++) pairs.push([a, b]);
1897
+ const group = world.createGroup(count, pairs);
1898
+ const bodies = [];
1899
+ const joints = [];
1900
+ for (let i = 0; i < count; i++) {
1901
+ const from = spec.points[i];
1902
+ const to = spec.points[i + 1];
1903
+ const span = lengths[i];
1904
+ const direction = [(to[0] - from[0]) / span, (to[1] - from[1]) / span, (to[2] - from[2]) / span];
1905
+ const link = world.createBody({
1906
+ shape: { kind: "box", half: [spec.halfWidth, span / 2, spec.halfDepth] },
1907
+ motion: "dynamic",
1908
+ mass: spec.totalMass * (span / total),
1909
+ position: [(from[0] + to[0]) / 2, (from[1] + to[1]) / 2, (from[2] + to[2]) / 2],
1910
+ rotation: alignY(direction),
1911
+ layer: "debris",
1912
+ group: { id: group, sub: i },
1913
+ linearDamping: spec.damping,
1914
+ angularDamping: spec.damping,
1915
+ user: -4
1916
+ });
1917
+ const previous = i === 0 ? spec.startBody : bodies[i - 1];
1918
+ if (i === 0) world.ignorePair(spec.startBody, link, true);
1919
+ joints.push(world.createJoint(swingTwist(spec, previous, link, from, direction)));
1920
+ bodies.push(link);
1921
+ }
1922
+ if (spec.endBody !== void 0) {
1923
+ const last = spec.points[count];
1924
+ const direction = sub(last, spec.points[count - 1]);
1925
+ const size = length(direction);
1926
+ world.ignorePair(spec.endBody, bodies[count - 1], true);
1927
+ joints.push(world.createJoint(swingTwist(spec, bodies[count - 1], spec.endBody, last, [direction[0] / size, direction[1] / size, direction[2] / size])));
1928
+ }
1929
+ return { bodies, joints, halfLengths: lengths.map((each) => each / 2), group };
1930
+ }
1931
+ function disposeChain(world, chain) {
1932
+ for (const joint of chain.joints) world.removeJoint(joint);
1933
+ for (const body of chain.bodies) world.removeBody(body);
1934
+ world.releaseGroup(chain.group);
1935
+ }
1936
+ var swingTwist = (spec, a, b, point, direction) => ({
1937
+ kind: "swingTwist",
1938
+ a,
1939
+ b,
1940
+ point,
1941
+ twistAxis: direction,
1942
+ planeAxis: perpendicular(direction),
1943
+ normalHalfCone: spec.halfCone,
1944
+ planeHalfCone: spec.halfCone,
1945
+ twistMin: -spec.twist,
1946
+ twistMax: spec.twist,
1947
+ friction: spec.jointFriction
1948
+ });
1949
+ var pose = new Float64Array(7);
1950
+ var writeLinkEnd = (world, chain, index, sign, out, offset) => {
1951
+ world.readPose(chain.bodies[index], pose, 0);
1952
+ const x = pose[3];
1953
+ const y = pose[4];
1954
+ const z = pose[5];
1955
+ const w = pose[6];
1956
+ const half = chain.halfLengths[index] * sign;
1957
+ out[offset] = pose[0] + half * 2 * (x * y - w * z);
1958
+ out[offset + 1] = pose[1] + half * (1 - 2 * (x * x + z * z));
1959
+ out[offset + 2] = pose[2] + half * 2 * (y * z + w * x);
1960
+ };
1961
+ function chainPoints(world, chain, out) {
1962
+ const count = chain.bodies.length;
1963
+ writeLinkEnd(world, chain, 0, -1, out, 0);
1964
+ for (let i = 0; i < count; i++) writeLinkEnd(world, chain, i, 1, out, (i + 1) * 3);
1965
+ }
1966
+ function chainEnd(world, chain, out) {
1967
+ writeLinkEnd(world, chain, chain.bodies.length - 1, 1, out, 0);
1968
+ }
1969
+
1970
+ // src/engine-soft.ts
1971
+ var STRIP_WIDTH = 0.12;
1972
+ var LINK_LENGTH = 0.15;
1973
+ var MAX_LINKS = 8;
1974
+ var FABRIC_KG_PER_M2 = 0.3;
1975
+ var FABRIC_HALF_DEPTH = 0.01;
1976
+ var TWIST_RANGE = 0.3;
1977
+ var DEFAULT_MAX_ANGLE = 0.7;
1978
+ var DEFAULT_DAMPING = 4;
1979
+ var JOINT_HOLD_NM = 2e-3;
1980
+ var PUSH_GAIN2 = 0.4;
1981
+ function attachStrips(world, soft, transform) {
1982
+ const [width, height, depth] = soft.size;
1983
+ const strips = Math.max(1, Math.round(width / STRIP_WIDTH));
1984
+ const links = Math.min(MAX_LINKS, Math.max(2, Math.round(height / LINK_LENGTH)));
1985
+ 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 });
1986
+ const damping = soft.damping ?? DEFAULT_DAMPING;
1987
+ const chains = [];
1988
+ const tops = [];
1989
+ for (let i = 0; i < strips; i++) {
1990
+ const x = soft.anchor[0] - width / 2 + width * (i + 0.5) / strips;
1991
+ const top = localToWorld(transform, [x, soft.anchor[1], soft.anchor[2]]);
1992
+ tops.push(top);
1993
+ chains.push(
1994
+ buildChain(world, {
1995
+ points: Array.from({ length: links + 1 }, (_, k) => [top[0], top[1] - height * k / links, top[2]]),
1996
+ halfWidth: width / strips * 0.45,
1997
+ halfDepth: FABRIC_HALF_DEPTH,
1998
+ totalMass: width / strips * height * FABRIC_KG_PER_M2,
1999
+ startBody: rail,
2000
+ halfCone: soft.maxAngle ?? DEFAULT_MAX_ANGLE,
2001
+ twist: TWIST_RANGE,
2002
+ jointFriction: JOINT_HOLD_NM,
2003
+ damping: damping / 4
2004
+ })
2005
+ );
2006
+ }
2007
+ const tip = new Float64Array(3);
2008
+ const lastLinks = chains.map((chain) => chain.bodies[chain.bodies.length - 1]);
2009
+ const cosYaw = Math.cos(-transform.ry);
2010
+ const sinYaw = Math.sin(-transform.ry);
2011
+ let restedAx = 0;
2012
+ let restedAz = 0;
2013
+ let isRested = false;
2014
+ let disposed = false;
2015
+ const everyLinkAsleep = () => {
2016
+ for (let i = 0; i < lastLinks.length; i++) if (!world.isSleeping(lastLinks[i])) return false;
2017
+ return true;
2018
+ };
2019
+ return {
2020
+ swing(out) {
2021
+ const asleep = everyLinkAsleep();
2022
+ if (asleep && isRested) {
2023
+ out[0] = restedAx;
2024
+ out[1] = restedAz;
2025
+ return;
2026
+ }
2027
+ let dx = 0;
2028
+ let dz = 0;
2029
+ for (let i = 0; i < chains.length; i++) {
2030
+ chainEnd(world, chains[i], tip);
2031
+ const top = tops[i];
2032
+ const offsetX = tip[0] - top[0];
2033
+ const offsetZ = tip[2] - top[2];
2034
+ dx += offsetX * cosYaw + offsetZ * sinYaw;
2035
+ dz += -offsetX * sinYaw + offsetZ * cosYaw;
2036
+ }
2037
+ out[0] = -Math.asin(Math.max(-1, Math.min(1, dz / strips / height)));
2038
+ out[1] = Math.asin(Math.max(-1, Math.min(1, dx / strips / height)));
2039
+ restedAx = out[0];
2040
+ restedAz = out[1];
2041
+ isRested = asleep;
2042
+ },
2043
+ push(velocity) {
2044
+ const world3 = rotY(velocity, transform.ry);
2045
+ for (const chain of chains) {
2046
+ 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)]);
2047
+ }
2048
+ },
2049
+ dispose() {
2050
+ if (disposed) return;
2051
+ disposed = true;
2052
+ for (const chain of chains) disposeChain(world, chain);
2053
+ world.removeBody(rail);
2054
+ }
2055
+ };
2056
+ }
2057
+ var linkMass = (soft, strips, links) => soft.size[0] / strips * (soft.size[1] / links) * FABRIC_KG_PER_M2;
2058
+
2059
+ // src/engine-leaf.ts
2060
+ var AIM_GAIN = 8;
2061
+ var ARRIVED_AT = 4e-3;
2062
+ var axisAngle = (axis, angle) => {
2063
+ const size = Math.hypot(axis[0], axis[1], axis[2]) || 1;
2064
+ const k = Math.sin(angle / 2) / size;
2065
+ return [axis[0] * k, axis[1] * k, axis[2] * k, Math.cos(angle / 2)];
2066
+ };
2067
+ var multiply = (a, b) => [
2068
+ a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
2069
+ a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
2070
+ a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
2071
+ a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2]
2072
+ ];
2073
+ var pose2 = new Float64Array(7);
2074
+ var linearScratch = [0, 0, 0];
2075
+ var angularScratch = [0, 0, 0];
2076
+ var NO_SPIN = [0, 0, 0];
2077
+ function createLeafDriver(spec) {
2078
+ const { world, leaf, frame, joint, drive } = spec;
2079
+ const { axis, pivot, centre, range } = drive;
2080
+ let target = 0;
2081
+ let settled = true;
2082
+ let broken = false;
2083
+ let disposed = false;
2084
+ let lastT = 0;
2085
+ let restedT;
2086
+ const measure = () => {
2087
+ if (drive.isHinge) return world.jointAngles(joint).hinge / range;
2088
+ world.readPose(leaf, pose2, 0);
2089
+ return ((pose2[0] - centre[0]) * axis[0] + (pose2[1] - centre[1]) * axis[1] + (pose2[2] - centre[2]) * axis[2]) / range;
2090
+ };
2091
+ const read = () => {
2092
+ if (broken) return lastT;
2093
+ if (settled && world.isSleeping(leaf)) {
2094
+ restedT ?? (restedT = measure());
2095
+ lastT = restedT;
2096
+ return lastT;
2097
+ }
2098
+ restedT = void 0;
2099
+ lastT = measure();
2100
+ return lastT;
2101
+ };
2102
+ const stop = () => {
2103
+ world.setVelocity(leaf, NO_SPIN, NO_SPIN);
2104
+ settled = true;
2105
+ };
2106
+ const move = (rate) => {
2107
+ world.wake(leaf);
2108
+ const speed = rate * range;
2109
+ angularScratch[0] = axis[0] * speed;
2110
+ angularScratch[1] = axis[1] * speed;
2111
+ angularScratch[2] = axis[2] * speed;
2112
+ if (!drive.isHinge) return world.setVelocity(leaf, angularScratch, NO_SPIN);
2113
+ world.readPose(leaf, pose2, 0);
2114
+ const armX = pose2[0] - pivot[0];
2115
+ const armY = pose2[1] - pivot[1];
2116
+ const armZ = pose2[2] - pivot[2];
2117
+ linearScratch[0] = angularScratch[1] * armZ - angularScratch[2] * armY;
2118
+ linearScratch[1] = angularScratch[2] * armX - angularScratch[0] * armZ;
2119
+ linearScratch[2] = angularScratch[0] * armY - angularScratch[1] * armX;
2120
+ world.setVelocity(leaf, linearScratch, angularScratch);
2121
+ };
2122
+ const place = (t) => {
2123
+ const amount = t * drive.range;
2124
+ if (drive.isHinge) {
2125
+ const centre2 = rotateAxis([drive.centre[0] - drive.pivot[0], drive.centre[1] - drive.pivot[1], drive.centre[2] - drive.pivot[2]], drive.axis, amount);
2126
+ 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);
2127
+ } else {
2128
+ 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);
2129
+ }
2130
+ world.setVelocity(leaf, [0, 0, 0], [0, 0, 0]);
2131
+ };
2132
+ return {
2133
+ leaf,
2134
+ aim(next) {
2135
+ if (broken) return;
2136
+ target = next;
2137
+ settled = false;
2138
+ restedT = void 0;
2139
+ },
2140
+ snap(t) {
2141
+ target = t;
2142
+ settled = true;
2143
+ restedT = void 0;
2144
+ if (!broken) place(t);
2145
+ },
2146
+ update() {
2147
+ if (broken) return;
2148
+ if (!world.jointExists(joint)) {
2149
+ broken = true;
2150
+ settled = true;
2151
+ return;
2152
+ }
2153
+ if (settled) return;
2154
+ const error = target - read();
2155
+ if (Math.abs(error) < ARRIVED_AT) return stop();
2156
+ move(clamp(error * AIM_GAIN, -drive.speed, drive.speed));
2157
+ },
2158
+ read,
2159
+ get settled() {
2160
+ return settled;
2161
+ },
2162
+ get broken() {
2163
+ return broken;
2164
+ },
2165
+ dispose() {
2166
+ if (disposed) return;
2167
+ disposed = true;
2168
+ spec.onDispose();
2169
+ if (!broken) world.removeJoint(joint);
2170
+ world.removeBody(leaf);
2171
+ if (spec.ownsFrame) world.removeBody(frame);
2172
+ }
2173
+ };
2174
+ }
2175
+
2176
+ // src/engine-parts.ts
2177
+ var HOLLOW_DOOR_DENSITY = 150;
2178
+ var MIN_LEAF_MASS = 1;
2179
+ var SLIDE_HOLD_N = 4;
2180
+ var HINGE_HOLD_NM = 0.4;
2181
+ var SOFT_KINDS = /* @__PURE__ */ new Set(["cloth", "curtain", "strip"]);
2182
+ var quatY = (ry) => [0, Math.sin(ry / 2), 0, Math.cos(ry / 2)];
2183
+ 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]];
2184
+ var perpendicularTo = (axis) => {
2185
+ const side = Math.abs(axis[0]) < 0.9 ? cross2(axis, [1, 0, 0]) : cross2(axis, [0, 0, 1]);
2186
+ const size = Math.hypot(side[0], side[1], side[2]);
2187
+ return [side[0] / size, side[1] / size, side[2] / size];
2188
+ };
2189
+ function createEnginePartPhysics(world, options = {}) {
2190
+ const disposers = /* @__PURE__ */ new Set();
2191
+ const leaves = /* @__PURE__ */ new Map();
2192
+ return {
2193
+ attachPart(object, part, transform) {
2194
+ const key = `${object}:${part.id}`;
2195
+ const retire = () => {
2196
+ leaves.delete(key);
2197
+ disposers.delete(dispose);
2198
+ };
2199
+ const dispose = () => driver?.dispose();
2200
+ const driver = attachLeaf(world, options, { object, part, transform, onDispose: retire });
2201
+ if (!driver) return void 0;
2202
+ leaves.set(key, driver.leaf);
2203
+ disposers.add(dispose);
2204
+ return driver;
2205
+ },
2206
+ leafOf: (object, part) => leaves.get(`${object}:${part}`),
2207
+ attachSoft(_object, soft, transform) {
2208
+ const driver = SOFT_KINDS.has(soft.kind) ? attachStrips(world, soft, transform) : void 0;
2209
+ if (!driver) return void 0;
2210
+ const dispose = () => driver.dispose();
2211
+ disposers.add(dispose);
2212
+ return { swing: driver.swing, push: driver.push, dispose: () => {
2213
+ disposers.delete(dispose);
2214
+ driver.dispose();
2215
+ } };
2216
+ },
2217
+ dispose() {
2218
+ for (const dispose of [...disposers]) dispose();
2219
+ disposers.clear();
2220
+ }
2221
+ };
2222
+ }
2223
+ function attachLeaf(world, options, request) {
2224
+ const { object, part, transform } = request;
2225
+ const motion = part.motion;
2226
+ const bounds = part.bounds;
2227
+ if (!bounds || motion.kind !== "hinge" && motion.kind !== "slide") return void 0;
2228
+ const isHinge = motion.kind === "hinge";
2229
+ const axisLocal = rotY(motion.axis ?? [0, 1, 0], part.ry ?? 0);
2230
+ const axis = rotY(axisLocal, transform.ry);
2231
+ const pivot = localToWorld(transform, part.pivot);
2232
+ const half = [(bounds[3] - bounds[0]) / 2, (bounds[4] - bounds[1]) / 2, (bounds[5] - bounds[2]) / 2];
2233
+ const centre = localToWorld(transform, [(bounds[0] + bounds[3]) / 2, (bounds[1] + bounds[4]) / 2, (bounds[2] + bounds[5]) / 2]);
2234
+ const rotation = quatY(transform.ry);
2235
+ const range = isHinge ? motion.angle ?? Math.PI / 2 : motion.distance ?? 0.4;
2236
+ const speed = motion.speed ?? 2.5;
2237
+ 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 });
2238
+ const ownsFrame = options.frameOf?.(object) === void 0;
2239
+ const mass = options.leafMass?.(object, part) ?? Math.max(MIN_LEAF_MASS, 8 * half[0] * half[1] * half[2] * HOLLOW_DOOR_DENSITY);
2240
+ const leaf = world.createBody({ shape: { kind: "box", half }, motion: "dynamic", mass, position: centre, rotation, layer: "prop", allowSleep: true, user: -3 });
2241
+ world.ignorePair(frame, leaf, true);
2242
+ const limits = { min: Math.min(0, range), max: Math.max(0, range) };
2243
+ const breakImpulse = options.breakImpulse?.(object, part);
2244
+ const joint = world.createJoint(
2245
+ 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 } }
2246
+ );
2247
+ return createLeafDriver({ world, leaf, frame, ownsFrame, joint, onDispose: request.onDispose, drive: { isHinge, axis, pivot, centre, range, speed, base: rotation } });
2248
+ }
1818
2249
  return __toCommonJS(index_exports);
1819
2250
  })();
1820
2251
  //# sourceMappingURL=index.global.js.map