motionary 10.6.0 → 10.8.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.
Files changed (153) hide show
  1. package/AGENTS.md +2 -0
  2. package/CHANGELOG.md +36 -0
  3. package/README.md +77 -4
  4. package/bin/motionary-mcp.mjs +160 -11
  5. package/dist/chunks/{anim-image-DECLkPH4.cjs → anim-image-BzTQa2k3.cjs} +2 -2
  6. package/dist/chunks/{anim-image-DECLkPH4.cjs.map → anim-image-BzTQa2k3.cjs.map} +1 -1
  7. package/dist/chunks/{anim-image-DNghy017.js → anim-image-iD646Q7i.js} +2 -2
  8. package/dist/chunks/{anim-image-DNghy017.js.map → anim-image-iD646Q7i.js.map} +1 -1
  9. package/dist/chunks/{keyframes-F6vp6tZZ.js → keyframes-B4zefz0k.js} +2 -2
  10. package/dist/chunks/{keyframes-F6vp6tZZ.js.map → keyframes-B4zefz0k.js.map} +1 -1
  11. package/dist/chunks/{keyframes-CbUZjD_5.cjs → keyframes-BlOyB1PP.cjs} +2 -2
  12. package/dist/chunks/{keyframes-CbUZjD_5.cjs.map → keyframes-BlOyB1PP.cjs.map} +1 -1
  13. package/dist/chunks/{registry-dr1CPTk1.cjs → registry-BZxDVL3h.cjs} +2 -2
  14. package/dist/chunks/{registry-dr1CPTk1.cjs.map → registry-BZxDVL3h.cjs.map} +1 -1
  15. package/dist/chunks/{registry-D8LAoXI-.js → registry-Bkt3-3ET.js} +3 -3
  16. package/dist/chunks/{registry-D8LAoXI-.js.map → registry-Bkt3-3ET.js.map} +1 -1
  17. package/dist/chunks/runtime-link-CUiZ8lbE.cjs +38 -0
  18. package/dist/chunks/runtime-link-CUiZ8lbE.cjs.map +1 -0
  19. package/dist/chunks/runtime-link-c13gskBZ.js +36 -0
  20. package/dist/chunks/runtime-link-c13gskBZ.js.map +1 -0
  21. package/dist/chunks/{ticker-DaAlEyOd.cjs → ticker-BfxAECfO.cjs} +2 -2
  22. package/dist/chunks/{ticker-DaAlEyOd.cjs.map → ticker-BfxAECfO.cjs.map} +1 -1
  23. package/dist/chunks/{ticker-Do9bGsaV.js → ticker-ZtweD3U4.js} +2 -2
  24. package/dist/chunks/{ticker-Do9bGsaV.js.map → ticker-ZtweD3U4.js.map} +1 -1
  25. package/dist/chunks/{tween-CmkdSyaC.cjs → tween--MD-39y0.cjs} +2 -2
  26. package/dist/chunks/{tween-CmkdSyaC.cjs.map → tween--MD-39y0.cjs.map} +1 -1
  27. package/dist/chunks/{tween-BzCGq_Ba.js → tween-DACdkLyO.js} +2 -2
  28. package/dist/chunks/{tween-BzCGq_Ba.js.map → tween-DACdkLyO.js.map} +1 -1
  29. package/dist/components/ai.cjs +314 -0
  30. package/dist/components/ai.cjs.map +1 -0
  31. package/dist/components/ai.d.cts +58 -0
  32. package/dist/components/ai.d.ts +58 -0
  33. package/dist/components/ai.js +311 -0
  34. package/dist/components/ai.js.map +1 -0
  35. package/dist/components/snap-carousel.cjs +216 -0
  36. package/dist/components/snap-carousel.cjs.map +1 -0
  37. package/dist/components/snap-carousel.d.cts +80 -0
  38. package/dist/components/snap-carousel.d.ts +80 -0
  39. package/dist/components/snap-carousel.js +214 -0
  40. package/dist/components/snap-carousel.js.map +1 -0
  41. package/dist/components/widgets.cjs +626 -268
  42. package/dist/components/widgets.cjs.map +1 -1
  43. package/dist/components/widgets.d.cts +515 -39
  44. package/dist/components/widgets.d.ts +515 -39
  45. package/dist/components/widgets.js +617 -262
  46. package/dist/components/widgets.js.map +1 -1
  47. package/dist/llms-full.txt +191 -6
  48. package/dist/llms.txt +10 -3
  49. package/dist/manifest.json +497 -19
  50. package/dist/runtime/drag-snap.cjs +269 -0
  51. package/dist/runtime/drag-snap.cjs.map +1 -0
  52. package/dist/runtime/drag-snap.d.cts +109 -0
  53. package/dist/runtime/drag-snap.d.ts +109 -0
  54. package/dist/runtime/drag-snap.iife.js +2 -0
  55. package/dist/runtime/drag-snap.iife.js.map +1 -0
  56. package/dist/runtime/drag-snap.js +261 -0
  57. package/dist/runtime/drag-snap.js.map +1 -0
  58. package/dist/runtime/format-apng.cjs +4 -4
  59. package/dist/runtime/format-apng.js +4 -4
  60. package/dist/runtime/format-css.cjs +4 -4
  61. package/dist/runtime/format-css.js +4 -4
  62. package/dist/runtime/format-gif.cjs +4 -4
  63. package/dist/runtime/format-gif.js +4 -4
  64. package/dist/runtime/format-gltf.cjs +37 -18
  65. package/dist/runtime/format-gltf.cjs.map +1 -1
  66. package/dist/runtime/format-gltf.d.cts +18 -2
  67. package/dist/runtime/format-gltf.d.ts +18 -2
  68. package/dist/runtime/format-gltf.iife.js +1 -1
  69. package/dist/runtime/format-gltf.iife.js.map +1 -1
  70. package/dist/runtime/format-gltf.js +37 -18
  71. package/dist/runtime/format-gltf.js.map +1 -1
  72. package/dist/runtime/format-motion.cjs +4 -4
  73. package/dist/runtime/format-motion.js +4 -4
  74. package/dist/runtime/format-obj.cjs +1 -1
  75. package/dist/runtime/format-obj.d.cts +2 -0
  76. package/dist/runtime/format-obj.d.ts +2 -0
  77. package/dist/runtime/format-obj.iife.js.map +1 -1
  78. package/dist/runtime/format-obj.js +1 -1
  79. package/dist/runtime/format-scene.cjs +241 -0
  80. package/dist/runtime/format-scene.cjs.map +1 -0
  81. package/dist/runtime/format-scene.d.cts +349 -0
  82. package/dist/runtime/format-scene.d.ts +349 -0
  83. package/dist/runtime/format-scene.iife.js +2 -0
  84. package/dist/runtime/format-scene.iife.js.map +1 -0
  85. package/dist/runtime/format-scene.js +233 -0
  86. package/dist/runtime/format-scene.js.map +1 -0
  87. package/dist/runtime/format-sprite.cjs +3 -3
  88. package/dist/runtime/format-sprite.js +3 -3
  89. package/dist/runtime/format-svg.cjs +3 -3
  90. package/dist/runtime/format-svg.js +3 -3
  91. package/dist/runtime/format-webp.cjs +4 -4
  92. package/dist/runtime/format-webp.js +4 -4
  93. package/dist/runtime/gl.cjs +17 -4
  94. package/dist/runtime/gl.cjs.map +1 -1
  95. package/dist/runtime/gl.d.cts +2 -0
  96. package/dist/runtime/gl.d.ts +2 -0
  97. package/dist/runtime/gl.iife.js +1 -1
  98. package/dist/runtime/gl.iife.js.map +1 -1
  99. package/dist/runtime/gl.js +17 -4
  100. package/dist/runtime/gl.js.map +1 -1
  101. package/dist/runtime/gltf-anim.cjs +251 -0
  102. package/dist/runtime/gltf-anim.cjs.map +1 -0
  103. package/dist/runtime/gltf-anim.d.cts +216 -0
  104. package/dist/runtime/gltf-anim.d.ts +216 -0
  105. package/dist/runtime/gltf-anim.iife.js +2 -0
  106. package/dist/runtime/gltf-anim.iife.js.map +1 -0
  107. package/dist/runtime/gltf-anim.js +243 -0
  108. package/dist/runtime/gltf-anim.js.map +1 -0
  109. package/dist/runtime/physics.cjs +739 -0
  110. package/dist/runtime/physics.cjs.map +1 -0
  111. package/dist/runtime/physics.d.cts +250 -0
  112. package/dist/runtime/physics.d.ts +250 -0
  113. package/dist/runtime/physics.iife.js +2 -0
  114. package/dist/runtime/physics.iife.js.map +1 -0
  115. package/dist/runtime/physics.js +731 -0
  116. package/dist/runtime/physics.js.map +1 -0
  117. package/dist/runtime/scroll.cjs +2 -2
  118. package/dist/runtime/scroll.js +2 -2
  119. package/dist/runtime/smooth.cjs +1 -1
  120. package/dist/runtime/smooth.js +1 -1
  121. package/dist/runtime/text.cjs +1 -1
  122. package/dist/runtime/text.js +1 -1
  123. package/dist/runtime/vector.cjs +421 -11
  124. package/dist/runtime/vector.cjs.map +1 -1
  125. package/dist/runtime/vector.d.cts +20 -4
  126. package/dist/runtime/vector.d.ts +20 -4
  127. package/dist/runtime/vector.iife.js +1 -1
  128. package/dist/runtime/vector.iife.js.map +1 -1
  129. package/dist/runtime/vector.js +421 -12
  130. package/dist/runtime/vector.js.map +1 -1
  131. package/dist/runtime.cjs +3 -3
  132. package/dist/runtime.d.cts +1 -1
  133. package/dist/runtime.d.ts +1 -1
  134. package/dist/runtime.iife.js +1 -1
  135. package/dist/runtime.iife.js.map +1 -1
  136. package/dist/runtime.js +5 -5
  137. package/dist/widgets.umd.js +1 -1
  138. package/dist/widgets.umd.js.map +1 -1
  139. package/docs/ROADMAP.md +2 -2
  140. package/docs/components/README.md +5 -2
  141. package/docs/components/usa-gl-scene.md +27 -6
  142. package/docs/components/usa-lottie-player.md +18 -0
  143. package/docs/components/usa-motion-prompt.md +32 -0
  144. package/docs/components/usa-physics-playground.md +63 -0
  145. package/docs/components/usa-snap-carousel.md +68 -0
  146. package/docs/components.md +95 -3
  147. package/docs/runtime/drag-snap.md +60 -0
  148. package/docs/runtime/format-gltf.md +3 -2
  149. package/docs/runtime/format-scene.md +59 -0
  150. package/docs/runtime/gltf-anim.md +62 -0
  151. package/docs/runtime/physics.md +61 -0
  152. package/docs/runtime/vector.md +3 -2
  153. package/package.json +61 -1
@@ -0,0 +1,731 @@
1
+ import { R as RUNTIME_VERSION, r as requireModule } from '../chunks/registry-Bkt3-3ET.js';
2
+
3
+ /**
4
+ * `motionary/runtime/physics` (10.7) — a small 2D rigid-body engine written
5
+ * for Motionary (own implementation and API; no Matter.js / Box2D code):
6
+ *
7
+ * - bodies: circles, boxes and convex polygons (static, dynamic or
8
+ * kinematic), density → mass + moment of inertia, restitution, friction,
9
+ * sensors, collision groups / masks, sleeping;
10
+ * - collisions: sort-and-sweep broad phase, separating-axis narrow phase
11
+ * with clipped contact manifolds (up to 2 points), sequential, accumulated and
12
+ * clamped impulses (normal + Coulomb friction) with a Baumgarte position bias;
13
+ * - constraints: `distance` (rigid or a spring with `stiffness` /
14
+ * `damping`), `pin` (a body point to a world point — also the pointer
15
+ * drag constraint) and `weld`-like stiff distance pairs;
16
+ * - a fixed time step (default 1/60 s) with an accumulator, so results are
17
+ * the same at any frame rate; `world.run()` drives it from the shared
18
+ * runtime ticker (pauses while the ticker sleeps).
19
+ *
20
+ * Pure maths — no DOM: safe in SSR and workers. Units are up to you (the
21
+ * defaults assume pixels: gravity 980 px/s²).
22
+ */
23
+ const v = (x = 0, y = 0) => ({ x, y });
24
+ const dot = (a, b) => a.x * b.x + a.y * b.y;
25
+ const cross = (a, b) => a.x * b.y - a.y * b.x;
26
+ const sub = (a, b) => v(a.x - b.x, a.y - b.y);
27
+ const add = (a, b) => v(a.x + b.x, a.y + b.y);
28
+ const mul = (a, s) => v(a.x * s, a.y * s);
29
+ const len = (a) => Math.hypot(a.x, a.y);
30
+ const crossSV = (s, a) => v(-s * a.y, s * a.x);
31
+ const rot = (a, c, s) => v(a.x * c - a.y * s, a.x * s + a.y * c);
32
+ let nextId = 1;
33
+ class Body {
34
+ constructor(o = {}) {
35
+ this.id = nextId++;
36
+ this.force = v();
37
+ this.torque = 0;
38
+ this.radius = 0;
39
+ /** Local convex vertices (counter-clockwise) for boxes / polygons. */
40
+ this.verts = [];
41
+ this.normals = [];
42
+ this.mass = 0;
43
+ this.invMass = 0;
44
+ this.inertia = 0;
45
+ this.invInertia = 0;
46
+ this.sleeping = false;
47
+ this.sleepTime = 0;
48
+ /** Bounding box, refreshed every step. */
49
+ this.aabb = { minX: 0, minY: 0, maxX: 0, maxY: 0 };
50
+ this.wv = [];
51
+ this.wvAngle = NaN;
52
+ this.wvPos = v(NaN, NaN);
53
+ this.shape = o.shape || (o.vertices ? 'polygon' : o.radius != null && o.width == null ? 'circle' : 'box');
54
+ this.type = o.type || 'dynamic';
55
+ this.label = o.label || this.shape;
56
+ this.position = v(o.x || 0, o.y || 0);
57
+ this.velocity = v(o.vx || 0, o.vy || 0);
58
+ this.angle = o.angle || 0;
59
+ this.angularVelocity = o.angularVelocity || 0;
60
+ this.restitution = o.restitution ?? 0.2;
61
+ this.friction = o.friction ?? 0.4;
62
+ this.damping = o.damping ?? 0;
63
+ this.angularDamping = o.angularDamping ?? 0.01;
64
+ this.sensor = !!o.sensor;
65
+ this.category = o.category ?? 1;
66
+ this.mask = o.mask ?? 0xffffffff;
67
+ this.data = o.data;
68
+ const density = o.density ?? 0.001;
69
+ let area = 0, I = 0;
70
+ if (this.shape === 'circle') {
71
+ this.radius = o.radius ?? 20;
72
+ area = Math.PI * this.radius ** 2;
73
+ I = 0.5 * this.radius ** 2; // per unit mass
74
+ }
75
+ else {
76
+ const pts = this.shape === 'box' ? boxVerts(o.width ?? 40, o.height ?? 40) : hull((o.vertices || []).map(([x, y]) => v(x, y)));
77
+ if (pts.length < 3)
78
+ throw new Error('[motionary] physics: a polygon needs at least 3 non-collinear vertices');
79
+ // centre the polygon on its centroid
80
+ const { c, a, i } = polyMass(pts);
81
+ this.verts = pts.map((p) => sub(p, c));
82
+ area = a;
83
+ I = i;
84
+ this.radius = Math.max(...this.verts.map(len));
85
+ this.normals = this.verts.map((p, k) => {
86
+ const q = this.verts[(k + 1) % this.verts.length];
87
+ const e = sub(q, p);
88
+ const n = v(e.y, -e.x);
89
+ return mul(n, 1 / len(n));
90
+ });
91
+ }
92
+ if (this.type === 'dynamic') {
93
+ this.mass = o.mass ?? density * area;
94
+ this.invMass = this.mass > 0 ? 1 / this.mass : 0;
95
+ this.inertia = o.fixedRotation ? Infinity : this.mass * I;
96
+ this.invInertia = o.fixedRotation || !this.inertia ? 0 : 1 / this.inertia;
97
+ }
98
+ }
99
+ get isStatic() {
100
+ return this.type === 'static';
101
+ }
102
+ /** Vertices in world space (cached per pose). */
103
+ worldVerts() {
104
+ if (this.wvAngle !== this.angle || this.wvPos.x !== this.position.x || this.wvPos.y !== this.position.y) {
105
+ const c = Math.cos(this.angle), s = Math.sin(this.angle);
106
+ this.wv = this.verts.map((p) => add(rot(p, c, s), this.position));
107
+ this.wvAngle = this.angle;
108
+ this.wvPos = v(this.position.x, this.position.y);
109
+ }
110
+ return this.wv;
111
+ }
112
+ worldNormal(i) {
113
+ return rot(this.normals[i], Math.cos(this.angle), Math.sin(this.angle));
114
+ }
115
+ updateAabb() {
116
+ if (this.shape === 'circle') {
117
+ const r = this.radius, p = this.position;
118
+ this.aabb = { minX: p.x - r, minY: p.y - r, maxX: p.x + r, maxY: p.y + r };
119
+ return;
120
+ }
121
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
122
+ for (const p of this.worldVerts()) {
123
+ if (p.x < minX)
124
+ minX = p.x;
125
+ if (p.y < minY)
126
+ minY = p.y;
127
+ if (p.x > maxX)
128
+ maxX = p.x;
129
+ if (p.y > maxY)
130
+ maxY = p.y;
131
+ }
132
+ this.aabb = { minX, minY, maxX, maxY };
133
+ }
134
+ applyImpulse(j, at) {
135
+ if (this.type !== 'dynamic')
136
+ return;
137
+ this.wake();
138
+ this.velocity = add(this.velocity, mul(j, this.invMass));
139
+ if (at)
140
+ this.angularVelocity += cross(sub(at, this.position), j) * this.invInertia;
141
+ }
142
+ applyForce(f, at) {
143
+ if (this.type !== 'dynamic')
144
+ return;
145
+ this.wake();
146
+ this.force = add(this.force, f);
147
+ if (at)
148
+ this.torque += cross(sub(at, this.position), f);
149
+ }
150
+ wake() {
151
+ this.sleeping = false;
152
+ this.sleepTime = 0;
153
+ }
154
+ /** Is the world point inside this body? */
155
+ contains(x, y) {
156
+ const p = v(x, y);
157
+ if (this.shape === 'circle')
158
+ return len(sub(p, this.position)) <= this.radius;
159
+ const w = this.worldVerts();
160
+ for (let i = 0; i < w.length; i++)
161
+ if (dot(this.worldNormal(i), sub(p, w[i])) > 0)
162
+ return false;
163
+ return true;
164
+ }
165
+ }
166
+ function boxVerts(w, h) {
167
+ return [v(-w / 2, -h / 2), v(w / 2, -h / 2), v(w / 2, h / 2), v(-w / 2, h / 2)];
168
+ }
169
+ /** Convex hull (monotone chain), counter-clockwise in a y-down screen space = clockwise maths; we keep "outward normals" consistent. */
170
+ function hull(pts) {
171
+ const p = pts.slice().sort((a, b) => a.x - b.x || a.y - b.y);
172
+ if (p.length < 3)
173
+ return p;
174
+ const lower = [], upper = [];
175
+ const turn = (o, a, b) => cross(sub(a, o), sub(b, o));
176
+ for (const q of p) {
177
+ while (lower.length >= 2 && turn(lower[lower.length - 2], lower[lower.length - 1], q) <= 0)
178
+ lower.pop();
179
+ lower.push(q);
180
+ }
181
+ for (let i = p.length - 1; i >= 0; i--) {
182
+ const q = p[i];
183
+ while (upper.length >= 2 && turn(upper[upper.length - 2], upper[upper.length - 1], q) <= 0)
184
+ upper.pop();
185
+ upper.push(q);
186
+ }
187
+ return lower.slice(0, -1).concat(upper.slice(0, -1));
188
+ }
189
+ /** Area, centroid and polar moment per unit mass of a convex polygon. */
190
+ function polyMass(p) {
191
+ let A = 0, cx = 0, cy = 0, I = 0;
192
+ for (let k = 0; k < p.length; k++) {
193
+ const a = p[k], b = p[(k + 1) % p.length];
194
+ const cr = cross(a, b);
195
+ A += cr;
196
+ cx += (a.x + b.x) * cr;
197
+ cy += (a.y + b.y) * cr;
198
+ I += cr * (dot(a, a) + dot(a, b) + dot(b, b));
199
+ }
200
+ A /= 2;
201
+ const c = v(cx / (6 * A), cy / (6 * A));
202
+ const Io = I / 12; // about the origin, times density
203
+ const iC = Io / A - dot(c, c); // per unit mass, about the centroid
204
+ return { a: Math.abs(A), c, i: Math.abs(iC) };
205
+ }
206
+ // ------------------------------------------------------------------ narrow phase
207
+ const SPECULATIVE = 1;
208
+ /** Squared speed (px²/s², incl. the rim speed of rotation) under which a body may fall asleep. */
209
+ const SLEEP_MOTION = 36;
210
+ /** Inverse mass / inertia as the solver sees them: static, kinematic and sleeping bodies do not move. */
211
+ const iM = (b) => (b.type === 'dynamic' && !b.sleeping ? b.invMass : 0);
212
+ const iI = (b) => (b.type === 'dynamic' && !b.sleeping ? b.invInertia : 0);
213
+ const motion = (b) => dot(b.velocity, b.velocity) + (b.angularVelocity * b.radius) ** 2;
214
+ function circleCircle(a, b) {
215
+ const d = sub(b.position, a.position), dist = len(d), r = a.radius + b.radius;
216
+ if (dist >= r)
217
+ return null;
218
+ const n = dist > 1e-9 ? mul(d, 1 / dist) : v(0, 1);
219
+ return { a, b, normal: n, contacts: [{ point: add(a.position, mul(n, a.radius - (r - dist) / 2)), depth: r - dist }] };
220
+ }
221
+ function polyCircle(p, c) {
222
+ // normal from the polygon to the circle
223
+ const w = p.worldVerts();
224
+ let best = -Infinity, bi = 0;
225
+ for (let i = 0; i < w.length; i++) {
226
+ const s = dot(p.worldNormal(i), sub(c.position, w[i]));
227
+ if (s > c.radius)
228
+ return null;
229
+ if (s > best)
230
+ (best = s), (bi = i);
231
+ }
232
+ if (best < 1e-9) {
233
+ // centre inside the polygon
234
+ const n = p.worldNormal(bi);
235
+ return { a: p, b: c, normal: n, contacts: [{ point: sub(c.position, mul(n, c.radius)), depth: c.radius - best }] };
236
+ }
237
+ // closest point on the polygon outline
238
+ let cp = w[0], cd = Infinity;
239
+ for (let i = 0; i < w.length; i++) {
240
+ const a = w[i], b = w[(i + 1) % w.length], e = sub(b, a);
241
+ const t = Math.max(0, Math.min(1, dot(sub(c.position, a), e) / dot(e, e)));
242
+ const q = add(a, mul(e, t)), d = len(sub(c.position, q));
243
+ if (d < cd)
244
+ (cd = d), (cp = q);
245
+ }
246
+ if (cd >= c.radius)
247
+ return null;
248
+ const n = cd > 1e-9 ? mul(sub(c.position, cp), 1 / cd) : p.worldNormal(bi);
249
+ return { a: p, b: c, normal: n, contacts: [{ point: cp, depth: c.radius - cd }] };
250
+ }
251
+ function maxSeparation(a, b) {
252
+ const wa = a.worldVerts(), wb = b.worldVerts();
253
+ let best = -Infinity, bi = 0;
254
+ for (let i = 0; i < wa.length; i++) {
255
+ const n = a.worldNormal(i);
256
+ let m = Infinity;
257
+ for (const q of wb)
258
+ m = Math.min(m, dot(n, sub(q, wa[i])));
259
+ if (m > best)
260
+ (best = m), (bi = i);
261
+ }
262
+ return { s: best, i: bi };
263
+ }
264
+ function polyPoly(a, b) {
265
+ const A = maxSeparation(a, b);
266
+ if (A.s > 0)
267
+ return null;
268
+ const B = maxSeparation(b, a);
269
+ if (B.s > 0)
270
+ return null;
271
+ let ref = a, inc = b, fi = A.i, flip = false;
272
+ if (B.s > A.s * 0.95 + 0.01)
273
+ (ref = b), (inc = a), (fi = B.i), (flip = true);
274
+ const rw = ref.worldVerts(), iw = inc.worldVerts();
275
+ const n = ref.worldNormal(fi);
276
+ // incident edge: the one whose normal is most anti-parallel to n
277
+ let ii = 0, md = Infinity;
278
+ for (let i = 0; i < iw.length; i++) {
279
+ const d = dot(inc.worldNormal(i), n);
280
+ if (d < md)
281
+ (md = d), (ii = i);
282
+ }
283
+ let pts = [iw[ii], iw[(ii + 1) % iw.length]];
284
+ const r1 = rw[fi], r2 = rw[(fi + 1) % rw.length];
285
+ const t = mul(sub(r2, r1), 1 / len(sub(r2, r1)));
286
+ // clip against the side planes of the reference edge
287
+ const clip = (ps, dir, off) => {
288
+ const out = [];
289
+ const d0 = dot(dir, ps[0]) - off, d1 = dot(dir, ps[1]) - off;
290
+ if (d0 <= 0)
291
+ out.push(ps[0]);
292
+ if (d1 <= 0)
293
+ out.push(ps[1]);
294
+ if (d0 * d1 < 0)
295
+ out.push(add(ps[0], mul(sub(ps[1], ps[0]), d0 / (d0 - d1))));
296
+ return out;
297
+ };
298
+ pts = clip(pts, mul(t, -1), -dot(t, r1));
299
+ if (pts.length < 2)
300
+ return null;
301
+ pts = clip(pts, t, dot(t, r2));
302
+ if (pts.length < 2)
303
+ return null;
304
+ const contacts = [];
305
+ for (const p of pts) {
306
+ const sep = dot(n, sub(p, r1));
307
+ // keep points that are just apart too (speculative): resting stacks keep two contacts instead of rocking on one
308
+ if (sep <= SPECULATIVE)
309
+ contacts.push({ point: p, depth: -sep });
310
+ }
311
+ if (!contacts.some((c) => c.depth >= 0))
312
+ return null;
313
+ return flip ? { a, b, normal: mul(n, -1), contacts } : { a, b, normal: n, contacts };
314
+ }
315
+ /** Contact manifold between two bodies (normal points from a to b), or null. */
316
+ function collide(a, b) {
317
+ if (a.shape === 'circle' && b.shape === 'circle')
318
+ return circleCircle(a, b);
319
+ if (a.shape !== 'circle' && b.shape === 'circle')
320
+ return polyCircle(a, b);
321
+ if (a.shape === 'circle' && b.shape !== 'circle') {
322
+ const m = polyCircle(b, a);
323
+ return m && { a, b, normal: mul(m.normal, -1), contacts: m.contacts };
324
+ }
325
+ return polyPoly(a, b);
326
+ }
327
+ class Constraint {
328
+ constructor(o) {
329
+ this.type = o.type;
330
+ this.a = o.a;
331
+ this.b = o.b || null;
332
+ this.anchorA = v(...(o.anchorA || [0, 0]));
333
+ this.anchorB = v(...(o.anchorB || [0, 0]));
334
+ this.point = v(...(o.point || [o.a.position.x, o.a.position.y]));
335
+ this.stiffness = o.stiffness ?? 1;
336
+ this.damping = o.damping ?? 0.1;
337
+ this.label = o.label || o.type;
338
+ this.length = o.length ?? len(sub(this.worldB(), this.worldA()));
339
+ }
340
+ worldA() {
341
+ return add(this.a.position, rot(this.anchorA, Math.cos(this.a.angle), Math.sin(this.a.angle)));
342
+ }
343
+ worldB() {
344
+ return this.b ? add(this.b.position, rot(this.anchorB, Math.cos(this.b.angle), Math.sin(this.b.angle))) : this.point;
345
+ }
346
+ /** One velocity iteration (impulse along the constraint axis, with a position bias). */
347
+ solve(h) {
348
+ const a = this.a, b = this.b;
349
+ const pa = this.worldA(), pb = this.worldB();
350
+ const d = sub(pb, pa), dist = len(d);
351
+ if (dist < 1e-9 && this.length < 1e-9 && this.type === 'distance')
352
+ return;
353
+ const n = dist > 1e-9 ? mul(d, 1 / dist) : v(1, 0);
354
+ const ra = sub(pa, a.position), rb = b ? sub(pb, b.position) : v();
355
+ const va = add(a.velocity, crossSV(a.angularVelocity, ra));
356
+ const vb = b ? add(b.velocity, crossSV(b.angularVelocity, rb)) : v();
357
+ const vn = dot(sub(vb, va), n);
358
+ const C = dist - this.length;
359
+ const k = a.invMass + (b ? b.invMass : 0) + cross(ra, n) ** 2 * a.invInertia + (b ? cross(rb, n) ** 2 * b.invInertia : 0);
360
+ if (k <= 0)
361
+ return;
362
+ const beta = this.stiffness >= 1 ? 0.2 : this.stiffness * 0.2;
363
+ let lambda = -(vn * (this.stiffness >= 1 ? 1 : this.damping + this.stiffness) + (beta / h) * C) / k;
364
+ if (this.stiffness < 1)
365
+ lambda *= this.stiffness;
366
+ const j = mul(n, lambda);
367
+ a.applyImpulse(mul(j, -1), pa);
368
+ b?.applyImpulse(j, pb);
369
+ }
370
+ }
371
+ class World {
372
+ constructor(o = {}) {
373
+ this.bodies = [];
374
+ this.constraints = [];
375
+ /** Manifolds of the last step (incl. sensor overlaps, which have no response). */
376
+ this.contacts = [];
377
+ this.time = 0;
378
+ this.steps = 0;
379
+ this.acc = 0;
380
+ this.warm = new Map();
381
+ this.listeners = new Set();
382
+ this.stop = null;
383
+ this.gravity = v(...(o.gravity || [0, 980]));
384
+ this.iterations = o.iterations ?? 10;
385
+ this.fixedStep = o.step ?? 1 / 60;
386
+ this.sleeping = o.sleeping ?? true;
387
+ }
388
+ add(...items) {
389
+ for (const it of items)
390
+ it instanceof Body ? this.bodies.push(it) : this.constraints.push(it);
391
+ return items[0];
392
+ }
393
+ body(o) {
394
+ return this.add(new Body(o));
395
+ }
396
+ constraint(o) {
397
+ return this.add(new Constraint(o));
398
+ }
399
+ remove(...items) {
400
+ for (const it of items) {
401
+ if (it instanceof Body) {
402
+ this.bodies = this.bodies.filter((b) => b !== it);
403
+ this.constraints = this.constraints.filter((c) => c.a !== it && c.b !== it);
404
+ }
405
+ else
406
+ this.constraints = this.constraints.filter((c) => c !== it);
407
+ }
408
+ }
409
+ clear() {
410
+ this.bodies = [];
411
+ this.constraints = [];
412
+ this.contacts = [];
413
+ }
414
+ /** Static walls around a w × h box (thickness t outside it). */
415
+ bounds(w, h, t = 50, o = {}) {
416
+ const walls = [
417
+ new Body({ type: 'static', x: w / 2, y: h + t / 2, width: w + 2 * t, height: t, label: 'floor' }),
418
+ new Body({ type: 'static', x: -t / 2, y: h / 2, width: t, height: h + 2 * t, label: 'wall-left' }),
419
+ new Body({ type: 'static', x: w + t / 2, y: h / 2, width: t, height: h + 2 * t, label: 'wall-right' }),
420
+ ];
421
+ if (o.top !== false)
422
+ walls.push(new Body({ type: 'static', x: w / 2, y: -t / 2, width: w + 2 * t, height: t, label: 'ceiling' }));
423
+ for (const b of walls)
424
+ this.add(b);
425
+ return walls;
426
+ }
427
+ onCollision(fn) {
428
+ this.listeners.add(fn);
429
+ return () => this.listeners.delete(fn);
430
+ }
431
+ /** Bodies under a world point (topmost = last added first). */
432
+ query(x, y) {
433
+ return this.bodies.filter((b) => b.contains(x, y)).reverse();
434
+ }
435
+ /** Advance by `seconds` of real time in fixed steps (max 8 steps per call). */
436
+ update(seconds) {
437
+ this.acc = Math.min(this.acc + seconds, this.fixedStep * 8);
438
+ let n = 0;
439
+ while (this.acc >= this.fixedStep) {
440
+ this.step(this.fixedStep);
441
+ this.acc -= this.fixedStep;
442
+ n++;
443
+ }
444
+ return n;
445
+ }
446
+ /** One fixed step of h seconds. */
447
+ step(h = this.fixedStep) {
448
+ const bodies = this.bodies;
449
+ // integrate forces
450
+ for (const b of bodies) {
451
+ if (b.type !== 'dynamic' || b.sleeping)
452
+ continue;
453
+ b.velocity = add(b.velocity, mul(add(this.gravity, mul(b.force, b.invMass)), h));
454
+ b.angularVelocity += b.torque * b.invInertia * h;
455
+ if (b.damping)
456
+ b.velocity = mul(b.velocity, Math.max(0, 1 - b.damping * h));
457
+ if (b.angularDamping)
458
+ b.angularVelocity *= Math.max(0, 1 - b.angularDamping * h);
459
+ }
460
+ // broad phase: sort and sweep on x
461
+ for (const b of bodies)
462
+ b.updateAabb();
463
+ const sorted = bodies.slice().sort((p, q) => p.aabb.minX - q.aabb.minX);
464
+ const manifolds = [];
465
+ for (let i = 0; i < sorted.length; i++) {
466
+ const a = sorted[i];
467
+ for (let j = i + 1; j < sorted.length; j++) {
468
+ const b = sorted[j];
469
+ if (b.aabb.minX > a.aabb.maxX)
470
+ break;
471
+ if (b.aabb.minY > a.aabb.maxY || b.aabb.maxY < a.aabb.minY)
472
+ continue;
473
+ if (a.type !== 'dynamic' && b.type !== 'dynamic')
474
+ continue;
475
+ if (!(a.category & b.mask) || !(b.category & a.mask))
476
+ continue;
477
+ if ((a.sleeping || a.type !== 'dynamic') && (b.sleeping || b.type !== 'dynamic'))
478
+ continue;
479
+ const m = a.id < b.id ? collide(a, b) : collide(b, a);
480
+ if (!m)
481
+ continue;
482
+ manifolds.push(m);
483
+ }
484
+ }
485
+ this.contacts = manifolds;
486
+ const solid = manifolds.filter((m) => !m.a.sensor && !m.b.sensor);
487
+ for (const m of solid) {
488
+ // wake a sleeper hit by a moving body
489
+ // wake a sleeper only when something hits it with some speed (resting neighbours do not keep a pile awake)
490
+ if (m.a.sleeping && !m.b.sleeping && m.b.type === 'dynamic' && motion(m.b) > SLEEP_MOTION * 4)
491
+ m.a.wake();
492
+ if (m.b.sleeping && !m.a.sleeping && m.a.type === 'dynamic' && motion(m.a) > SLEEP_MOTION * 4)
493
+ m.b.wake();
494
+ }
495
+ // velocity iterations
496
+ const key = (m) => m.a.id + ':' + m.b.id;
497
+ for (const m of solid)
498
+ prepare(m, h, this.warm.get(key(m)));
499
+ for (let it = 0; it < this.iterations; it++) {
500
+ for (const m of solid)
501
+ resolve(m);
502
+ for (const c of this.constraints)
503
+ c.solve(h);
504
+ }
505
+ this.warm = new Map(solid.map((m) => [key(m), { pts: m.contacts.map((c) => c.point), pn: m.contacts.map((c) => c.pn), pt: m.contacts.map((c) => c.pt) }]));
506
+ // integrate positions
507
+ for (const b of bodies) {
508
+ if (b.type === 'static' || b.sleeping)
509
+ continue;
510
+ b.position = add(b.position, mul(b.velocity, h));
511
+ b.angle += b.angularVelocity * h;
512
+ b.force = v();
513
+ b.torque = 0;
514
+ }
515
+ // sleeping, per island (bodies linked by contacts / constraints sleep and wake together)
516
+ if (this.sleeping) {
517
+ const parent = new Map();
518
+ const find = (x) => {
519
+ let r = x;
520
+ while (parent.get(r) !== r)
521
+ r = parent.get(r);
522
+ parent.set(x, r);
523
+ return r;
524
+ };
525
+ const dyn = (x) => !!x && x.type === 'dynamic';
526
+ for (const b of bodies)
527
+ if (dyn(b))
528
+ parent.set(b, b);
529
+ const link = (x, y) => {
530
+ if (dyn(x) && dyn(y))
531
+ parent.set(find(x), find(y));
532
+ };
533
+ for (const m of solid)
534
+ link(m.a, m.b);
535
+ for (const c of this.constraints)
536
+ link(c.a, c.b);
537
+ const dragged = new Set(this.constraints.filter((c) => c.label === 'drag').map((c) => c.a));
538
+ for (const b of parent.keys()) {
539
+ if (b.sleeping)
540
+ continue;
541
+ if (motion(b) < SLEEP_MOTION && !dragged.has(b))
542
+ b.sleepTime += h;
543
+ else
544
+ b.sleepTime = 0;
545
+ }
546
+ const islands = new Map();
547
+ for (const b of parent.keys()) {
548
+ const r = find(b);
549
+ islands.set(r, [...(islands.get(r) || []), b]);
550
+ }
551
+ for (const isl of islands.values()) {
552
+ if (isl.some((b) => !b.sleeping && b.sleepTime <= 0.6)) {
553
+ // anything awake and moving keeps (or makes) the whole island awake
554
+ if (isl.some((b) => b.sleeping) && isl.some((b) => !b.sleeping && b.sleepTime === 0))
555
+ for (const b of isl)
556
+ b.wake();
557
+ continue;
558
+ }
559
+ for (const b of isl) {
560
+ b.sleeping = true;
561
+ b.velocity = v();
562
+ b.angularVelocity = 0;
563
+ }
564
+ }
565
+ }
566
+ this.time += h;
567
+ this.steps++;
568
+ if (this.listeners.size)
569
+ for (const m of manifolds)
570
+ for (const fn of this.listeners)
571
+ fn(m);
572
+ }
573
+ /** Drive the world from the shared runtime ticker (needs the core). Returns a stopper. */
574
+ run() {
575
+ if (this.stop)
576
+ return this.stop;
577
+ const off = requireModule('core', 'motionary/runtime/physics').getTicker().add((_, dt) => this.update(Math.min(dt, 100) / 1000));
578
+ this.stop = () => {
579
+ off();
580
+ this.stop = null;
581
+ };
582
+ return this.stop;
583
+ }
584
+ get running() {
585
+ return !!this.stop;
586
+ }
587
+ /** Total kinetic energy (handy for tests / "settled" checks). */
588
+ energy() {
589
+ return this.bodies.reduce((e, b) => e + (b.type === 'dynamic' ? 0.5 * b.mass * dot(b.velocity, b.velocity) + 0.5 * (isFinite(b.inertia) ? b.inertia : 0) * b.angularVelocity ** 2 : 0), 0);
590
+ }
591
+ }
592
+ function prepare(m, h, warm) {
593
+ const { a, b, normal: n } = m;
594
+ const t = v(n.y, -n.x);
595
+ const e = Math.max(a.restitution, b.restitution);
596
+ for (const c of m.contacts) {
597
+ c.ra = sub(c.point, a.position);
598
+ c.rb = sub(c.point, b.position);
599
+ const kn = iM(a) + iM(b) + cross(c.ra, n) ** 2 * iI(a) + cross(c.rb, n) ** 2 * iI(b);
600
+ const kt = iM(a) + iM(b) + cross(c.ra, t) ** 2 * iI(a) + cross(c.rb, t) ** 2 * iI(b);
601
+ c.mn = kn > 0 ? 1 / kn : 0;
602
+ c.mt = kt > 0 ? 1 / kt : 0;
603
+ const rv = sub(add(b.velocity, crossSV(b.angularVelocity, c.rb)), add(a.velocity, crossSV(a.angularVelocity, c.ra)));
604
+ const vn = dot(rv, n);
605
+ // penetrating: push out (Baumgarte); just apart: allow closing the gap this step but not more
606
+ c.bias = c.depth < 0 ? c.depth / h : Math.max((0.2 / h) * Math.max(0, c.depth - 0.25), vn < -150 ? -e * vn : 0);
607
+ c.pn = 0;
608
+ c.pt = 0;
609
+ }
610
+ // warm start: reuse last step's impulses for contacts that stayed put (stacks settle instead of creeping)
611
+ if (warm) {
612
+ m.contacts.forEach((c) => {
613
+ if (c.depth < 0)
614
+ return;
615
+ // match by position (the clipper may list the two points in either order)
616
+ let i = -1, best = 2;
617
+ warm.pts.forEach((q, k) => {
618
+ const d = len(sub(c.point, q));
619
+ if (d < best)
620
+ (best = d), (i = k);
621
+ });
622
+ if (i < 0)
623
+ return;
624
+ c.pn = warm.pn[i];
625
+ c.pt = warm.pt[i];
626
+ applyPair(a, b, add(mul(n, c.pn), mul(t, c.pt)), c.ra, c.rb);
627
+ });
628
+ }
629
+ }
630
+ /** One velocity iteration with accumulated, clamped impulses: friction first (|friction| ≤ μ·normal), then the
631
+ * normal impulses (≥ 0) — two-point manifolds are solved as one 2 × 2 block (a tiny LCP), which keeps boxes resting
632
+ * on boxes from rocking from corner to corner. */
633
+ function resolve(m) {
634
+ const { a, b, normal: n } = m;
635
+ const t = v(n.y, -n.x);
636
+ const mu = Math.sqrt(a.friction * b.friction);
637
+ const cs = m.contacts;
638
+ const rel = (c) => sub(add(b.velocity, crossSV(b.angularVelocity, c.rb)), add(a.velocity, crossSV(a.angularVelocity, c.ra)));
639
+ for (const c of cs) {
640
+ let dt = -c.mt * dot(rel(c), t);
641
+ const t0 = c.pt, max = mu * c.pn;
642
+ c.pt = Math.max(-max, Math.min(max, t0 + dt));
643
+ dt = c.pt - t0;
644
+ applyPair(a, b, mul(t, dt), c.ra, c.rb);
645
+ }
646
+ if (cs.length === 2) {
647
+ const [c1, c2] = cs;
648
+ const im = iM, ii = iI;
649
+ const rn1a = cross(c1.ra, n), rn1b = cross(c1.rb, n), rn2a = cross(c2.ra, n), rn2b = cross(c2.rb, n);
650
+ const k11 = im(a) + im(b) + ii(a) * rn1a * rn1a + ii(b) * rn1b * rn1b;
651
+ const k22 = im(a) + im(b) + ii(a) * rn2a * rn2a + ii(b) * rn2b * rn2b;
652
+ const k12 = im(a) + im(b) + ii(a) * rn1a * rn2a + ii(b) * rn1b * rn2b;
653
+ const det = k11 * k22 - k12 * k12;
654
+ if (k11 * k11 < 1000 * det) {
655
+ const a1 = c1.pn, a2 = c2.pn;
656
+ const b1 = dot(rel(c1), n) - c1.bias - (k11 * a1 + k12 * a2);
657
+ const b2 = dot(rel(c2), n) - c2.bias - (k12 * a1 + k22 * a2);
658
+ let x1 = 0, x2 = 0;
659
+ const sol = () => {
660
+ // 1. both active
661
+ x1 = -(k22 * b1 - k12 * b2) / det;
662
+ x2 = -(k11 * b2 - k12 * b1) / det;
663
+ if (x1 >= 0 && x2 >= 0)
664
+ return true;
665
+ // 2. only the first
666
+ x1 = -b1 / k11;
667
+ x2 = 0;
668
+ if (x1 >= 0 && k12 * x1 + b2 >= 0)
669
+ return true;
670
+ // 3. only the second
671
+ x1 = 0;
672
+ x2 = -b2 / k22;
673
+ if (x2 >= 0 && k12 * x2 + b1 >= 0)
674
+ return true;
675
+ // 4. none
676
+ x1 = x2 = 0;
677
+ return b1 >= 0 && b2 >= 0;
678
+ };
679
+ if (sol()) {
680
+ applyPair(a, b, mul(n, x1 - a1), c1.ra, c1.rb);
681
+ applyPair(a, b, mul(n, x2 - a2), c2.ra, c2.rb);
682
+ c1.pn = x1;
683
+ c2.pn = x2;
684
+ return;
685
+ }
686
+ }
687
+ }
688
+ for (const c of cs) {
689
+ let d = c.mn * (-dot(rel(c), n) + c.bias);
690
+ const p0 = c.pn;
691
+ c.pn = Math.max(p0 + d, 0);
692
+ d = c.pn - p0;
693
+ applyPair(a, b, mul(n, d), c.ra, c.rb);
694
+ }
695
+ }
696
+ function applyPair(a, b, J, ra, rb) {
697
+ if (a.type === 'dynamic' && !a.sleeping) {
698
+ a.velocity = sub(a.velocity, mul(J, a.invMass));
699
+ a.angularVelocity -= cross(ra, J) * a.invInertia;
700
+ }
701
+ if (b.type === 'dynamic' && !b.sleeping) {
702
+ b.velocity = add(b.velocity, mul(J, b.invMass));
703
+ b.angularVelocity += cross(rb, J) * b.invInertia;
704
+ }
705
+ }
706
+ /** A world (also registers nothing — pure). */
707
+ function createWorld(o = {}) {
708
+ return new World(o);
709
+ }
710
+ /** Pointer dragging: a soft pin from a body point to a moving world point. */
711
+ function dragConstraint(world, body, x, y, stiffness = 0.6) {
712
+ const c = body.angle;
713
+ const local = rot(sub(v(x, y), body.position), Math.cos(-c), Math.sin(-c));
714
+ const k = world.constraint({ type: 'pin', a: body, anchorA: [local.x, local.y], point: [x, y], length: 0, stiffness, damping: 0.3, label: 'drag' });
715
+ body.wake();
716
+ return {
717
+ constraint: k,
718
+ move(px, py) {
719
+ k.point = v(px, py);
720
+ body.wake();
721
+ },
722
+ release() {
723
+ world.remove(k);
724
+ },
725
+ };
726
+ }
727
+ /** The module object for `use(physics)`. */
728
+ const physics = { id: 'physics', version: RUNTIME_VERSION, requires: ['core'], api: { createWorld, World, Body, Constraint, collide, dragConstraint } };
729
+
730
+ export { Body, Constraint, World, collide, createWorld, dragConstraint, physics };
731
+ //# sourceMappingURL=physics.js.map