@perplexdotgg/bounce 1.0.0 → 1.0.1

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 (134) hide show
  1. package/build/bounce.d.ts +39501 -0
  2. package/build/bounce.js +17166 -0
  3. package/package.json +1 -1
  4. package/src/builders/ConvexHullBuilder.ts +0 -437
  5. package/src/builders/ConvexHullBuilder2d.ts +0 -344
  6. package/src/builders/ConvexHullBuilder3d.ts +0 -1689
  7. package/src/builders/HeightMapBuilder.ts +0 -414
  8. package/src/builders/TriangleMeshBuilder.ts +0 -92
  9. package/src/collision/CastShapesModule.ts +0 -184
  10. package/src/collision/CollideShapesModule.ts +0 -152
  11. package/src/collision/HeightMapCaster.ts +0 -38
  12. package/src/collision/HeightMapCollider.ts +0 -33
  13. package/src/collision/TriangleCaster.ts +0 -249
  14. package/src/collision/TriangleCollider.ts +0 -308
  15. package/src/collision/TriangleCollider2.ts +0 -379
  16. package/src/collision/activeEdge.ts +0 -146
  17. package/src/collision/cast/cast.ts +0 -139
  18. package/src/collision/cast/castCompoundVsCompound.ts +0 -59
  19. package/src/collision/cast/castCompoundVsConvex.ts +0 -116
  20. package/src/collision/cast/castConvexVsCompound.ts +0 -123
  21. package/src/collision/cast/castConvexVsConvex.ts +0 -213
  22. package/src/collision/cast/castConvexVsHeightMap.ts +0 -73
  23. package/src/collision/cast/castConvexVsTriangleMesh.ts +0 -56
  24. package/src/collision/cast/castRayVsCompound.ts +0 -44
  25. package/src/collision/cast/castRayVsConvex.ts +0 -45
  26. package/src/collision/cast/castRayVsHeightMap.ts +0 -58
  27. package/src/collision/cast/castRayVsTriangleMesh.ts +0 -58
  28. package/src/collision/closestPoints/closestPoints.ts +0 -23
  29. package/src/collision/closestPoints/computeBarycentricCoordinates2d.ts +0 -32
  30. package/src/collision/closestPoints/computeBarycentricCoordinates3d.ts +0 -81
  31. package/src/collision/closestPoints/computeClosestPointOnLine.ts +0 -30
  32. package/src/collision/closestPoints/computeClosestPointOnTetrahedron.ts +0 -96
  33. package/src/collision/closestPoints/computeClosestPointOnTriangle.ts +0 -195
  34. package/src/collision/closestPoints/isOriginOutsideOfPlane.ts +0 -25
  35. package/src/collision/closestPoints/isOriginOutsideOfTrianglePlanes.ts +0 -72
  36. package/src/collision/collide/collide.ts +0 -146
  37. package/src/collision/collide/collideCompoundVsCompound.ts +0 -60
  38. package/src/collision/collide/collideCompoundVsConvex.ts +0 -59
  39. package/src/collision/collide/collideCompoundVsHeightMap.ts +0 -73
  40. package/src/collision/collide/collideCompoundVsTriangleMesh.ts +0 -56
  41. package/src/collision/collide/collideConvexVsCompound.ts +0 -57
  42. package/src/collision/collide/collideConvexVsConvex.ts +0 -225
  43. package/src/collision/collide/collideConvexVsConvexImp.ts +0 -236
  44. package/src/collision/collide/collideConvexVsHeightMap.ts +0 -53
  45. package/src/collision/collide/collideConvexVsTriangleMesh.ts +0 -58
  46. package/src/collision/collide/collideHeightMapVsCompound.ts +0 -69
  47. package/src/collision/collide/collideHeightMapVsConvex.ts +0 -53
  48. package/src/collision/collide/collideSphereVsSphere.ts +0 -81
  49. package/src/collision/collide/collideTriangleMeshVsCompound.ts +0 -58
  50. package/src/collision/collide/collideTriangleMeshVsConvex.ts +0 -58
  51. package/src/collision/epa/EpaConvexHullBuilder.ts +0 -397
  52. package/src/collision/epa/StaticArray.ts +0 -154
  53. package/src/collision/epa/TriangleFactory.ts +0 -32
  54. package/src/collision/epa/arrays.ts +0 -99
  55. package/src/collision/epa/binaryHeap.ts +0 -82
  56. package/src/collision/epa/structs.ts +0 -227
  57. package/src/collision/gjk/GjkModule.ts +0 -864
  58. package/src/collision/gjk/PenetrationDepthModule.ts +0 -493
  59. package/src/collision/gjk/SupportPoints.ts +0 -50
  60. package/src/collision/imp/MinkowskiDifference.ts +0 -36
  61. package/src/collision/imp/computeExploredDistanceLowerUpperBound.ts +0 -40
  62. package/src/collision/imp/finalizeImpResult.ts +0 -69
  63. package/src/collision/imp/findContactImp.ts +0 -196
  64. package/src/collision/imp/imp.ts +0 -28
  65. package/src/collision/imp/incrementalMinimumDistanceExploreDirection.ts +0 -207
  66. package/src/collision/mpr/findPortal.ts +0 -152
  67. package/src/collision/mpr/mpr.ts +0 -29
  68. package/src/collision/mpr/updatePortal.ts +0 -52
  69. package/src/constraints/BaseConstraint.ts +0 -50
  70. package/src/constraints/ConstraintOptions.ts +0 -22
  71. package/src/constraints/ConstraintSolver.ts +0 -119
  72. package/src/constraints/DistanceConstraint.ts +0 -229
  73. package/src/constraints/FixedConstraint.ts +0 -203
  74. package/src/constraints/HingeConstraint.ts +0 -460
  75. package/src/constraints/PointConstraint.ts +0 -108
  76. package/src/constraints/components/AngleComponent.ts +0 -226
  77. package/src/constraints/components/AxisComponent.ts +0 -263
  78. package/src/constraints/components/HingeComponent.ts +0 -215
  79. package/src/constraints/components/Motor.ts +0 -36
  80. package/src/constraints/components/PointConstraintComponent.ts +0 -179
  81. package/src/constraints/components/RotationEulerComponent.ts +0 -139
  82. package/src/constraints/components/Spring.ts +0 -30
  83. package/src/constraints/components/SpringComponent.ts +0 -71
  84. package/src/constraints/types.ts +0 -6
  85. package/src/helpers.ts +0 -147
  86. package/src/index.ts +0 -50
  87. package/src/math/BasicTransform.ts +0 -19
  88. package/src/math/NumberValue.ts +0 -13
  89. package/src/math/isometry.ts +0 -64
  90. package/src/math/mat3.ts +0 -529
  91. package/src/math/mat4.ts +0 -588
  92. package/src/math/quat.ts +0 -193
  93. package/src/math/scalar.ts +0 -81
  94. package/src/math/tensor.ts +0 -17
  95. package/src/math/vec3.ts +0 -589
  96. package/src/math/vec4.ts +0 -10
  97. package/src/physics/Body.ts +0 -581
  98. package/src/physics/CollisionFilter.ts +0 -52
  99. package/src/physics/SleepModule.ts +0 -163
  100. package/src/physics/broadphase/BodyPairsModule.ts +0 -363
  101. package/src/physics/broadphase/BvhModule.ts +0 -237
  102. package/src/physics/broadphase/BvhTree.ts +0 -803
  103. package/src/physics/broadphase/ConstraintPairsModule.ts +0 -385
  104. package/src/physics/broadphase/TriangleMeshBvhTree.ts +0 -379
  105. package/src/physics/manifold/ContactManifold.ts +0 -227
  106. package/src/physics/manifold/ContactManifoldModule.ts +0 -623
  107. package/src/physics/manifold/Face.ts +0 -119
  108. package/src/physics/manifold/ManifoldCache.ts +0 -116
  109. package/src/physics/manifold/clipping/clipPolyVsEdge.ts +0 -131
  110. package/src/physics/manifold/clipping/clipPolyVsPlane.ts +0 -73
  111. package/src/physics/manifold/clipping/clipPolyVsPoly.ts +0 -72
  112. package/src/physics/narrowphase/CollideBodiesModule.ts +0 -755
  113. package/src/physics/solver/ContactConstraintModule.ts +0 -659
  114. package/src/physics/solver/ManifoldConstraint.ts +0 -420
  115. package/src/physics/solver/estimateCollisionResponse.ts +0 -146
  116. package/src/shape/Aabb.ts +0 -400
  117. package/src/shape/Box.ts +0 -231
  118. package/src/shape/Capsule.ts +0 -332
  119. package/src/shape/CompoundShape.ts +0 -288
  120. package/src/shape/Convex.ts +0 -130
  121. package/src/shape/ConvexHull.ts +0 -423
  122. package/src/shape/Cylinder.ts +0 -313
  123. package/src/shape/HeightMap.ts +0 -511
  124. package/src/shape/Line.ts +0 -14
  125. package/src/shape/Plane.ts +0 -116
  126. package/src/shape/Ray.ts +0 -81
  127. package/src/shape/Segment.ts +0 -25
  128. package/src/shape/Shape.ts +0 -77
  129. package/src/shape/Sphere.ts +0 -181
  130. package/src/shape/TransformedShape.ts +0 -51
  131. package/src/shape/Triangle.ts +0 -122
  132. package/src/shape/TriangleMesh.ts +0 -186
  133. package/src/types.ts +0 -1
  134. package/src/world.ts +0 -1335
@@ -1,82 +0,0 @@
1
- /// Push a new element into a binary max-heap.
2
- /// [inBegin, inEnd - 1) must be a a valid heap. Element inEnd - 1 will be inserted into the heap. The heap will be [inBegin, inEnd) after this call.
3
- /// inPred is a function that returns true if the first element is less or equal than the second element.
4
- /// See: https://en.wikipedia.org/wiki/Binary_heap
5
- export function binaryHeapPush<T>(inArray: Array<T>, inBegin: number, inEnd: number, inPred: (a: T, b: T) => boolean) {
6
- // new heap size
7
- const count = inEnd - inBegin;
8
-
9
- // start from the last element
10
- let current = count - 1;
11
- while (current > 0) {
12
- // get current element
13
- const currentElement = inArray[inBegin + current];
14
-
15
- // get parent element
16
- const parent = (current - 1) >> 1;
17
- const parentElement = inArray[inBegin + parent];
18
-
19
- // sort them so taht the parent is larger than the child
20
- if (inPred(parentElement, currentElement)) {
21
- const tempElement = parentElement;
22
- inArray[inBegin + parent] = currentElement;
23
- inArray[inBegin + current] = tempElement;
24
- current = parent;
25
- } else {
26
- // When there's no change, we're done
27
- break;
28
- }
29
- }
30
- }
31
-
32
- /// Pop an element from a binary max-heap.
33
- /// [inBegin, inEnd) must be a valid heap. The largest element will be removed from the heap. The heap will be [inBegin, inEnd - 1) after this call.
34
- /// inPred is a function that returns true if the first element is less or equal than the second element.
35
- /// See: https://en.wikipedia.org/wiki/Binary_heap
36
- export function binaryHeapPop<T>(inArray: Array<T>, inBegin: number, inEnd: number, inPred: (a: T, b: T) => boolean) {
37
- // Begin by moving the highest element to the end, this is the popped element
38
- const temp = inArray[inEnd - 1];
39
- inArray[inEnd - 1] = inArray[inBegin];
40
- inArray[inBegin] = temp;
41
-
42
- // New heap size
43
- const count = inEnd - inBegin - 1;
44
-
45
- // Start from the root
46
- let largest = 0;
47
- while (true) {
48
- // Get first child
49
- let child = (largest << 1) + 1;
50
-
51
- // Check if we're beyond the end of the heap, if so the 2nd child is also beyond the end
52
- if (child >= count) {
53
- break;
54
- }
55
-
56
- // Remember the largest element from the previous iteration
57
- const prevLargest = largest;
58
-
59
- // Check if first child is bigger, if so select it
60
- if (inPred(inArray[inBegin + largest], inArray[inBegin + child])) {
61
- largest = child;
62
- }
63
-
64
- // Switch to the second child
65
- ++child;
66
-
67
- // Check if second child is bigger, if so select it
68
- if (child < count && inPred(inArray[inBegin + largest], inArray[inBegin + child])) {
69
- largest = child;
70
- }
71
-
72
- // If there was no change, we're done
73
- if (prevLargest === largest) {
74
- break;
75
- }
76
-
77
- // Swap element
78
- const tempElement = inArray[inBegin + prevLargest];
79
- inArray[inBegin + prevLargest] = inArray[inBegin + largest];
80
- inArray[inBegin + largest] = tempElement;
81
- }
82
- }
@@ -1,227 +0,0 @@
1
- import {
2
- BooleanType,
3
- createClass,
4
- LazyReferenceType,
5
- MonomorphType,
6
- NumberType,
7
- PropertyDefinitionMap,
8
- PropertyDefinitionReference,
9
- } from "monomorph";
10
- import { Vec3 } from "../../math/vec3";
11
- import { assert } from "../../helpers";
12
-
13
- const edgeProps = {
14
- neighbourTriangle: LazyReferenceType((() => Triangle) as () => never) as PropertyDefinitionReference<
15
- Triangle | null,
16
- true
17
- >,
18
- neighbourEdge: NumberType(0),
19
- startIndex: NumberType(0),
20
- } as const satisfies PropertyDefinitionMap;
21
-
22
- export class Edge extends createClass<Edge, typeof edgeProps>(edgeProps) {
23
- //
24
- }
25
-
26
- const triangleProps = {
27
- edge0: MonomorphType(Edge),
28
- edge1: MonomorphType(Edge),
29
- edge2: MonomorphType(Edge),
30
- normal: MonomorphType(Vec3),
31
- centroid: MonomorphType(Vec3),
32
- closestLengthSq: NumberType(+Infinity),
33
- lambda0: NumberType(0.0),
34
- lambda1: NumberType(0.0),
35
- lambdaRelativeTo0: BooleanType(false),
36
- closestPointInterior: BooleanType(false),
37
- removed: BooleanType(false),
38
- inQueue: BooleanType(false),
39
- iteration: NumberType(0),
40
- } as const satisfies PropertyDefinitionMap;
41
-
42
- const vectorAB = /*@__PURE__*/ Vec3.create();
43
-
44
- const y10 = /*@__PURE__*/ Vec3.create();
45
- const y20 = /*@__PURE__*/ Vec3.create();
46
- const y21 = /*@__PURE__*/ Vec3.create();
47
-
48
- export function initTriangle(
49
- m: Triangle,
50
- inIdx0: number,
51
- inIdx1: number,
52
- inIdx2: number,
53
- inPositions: Vec3[],
54
- minTriangleArea: number,
55
- barycentricEpsilon: number
56
- ) {
57
- // reset defaults
58
- m.closestLengthSq = +Infinity;
59
- m.lambda[0] = 0.0;
60
- m.lambda[1] = 0.0;
61
- m.lambdaRelativeTo0 = false;
62
- m.closestPointInterior = false;
63
- m.removed = false;
64
- m.inQueue = false;
65
- m.iteration = 0;
66
-
67
- // #v-ifdef DEV
68
- // Fill in indexes
69
- assert(inIdx0 !== inIdx1 && inIdx0 !== inIdx2 && inIdx1 !== inIdx2, "Triangle indices must be unique");
70
- // #v-endif
71
- m.edge[0].startIndex = inIdx0;
72
- m.edge[1].startIndex = inIdx1;
73
- m.edge[2].startIndex = inIdx2;
74
-
75
- // Clear links
76
- m.edge[0].neighbourTriangle = null;
77
- m.edge[1].neighbourTriangle = null;
78
- m.edge[2].neighbourTriangle = null;
79
-
80
- // Get vertex positions
81
- const y0 = inPositions[inIdx0];
82
- const y1 = inPositions[inIdx1];
83
- const y2 = inPositions[inIdx2];
84
-
85
- // Calculate centroid
86
- m.centroid.zero();
87
- m.centroid.addVector(y0);
88
- m.centroid.addVector(y1);
89
- m.centroid.addVector(y2);
90
- m.centroid.scale(1 / 3.0);
91
-
92
- // Calculate edges
93
- y10.subtractVectors(y1, y0);
94
- y20.subtractVectors(y2, y0);
95
- y21.subtractVectors(y2, y1);
96
-
97
- // The most accurate normal is calculated by using the two shortest edges
98
- // See: https://box2d.org/posts/2014/01/troublesome-triangle/
99
- // The difference in normals is most pronounced when one edge is much smaller than the others (in which case the other 2 must have roughly the same length).
100
- // Therefore we can suffice by just picking the shortest from 2 edges and use that with the 3rd edge to calculate the normal.
101
- // We first check which of the edges is shorter.
102
- const y20DotY20 = y20.dot(y20);
103
- const y21DotY21 = y21.dot(y21);
104
- if (y20DotY20 < y21DotY21) {
105
- // We select the edges y10 and y20
106
- m.normal.crossVectors(y10, y20);
107
-
108
- // Check if triangle is degenerate
109
- const normalLenSq = m.normal.squaredLength();
110
- if (normalLenSq > minTriangleArea) {
111
- // Determine distance between triangle and origin: distance = (centroid - origin) . normal / |normal|
112
- // Note that this way of calculating the closest point is much more accurate than first calculating barycentric coordinates and then calculating the closest
113
- // point based on those coordinates. Note that we preserve the sign of the distance to check on which side the origin is.
114
- const cDotN = m.centroid.dot(m.normal);
115
- m.closestLengthSq = (Math.abs(cDotN) * cDotN) / normalLenSq;
116
-
117
- // Calculate closest point to origin using barycentric coordinates:
118
- //
119
- // v = y0 + l0 * (y1 - y0) + l1 * (y2 - y0)
120
- // v . (y1 - y0) = 0
121
- // v . (y2 - y0) = 0
122
- //
123
- // Written in matrix form:
124
- //
125
- // | y10.y10 y20.y10 | | l0 | = | -y0.y10 |
126
- // | y10.y20 y20.y20 | | l1 | | -y0.y20 |
127
- //
128
- // (y10 = y1 - y0 etc.)
129
- //
130
- // Cramers rule to invert matrix:
131
- const y10DotY10 = y10.squaredLength();
132
- const y10DotY20 = y10.dot(y20);
133
- const determinant = y10DotY10 * y20DotY20 - y10DotY20 * y10DotY20;
134
- if (determinant > 0.0) {
135
- // If determinant == 0 then the system is linearly dependent and the triangle is degenerate, since y10.y10 * y20.y20 > y10.y20^2 it should also be > 0
136
- const y0DotY10 = y0.dot(y10);
137
- const y0DotY20 = y0.dot(y20);
138
- const l0 = (y10DotY20 * y0DotY20 - y20DotY20 * y0DotY10) / determinant;
139
- const l1 = (y10DotY20 * y0DotY10 - y10DotY10 * y0DotY20) / determinant;
140
- m.lambda[0] = l0;
141
- m.lambda[1] = l1;
142
- m.lambdaRelativeTo0 = true;
143
-
144
- // Check if closest point is interior to the triangle. For a convex hull which contains the origin each face must contain the origin, but because
145
- // our faces are triangles, we can have multiple coplanar triangles and only 1 will have the origin as an interior point. We want to use this triangle
146
- // to calculate the contact points because it gives the most accurate results, so we will only add these triangles to the priority queue.
147
- if (l0 > -barycentricEpsilon && l1 > -barycentricEpsilon && l0 + l1 < 1.0 + barycentricEpsilon) {
148
- m.closestPointInterior = true;
149
- }
150
- }
151
- }
152
- } else {
153
- // We select the edges y10 and y21
154
- m.normal.crossVectors(y10, y21);
155
-
156
- // Check if triangle is degenerate
157
- const normalLenSq = m.normal.squaredLength();
158
- if (normalLenSq > minTriangleArea) {
159
- // Again calculate distance between triangle and origin
160
- const cDotN = m.centroid.dot(m.normal);
161
- m.closestLengthSq = (Math.abs(cDotN) * cDotN) / normalLenSq;
162
-
163
- // Calculate closest point to origin using barycentric coordinates but this time using y1 as the reference vertex
164
- //
165
- // v = y1 + l0 * (y0 - y1) + l1 * (y2 - y1)
166
- // v . (y0 - y1) = 0
167
- // v . (y2 - y1) = 0
168
- //
169
- // Written in matrix form:
170
- //
171
- // | y10.y10 -y21.y10 | | l0 | = | y1.y10 |
172
- // | -y10.y21 y21.y21 | | l1 | | -y1.y21 |
173
- //
174
- // Cramers rule to invert matrix:
175
- const y10DotY10 = y10.squaredLength();
176
- const y10DotY21 = y10.dot(y21);
177
- const determinant = y10DotY10 * y21DotY21 - y10DotY21 * y10DotY21;
178
- if (determinant > 0.0) {
179
- const y1DotY10 = y1.dot(y10);
180
- const y1DotY21 = y1.dot(y21);
181
- const l0 = (y21DotY21 * y1DotY10 - y10DotY21 * y1DotY21) / determinant;
182
- const l1 = (y10DotY21 * y1DotY10 - y10DotY10 * y1DotY21) / determinant;
183
- m.lambda[0] = l0;
184
- m.lambda[1] = l1;
185
- m.lambdaRelativeTo0 = false;
186
-
187
- // Again check if the closest point is inside the triangle
188
- if (l0 > -barycentricEpsilon && l1 > -barycentricEpsilon && l0 + l1 < 1.0 + barycentricEpsilon) {
189
- m.closestPointInterior = true;
190
- }
191
- }
192
- }
193
- }
194
- }
195
-
196
- // TODO: pass empty arrays into createClass
197
- export class Triangle extends createClass<Triangle, typeof triangleProps>(triangleProps) {
198
- declare edge: [Edge, Edge, Edge];
199
- declare lambda: [number, number];
200
-
201
- isFacing(inPosition: Vec3) {
202
- // #v-ifdef DEV
203
- assert(!this.removed, "Triangle is removed, cannot check if facing");
204
- // #v-endif
205
- vectorAB.subtractVectors(inPosition, this.centroid);
206
- return this.normal.dot(vectorAB) > 0.0;
207
- }
208
-
209
- isFacingOrigin() {
210
- // #v-ifdef DEV
211
- assert(!this.removed, "Triangle is removed, cannot check if facing origin");
212
- // #v-endif
213
- return this.normal.dot(this.centroid) < 0.0;
214
- }
215
-
216
- getNextEdge(inIndex: number) {
217
- return this.edge[(inIndex + 1) % 3];
218
- }
219
- }
220
-
221
- const oldTriangleCreate = Triangle.create;
222
- Triangle.create = function () {
223
- const self = oldTriangleCreate.apply(this, arguments as any);
224
- self.edge = [self.edge0, self.edge1, self.edge2];
225
- self.lambda = [self.lambda0, self.lambda1];
226
- return self;
227
- };