@benev/math 0.2.0-4 → 0.3.0-2
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/LICENSE +1 -1
- package/README.md +3 -3
- package/package.json +5 -5
- package/s/{tools → core}/circular.ts +1 -1
- package/s/core/quat.ts +12 -8
- package/s/{tools → core}/scalar.ts +2 -2
- package/s/core/vec2.ts +24 -20
- package/s/core/vec3.ts +24 -20
- package/s/core/vec4.ts +6 -2
- package/s/index.ts +5 -10
- package/s/optimizers/hash-map.ts +2 -2
- package/s/optimizers/zen.ts +4 -4
- package/s/physics/2d/collide2d.barrel.ts +3 -0
- package/s/physics/2d/collide2d.ts +2 -2
- package/s/physics/2d/intersect2d.barrel.ts +3 -0
- package/s/physics/2d/intersect2d.ts +11 -11
- package/s/physics/index.ts +4 -0
- package/s/shapes/2d/circle.ts +3 -3
- package/s/shapes/2d/rect.ts +7 -7
- package/s/shapes/3d/box.ts +10 -10
- package/s/shapes/3d/segment.ts +8 -8
- package/s/shapes/index.ts +7 -0
- package/s/tools/angles.ts +26 -26
- package/s/tools/make-noise.ts +13 -0
- package/s/tools/spline.ts +30 -36
- package/x/concepts/angles.d.ts +29 -0
- package/x/concepts/angles.js +62 -0
- package/x/concepts/angles.js.map +1 -0
- package/x/concepts/circular.d.ts +17 -0
- package/x/concepts/circular.js +70 -0
- package/x/concepts/circular.js.map +1 -0
- package/x/concepts/noise.d.ts +9 -0
- package/x/concepts/noise.js +20 -0
- package/x/concepts/noise.js.map +1 -0
- package/x/concepts/quat.d.ts +35 -0
- package/x/concepts/quat.js +110 -0
- package/x/concepts/quat.js.map +1 -0
- package/x/concepts/randy.d.ts +39 -0
- package/x/concepts/randy.js +95 -0
- package/x/concepts/randy.js.map +1 -0
- package/x/concepts/scalar.d.ts +51 -0
- package/x/concepts/scalar.js +219 -0
- package/x/concepts/scalar.js.map +1 -0
- package/x/concepts/spline.d.ts +9 -0
- package/x/concepts/spline.js +59 -0
- package/x/concepts/spline.js.map +1 -0
- package/x/concepts/vec2.d.ts +114 -0
- package/x/concepts/vec2.js +314 -0
- package/x/concepts/vec2.js.map +1 -0
- package/x/concepts/vec3.d.ts +117 -0
- package/x/concepts/vec3.js +357 -0
- package/x/concepts/vec3.js.map +1 -0
- package/x/concepts/vec4.d.ts +21 -0
- package/x/concepts/vec4.js +62 -0
- package/x/concepts/vec4.js.map +1 -0
- package/x/core/circular.d.ts +17 -0
- package/x/core/circular.js +71 -0
- package/x/core/circular.js.map +1 -0
- package/x/core/quat.d.ts +4 -3
- package/x/core/quat.js +11 -8
- package/x/core/quat.js.map +1 -1
- package/x/core/scalar.d.ts +51 -0
- package/x/core/scalar.js +219 -0
- package/x/core/scalar.js.map +1 -0
- package/x/core/vec2.d.ts +11 -10
- package/x/core/vec2.js +23 -20
- package/x/core/vec2.js.map +1 -1
- package/x/core/vec3.d.ts +11 -10
- package/x/core/vec3.js +23 -20
- package/x/core/vec3.js.map +1 -1
- package/x/core/vec4.d.ts +2 -1
- package/x/core/vec4.js +5 -2
- package/x/core/vec4.js.map +1 -1
- package/x/helpers/angles.d.ts +19 -0
- package/x/helpers/angles.js +41 -0
- package/x/helpers/angles.js.map +1 -0
- package/x/helpers/circular.d.ts +17 -0
- package/x/helpers/circular.js +71 -0
- package/x/helpers/circular.js.map +1 -0
- package/x/helpers/noise.d.ts +9 -0
- package/x/helpers/noise.js +20 -0
- package/x/helpers/noise.js.map +1 -0
- package/x/helpers/randy.d.ts +31 -0
- package/x/helpers/randy.js +85 -0
- package/x/helpers/randy.js.map +1 -0
- package/x/helpers/scalar.d.ts +51 -0
- package/x/helpers/scalar.js +219 -0
- package/x/helpers/scalar.js.map +1 -0
- package/x/helpers/spline.d.ts +9 -0
- package/x/helpers/spline.js +61 -0
- package/x/helpers/spline.js.map +1 -0
- package/x/importmap.json +7 -0
- package/x/index.d.ts +4 -10
- package/x/index.js +4 -10
- package/x/index.js.map +1 -1
- package/x/optimizers/hash-map.js +2 -2
- package/x/optimizers/zen.js +4 -4
- package/x/optimizers/zen.js.map +1 -1
- package/x/physics/2d/collide2d.barrel.d.ts +1 -0
- package/x/physics/2d/collide2d.barrel.js +2 -0
- package/x/physics/2d/collide2d.barrel.js.map +1 -0
- package/x/physics/2d/collide2d.js +2 -2
- package/x/physics/2d/collide2d.js.map +1 -1
- package/x/physics/2d/intersect2d.barrel.d.ts +1 -0
- package/x/physics/2d/intersect2d.barrel.js +2 -0
- package/x/physics/2d/intersect2d.barrel.js.map +1 -0
- package/x/physics/2d/intersect2d.d.ts +3 -3
- package/x/physics/2d/intersect2d.js +11 -11
- package/x/physics/2d/intersect2d.js.map +1 -1
- package/x/physics/index.d.ts +2 -0
- package/x/physics/index.js +3 -0
- package/x/physics/index.js.map +1 -0
- package/x/primitives/circular.d.ts +17 -0
- package/x/primitives/circular.js +70 -0
- package/x/primitives/circular.js.map +1 -0
- package/x/primitives/quat.d.ts +35 -0
- package/x/primitives/quat.js +110 -0
- package/x/primitives/quat.js.map +1 -0
- package/x/primitives/scalar.d.ts +51 -0
- package/x/primitives/scalar.js +219 -0
- package/x/primitives/scalar.js.map +1 -0
- package/x/primitives/vec2.d.ts +114 -0
- package/x/primitives/vec2.js +319 -0
- package/x/primitives/vec2.js.map +1 -0
- package/x/primitives/vec3.d.ts +117 -0
- package/x/primitives/vec3.js +363 -0
- package/x/primitives/vec3.js.map +1 -0
- package/x/primitives/vec4.d.ts +21 -0
- package/x/primitives/vec4.js +62 -0
- package/x/primitives/vec4.js.map +1 -0
- package/x/shapes/2d/circle.js +3 -3
- package/x/shapes/2d/circle.js.map +1 -1
- package/x/shapes/2d/rect.js +7 -7
- package/x/shapes/2d/rect.js.map +1 -1
- package/x/shapes/3d/box.js +10 -10
- package/x/shapes/3d/box.js.map +1 -1
- package/x/shapes/3d/segment.js +7 -7
- package/x/shapes/3d/segment.js.map +1 -1
- package/x/shapes/index.d.ts +4 -0
- package/x/shapes/index.js +5 -0
- package/x/shapes/index.js.map +1 -0
- package/x/tools/angles.d.ts +22 -13
- package/x/tools/angles.js +27 -29
- package/x/tools/angles.js.map +1 -1
- package/x/tools/spline.d.ts +2 -4
- package/x/tools/spline.js +20 -26
- package/x/tools/spline.js.map +1 -1
- package/x/utils/angles.d.ts +19 -0
- package/x/utils/angles.js +41 -0
- package/x/utils/angles.js.map +1 -0
- package/x/utils/circular.d.ts +17 -0
- package/x/utils/circular.js +70 -0
- package/x/utils/circular.js.map +1 -0
- package/x/utils/noise.d.ts +9 -0
- package/x/utils/noise.js +20 -0
- package/x/utils/noise.js.map +1 -0
- package/x/utils/randy.d.ts +31 -0
- package/x/utils/randy.js +85 -0
- package/x/utils/randy.js.map +1 -0
- package/x/utils/scalar.d.ts +51 -0
- package/x/utils/scalar.js +219 -0
- package/x/utils/scalar.js.map +1 -0
- package/x/utils/spline.d.ts +9 -0
- package/x/utils/spline.js +61 -0
- package/x/utils/spline.js.map +1 -0
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -17,14 +17,14 @@
|
|
|
17
17
|
// allocate a single vector instance
|
|
18
18
|
const vector = new Vec2(0, 0)
|
|
19
19
|
.add({x: 1, y: 2})
|
|
20
|
-
.
|
|
20
|
+
.mulBy(2)
|
|
21
21
|
```
|
|
22
22
|
- **explicit cloning.**
|
|
23
|
-
use `.
|
|
23
|
+
use `.dup()` to avoid mutating the original.
|
|
24
24
|
```ts
|
|
25
25
|
// modify a clone (not the original)
|
|
26
26
|
const vector2 = vector
|
|
27
|
-
.
|
|
27
|
+
.dup()
|
|
28
28
|
.normalize()
|
|
29
29
|
```
|
|
30
30
|
- **underscore-suffixed methods take direct args.**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@benev/math",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-2",
|
|
4
4
|
"description": "game math toolkit",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Chase Moskal <chasemoskal@gmail.com>",
|
|
@@ -22,14 +22,14 @@
|
|
|
22
22
|
"count": "find s -path '*/_archive' -prune -o -name '*.ts' -exec wc -l {} +"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@e280/stz": "^0.2.
|
|
25
|
+
"@e280/stz": "^0.2.26",
|
|
26
26
|
"simplex-noise": "^4.0.3"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@e280/science": "^0.1.
|
|
30
|
-
"@e280/scute": "^0.
|
|
29
|
+
"@e280/science": "^0.1.8",
|
|
30
|
+
"@e280/scute": "^0.3.0-1",
|
|
31
31
|
"npm-run-all": "^4.1.5",
|
|
32
|
-
"typescript": "^
|
|
32
|
+
"typescript": "^6.0.2"
|
|
33
33
|
},
|
|
34
34
|
"repository": {
|
|
35
35
|
"type": "git",
|
package/s/core/quat.ts
CHANGED
|
@@ -34,16 +34,20 @@ export class Quat {
|
|
|
34
34
|
return this.identity().rotate(vec)
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
array(): XyzwArray {
|
|
38
38
|
const {x, y, z, w} = this
|
|
39
39
|
return [x, y, z, w]
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
toJSON(): XyzwArray {
|
|
43
|
+
return this.array()
|
|
44
|
+
}
|
|
45
|
+
|
|
42
46
|
toString() {
|
|
43
47
|
return `(Quat x${this.x.toFixed(2)}, y${this.y.toFixed(2)}, z${this.z.toFixed(2)}, w${this.w.toFixed(2)})`
|
|
44
48
|
}
|
|
45
49
|
|
|
46
|
-
|
|
50
|
+
dup() {
|
|
47
51
|
return new Quat(...this.toJSON())
|
|
48
52
|
}
|
|
49
53
|
|
|
@@ -97,11 +101,11 @@ export class Quat {
|
|
|
97
101
|
|
|
98
102
|
transform_(x: number, y: number, z: number, w: number, global = false) {
|
|
99
103
|
if (global) {
|
|
100
|
-
const original = this.
|
|
101
|
-
return this.set_(x, y, z, w).
|
|
104
|
+
const original = this.dup()
|
|
105
|
+
return this.set_(x, y, z, w).mul(original)
|
|
102
106
|
}
|
|
103
107
|
else {
|
|
104
|
-
return this.
|
|
108
|
+
return this.mul_(x, y, z, w)
|
|
105
109
|
}
|
|
106
110
|
}
|
|
107
111
|
|
|
@@ -154,7 +158,7 @@ export class Quat {
|
|
|
154
158
|
return this
|
|
155
159
|
}
|
|
156
160
|
|
|
157
|
-
|
|
161
|
+
mul_(x2: number, y2: number, z2: number, w2: number): Quat {
|
|
158
162
|
const {x, y, z, w} = this
|
|
159
163
|
this.x = w * x2 + x * w2 + y * z2 - z * y2
|
|
160
164
|
this.y = w * y2 - x * z2 + y * w2 + z * x2
|
|
@@ -163,8 +167,8 @@ export class Quat {
|
|
|
163
167
|
return this
|
|
164
168
|
}
|
|
165
169
|
|
|
166
|
-
|
|
167
|
-
for (const {x, y, z, w} of quats) this.
|
|
170
|
+
mul(...quats: Quat[]) {
|
|
171
|
+
for (const {x, y, z, w} of quats) this.mul_(x, y, z, w)
|
|
168
172
|
return this
|
|
169
173
|
}
|
|
170
174
|
}
|
|
@@ -6,7 +6,7 @@ export class Scalar {
|
|
|
6
6
|
return new this(x)
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
dup() {
|
|
10
10
|
return new Scalar(this.x)
|
|
11
11
|
}
|
|
12
12
|
|
|
@@ -47,7 +47,7 @@ export class Scalar {
|
|
|
47
47
|
return this
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
sub(...nums: number[]) {
|
|
51
51
|
for (const n of nums)
|
|
52
52
|
this.x -= n
|
|
53
53
|
return this
|
package/s/core/vec2.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import {Scalar} from "
|
|
2
|
+
import {Scalar} from "./scalar.js"
|
|
3
3
|
|
|
4
4
|
export type XyArray = [x: number, y: number]
|
|
5
5
|
export type Xy = {x: number, y: number}
|
|
@@ -39,7 +39,7 @@ export class Vec2 implements Xy {
|
|
|
39
39
|
static average(...vectors: Xy[]) {
|
|
40
40
|
return this.zero()
|
|
41
41
|
.add(...vectors)
|
|
42
|
-
.
|
|
42
|
+
.divBy(vectors.length)
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
static min(...vecs: Xy[]) {
|
|
@@ -63,7 +63,7 @@ export class Vec2 implements Xy {
|
|
|
63
63
|
)
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
dup() {
|
|
67
67
|
return new Vec2(this.x, this.y)
|
|
68
68
|
}
|
|
69
69
|
|
|
@@ -72,10 +72,14 @@ export class Vec2 implements Xy {
|
|
|
72
72
|
yield this.y
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
array(): XyArray {
|
|
76
76
|
return [this.x, this.y]
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
+
toJSON(): XyArray {
|
|
80
|
+
return this.array()
|
|
81
|
+
}
|
|
82
|
+
|
|
79
83
|
toString() {
|
|
80
84
|
return `(Vec2 x${this.x.toFixed(2)}, y${this.y.toFixed(2)})`
|
|
81
85
|
}
|
|
@@ -168,17 +172,17 @@ export class Vec2 implements Xy {
|
|
|
168
172
|
|
|
169
173
|
/** mutator */
|
|
170
174
|
normalize() {
|
|
171
|
-
return this.
|
|
175
|
+
return this.divBy(this.magnitude())
|
|
172
176
|
}
|
|
173
177
|
|
|
174
178
|
/** mutator */
|
|
175
179
|
half() {
|
|
176
|
-
return this.
|
|
180
|
+
return this.divBy(2)
|
|
177
181
|
}
|
|
178
182
|
|
|
179
183
|
/** mutator */
|
|
180
184
|
double() {
|
|
181
|
-
return this.
|
|
185
|
+
return this.mulBy(2)
|
|
182
186
|
}
|
|
183
187
|
|
|
184
188
|
/** mutator */
|
|
@@ -206,7 +210,7 @@ export class Vec2 implements Xy {
|
|
|
206
210
|
clampMagnitude(max: number) {
|
|
207
211
|
const mag = this.magnitude()
|
|
208
212
|
if (mag > max)
|
|
209
|
-
this.normalize().
|
|
213
|
+
this.normalize().mulBy(max)
|
|
210
214
|
return this
|
|
211
215
|
}
|
|
212
216
|
|
|
@@ -272,21 +276,21 @@ export class Vec2 implements Xy {
|
|
|
272
276
|
}
|
|
273
277
|
|
|
274
278
|
/** mutator */
|
|
275
|
-
|
|
279
|
+
subBy(delta: number) {
|
|
276
280
|
this.x -= delta
|
|
277
281
|
this.y -= delta
|
|
278
282
|
return this
|
|
279
283
|
}
|
|
280
284
|
|
|
281
285
|
/** mutator */
|
|
282
|
-
|
|
286
|
+
mulBy(coefficient: number) {
|
|
283
287
|
this.x *= coefficient
|
|
284
288
|
this.y *= coefficient
|
|
285
289
|
return this
|
|
286
290
|
}
|
|
287
291
|
|
|
288
292
|
/** mutator */
|
|
289
|
-
|
|
293
|
+
divBy(divisor: number) {
|
|
290
294
|
if (divisor === 0) return this
|
|
291
295
|
this.x /= divisor
|
|
292
296
|
this.y /= divisor
|
|
@@ -309,41 +313,41 @@ export class Vec2 implements Xy {
|
|
|
309
313
|
}
|
|
310
314
|
|
|
311
315
|
/** mutator */
|
|
312
|
-
|
|
316
|
+
sub_(x: number, y: number) {
|
|
313
317
|
this.x -= x
|
|
314
318
|
this.y -= y
|
|
315
319
|
return this
|
|
316
320
|
}
|
|
317
321
|
|
|
318
322
|
/** mutator */
|
|
319
|
-
|
|
320
|
-
for (const {x, y} of vecs) this.
|
|
323
|
+
sub(...vecs: Xy[]) {
|
|
324
|
+
for (const {x, y} of vecs) this.sub_(x, y)
|
|
321
325
|
return this
|
|
322
326
|
}
|
|
323
327
|
|
|
324
328
|
/** mutator */
|
|
325
|
-
|
|
329
|
+
mul_(x: number, y: number) {
|
|
326
330
|
this.x *= x
|
|
327
331
|
this.y *= y
|
|
328
332
|
return this
|
|
329
333
|
}
|
|
330
334
|
|
|
331
335
|
/** mutator */
|
|
332
|
-
|
|
333
|
-
for (const {x, y} of vecs) this.
|
|
336
|
+
mul(...vecs: Xy[]) {
|
|
337
|
+
for (const {x, y} of vecs) this.mul_(x, y)
|
|
334
338
|
return this
|
|
335
339
|
}
|
|
336
340
|
|
|
337
341
|
/** mutator */
|
|
338
|
-
|
|
342
|
+
div_(x: number, y: number) {
|
|
339
343
|
if (x !== 0) this.x /= x
|
|
340
344
|
if (y !== 0) this.y /= y
|
|
341
345
|
return this
|
|
342
346
|
}
|
|
343
347
|
|
|
344
348
|
/** mutator */
|
|
345
|
-
|
|
346
|
-
for (const {x, y} of vecs) this.
|
|
349
|
+
div(...vecs: Xy[]) {
|
|
350
|
+
for (const {x, y} of vecs) this.div_(x, y)
|
|
347
351
|
return this
|
|
348
352
|
}
|
|
349
353
|
|
package/s/core/vec3.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import {Scalar} from "
|
|
2
|
+
import {Scalar} from "./scalar.js"
|
|
3
3
|
|
|
4
4
|
export type XyzArray = [x: number, y: number, z: number]
|
|
5
5
|
export type Xyz = {x: number, y: number, z: number}
|
|
@@ -40,7 +40,7 @@ export class Vec3 {
|
|
|
40
40
|
static average(...vecs: Xyz[]) {
|
|
41
41
|
return this.zero()
|
|
42
42
|
.add(...vecs)
|
|
43
|
-
.
|
|
43
|
+
.divBy(vecs.length)
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
static min(...vecs: Xyz[]) {
|
|
@@ -70,7 +70,7 @@ export class Vec3 {
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
dup() {
|
|
74
74
|
return new Vec3(this.x, this.y, this.z)
|
|
75
75
|
}
|
|
76
76
|
|
|
@@ -80,10 +80,14 @@ export class Vec3 {
|
|
|
80
80
|
yield this.z
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
|
|
83
|
+
array(): XyzArray {
|
|
84
84
|
return [this.x, this.y, this.z]
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
+
toJSON(): XyzArray {
|
|
88
|
+
return this.array()
|
|
89
|
+
}
|
|
90
|
+
|
|
87
91
|
toString() {
|
|
88
92
|
return `(Vec3 x${this.x.toFixed(2)}, y${this.y.toFixed(2)}, z${this.z.toFixed(2)})`
|
|
89
93
|
}
|
|
@@ -204,7 +208,7 @@ export class Vec3 {
|
|
|
204
208
|
}
|
|
205
209
|
|
|
206
210
|
/** mutator */
|
|
207
|
-
|
|
211
|
+
sub_(x: number, y: number, z: number) {
|
|
208
212
|
this.x -= x
|
|
209
213
|
this.y -= y
|
|
210
214
|
this.z -= z
|
|
@@ -212,13 +216,13 @@ export class Vec3 {
|
|
|
212
216
|
}
|
|
213
217
|
|
|
214
218
|
/** mutator */
|
|
215
|
-
|
|
216
|
-
for (const {x, y, z} of vecs) this.
|
|
219
|
+
sub(...vecs: Xyz[]) {
|
|
220
|
+
for (const {x, y, z} of vecs) this.sub_(x, y, z)
|
|
217
221
|
return this
|
|
218
222
|
}
|
|
219
223
|
|
|
220
224
|
/** mutator */
|
|
221
|
-
|
|
225
|
+
mul_(x: number, y: number, z: number) {
|
|
222
226
|
this.x *= x
|
|
223
227
|
this.y *= y
|
|
224
228
|
this.z *= z
|
|
@@ -226,13 +230,13 @@ export class Vec3 {
|
|
|
226
230
|
}
|
|
227
231
|
|
|
228
232
|
/** mutator */
|
|
229
|
-
|
|
230
|
-
for (const {x, y, z} of vecs) this.
|
|
233
|
+
mul(...vecs: Xyz[]) {
|
|
234
|
+
for (const {x, y, z} of vecs) this.mul_(x, y, z)
|
|
231
235
|
return this
|
|
232
236
|
}
|
|
233
237
|
|
|
234
238
|
/** mutator */
|
|
235
|
-
|
|
239
|
+
div_(x: number, y: number, z: number) {
|
|
236
240
|
if (x !== 0) this.x /= x
|
|
237
241
|
if (y !== 0) this.y /= y
|
|
238
242
|
if (z !== 0) this.z /= z
|
|
@@ -240,8 +244,8 @@ export class Vec3 {
|
|
|
240
244
|
}
|
|
241
245
|
|
|
242
246
|
/** mutator */
|
|
243
|
-
|
|
244
|
-
for (const {x, y, z} of vecs) this.
|
|
247
|
+
div(...vecs: Xyz[]) {
|
|
248
|
+
for (const {x, y, z} of vecs) this.div_(x, y, z)
|
|
245
249
|
return this
|
|
246
250
|
}
|
|
247
251
|
|
|
@@ -249,12 +253,12 @@ export class Vec3 {
|
|
|
249
253
|
|
|
250
254
|
/** mutator */
|
|
251
255
|
half() {
|
|
252
|
-
return this.
|
|
256
|
+
return this.divBy(2)
|
|
253
257
|
}
|
|
254
258
|
|
|
255
259
|
/** mutator */
|
|
256
260
|
double() {
|
|
257
|
-
return this.
|
|
261
|
+
return this.mulBy(2)
|
|
258
262
|
}
|
|
259
263
|
|
|
260
264
|
/** mutator */
|
|
@@ -310,7 +314,7 @@ export class Vec3 {
|
|
|
310
314
|
}
|
|
311
315
|
|
|
312
316
|
/** mutator */
|
|
313
|
-
|
|
317
|
+
subBy(delta: number) {
|
|
314
318
|
this.x -= delta
|
|
315
319
|
this.y -= delta
|
|
316
320
|
this.z -= delta
|
|
@@ -318,7 +322,7 @@ export class Vec3 {
|
|
|
318
322
|
}
|
|
319
323
|
|
|
320
324
|
/** mutator */
|
|
321
|
-
|
|
325
|
+
mulBy(delta: number) {
|
|
322
326
|
this.x *= delta
|
|
323
327
|
this.y *= delta
|
|
324
328
|
this.z *= delta
|
|
@@ -326,7 +330,7 @@ export class Vec3 {
|
|
|
326
330
|
}
|
|
327
331
|
|
|
328
332
|
/** mutator */
|
|
329
|
-
|
|
333
|
+
divBy(divisor: number) {
|
|
330
334
|
if (divisor === 0) return this
|
|
331
335
|
this.x /= divisor
|
|
332
336
|
this.y /= divisor
|
|
@@ -336,13 +340,13 @@ export class Vec3 {
|
|
|
336
340
|
|
|
337
341
|
/** mutator */
|
|
338
342
|
normalize() {
|
|
339
|
-
return this.
|
|
343
|
+
return this.divBy(this.magnitude())
|
|
340
344
|
}
|
|
341
345
|
|
|
342
346
|
/** mutator */
|
|
343
347
|
clampMagnitude(max: number) {
|
|
344
348
|
const magnitude = this.magnitude()
|
|
345
|
-
if (magnitude > max) this.normalize().
|
|
349
|
+
if (magnitude > max) this.normalize().mulBy(max)
|
|
346
350
|
return this
|
|
347
351
|
}
|
|
348
352
|
|
package/s/core/vec4.ts
CHANGED
|
@@ -23,7 +23,7 @@ export class Vec4 {
|
|
|
23
23
|
: new this(v.x, v.y, v.z, v.w)
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
dup() {
|
|
27
27
|
return new Vec4(this.x, this.y, this.z, this.w)
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -34,11 +34,15 @@ export class Vec4 {
|
|
|
34
34
|
yield this.w
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
array(): XyzwArray {
|
|
38
38
|
const {x, y, z, w} = this
|
|
39
39
|
return [x, y, z, w]
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
toJSON(): XyzwArray {
|
|
43
|
+
return this.array()
|
|
44
|
+
}
|
|
45
|
+
|
|
42
46
|
toString() {
|
|
43
47
|
return `(Vec4 x${this.x.toFixed(2)}, y${this.y.toFixed(2)}, z${this.z.toFixed(2)}, w${this.w.toFixed(2)})`
|
|
44
48
|
}
|
package/s/index.ts
CHANGED
|
@@ -1,27 +1,22 @@
|
|
|
1
1
|
|
|
2
|
+
export * from "./core/circular.js"
|
|
2
3
|
export * from "./core/quat.js"
|
|
4
|
+
export * from "./core/scalar.js"
|
|
3
5
|
export * from "./core/vec2.js"
|
|
4
6
|
export * from "./core/vec3.js"
|
|
5
7
|
export * from "./core/vec4.js"
|
|
6
8
|
|
|
7
9
|
export * from "./tools/angles.js"
|
|
8
|
-
export * from "./tools/circular.js"
|
|
9
10
|
export * from "./tools/noise.js"
|
|
10
11
|
export * from "./tools/randy.js"
|
|
11
|
-
export * from "./tools/
|
|
12
|
+
export * from "./tools/make-noise.js"
|
|
12
13
|
export * as spline from "./tools/spline.js"
|
|
13
14
|
|
|
14
15
|
export * from "./optimizers/hash-map.js"
|
|
15
16
|
export * from "./optimizers/hash-set.js"
|
|
16
17
|
export * from "./optimizers/zen.js"
|
|
17
18
|
|
|
18
|
-
export * from "./physics/
|
|
19
|
-
export * as collide2d from "./physics/2d/collide2d.js"
|
|
20
|
-
export * from "./physics/2d/intersect2d.js"
|
|
21
|
-
export * as intersect2d from "./physics/2d/intersect2d.js"
|
|
19
|
+
export * from "./physics/index.js"
|
|
22
20
|
|
|
23
|
-
export * from "./shapes/
|
|
24
|
-
export * as shapes2d from "./shapes/2d/index.js"
|
|
25
|
-
export * from "./shapes/3d/index.js"
|
|
26
|
-
export * as shapes3d from "./shapes/3d/index.js"
|
|
21
|
+
export * from "./shapes/index.js"
|
|
27
22
|
|
package/s/optimizers/hash-map.ts
CHANGED
package/s/optimizers/zen.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import {
|
|
2
|
+
import {GMap} from "@e280/stz"
|
|
3
3
|
import {Vec2} from "../core/vec2.js"
|
|
4
4
|
import {Rect} from "../shapes/2d/rect.js"
|
|
5
5
|
import {rectVsRect} from "../physics/2d/collide2d.js"
|
|
@@ -32,7 +32,7 @@ export class ZenZone<X> {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
export class ZenGrid<X> {
|
|
35
|
-
#zones = new
|
|
35
|
+
#zones = new GMap<string, ZenZone<X>>()
|
|
36
36
|
|
|
37
37
|
constructor(private zoneExtent: Vec2) {}
|
|
38
38
|
|
|
@@ -124,14 +124,14 @@ export class ZenGrid<X> {
|
|
|
124
124
|
return new Vec2(
|
|
125
125
|
Math.floor(point.x / this.zoneExtent.x),
|
|
126
126
|
Math.floor(point.y / this.zoneExtent.y),
|
|
127
|
-
).
|
|
127
|
+
).mul(this.zoneExtent)
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
#obtainZone(zoneCorner: Vec2) {
|
|
131
131
|
const hash = this.#hash(zoneCorner)
|
|
132
132
|
return this.#zones.guarantee(hash, () => new ZenZone(
|
|
133
133
|
hash,
|
|
134
|
-
zoneCorner.
|
|
134
|
+
zoneCorner.dup().add(this.zoneExtent.dup().half()),
|
|
135
135
|
this.zoneExtent,
|
|
136
136
|
))
|
|
137
137
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import {Vec2} from "../../core/vec2.js"
|
|
3
|
+
import {Scalar} from "../../core/scalar.js"
|
|
3
4
|
import {Rect} from "../../shapes/2d/rect.js"
|
|
4
|
-
import {Scalar} from "../../tools/scalar.js"
|
|
5
5
|
import {Circle} from "../../shapes/2d/circle.js"
|
|
6
6
|
|
|
7
7
|
export function pointVsRect(point: Vec2, box: Rect) {
|
|
@@ -35,7 +35,7 @@ export function rectVsCircle(rect: Rect, circle: Circle) {
|
|
|
35
35
|
Scalar.clamp(circle.center.x, rect.min.x, rect.max.x),
|
|
36
36
|
Scalar.clamp(circle.center.y, rect.min.y, rect.max.y),
|
|
37
37
|
)
|
|
38
|
-
const difference = circle.center.
|
|
38
|
+
const difference = circle.center.dup().sub(clamped)
|
|
39
39
|
const distanceSquared = (difference.x ** 2) + (difference.y ** 2)
|
|
40
40
|
const radiusSquared = circle.radius ** 2
|
|
41
41
|
return distanceSquared <= radiusSquared
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
2
|
import {Vec2} from "../../core/vec2.js"
|
|
3
|
+
import {Scalar} from "../../core/scalar.js"
|
|
3
4
|
import {Rect} from "../../shapes/2d/rect.js"
|
|
4
|
-
import {
|
|
5
|
+
import {collide2d} from "./collide2d.barrel.js"
|
|
5
6
|
import {Circle} from "../../shapes/2d/circle.js"
|
|
6
|
-
import {rectVsCircle, rectVsRect} from "./collide2d.js"
|
|
7
7
|
|
|
8
8
|
export class Intersection {
|
|
9
9
|
constructor(
|
|
@@ -14,8 +14,8 @@ export class Intersection {
|
|
|
14
14
|
) {}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
export function
|
|
18
|
-
if (!rectVsRect(a, b)) return null
|
|
17
|
+
export function rectVsRect(a: Rect, b: Rect) {
|
|
18
|
+
if (!collide2d.rectVsRect(a, b)) return null
|
|
19
19
|
|
|
20
20
|
const overlapX = Math.min(a.max.x - b.min.x, b.max.x - a.min.x)
|
|
21
21
|
const overlapY = Math.min(a.max.y - b.min.y, b.max.y - a.min.y)
|
|
@@ -33,30 +33,30 @@ export function intersectRectVsRect(a: Rect, b: Rect) {
|
|
|
33
33
|
? new Vec2(bCenter.x > aCenter.x ? -1 : 1, 0)
|
|
34
34
|
: new Vec2(0, bCenter.y > aCenter.y ? -1 : 1)
|
|
35
35
|
|
|
36
|
-
const normalB = normalA.
|
|
36
|
+
const normalB = normalA.dup().mulBy(-1)
|
|
37
37
|
|
|
38
38
|
return new Intersection(contactPoint, depth, normalA, normalB)
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
export function
|
|
42
|
-
if (!rectVsCircle(rect, circle)) return null
|
|
41
|
+
export function rectVsCircle(rect: Rect, circle: Circle) {
|
|
42
|
+
if (!collide2d.rectVsCircle(rect, circle)) return null
|
|
43
43
|
|
|
44
44
|
const clamped = new Vec2(
|
|
45
45
|
Scalar.clamp(circle.center.x, rect.min.x, rect.max.x),
|
|
46
46
|
Scalar.clamp(circle.center.y, rect.min.y, rect.max.y),
|
|
47
47
|
)
|
|
48
|
-
const difference = circle.center.
|
|
48
|
+
const difference = circle.center.dup().sub(clamped)
|
|
49
49
|
const distance = difference.magnitude()
|
|
50
50
|
const depth = circle.radius - distance
|
|
51
51
|
|
|
52
52
|
const contactPoint = clamped
|
|
53
53
|
const normalA = difference.normalize()
|
|
54
|
-
const normalB = normalA.
|
|
54
|
+
const normalB = normalA.dup().mulBy(-1)
|
|
55
55
|
|
|
56
56
|
return new Intersection(contactPoint, depth, normalA, normalB)
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
export function
|
|
59
|
+
export function circleVsCircle(a: Circle, b: Circle) {
|
|
60
60
|
const dx = b.center.x - a.center.x
|
|
61
61
|
const dy = b.center.y - a.center.y
|
|
62
62
|
const distance = Math.sqrt(dx ** 2 + dy ** 2)
|
|
@@ -68,7 +68,7 @@ export function intersectCircleVsCircle(a: Circle, b: Circle) {
|
|
|
68
68
|
? new Vec2(1, 0) // fallback for perfectly overlapping circles
|
|
69
69
|
: new Vec2(dx / distance, dy / distance)
|
|
70
70
|
|
|
71
|
-
const normalB = normalA.
|
|
71
|
+
const normalB = normalA.dup().mulBy(-1)
|
|
72
72
|
|
|
73
73
|
const contactPoint = new Vec2(
|
|
74
74
|
(a.center.x + b.center.x) / 2,
|
package/s/shapes/2d/circle.ts
CHANGED
|
@@ -18,11 +18,11 @@ export class Circle {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
toJSON(): CircleJson {
|
|
21
|
-
return [this.center.
|
|
21
|
+
return [this.center.dup().toJSON(), this.radius]
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
clone() {
|
|
25
|
-
return new Circle(this.center.
|
|
25
|
+
return new Circle(this.center.dup(), this.radius)
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
set(circle: CircleLike) {
|
|
@@ -37,7 +37,7 @@ export class Circle {
|
|
|
37
37
|
|
|
38
38
|
boundingBox() {
|
|
39
39
|
const size = Vec2.all(this.radius * 2)
|
|
40
|
-
return Rect.fromCenter(this.center.
|
|
40
|
+
return Rect.fromCenter(this.center.dup(), size)
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|