planck-v2 2.0.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.

Potentially problematic release.


This version of planck-v2 might be problematic. Click here for more details.

Files changed (80) hide show
  1. package/LICENSE.txt +20 -0
  2. package/README.md +21 -0
  3. package/dist/planck-with-testbed.d.ts +4433 -0
  4. package/dist/planck-with-testbed.js +20730 -0
  5. package/dist/planck-with-testbed.js.map +1 -0
  6. package/dist/planck-with-testbed.umd.cjs +20730 -0
  7. package/dist/planck-with-testbed.umd.cjs.map +1 -0
  8. package/dist/planck.d.ts +4343 -0
  9. package/dist/planck.js +13516 -0
  10. package/dist/planck.js.map +1 -0
  11. package/dist/planck.umd.cjs +13516 -0
  12. package/dist/planck.umd.cjs.map +1 -0
  13. package/package.json +105 -0
  14. package/src/Settings.ts +238 -0
  15. package/src/__test__/Basic.test.ts +43 -0
  16. package/src/__test__/CCD.test.ts +70 -0
  17. package/src/__test__/Collision.test.ts +133 -0
  18. package/src/__test__/Math.test.ts +105 -0
  19. package/src/__test__/Pool.test.ts +48 -0
  20. package/src/__test__/World.test.ts +73 -0
  21. package/src/collision/AABB.ts +287 -0
  22. package/src/collision/BroadPhase.ts +210 -0
  23. package/src/collision/Distance.ts +962 -0
  24. package/src/collision/DynamicTree.ts +907 -0
  25. package/src/collision/Manifold.ts +420 -0
  26. package/src/collision/Raycast.ts +30 -0
  27. package/src/collision/Shape.ts +114 -0
  28. package/src/collision/TimeOfImpact.ts +502 -0
  29. package/src/collision/shape/BoxShape.ts +34 -0
  30. package/src/collision/shape/ChainShape.ts +360 -0
  31. package/src/collision/shape/CircleShape.ts +202 -0
  32. package/src/collision/shape/CollideCircle.ts +66 -0
  33. package/src/collision/shape/CollideCirclePolygon.ts +142 -0
  34. package/src/collision/shape/CollideEdgeCircle.ts +185 -0
  35. package/src/collision/shape/CollideEdgePolygon.ts +528 -0
  36. package/src/collision/shape/CollidePolygon.ts +280 -0
  37. package/src/collision/shape/EdgeShape.ts +316 -0
  38. package/src/collision/shape/PolygonShape.ts +581 -0
  39. package/src/common/Geo.ts +589 -0
  40. package/src/common/Jacobian.ts +17 -0
  41. package/src/common/Mat22.ts +221 -0
  42. package/src/common/Mat33.ts +224 -0
  43. package/src/common/Math.ts +96 -0
  44. package/src/common/Rot.ts +218 -0
  45. package/src/common/Sweep.ts +119 -0
  46. package/src/common/Transform.ts +203 -0
  47. package/src/common/Vec2.ts +624 -0
  48. package/src/common/Vec3.ts +188 -0
  49. package/src/dynamics/Body.ts +1198 -0
  50. package/src/dynamics/Contact.ts +1366 -0
  51. package/src/dynamics/Fixture.ts +506 -0
  52. package/src/dynamics/Joint.ts +226 -0
  53. package/src/dynamics/Position.ts +44 -0
  54. package/src/dynamics/Solver.ts +890 -0
  55. package/src/dynamics/Velocity.ts +18 -0
  56. package/src/dynamics/World.ts +1169 -0
  57. package/src/dynamics/joint/DistanceJoint.ts +463 -0
  58. package/src/dynamics/joint/FrictionJoint.ts +396 -0
  59. package/src/dynamics/joint/GearJoint.ts +591 -0
  60. package/src/dynamics/joint/MotorJoint.ts +430 -0
  61. package/src/dynamics/joint/MouseJoint.ts +390 -0
  62. package/src/dynamics/joint/PrismaticJoint.ts +903 -0
  63. package/src/dynamics/joint/PulleyJoint.ts +529 -0
  64. package/src/dynamics/joint/RevoluteJoint.ts +745 -0
  65. package/src/dynamics/joint/RopeJoint.ts +383 -0
  66. package/src/dynamics/joint/WeldJoint.ts +544 -0
  67. package/src/dynamics/joint/WheelJoint.ts +683 -0
  68. package/src/dynamics/joint/__test__/DistanceJoint.test.ts +66 -0
  69. package/src/index.ts +60 -0
  70. package/src/internal.ts +20 -0
  71. package/src/main.ts +3 -0
  72. package/src/serializer/__test__/Serialize.test.ts +52 -0
  73. package/src/serializer/__test__/Validator.test.ts +55 -0
  74. package/src/serializer/index.ts +257 -0
  75. package/src/serializer/schema.json +168 -0
  76. package/src/util/Pool.ts +120 -0
  77. package/src/util/Testbed.ts +157 -0
  78. package/src/util/Timer.ts +15 -0
  79. package/src/util/options.ts +28 -0
  80. package/src/util/stats.ts +26 -0
@@ -0,0 +1,589 @@
1
+ /*
2
+ * Planck.js
3
+ *
4
+ * Copyright (c) Ali Shakiba
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+
10
+ import type { RotValue } from "./Rot";
11
+ import type { TransformValue } from "./Transform";
12
+ import type { Vec2Value } from "./Vec2";
13
+ import type { Vec3Value } from "./Vec3";
14
+ import type { Mat22Value } from "./Mat22";
15
+ import type { Mat33Value } from "./Mat33";
16
+
17
+ /** Return a new Vec2 object with the given x and y. */
18
+ export function vec2(inx: number, iny: number): Vec2Value {
19
+ return { x: inx, y: iny };
20
+ }
21
+
22
+ /** Return a new Vec3 object with the given x and y. */
23
+ export function vec3(inx: number, iny: number, inz: number): Vec3Value {
24
+ return { x: inx, y: iny, z: inz };
25
+ }
26
+
27
+ /** Return a new Rot object with the given angle. */
28
+ export function rotation(angle: number): RotValue {
29
+ return { s: Math.sin(angle), c: Math.cos(angle) };
30
+ }
31
+
32
+ /** Assigns the given x and y to the out Vec2Value. */
33
+ export function setVec2(out: Vec2Value, inx: number, iny: number): void {
34
+ out.x = inx;
35
+ out.y = iny;
36
+ }
37
+
38
+ /** Assigns the given x, y and z to the out Vec3Value. */
39
+ export function setVec3(out: Vec3Value, x: number, y: number, z: number): void {
40
+ out.x = x;
41
+ out.y = y;
42
+ out.z = z;
43
+ }
44
+
45
+ /** Copies the x and y from the w to the out. */
46
+ export function copyVec2(out: Vec2Value, w: Vec2Value): void {
47
+ out.x = w.x;
48
+ out.y = w.y;
49
+ }
50
+
51
+ /** Copies the x, y, z from the w to the out. */
52
+ export function copyVec3(out: Vec2Value, w: Vec2Value): void {
53
+ out.x = w.x;
54
+ out.y = w.y;
55
+ }
56
+
57
+ /** Assigns zero to the x and y of the out. */
58
+ export function zeroVec2(out: Vec2Value): void {
59
+ out.x = 0;
60
+ out.y = 0;
61
+ }
62
+
63
+ /** Assigns zero to the x, y and z of the out. */
64
+ export function zeroVec3(out: Vec3Value): void {
65
+ out.x = 0;
66
+ out.y = 0;
67
+ out.z = 0;
68
+ }
69
+
70
+ export function negVec2(out: Vec2Value): void {
71
+ out.x = -out.x;
72
+ out.y = -out.y;
73
+ }
74
+
75
+ export function negVec3(out: Vec3Value): void {
76
+ out.x = -out.x;
77
+ out.y = -out.y;
78
+ out.z = -out.z;
79
+ }
80
+
81
+ /** Adds w to out, equivalent to out += w, or out.add(w) */
82
+ export function plusVec2(out: Vec2Value, w: Vec2Value): void {
83
+ out.x += w.x;
84
+ out.y += w.y;
85
+ }
86
+
87
+ /** Adds w to out, equivalent to out += w, or out.add(w) */
88
+ export function plusVec3(out: Vec3Value, w: Vec3Value): void {
89
+ out.x += w.x;
90
+ out.y += w.y;
91
+ out.z += w.z;
92
+ }
93
+
94
+ /** Adds w and v and assigns the result to out, equivalent to out = v + w */
95
+ export function addVec2(out: Vec2Value, v: Vec2Value, w: Vec2Value): void {
96
+ out.x = v.x + w.x;
97
+ out.y = v.x + w.y;
98
+ }
99
+
100
+ export function clampVec2(out: Vec2Value, max: number): void {
101
+ const lengthSqr = out.x * out.x + out.y * out.y;
102
+ if (lengthSqr > max * max) {
103
+ const scale = max / Math.sqrt(lengthSqr);
104
+ out.x *= scale;
105
+ out.y *= scale;
106
+ }
107
+ }
108
+
109
+ /**
110
+ * Subtract w from out and assign the result to out.
111
+ * This is equivalent to out = out - w, or out.sub(w).
112
+ */
113
+ export function minusVec2(out: Vec2Value, w: Vec2Value): void {
114
+ out.x -= w.x;
115
+ out.y -= w.y;
116
+ }
117
+
118
+ export function minusCrossNumVec2(out: Vec2Value, w: number, v: Vec2Value): void {
119
+ out.x -= -w * v.y;
120
+ out.y -= w * v.x;
121
+ }
122
+
123
+ export function plusCrossNumVec2(out: Vec2Value, w: number, v: Vec2Value): void {
124
+ out.x += -w * v.y;
125
+ out.y += w * v.x;
126
+ }
127
+
128
+ export function dotCrossNumVec2(i: Vec2Value, w: number, v: Vec2Value): number {
129
+ return i.x * (-w * v.y) + i.y * (w * v.x);
130
+ }
131
+
132
+ export function dotSubVec2(a: Vec2Value, b: Vec2Value, c: Vec2Value): number {
133
+ return (a.x - b.x) * c.x + (a.y - b.y) * c.y;
134
+ }
135
+
136
+ /** Subtracts w from v and assigns the result to out, equivalent to out = v - w */
137
+ export function subVec2(out: Vec2Value, v: Vec2Value, w: Vec2Value): void {
138
+ out.x = v.x - w.x;
139
+ out.y = v.y - w.y;
140
+ }
141
+
142
+ export function absVec2(out: Vec2Value, v: Vec2Value): void {
143
+ out.x = Math.abs(v.x);
144
+ out.y = Math.abs(v.y);
145
+ }
146
+
147
+ /** Multiplies out by m, equivalent to out *= m, or out.mul(m) */
148
+ export function mulVec2(out: Vec2Value, m: number): void {
149
+ out.x *= m;
150
+ out.y *= m;
151
+ }
152
+
153
+ /** Multiplies out by m, equivalent to out *= m, or out.mul(m) */
154
+ export function mulVec3(out: Vec3Value, m: number): void {
155
+ out.x *= m;
156
+ out.y *= m;
157
+ out.z *= m;
158
+ }
159
+
160
+ /** Multiplies w by m and assigns the result to out, equivalent to out = m * w */
161
+ export function scaleVec2(out: Vec2Value, m: number, w: Vec2Value): void {
162
+ out.x = m * w.x;
163
+ out.y = m * w.y;
164
+ }
165
+
166
+ /** Multiplies w by m and adds the result to out, equivalent to out += m * w */
167
+ export function plusScaleVec2(out: Vec2Value, m: number, w: Vec2Value): void {
168
+ out.x += m * w.x;
169
+ out.y += m * w.y;
170
+ }
171
+
172
+ /** Multiplies w by m and subtracts the result from out, equivalent to out -= m * w */
173
+ export function minusScaleVec2(out: Vec2Value, m: number, w: Vec2Value): void {
174
+ const ox = m * w.x;
175
+ const oy = m * w.y;
176
+ out.x -= ox;
177
+ out.y -= oy;
178
+ }
179
+
180
+ export function crossSubVec2Num(out: Vec2Value, a: Vec2Value, b: Vec2Value, w: number): void {
181
+ // we need temporary variables, because out might be an input
182
+ const ox = w * (a.y - b.y);
183
+ const oy = -w * (a.x - b.x);
184
+ out.x = ox;
185
+ out.y = oy;
186
+ }
187
+
188
+ export function combine2Vec2(out: Vec2Value, am: number, a: Vec2Value, bm: number, b: Vec2Value): void {
189
+ const ox = am * a.x + bm * b.x;
190
+ const oy = am * a.y + bm * b.y;
191
+ out.x = ox;
192
+ out.y = oy;
193
+ }
194
+
195
+ export function combine3Vec2(
196
+ out: Vec2Value,
197
+ am: number,
198
+ a: Vec2Value,
199
+ bm: number,
200
+ b: Vec2Value,
201
+ cm: number,
202
+ c: Vec2Value,
203
+ ): void {
204
+ const ox = am * a.x + bm * b.x + cm * c.x;
205
+ const oy = am * a.y + bm * b.y + cm * c.y;
206
+ out.x = ox;
207
+ out.y = oy;
208
+ }
209
+
210
+ export function combine4Vec2(
211
+ out: Vec2Value,
212
+ am: number,
213
+ a: Vec2Value,
214
+ bm: number,
215
+ b: Vec2Value,
216
+ cm: number,
217
+ c: Vec2Value,
218
+ dm: number,
219
+ d: Vec2Value,
220
+ ): void {
221
+ const ox = am * a.x + bm * b.x + cm * c.x + dm * d.x;
222
+ const oy = am * a.y + bm * b.y + cm * c.y + dm * d.y;
223
+ out.x = ox;
224
+ out.y = oy;
225
+ }
226
+
227
+ export function normalizeVec2Length(out: Vec2Value): number {
228
+ const length = Math.sqrt(out.x * out.x + out.y * out.y);
229
+ if (length !== 0) {
230
+ const invLength = 1 / length;
231
+ out.x *= invLength;
232
+ out.y *= invLength;
233
+ }
234
+ return length;
235
+ }
236
+
237
+ export function normalizeVec2(out: Vec2Value): void {
238
+ const length = Math.sqrt(out.x * out.x + out.y * out.y);
239
+ if (length > 0) {
240
+ const invLength = 1 / length;
241
+ out.x *= invLength;
242
+ out.y *= invLength;
243
+ }
244
+ }
245
+
246
+ export function crossVec2Num(out: Vec2Value, v: Vec2Value, w: number): void {
247
+ // we need temporary variables, because out might be an input
248
+ const ox = w * v.y;
249
+ const oy = -w * v.x;
250
+ out.x = ox;
251
+ out.y = oy;
252
+ }
253
+
254
+ export function crossNumVec2(out: Vec2Value, w: number, v: Vec2Value): void {
255
+ // we need temporary variables, because out might be an input
256
+ const ox = -w * v.y;
257
+ const oy = w * v.x;
258
+ out.x = ox;
259
+ out.y = oy;
260
+ }
261
+
262
+ export function crossVec2Vec2(a: Vec2Value, b: Vec2Value): number {
263
+ return a.x * b.y - a.y * b.x;
264
+ }
265
+
266
+ export function dotVec2(a: Vec2Value, b: Vec2Value): number {
267
+ return a.x * b.x + a.y * b.y;
268
+ }
269
+
270
+ export function lengthVec2(a: Vec2Value): number {
271
+ return Math.sqrt(a.x * a.x + a.y * a.y);
272
+ }
273
+
274
+ export function lengthSqrVec2(a: Vec2Value): number {
275
+ return a.x * a.x + a.y * a.y;
276
+ }
277
+
278
+ export function distVec2(a: Vec2Value, b: Vec2Value): number {
279
+ const dx = a.x - b.x;
280
+ const dy = a.y - b.y;
281
+ return Math.sqrt(dx * dx + dy * dy);
282
+ }
283
+
284
+ export function distSqrVec2(a: Vec2Value, b: Vec2Value): number {
285
+ const dx = a.x - b.x;
286
+ const dy = a.y - b.y;
287
+ return dx * dx + dy * dy;
288
+ }
289
+
290
+ export function dotVec3(v: Vec3Value, w: Vec3Value): number {
291
+ return v.x * w.x + v.y * w.y + v.z * w.z;
292
+ }
293
+
294
+ export function setRotAngle(out: RotValue, a: number): void {
295
+ out.c = Math.cos(a);
296
+ out.s = Math.sin(a);
297
+ }
298
+
299
+ export function getRotAngle(out: RotValue): number {
300
+ return Math.atan2(out.s, out.c);
301
+ }
302
+
303
+ export function rotVec2(out: Vec2Value, q: RotValue, v: Vec2Value): void {
304
+ const ox = q.c * v.x - q.s * v.y;
305
+ const oy = q.s * v.x + q.c * v.y;
306
+ out.x = ox;
307
+ out.y = oy;
308
+ }
309
+
310
+ export function rotSubVec2(out: Vec2Value, q: RotValue, v: Vec2Value, w: Vec2Value): void {
311
+ const ox = q.c * (v.x - w.x) - q.s * (v.y - w.y);
312
+ const oy = q.s * (v.x - w.x) + q.c * (v.y - w.y);
313
+ out.x = ox;
314
+ out.y = oy;
315
+ }
316
+
317
+ export function rotNegVec2(out: Vec2Value, q: RotValue, w: Vec2Value): void {
318
+ const ox = q.c * -w.x - q.s * -w.y;
319
+ const oy = q.s * -w.x + q.c * -w.y;
320
+ out.x = ox;
321
+ out.y = oy;
322
+ }
323
+
324
+ export function derotVec2(out: Vec2Value, q: RotValue, v: Vec2Value): void {
325
+ const ox = q.c * v.x + q.s * v.y;
326
+ const oy = -q.s * v.x + q.c * v.y;
327
+ out.x = ox;
328
+ out.y = oy;
329
+ }
330
+
331
+ export function derotNegVec2(out: Vec2Value, q: RotValue, v: Vec2Value): void {
332
+ const ox = q.c * -v.x + q.s * -v.y;
333
+ const oy = -q.s * -v.x + q.c * -v.y;
334
+ out.x = ox;
335
+ out.y = oy;
336
+ }
337
+
338
+ export function rerotVec2(out: Vec2Value, before: RotValue, after: RotValue, v: Vec2Value): void {
339
+ const x0 = before.c * v.x + before.s * v.y;
340
+ const y0 = -before.s * v.x + before.c * v.y;
341
+ const ox = after.c * x0 - after.s * y0;
342
+ const oy = after.s * x0 + after.c * y0;
343
+ out.x = ox;
344
+ out.y = oy;
345
+ }
346
+
347
+ export function transform(inx: number, iny: number, ina: number): TransformValue {
348
+ return {
349
+ p: { x: inx, y: iny },
350
+ q: { s: Math.sin(ina), c: Math.cos(ina) },
351
+ };
352
+ }
353
+
354
+ export function setTransform(xf: TransformValue, inx: number, iny: number, ina: number): void {
355
+ xf.p.x = inx;
356
+ xf.p.y = iny;
357
+ xf.q.s = Math.sin(ina);
358
+ xf.q.c = Math.cos(ina);
359
+ }
360
+
361
+ export function copyTransform(out: TransformValue, transform: TransformValue): void {
362
+ out.p.x = transform.p.x;
363
+ out.p.y = transform.p.y;
364
+ out.q.s = transform.q.s;
365
+ out.q.c = transform.q.c;
366
+ }
367
+
368
+ export function transformVec2(out: Vec2Value, xf: TransformValue, v: Vec2Value): void {
369
+ const ox = xf.q.c * v.x - xf.q.s * v.y + xf.p.x;
370
+ const oy = xf.q.s * v.x + xf.q.c * v.y + xf.p.y;
371
+ out.x = ox;
372
+ out.y = oy;
373
+ }
374
+
375
+ export function detransformVec2(out: Vec2Value, xf: TransformValue, v: Vec2Value): void {
376
+ const px = v.x - xf.p.x;
377
+ const py = v.y - xf.p.y;
378
+ const ox = xf.q.c * px + xf.q.s * py;
379
+ const oy = -xf.q.s * px + xf.q.c * py;
380
+ out.x = ox;
381
+ out.y = oy;
382
+ }
383
+
384
+ export function retransformVec2(out: Vec2Value, from: TransformValue, to: TransformValue, v: Vec2Value): void {
385
+ const x0 = from.q.c * v.x - from.q.s * v.y + from.p.x;
386
+ const y0 = from.q.s * v.x + from.q.c * v.y + from.p.y;
387
+ const px = x0 - to.p.x;
388
+ const py = y0 - to.p.y;
389
+ const ox = to.q.c * px + to.q.s * py;
390
+ const oy = -to.q.s * px + to.q.c * py;
391
+ out.x = ox;
392
+ out.y = oy;
393
+ }
394
+
395
+ export function detransformTransform(out: TransformValue, a: TransformValue, b: TransformValue): void {
396
+ const c = a.q.c * b.q.c + a.q.s * b.q.s;
397
+ const s = a.q.c * b.q.s - a.q.s * b.q.c;
398
+ const ox = a.q.c * (b.p.x - a.p.x) + a.q.s * (b.p.y - a.p.y);
399
+ const oy = -a.q.s * (b.p.x - a.p.x) + a.q.c * (b.p.y - a.p.y);
400
+ out.q.c = c;
401
+ out.q.s = s;
402
+ out.p.x = ox;
403
+ out.p.y = oy;
404
+ }
405
+
406
+ export function mat22(): Mat22Value {
407
+ return {
408
+ ex: { x: 0, y: 0 },
409
+ ey: { x: 0, y: 0 },
410
+ };
411
+ }
412
+
413
+ export function mat33() {
414
+ return {
415
+ ex: { x: 0, y: 0, z: 0 },
416
+ ey: { x: 0, y: 0, z: 0 },
417
+ ez: { x: 0, y: 0, z: 0 },
418
+ };
419
+ }
420
+
421
+ export function zeroMat22(out: Mat22Value): void {
422
+ out.ex.x = 0;
423
+ out.ex.y = 0;
424
+ out.ey.x = 0;
425
+ out.ey.y = 0;
426
+ }
427
+
428
+ export function zeroMat33(out: Mat33Value): void {
429
+ out.ex.x = 0;
430
+ out.ex.y = 0;
431
+ out.ex.z = 0;
432
+ out.ey.x = 0;
433
+ out.ey.y = 0;
434
+ out.ey.z = 0;
435
+ out.ez.x = 0;
436
+ out.ez.y = 0;
437
+ out.ez.z = 0;
438
+ }
439
+
440
+ export function mulMat22Vec2(out: Vec2Value, m: Mat22Value, v: Vec2Value): void {
441
+ const ox = m.ex.x * v.x + m.ey.x * v.y;
442
+ const oy = m.ex.y * v.x + m.ey.y * v.y;
443
+ out.x = ox;
444
+ out.y = oy;
445
+ }
446
+
447
+ export function mulMat33Vec2(out: Vec2Value, m: Mat33Value, v: Vec2Value): void {
448
+ const ox = m.ex.x * v.x + m.ey.x * v.y;
449
+ const oy = m.ex.y * v.x + m.ey.y * v.y;
450
+ out.x = ox;
451
+ out.y = oy;
452
+ }
453
+
454
+ export function mulMat33Vec3(out: Vec3Value, m: Mat33Value, v: Vec3Value): void {
455
+ const ox = m.ex.x * v.x + m.ey.x * v.y + m.ez.x * v.z;
456
+ const oy = m.ex.y * v.x + m.ey.y * v.y + m.ez.y * v.z;
457
+ const oz = m.ex.z * v.x + m.ey.z * v.y + m.ez.z * v.z;
458
+ out.x = ox;
459
+ out.y = oy;
460
+ out.z = oz;
461
+ }
462
+
463
+ export function inverseMat22(out: Mat22Value, m: Mat22Value): void {
464
+ const a = m.ex.x;
465
+ const b = m.ey.x;
466
+ const c = m.ex.y;
467
+ const d = m.ey.y;
468
+ let det = a * d - b * c;
469
+ if (det !== 0) {
470
+ det = 1 / det;
471
+ }
472
+ out.ex.x = det * d;
473
+ out.ey.x = -det * b;
474
+ out.ex.y = -det * c;
475
+ out.ey.y = det * a;
476
+ }
477
+
478
+ /**
479
+ * Get the symmetric inverse of this matrix as a 3-by-3. Returns the zero matrix
480
+ * if singular.
481
+ */
482
+ export function symInverseMat33(out: Mat33Value, m: Mat33Value): void {
483
+ // let det = Vec3.dot(m.ex, Vec3.cross(m.ey, m.ez));
484
+
485
+ let det =
486
+ m.ex.x * (m.ey.y * m.ez.z - m.ey.z * m.ez.y) +
487
+ m.ex.y * (m.ey.z * m.ez.x - m.ey.x * m.ez.z) +
488
+ m.ex.z * (m.ey.x * m.ez.y - m.ey.y * m.ez.x);
489
+
490
+ if (det !== 0) {
491
+ det = 1 / det;
492
+ }
493
+ const a11 = m.ex.x;
494
+ const a12 = m.ey.x;
495
+ const a13 = m.ez.x;
496
+ const a22 = m.ey.y;
497
+ const a23 = m.ez.y;
498
+ const a33 = m.ez.z;
499
+
500
+ out.ex.x = det * (a22 * a33 - a23 * a23);
501
+ out.ex.y = det * (a13 * a23 - a12 * a33);
502
+ out.ex.z = det * (a12 * a23 - a13 * a22);
503
+
504
+ out.ey.x = out.ex.y;
505
+ out.ey.y = det * (a11 * a33 - a13 * a13);
506
+ out.ey.z = det * (a13 * a12 - a11 * a23);
507
+
508
+ out.ez.x = out.ex.z;
509
+ out.ez.y = out.ey.z;
510
+ out.ez.z = det * (a11 * a22 - a12 * a12);
511
+ }
512
+
513
+ /**
514
+ * Solve A * x = b, where b is a column vector. This is more efficient than
515
+ * computing the inverse in one-shot cases.
516
+ */
517
+ export function solveMat22Num(out: Vec2Value, m: Mat22Value, x: number, y: number): void {
518
+ const a11 = m.ex.x;
519
+ const a12 = m.ey.x;
520
+ const a21 = m.ex.y;
521
+ const a22 = m.ey.y;
522
+ let det = a11 * a22 - a12 * a21;
523
+ if (det !== 0) {
524
+ det = 1 / det;
525
+ }
526
+ out.x = det * (a22 * x - a12 * y);
527
+ out.y = det * (a11 * y - a21 * x);
528
+ }
529
+
530
+ /**
531
+ * Solve A * x = b, where b is a column vector. This is more efficient than
532
+ * computing the inverse in one-shot cases.
533
+ */
534
+ export function solveMat33Num(out: Vec3Value, m: Mat33Value, x: number, y: number, z: number): void {
535
+ // let det = matrix.dotVec3(this.ex, matrix.newCrossVec3(this.ey, this.ez));
536
+ let cross_x = m.ey.y * m.ez.z - m.ey.z * m.ez.y;
537
+ let cross_y = m.ey.z * m.ez.x - m.ey.x * m.ez.z;
538
+ let cross_z = m.ey.x * m.ez.y - m.ey.y * m.ez.x;
539
+ let det = m.ex.x * cross_x + m.ex.y * cross_y + m.ex.z * cross_z;
540
+ if (det !== 0) {
541
+ det = 1 / det;
542
+ }
543
+
544
+ // r.x = det * matrix.dotVec3(v, matrix.newCrossVec3(this.ey, this.ez));
545
+ cross_x = m.ey.y * m.ez.z - m.ey.z * m.ez.y;
546
+ cross_y = m.ey.z * m.ez.x - m.ey.x * m.ez.z;
547
+ cross_z = m.ey.x * m.ez.y - m.ey.y * m.ez.x;
548
+ out.x = det * (x * cross_x + y * cross_y + z * cross_z);
549
+
550
+ // r.y = det * matrix.dotVec3(this.ex, matrix.newCrossVec3(v, this.ez));
551
+ cross_x = y * m.ez.z - z * m.ez.y;
552
+ cross_y = z * m.ez.x - x * m.ez.z;
553
+ cross_z = x * m.ez.y - y * m.ez.x;
554
+ out.y = det * (m.ex.x * cross_x + m.ex.y * cross_y + m.ex.z * cross_z);
555
+
556
+ // r.z = det * matrix.dotVec3(this.ex, matrix.newCrossVec3(this.ey, v));
557
+ cross_x = m.ey.y * z - m.ey.z * y;
558
+ cross_y = m.ey.z * x - m.ey.x * z;
559
+ cross_z = m.ey.x * y - m.ey.y * x;
560
+ out.z = det * (m.ex.x * cross_x + m.ex.y * cross_y + m.ex.z * cross_z);
561
+ }
562
+
563
+ /** Velocity of point r on a body, given a body linear and angular velocity. */
564
+ export function vp(out: Vec2Value, v: Vec2Value, w: number, r: Vec2Value): void {
565
+ const ox = v.x - w * r.y;
566
+ const oy = v.y + w * r.x;
567
+ out.x = ox;
568
+ out.y = oy;
569
+ }
570
+
571
+ /** Relative velocity of two points of two bodies, given linear and angular velocity of the bodies . */
572
+ export function dvp(
573
+ out: Vec2Value,
574
+ vB: Vec2Value,
575
+ wB: number,
576
+ rB: Vec2Value,
577
+ vA: Vec2Value,
578
+ wA: number,
579
+ rA: Vec2Value,
580
+ ) {
581
+ const ox = vB.x - wB * rB.y - (vA.x - wA * rA.y);
582
+ const oy = vB.y + wB * rB.x - (vA.y + wA * rA.x);
583
+ out.x = ox;
584
+ out.y = oy;
585
+ }
586
+
587
+ export function isVec2(obj: any): boolean {
588
+ return typeof obj === "object" && obj !== null && Number.isFinite(obj.x) && Number.isFinite(obj.y);
589
+ }
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Planck.js
3
+ *
4
+ * Copyright (c) Erin Catto, Ali Shakiba
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+
10
+ import { Vec2Value } from "./Vec2";
11
+
12
+ /** @internal */
13
+ export class Jacobian {
14
+ linear: Vec2Value;
15
+ angularA: number;
16
+ angularB: number;
17
+ }