@perplexdotgg/bounce 1.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.
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/package.json +32 -0
- package/src/builders/ConvexHullBuilder.ts +437 -0
- package/src/builders/ConvexHullBuilder2d.ts +344 -0
- package/src/builders/ConvexHullBuilder3d.ts +1689 -0
- package/src/builders/HeightMapBuilder.ts +414 -0
- package/src/builders/TriangleMeshBuilder.ts +92 -0
- package/src/collision/CastShapesModule.ts +184 -0
- package/src/collision/CollideShapesModule.ts +152 -0
- package/src/collision/HeightMapCaster.ts +38 -0
- package/src/collision/HeightMapCollider.ts +33 -0
- package/src/collision/TriangleCaster.ts +249 -0
- package/src/collision/TriangleCollider.ts +308 -0
- package/src/collision/TriangleCollider2.ts +379 -0
- package/src/collision/activeEdge.ts +146 -0
- package/src/collision/cast/cast.ts +139 -0
- package/src/collision/cast/castCompoundVsCompound.ts +59 -0
- package/src/collision/cast/castCompoundVsConvex.ts +116 -0
- package/src/collision/cast/castConvexVsCompound.ts +123 -0
- package/src/collision/cast/castConvexVsConvex.ts +213 -0
- package/src/collision/cast/castConvexVsHeightMap.ts +73 -0
- package/src/collision/cast/castConvexVsTriangleMesh.ts +56 -0
- package/src/collision/cast/castRayVsCompound.ts +44 -0
- package/src/collision/cast/castRayVsConvex.ts +45 -0
- package/src/collision/cast/castRayVsHeightMap.ts +58 -0
- package/src/collision/cast/castRayVsTriangleMesh.ts +58 -0
- package/src/collision/closestPoints/closestPoints.ts +23 -0
- package/src/collision/closestPoints/computeBarycentricCoordinates2d.ts +32 -0
- package/src/collision/closestPoints/computeBarycentricCoordinates3d.ts +81 -0
- package/src/collision/closestPoints/computeClosestPointOnLine.ts +30 -0
- package/src/collision/closestPoints/computeClosestPointOnTetrahedron.ts +96 -0
- package/src/collision/closestPoints/computeClosestPointOnTriangle.ts +195 -0
- package/src/collision/closestPoints/isOriginOutsideOfPlane.ts +25 -0
- package/src/collision/closestPoints/isOriginOutsideOfTrianglePlanes.ts +72 -0
- package/src/collision/collide/collide.ts +146 -0
- package/src/collision/collide/collideCompoundVsCompound.ts +60 -0
- package/src/collision/collide/collideCompoundVsConvex.ts +59 -0
- package/src/collision/collide/collideCompoundVsHeightMap.ts +73 -0
- package/src/collision/collide/collideCompoundVsTriangleMesh.ts +56 -0
- package/src/collision/collide/collideConvexVsCompound.ts +57 -0
- package/src/collision/collide/collideConvexVsConvex.ts +225 -0
- package/src/collision/collide/collideConvexVsConvexImp.ts +236 -0
- package/src/collision/collide/collideConvexVsHeightMap.ts +53 -0
- package/src/collision/collide/collideConvexVsTriangleMesh.ts +58 -0
- package/src/collision/collide/collideHeightMapVsCompound.ts +69 -0
- package/src/collision/collide/collideHeightMapVsConvex.ts +53 -0
- package/src/collision/collide/collideSphereVsSphere.ts +81 -0
- package/src/collision/collide/collideTriangleMeshVsCompound.ts +58 -0
- package/src/collision/collide/collideTriangleMeshVsConvex.ts +58 -0
- package/src/collision/epa/EpaConvexHullBuilder.ts +397 -0
- package/src/collision/epa/StaticArray.ts +154 -0
- package/src/collision/epa/TriangleFactory.ts +32 -0
- package/src/collision/epa/arrays.ts +99 -0
- package/src/collision/epa/binaryHeap.ts +82 -0
- package/src/collision/epa/structs.ts +227 -0
- package/src/collision/gjk/GjkModule.ts +864 -0
- package/src/collision/gjk/PenetrationDepthModule.ts +493 -0
- package/src/collision/gjk/SupportPoints.ts +50 -0
- package/src/collision/imp/MinkowskiDifference.ts +36 -0
- package/src/collision/imp/computeExploredDistanceLowerUpperBound.ts +40 -0
- package/src/collision/imp/finalizeImpResult.ts +69 -0
- package/src/collision/imp/findContactImp.ts +196 -0
- package/src/collision/imp/imp.ts +28 -0
- package/src/collision/imp/incrementalMinimumDistanceExploreDirection.ts +207 -0
- package/src/collision/mpr/findPortal.ts +152 -0
- package/src/collision/mpr/mpr.ts +29 -0
- package/src/collision/mpr/updatePortal.ts +52 -0
- package/src/constraints/BaseConstraint.ts +50 -0
- package/src/constraints/ConstraintOptions.ts +22 -0
- package/src/constraints/ConstraintSolver.ts +119 -0
- package/src/constraints/DistanceConstraint.ts +229 -0
- package/src/constraints/FixedConstraint.ts +203 -0
- package/src/constraints/HingeConstraint.ts +460 -0
- package/src/constraints/PointConstraint.ts +108 -0
- package/src/constraints/components/AngleComponent.ts +226 -0
- package/src/constraints/components/AxisComponent.ts +263 -0
- package/src/constraints/components/HingeComponent.ts +215 -0
- package/src/constraints/components/Motor.ts +36 -0
- package/src/constraints/components/PointConstraintComponent.ts +179 -0
- package/src/constraints/components/RotationEulerComponent.ts +139 -0
- package/src/constraints/components/Spring.ts +30 -0
- package/src/constraints/components/SpringComponent.ts +71 -0
- package/src/constraints/types.ts +6 -0
- package/src/helpers.ts +147 -0
- package/src/index.ts +50 -0
- package/src/math/BasicTransform.ts +19 -0
- package/src/math/NumberValue.ts +13 -0
- package/src/math/isometry.ts +64 -0
- package/src/math/mat3.ts +529 -0
- package/src/math/mat4.ts +588 -0
- package/src/math/quat.ts +193 -0
- package/src/math/scalar.ts +81 -0
- package/src/math/tensor.ts +17 -0
- package/src/math/vec3.ts +589 -0
- package/src/math/vec4.ts +10 -0
- package/src/physics/Body.ts +581 -0
- package/src/physics/CollisionFilter.ts +52 -0
- package/src/physics/SleepModule.ts +163 -0
- package/src/physics/broadphase/BodyPairsModule.ts +363 -0
- package/src/physics/broadphase/BvhModule.ts +237 -0
- package/src/physics/broadphase/BvhTree.ts +803 -0
- package/src/physics/broadphase/ConstraintPairsModule.ts +385 -0
- package/src/physics/broadphase/TriangleMeshBvhTree.ts +379 -0
- package/src/physics/manifold/ContactManifold.ts +227 -0
- package/src/physics/manifold/ContactManifoldModule.ts +623 -0
- package/src/physics/manifold/Face.ts +119 -0
- package/src/physics/manifold/ManifoldCache.ts +116 -0
- package/src/physics/manifold/clipping/clipPolyVsEdge.ts +131 -0
- package/src/physics/manifold/clipping/clipPolyVsPlane.ts +73 -0
- package/src/physics/manifold/clipping/clipPolyVsPoly.ts +72 -0
- package/src/physics/narrowphase/CollideBodiesModule.ts +755 -0
- package/src/physics/solver/ContactConstraintModule.ts +659 -0
- package/src/physics/solver/ManifoldConstraint.ts +420 -0
- package/src/physics/solver/estimateCollisionResponse.ts +146 -0
- package/src/shape/Aabb.ts +400 -0
- package/src/shape/Box.ts +231 -0
- package/src/shape/Capsule.ts +332 -0
- package/src/shape/CompoundShape.ts +288 -0
- package/src/shape/Convex.ts +130 -0
- package/src/shape/ConvexHull.ts +423 -0
- package/src/shape/Cylinder.ts +313 -0
- package/src/shape/HeightMap.ts +511 -0
- package/src/shape/Line.ts +14 -0
- package/src/shape/Plane.ts +116 -0
- package/src/shape/Ray.ts +81 -0
- package/src/shape/Segment.ts +25 -0
- package/src/shape/Shape.ts +77 -0
- package/src/shape/Sphere.ts +181 -0
- package/src/shape/TransformedShape.ts +51 -0
- package/src/shape/Triangle.ts +122 -0
- package/src/shape/TriangleMesh.ts +186 -0
- package/src/types.ts +1 -0
- package/src/world.ts +1335 -0
- package/tests/BodyPairsModule.test.ts +71 -0
- package/tests/BvhTree.test.ts +406 -0
- package/tests/test.md +642 -0
- package/tests/vec3.test.ts +12 -0
- package/tsconfig.json +20 -0
- package/vite.config.js +40 -0
package/src/math/vec3.ts
ADDED
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
import { createClass, NumberType, PropertyDefinitionMap } from "monomorph";
|
|
2
|
+
import { Mat3 } from "./mat3";
|
|
3
|
+
import { Mat4 } from "./mat4";
|
|
4
|
+
import { Quat } from "./quat";
|
|
5
|
+
|
|
6
|
+
const componentIndexToKeyMap = ["x", "y", "z"] as const;
|
|
7
|
+
|
|
8
|
+
export function squaredDistance(a: Vec3, b: Vec3) {
|
|
9
|
+
let x = b.x - a.x;
|
|
10
|
+
let y = b.y - a.y;
|
|
11
|
+
let z = b.z - a.z;
|
|
12
|
+
return x * x + y * y + z * z;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const vec3Props = {
|
|
16
|
+
x: NumberType(0),
|
|
17
|
+
y: NumberType(0),
|
|
18
|
+
z: NumberType(0),
|
|
19
|
+
} as const satisfies PropertyDefinitionMap;
|
|
20
|
+
|
|
21
|
+
export class Vec3 extends createClass<Vec3, typeof vec3Props>(vec3Props) {
|
|
22
|
+
toObject() {
|
|
23
|
+
return {
|
|
24
|
+
x: this.x,
|
|
25
|
+
y: this.y,
|
|
26
|
+
z: this.z,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
addVector(a: Vec3) {
|
|
31
|
+
this.x += a.x;
|
|
32
|
+
this.y += a.y;
|
|
33
|
+
this.z += a.z;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
addVectors(a: Vec3, b: Vec3) {
|
|
38
|
+
this.x = a.x + b.x;
|
|
39
|
+
this.y = a.y + b.y;
|
|
40
|
+
this.z = a.z + b.z;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
subtractVector(other: Vec3) {
|
|
45
|
+
this.x -= other.x;
|
|
46
|
+
this.y -= other.y;
|
|
47
|
+
this.z -= other.z;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
subtractVectors(a: Vec3, b: Vec3) {
|
|
52
|
+
this.x = a.x - b.x;
|
|
53
|
+
this.y = a.y - b.y;
|
|
54
|
+
this.z = a.z - b.z;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
scale(scale: number) {
|
|
59
|
+
this.x *= scale;
|
|
60
|
+
this.y *= scale;
|
|
61
|
+
this.z *= scale;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
scaleVector(other: Vec3, scale: number) {
|
|
66
|
+
this.x = other.x * scale;
|
|
67
|
+
this.y = other.y * scale;
|
|
68
|
+
this.z = other.z * scale;
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
addScaled(other: Vec3, scale: number) {
|
|
73
|
+
this.x += other.x * scale;
|
|
74
|
+
this.y += other.y * scale;
|
|
75
|
+
this.z += other.z * scale;
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
addScaledToVector(a: Vec3, b: Vec3, scale: number) {
|
|
80
|
+
this.x = a.x + b.x * scale;
|
|
81
|
+
this.y = a.y + b.y * scale;
|
|
82
|
+
this.z = a.z + b.z * scale;
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
negate() {
|
|
87
|
+
this.x = -this.x;
|
|
88
|
+
this.y = -this.y;
|
|
89
|
+
this.z = -this.z;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
negateVector(a: Vec3) {
|
|
94
|
+
this.x = -a.x;
|
|
95
|
+
this.y = -a.y;
|
|
96
|
+
this.z = -a.z;
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
normalize() {
|
|
101
|
+
const x = this.x;
|
|
102
|
+
const y = this.y;
|
|
103
|
+
const z = this.z;
|
|
104
|
+
|
|
105
|
+
let length = x * x + y * y + z * z;
|
|
106
|
+
if (length > 0) {
|
|
107
|
+
length = 1 / Math.sqrt(length);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
this.x = this.x * length;
|
|
111
|
+
this.y = this.y * length;
|
|
112
|
+
this.z = this.z * length;
|
|
113
|
+
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
normalizeVector(a: Vec3) {
|
|
118
|
+
const x = a.x;
|
|
119
|
+
const y = a.y;
|
|
120
|
+
const z = a.z;
|
|
121
|
+
|
|
122
|
+
let length = x * x + y * y + z * z;
|
|
123
|
+
if (length > 0) {
|
|
124
|
+
length = 1 / Math.sqrt(length);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
this.x = x * length;
|
|
128
|
+
this.y = y * length;
|
|
129
|
+
this.z = z * length;
|
|
130
|
+
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
crossVector(other: Vec3) {
|
|
135
|
+
const ax = this.x;
|
|
136
|
+
const ay = this.y;
|
|
137
|
+
const az = this.z;
|
|
138
|
+
|
|
139
|
+
const bx = other.x;
|
|
140
|
+
const by = other.y;
|
|
141
|
+
const bz = other.z;
|
|
142
|
+
|
|
143
|
+
this.x = ay * bz - az * by;
|
|
144
|
+
this.y = az * bx - ax * bz;
|
|
145
|
+
this.z = ax * by - ay * bx;
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
crossVectors(a: Vec3, b: Vec3) {
|
|
150
|
+
const ax = a.x;
|
|
151
|
+
const ay = a.y;
|
|
152
|
+
const az = a.z;
|
|
153
|
+
|
|
154
|
+
const bx = b.x;
|
|
155
|
+
const by = b.y;
|
|
156
|
+
const bz = b.z;
|
|
157
|
+
|
|
158
|
+
this.x = ay * bz - az * by;
|
|
159
|
+
this.y = az * bx - ax * bz;
|
|
160
|
+
this.z = ax * by - ay * bx;
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
zero() {
|
|
165
|
+
this.x = 0;
|
|
166
|
+
this.y = 0;
|
|
167
|
+
this.z = 0;
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
length() {
|
|
172
|
+
const x = this.x;
|
|
173
|
+
const y = this.y;
|
|
174
|
+
const z = this.z;
|
|
175
|
+
return Math.sqrt(x * x + y * y + z * z);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
squaredLength() {
|
|
179
|
+
const x = this.x;
|
|
180
|
+
const y = this.y;
|
|
181
|
+
const z = this.z;
|
|
182
|
+
return x * x + y * y + z * z;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
dot(other: Vec3) {
|
|
186
|
+
return this.x * other.x + this.y * other.y + this.z * other.z;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
transformFromMat3(matrix: Mat3) {
|
|
190
|
+
const x = this.x;
|
|
191
|
+
const y = this.y;
|
|
192
|
+
const z = this.z;
|
|
193
|
+
|
|
194
|
+
this.x = x * matrix.e0 + y * matrix.e3 + z * matrix.e6;
|
|
195
|
+
this.y = x * matrix.e1 + y * matrix.e4 + z * matrix.e7;
|
|
196
|
+
this.z = x * matrix.e2 + y * matrix.e5 + z * matrix.e8;
|
|
197
|
+
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
transformVectorFromMat3(vector: Vec3, matrix: Mat3) {
|
|
202
|
+
const x = vector.x;
|
|
203
|
+
const y = vector.y;
|
|
204
|
+
const z = vector.z;
|
|
205
|
+
|
|
206
|
+
this.x = x * matrix.e0 + y * matrix.e3 + z * matrix.e6;
|
|
207
|
+
this.y = x * matrix.e1 + y * matrix.e4 + z * matrix.e7;
|
|
208
|
+
this.z = x * matrix.e2 + y * matrix.e5 + z * matrix.e8;
|
|
209
|
+
|
|
210
|
+
return this;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
transformFromMat4(matrix: Mat4) {
|
|
214
|
+
const x = this.x;
|
|
215
|
+
const y = this.y;
|
|
216
|
+
const z = this.z;
|
|
217
|
+
|
|
218
|
+
let w = matrix.e3 * x + matrix.e7 * y + matrix.e11 * z + matrix.e15;
|
|
219
|
+
w = w || 1.0;
|
|
220
|
+
|
|
221
|
+
this.x = (matrix.e0 * x + matrix.e4 * y + matrix.e8 * z + matrix.e12) / w;
|
|
222
|
+
this.y = (matrix.e1 * x + matrix.e5 * y + matrix.e9 * z + matrix.e13) / w;
|
|
223
|
+
this.z = (matrix.e2 * x + matrix.e6 * y + matrix.e10 * z + matrix.e14) / w;
|
|
224
|
+
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
transformVectorFromMat4(vector: Vec3, matrix: Mat4) {
|
|
229
|
+
const x = vector.x;
|
|
230
|
+
const y = vector.y;
|
|
231
|
+
const z = vector.z;
|
|
232
|
+
|
|
233
|
+
let w = matrix.e3 * x + matrix.e7 * y + matrix.e11 * z + matrix.e15;
|
|
234
|
+
w = w || 1.0;
|
|
235
|
+
|
|
236
|
+
this.x = (matrix.e0 * x + matrix.e4 * y + matrix.e8 * z + matrix.e12) / w;
|
|
237
|
+
this.y = (matrix.e1 * x + matrix.e5 * y + matrix.e9 * z + matrix.e13) / w;
|
|
238
|
+
this.z = (matrix.e2 * x + matrix.e6 * y + matrix.e10 * z + matrix.e14) / w;
|
|
239
|
+
|
|
240
|
+
return this;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
isNearZero(squaredTolerance: number = 1e-6) {
|
|
244
|
+
return this.squaredLength() <= squaredTolerance;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// TODO: refactor this to match new function and naming style, a bit tricky to do until everything is working
|
|
248
|
+
normalizedOr(out: Vec3, safeFallback: Vec3) {
|
|
249
|
+
if (this.isNearZero()) {
|
|
250
|
+
out.copy(safeFallback);
|
|
251
|
+
} else {
|
|
252
|
+
out.normalizeVector(this);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
swap(other: Vec3) {
|
|
257
|
+
temp.x = this.x;
|
|
258
|
+
temp.y = this.y;
|
|
259
|
+
temp.z = this.z;
|
|
260
|
+
|
|
261
|
+
this.x = other.x;
|
|
262
|
+
this.y = other.y;
|
|
263
|
+
this.z = other.z;
|
|
264
|
+
|
|
265
|
+
other.x = temp.x;
|
|
266
|
+
other.y = temp.y;
|
|
267
|
+
other.z = temp.z;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
transformByMat4(matrix: Mat4) {
|
|
271
|
+
const x = this.x;
|
|
272
|
+
const y = this.y;
|
|
273
|
+
const z = this.z;
|
|
274
|
+
|
|
275
|
+
let w = matrix.e3 * x + matrix.e7 * y + matrix.e11 * z + matrix.e15;
|
|
276
|
+
w = w || 1.0;
|
|
277
|
+
|
|
278
|
+
this.x = (matrix.e0 * x + matrix.e4 * y + matrix.e8 * z + matrix.e12) / w;
|
|
279
|
+
this.y = (matrix.e1 * x + matrix.e5 * y + matrix.e9 * z + matrix.e13) / w;
|
|
280
|
+
this.z = (matrix.e2 * x + matrix.e6 * y + matrix.e10 * z + matrix.e14) / w;
|
|
281
|
+
|
|
282
|
+
return this;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
transformByQuat(quaternion: Quat) {
|
|
286
|
+
const qx = quaternion.x;
|
|
287
|
+
const qy = quaternion.y;
|
|
288
|
+
const qz = quaternion.z;
|
|
289
|
+
const qw = quaternion.w;
|
|
290
|
+
|
|
291
|
+
const x = this.x;
|
|
292
|
+
const y = this.y;
|
|
293
|
+
const z = this.z;
|
|
294
|
+
|
|
295
|
+
const uvx = qy * z - qz * y;
|
|
296
|
+
const uvy = qz * x - qx * z;
|
|
297
|
+
const uvz = qx * y - qy * x;
|
|
298
|
+
|
|
299
|
+
const uuvx = qy * uvz - qz * uvy;
|
|
300
|
+
const uuvy = qz * uvx - qx * uvz;
|
|
301
|
+
const uuvz = qx * uvy - qy * uvx;
|
|
302
|
+
|
|
303
|
+
let w2 = qw * 2;
|
|
304
|
+
|
|
305
|
+
this.x = x + w2 * uvx + 2 * uuvx;
|
|
306
|
+
this.y = y + w2 * uvy + 2 * uuvy;
|
|
307
|
+
this.z = z + w2 * uvz + 2 * uuvz;
|
|
308
|
+
|
|
309
|
+
return this;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
transformVectorByQuat(a: Vec3, quaternion: Quat) {
|
|
313
|
+
const qx = quaternion.x;
|
|
314
|
+
const qy = quaternion.y;
|
|
315
|
+
const qz = quaternion.z;
|
|
316
|
+
const qw = quaternion.w;
|
|
317
|
+
|
|
318
|
+
const x = a.x;
|
|
319
|
+
const y = a.y;
|
|
320
|
+
const z = a.z;
|
|
321
|
+
|
|
322
|
+
const uvx = qy * z - qz * y;
|
|
323
|
+
const uvy = qz * x - qx * z;
|
|
324
|
+
const uvz = qx * y - qy * x;
|
|
325
|
+
|
|
326
|
+
const uuvx = qy * uvz - qz * uvy;
|
|
327
|
+
const uuvy = qz * uvx - qx * uvz;
|
|
328
|
+
const uuvz = qx * uvy - qy * uvx;
|
|
329
|
+
|
|
330
|
+
let w2 = qw * 2;
|
|
331
|
+
|
|
332
|
+
this.x = x + w2 * uvx + 2 * uuvx;
|
|
333
|
+
this.y = y + w2 * uvy + 2 * uuvy;
|
|
334
|
+
this.z = z + w2 * uvz + 2 * uuvz;
|
|
335
|
+
|
|
336
|
+
return this;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
distance(other: Vec3) {
|
|
340
|
+
const x = other.x - this.x;
|
|
341
|
+
const y = other.y - this.y;
|
|
342
|
+
const z = other.z - this.z;
|
|
343
|
+
return Math.sqrt(x * x + y * y + z * z);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
applyQuatThenAddToVector(a: Vec3, r: Quat, b: Vec3) {
|
|
347
|
+
rb.transformVectorByQuat(b, r);
|
|
348
|
+
this.addVectors(a, rb);
|
|
349
|
+
return this;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
replicate(value: number) {
|
|
353
|
+
this.x = value;
|
|
354
|
+
this.y = value;
|
|
355
|
+
this.z = value;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
getComponentAtIndex(index: number) {
|
|
359
|
+
return this[componentIndexToKeyMap[index]];
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
setComponentAtIndex(index: number, value: number) {
|
|
363
|
+
this[componentIndexToKeyMap[index]] = value;
|
|
364
|
+
return this;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
isNormalized(tolerance: number = 1e-3) {
|
|
368
|
+
return Math.abs(this.length() - 1) < tolerance;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
computeNormalizedPerpendicular(vector: Vec3) {
|
|
372
|
+
if (Math.abs(vector.x) > Math.abs(vector.y)) {
|
|
373
|
+
const len = Math.sqrt(vector.x * vector.x + vector.z * vector.z);
|
|
374
|
+
this.x = vector.z / len;
|
|
375
|
+
this.y = 0;
|
|
376
|
+
this.z = -vector.x / len;
|
|
377
|
+
} else {
|
|
378
|
+
const len = Math.sqrt(vector.y * vector.y + vector.z * vector.z);
|
|
379
|
+
this.x = 0;
|
|
380
|
+
this.y = vector.z / len;
|
|
381
|
+
this.z = -vector.y / len;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
isClose(other: Vec3, tolerance: number = 1e-6) {
|
|
386
|
+
return squaredDistance(this, other) <= tolerance;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
isCloseToZero(tolerance: number = 1e-6) {
|
|
390
|
+
return this.squaredLength() <= tolerance;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
averageOfVectors(a: Vec3, b: Vec3) {
|
|
394
|
+
this.x = (a.x + b.x) * 0.5;
|
|
395
|
+
this.y = (a.y + b.y) * 0.5;
|
|
396
|
+
this.z = (a.z + b.z) * 0.5;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
absNorm() {
|
|
400
|
+
return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
isUnitVector(tolerance: number = 1e-3) {
|
|
404
|
+
return Math.abs(this.length() - 1) < tolerance;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
addScalar(s: number) {
|
|
408
|
+
this.x = this.x + s;
|
|
409
|
+
this.y = this.y + s;
|
|
410
|
+
this.z = this.z + s;
|
|
411
|
+
return this;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
addScalarToVector(a: Vec3, s: number) {
|
|
415
|
+
this.x = a.x + s;
|
|
416
|
+
this.y = a.y + s;
|
|
417
|
+
this.z = a.z + s;
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
squaredDistance(b: Vec3) {
|
|
422
|
+
const x = b.x - this.x;
|
|
423
|
+
const y = b.y - this.y;
|
|
424
|
+
const z = b.z - this.z;
|
|
425
|
+
return x * x + y * y + z * z;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
notEquals(other: Vec3, tolerance: number = 1e-6) {
|
|
429
|
+
return (
|
|
430
|
+
Math.abs(this.x - other.x) > tolerance ||
|
|
431
|
+
Math.abs(this.y - other.y) > tolerance ||
|
|
432
|
+
Math.abs(this.z - other.z) > tolerance
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
equals(other: Vec3, tolerance: number = 1e-6) {
|
|
437
|
+
return (
|
|
438
|
+
Math.abs(this.x - other.x) <= tolerance &&
|
|
439
|
+
Math.abs(this.y - other.y) <= tolerance &&
|
|
440
|
+
Math.abs(this.z - other.z) <= tolerance
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
minVector(a: Vec3) {
|
|
445
|
+
this.x = Math.min(this.x, a.x);
|
|
446
|
+
this.y = Math.min(this.y, a.y);
|
|
447
|
+
this.z = Math.min(this.z, a.z);
|
|
448
|
+
return this;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
minVectors(a: Vec3, b: Vec3) {
|
|
452
|
+
this.x = Math.min(a.x, b.x);
|
|
453
|
+
this.y = Math.min(a.y, b.y);
|
|
454
|
+
this.z = Math.min(a.z, b.z);
|
|
455
|
+
return this;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
maxVector(a: Vec3) {
|
|
459
|
+
this.x = Math.max(this.x, a.x);
|
|
460
|
+
this.y = Math.max(this.y, a.y);
|
|
461
|
+
this.z = Math.max(this.z, a.z);
|
|
462
|
+
return this;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
maxVectors(a: Vec3, b: Vec3) {
|
|
466
|
+
this.x = Math.max(a.x, b.x);
|
|
467
|
+
this.y = Math.max(a.y, b.y);
|
|
468
|
+
this.z = Math.max(a.z, b.z);
|
|
469
|
+
return this;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
computeMaxAxis() {
|
|
473
|
+
const absX = Math.abs(this.x);
|
|
474
|
+
const absY = Math.abs(this.y);
|
|
475
|
+
const absZ = Math.abs(this.z);
|
|
476
|
+
if (absX >= absY && absX >= absZ) {
|
|
477
|
+
return 0;
|
|
478
|
+
}
|
|
479
|
+
if (absY >= absX && absY >= absZ) {
|
|
480
|
+
return 1;
|
|
481
|
+
}
|
|
482
|
+
return 2;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
computeMaxComponent() {
|
|
486
|
+
const absX = Math.abs(this.x);
|
|
487
|
+
const absY = Math.abs(this.y);
|
|
488
|
+
const absZ = Math.abs(this.z);
|
|
489
|
+
if (absX >= absY && absX >= absZ) {
|
|
490
|
+
return "x";
|
|
491
|
+
}
|
|
492
|
+
if (absY >= absX && absY >= absZ) {
|
|
493
|
+
return "y";
|
|
494
|
+
}
|
|
495
|
+
return "z";
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
abs() {
|
|
499
|
+
this.x = Math.abs(this.x);
|
|
500
|
+
this.y = Math.abs(this.y);
|
|
501
|
+
this.z = Math.abs(this.z);
|
|
502
|
+
return this;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
absVector(a: Vec3) {
|
|
506
|
+
this.x = Math.abs(a.x);
|
|
507
|
+
this.y = Math.abs(a.y);
|
|
508
|
+
this.z = Math.abs(a.z);
|
|
509
|
+
return this;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
computeLowestComponentIndex(): number {
|
|
513
|
+
return this.x < this.y ? (this.z < this.x ? 2 : 0) : this.z < this.y ? 2 : 1;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
projectOnPlane(normal: Vec3) {
|
|
517
|
+
const dot = this.dot(normal);
|
|
518
|
+
this.x -= normal.x * dot;
|
|
519
|
+
this.y -= normal.y * dot;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
projectOnVector(vector: Vec3): void {
|
|
523
|
+
const projectionAmount = this.dot(vector) / vector.dot(vector);
|
|
524
|
+
this.x = vector.x * projectionAmount;
|
|
525
|
+
this.y = vector.y * projectionAmount;
|
|
526
|
+
this.z = vector.z * projectionAmount;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
isExactlyZero() {
|
|
530
|
+
return this.x === 0 && this.y === 0 && this.z === 0;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
addScaledVector(vector: Vec3, scalar: number) {
|
|
534
|
+
this.x += vector.x * scalar;
|
|
535
|
+
this.y += vector.y * scalar;
|
|
536
|
+
this.z += vector.z * scalar;
|
|
537
|
+
return this;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
subScaledVector(vector: Vec3, scalar: number) {
|
|
541
|
+
this.x -= vector.x * scalar;
|
|
542
|
+
this.y -= vector.y * scalar;
|
|
543
|
+
this.z -= vector.z * scalar;
|
|
544
|
+
return this;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
addRotatedVector(vector: Vec3, rotation: Mat3) {
|
|
548
|
+
rotatedVector.transformVectorFromMat3(vector, rotation);
|
|
549
|
+
this.x += rotatedVector.x;
|
|
550
|
+
this.y += rotatedVector.y;
|
|
551
|
+
this.z += rotatedVector.z;
|
|
552
|
+
return this;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
subRotatedVector(vector: Vec3, rotation: Mat3) {
|
|
556
|
+
rotatedVector.transformVectorFromMat3(vector, rotation);
|
|
557
|
+
this.x -= rotatedVector.x;
|
|
558
|
+
this.y -= rotatedVector.y;
|
|
559
|
+
this.z -= rotatedVector.z;
|
|
560
|
+
return this;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
exactEquals(b: Vec3) {
|
|
564
|
+
return this.x === b.x && this.y === b.y && this.z === b.z;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
multiplyVectors(a: Vec3, b: Vec3) {
|
|
568
|
+
this.x = a.x * b.x;
|
|
569
|
+
this.y = a.y * b.y;
|
|
570
|
+
this.z = a.z * b.z;
|
|
571
|
+
return this;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
rotateVectorByMat4(vector: Vec3, matrix: Mat4) {
|
|
575
|
+
rotationMatrix.fromMat4(matrix);
|
|
576
|
+
this.transformVectorFromMat3(vector, rotationMatrix);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
distanceXZ(other: Vec3) {
|
|
580
|
+
const dx = other.x - this.x;
|
|
581
|
+
const dz = other.z - this.z;
|
|
582
|
+
return Math.sqrt(dx * dx + dz * dz);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const rb = /*@__PURE__*/ Vec3.create();
|
|
587
|
+
const temp = /*@__PURE__*/ Vec3.create();
|
|
588
|
+
const rotatedVector = /*@__PURE__*/ Vec3.create();
|
|
589
|
+
const rotationMatrix = /*@__PURE__*/ Mat3.create();
|
package/src/math/vec4.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createClass, NumberType, PropertyDefinitionMap } from "monomorph";
|
|
2
|
+
|
|
3
|
+
const vec4Props = {
|
|
4
|
+
x: NumberType(0),
|
|
5
|
+
y: NumberType(0),
|
|
6
|
+
z: NumberType(0),
|
|
7
|
+
w: NumberType(0),
|
|
8
|
+
} as const satisfies PropertyDefinitionMap;
|
|
9
|
+
|
|
10
|
+
export class Vec4 extends createClass<Vec4, typeof vec4Props>(vec4Props) {}
|