@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.
Files changed (140) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +30 -0
  3. package/package.json +32 -0
  4. package/src/builders/ConvexHullBuilder.ts +437 -0
  5. package/src/builders/ConvexHullBuilder2d.ts +344 -0
  6. package/src/builders/ConvexHullBuilder3d.ts +1689 -0
  7. package/src/builders/HeightMapBuilder.ts +414 -0
  8. package/src/builders/TriangleMeshBuilder.ts +92 -0
  9. package/src/collision/CastShapesModule.ts +184 -0
  10. package/src/collision/CollideShapesModule.ts +152 -0
  11. package/src/collision/HeightMapCaster.ts +38 -0
  12. package/src/collision/HeightMapCollider.ts +33 -0
  13. package/src/collision/TriangleCaster.ts +249 -0
  14. package/src/collision/TriangleCollider.ts +308 -0
  15. package/src/collision/TriangleCollider2.ts +379 -0
  16. package/src/collision/activeEdge.ts +146 -0
  17. package/src/collision/cast/cast.ts +139 -0
  18. package/src/collision/cast/castCompoundVsCompound.ts +59 -0
  19. package/src/collision/cast/castCompoundVsConvex.ts +116 -0
  20. package/src/collision/cast/castConvexVsCompound.ts +123 -0
  21. package/src/collision/cast/castConvexVsConvex.ts +213 -0
  22. package/src/collision/cast/castConvexVsHeightMap.ts +73 -0
  23. package/src/collision/cast/castConvexVsTriangleMesh.ts +56 -0
  24. package/src/collision/cast/castRayVsCompound.ts +44 -0
  25. package/src/collision/cast/castRayVsConvex.ts +45 -0
  26. package/src/collision/cast/castRayVsHeightMap.ts +58 -0
  27. package/src/collision/cast/castRayVsTriangleMesh.ts +58 -0
  28. package/src/collision/closestPoints/closestPoints.ts +23 -0
  29. package/src/collision/closestPoints/computeBarycentricCoordinates2d.ts +32 -0
  30. package/src/collision/closestPoints/computeBarycentricCoordinates3d.ts +81 -0
  31. package/src/collision/closestPoints/computeClosestPointOnLine.ts +30 -0
  32. package/src/collision/closestPoints/computeClosestPointOnTetrahedron.ts +96 -0
  33. package/src/collision/closestPoints/computeClosestPointOnTriangle.ts +195 -0
  34. package/src/collision/closestPoints/isOriginOutsideOfPlane.ts +25 -0
  35. package/src/collision/closestPoints/isOriginOutsideOfTrianglePlanes.ts +72 -0
  36. package/src/collision/collide/collide.ts +146 -0
  37. package/src/collision/collide/collideCompoundVsCompound.ts +60 -0
  38. package/src/collision/collide/collideCompoundVsConvex.ts +59 -0
  39. package/src/collision/collide/collideCompoundVsHeightMap.ts +73 -0
  40. package/src/collision/collide/collideCompoundVsTriangleMesh.ts +56 -0
  41. package/src/collision/collide/collideConvexVsCompound.ts +57 -0
  42. package/src/collision/collide/collideConvexVsConvex.ts +225 -0
  43. package/src/collision/collide/collideConvexVsConvexImp.ts +236 -0
  44. package/src/collision/collide/collideConvexVsHeightMap.ts +53 -0
  45. package/src/collision/collide/collideConvexVsTriangleMesh.ts +58 -0
  46. package/src/collision/collide/collideHeightMapVsCompound.ts +69 -0
  47. package/src/collision/collide/collideHeightMapVsConvex.ts +53 -0
  48. package/src/collision/collide/collideSphereVsSphere.ts +81 -0
  49. package/src/collision/collide/collideTriangleMeshVsCompound.ts +58 -0
  50. package/src/collision/collide/collideTriangleMeshVsConvex.ts +58 -0
  51. package/src/collision/epa/EpaConvexHullBuilder.ts +397 -0
  52. package/src/collision/epa/StaticArray.ts +154 -0
  53. package/src/collision/epa/TriangleFactory.ts +32 -0
  54. package/src/collision/epa/arrays.ts +99 -0
  55. package/src/collision/epa/binaryHeap.ts +82 -0
  56. package/src/collision/epa/structs.ts +227 -0
  57. package/src/collision/gjk/GjkModule.ts +864 -0
  58. package/src/collision/gjk/PenetrationDepthModule.ts +493 -0
  59. package/src/collision/gjk/SupportPoints.ts +50 -0
  60. package/src/collision/imp/MinkowskiDifference.ts +36 -0
  61. package/src/collision/imp/computeExploredDistanceLowerUpperBound.ts +40 -0
  62. package/src/collision/imp/finalizeImpResult.ts +69 -0
  63. package/src/collision/imp/findContactImp.ts +196 -0
  64. package/src/collision/imp/imp.ts +28 -0
  65. package/src/collision/imp/incrementalMinimumDistanceExploreDirection.ts +207 -0
  66. package/src/collision/mpr/findPortal.ts +152 -0
  67. package/src/collision/mpr/mpr.ts +29 -0
  68. package/src/collision/mpr/updatePortal.ts +52 -0
  69. package/src/constraints/BaseConstraint.ts +50 -0
  70. package/src/constraints/ConstraintOptions.ts +22 -0
  71. package/src/constraints/ConstraintSolver.ts +119 -0
  72. package/src/constraints/DistanceConstraint.ts +229 -0
  73. package/src/constraints/FixedConstraint.ts +203 -0
  74. package/src/constraints/HingeConstraint.ts +460 -0
  75. package/src/constraints/PointConstraint.ts +108 -0
  76. package/src/constraints/components/AngleComponent.ts +226 -0
  77. package/src/constraints/components/AxisComponent.ts +263 -0
  78. package/src/constraints/components/HingeComponent.ts +215 -0
  79. package/src/constraints/components/Motor.ts +36 -0
  80. package/src/constraints/components/PointConstraintComponent.ts +179 -0
  81. package/src/constraints/components/RotationEulerComponent.ts +139 -0
  82. package/src/constraints/components/Spring.ts +30 -0
  83. package/src/constraints/components/SpringComponent.ts +71 -0
  84. package/src/constraints/types.ts +6 -0
  85. package/src/helpers.ts +147 -0
  86. package/src/index.ts +50 -0
  87. package/src/math/BasicTransform.ts +19 -0
  88. package/src/math/NumberValue.ts +13 -0
  89. package/src/math/isometry.ts +64 -0
  90. package/src/math/mat3.ts +529 -0
  91. package/src/math/mat4.ts +588 -0
  92. package/src/math/quat.ts +193 -0
  93. package/src/math/scalar.ts +81 -0
  94. package/src/math/tensor.ts +17 -0
  95. package/src/math/vec3.ts +589 -0
  96. package/src/math/vec4.ts +10 -0
  97. package/src/physics/Body.ts +581 -0
  98. package/src/physics/CollisionFilter.ts +52 -0
  99. package/src/physics/SleepModule.ts +163 -0
  100. package/src/physics/broadphase/BodyPairsModule.ts +363 -0
  101. package/src/physics/broadphase/BvhModule.ts +237 -0
  102. package/src/physics/broadphase/BvhTree.ts +803 -0
  103. package/src/physics/broadphase/ConstraintPairsModule.ts +385 -0
  104. package/src/physics/broadphase/TriangleMeshBvhTree.ts +379 -0
  105. package/src/physics/manifold/ContactManifold.ts +227 -0
  106. package/src/physics/manifold/ContactManifoldModule.ts +623 -0
  107. package/src/physics/manifold/Face.ts +119 -0
  108. package/src/physics/manifold/ManifoldCache.ts +116 -0
  109. package/src/physics/manifold/clipping/clipPolyVsEdge.ts +131 -0
  110. package/src/physics/manifold/clipping/clipPolyVsPlane.ts +73 -0
  111. package/src/physics/manifold/clipping/clipPolyVsPoly.ts +72 -0
  112. package/src/physics/narrowphase/CollideBodiesModule.ts +755 -0
  113. package/src/physics/solver/ContactConstraintModule.ts +659 -0
  114. package/src/physics/solver/ManifoldConstraint.ts +420 -0
  115. package/src/physics/solver/estimateCollisionResponse.ts +146 -0
  116. package/src/shape/Aabb.ts +400 -0
  117. package/src/shape/Box.ts +231 -0
  118. package/src/shape/Capsule.ts +332 -0
  119. package/src/shape/CompoundShape.ts +288 -0
  120. package/src/shape/Convex.ts +130 -0
  121. package/src/shape/ConvexHull.ts +423 -0
  122. package/src/shape/Cylinder.ts +313 -0
  123. package/src/shape/HeightMap.ts +511 -0
  124. package/src/shape/Line.ts +14 -0
  125. package/src/shape/Plane.ts +116 -0
  126. package/src/shape/Ray.ts +81 -0
  127. package/src/shape/Segment.ts +25 -0
  128. package/src/shape/Shape.ts +77 -0
  129. package/src/shape/Sphere.ts +181 -0
  130. package/src/shape/TransformedShape.ts +51 -0
  131. package/src/shape/Triangle.ts +122 -0
  132. package/src/shape/TriangleMesh.ts +186 -0
  133. package/src/types.ts +1 -0
  134. package/src/world.ts +1335 -0
  135. package/tests/BodyPairsModule.test.ts +71 -0
  136. package/tests/BvhTree.test.ts +406 -0
  137. package/tests/test.md +642 -0
  138. package/tests/vec3.test.ts +12 -0
  139. package/tsconfig.json +20 -0
  140. package/vite.config.js +40 -0
@@ -0,0 +1,237 @@
1
+ import { NumberArray, WithPool } from "monomorph";
2
+ import { Vec3 } from "../../math/vec3";
3
+ import { Aabb } from "../../shape/Aabb";
4
+ import { Ray } from "../../shape/Ray";
5
+ import type { World } from "../../world";
6
+ import { Body, BodyType } from "../Body";
7
+ import { BodyPairsModule } from "./BodyPairsModule";
8
+ import { BvhTree, BvhTreeOptions } from "./BvhTree";
9
+
10
+ export interface BvhModuleOptions {
11
+ dynamicTreeOptions: BvhTreeOptions;
12
+ staticTreeOptions: BvhTreeOptions;
13
+ }
14
+
15
+ export class BvhModule {
16
+ _intersectTargetBody: Body | null;
17
+ _createPairCallback(bodyB: Body) {
18
+ if (bodyB !== this._intersectTargetBody) {
19
+ this.pairs.createPair(this._intersectTargetBody!, bodyB);
20
+ }
21
+ return false;
22
+ }
23
+
24
+ world: World;
25
+ options: BvhModuleOptions;
26
+ dynamicTree: BvhTree;
27
+ staticTree: BvhTree;
28
+ pairs: BodyPairsModule;
29
+
30
+ bodiesThatDidRefit: Set<Body>;
31
+
32
+ constructor(world: World, options?: Partial<BvhModuleOptions>) {
33
+ this._intersectTargetBody = null;
34
+
35
+ this.options = {
36
+ dynamicTreeOptions: {
37
+ expansionMargin: 0.1,
38
+ maxDepth: 32,
39
+ maxObjectsPerLeaf: 8,
40
+ },
41
+ staticTreeOptions: {
42
+ expansionMargin: 0.1,
43
+ maxDepth: 32,
44
+ maxObjectsPerLeaf: 8,
45
+ },
46
+ ...options,
47
+ };
48
+ this.world = world;
49
+ this.dynamicTree = new BvhTree(this.options.dynamicTreeOptions, world.pools.bvhNodes, world.pools.body);
50
+ this.staticTree = new BvhTree(this.options.staticTreeOptions, world.pools.bvhNodes, world.pools.body);
51
+ this.pairs = new BodyPairsModule(world.pools.body, world.pools.bodyPairNode, world.pools.bodyPairEdge);
52
+ this._createPairCallback = this._createPairCallback.bind(this);
53
+ this.bodiesThatDidRefit = new Set<Body>();
54
+ }
55
+
56
+ toArray(array: NumberArray, startOffset: number): number {
57
+ this.dynamicTree.updateDirtyObjects();
58
+ this.staticTree.updateDirtyObjects();
59
+ startOffset = this.world.pools.bvhNodes.toArray(array, startOffset);
60
+ startOffset = this.world.pools.bodyPairNode.toArray(array, startOffset);
61
+ startOffset = this.world.pools.bodyPairEdge.toArray(array, startOffset);
62
+ array[startOffset++] = this.dynamicTree.root?.index ?? -1;
63
+ array[startOffset++] = this.dynamicTree.root?.version ?? -1;
64
+ array[startOffset++] = this.staticTree.root?.index ?? -1;
65
+ array[startOffset++] = this.staticTree.root?.version ?? -1;
66
+ return startOffset;
67
+ }
68
+
69
+ fromArray(array: NumberArray, startOffset: number): number {
70
+ this.dynamicTree.dirtyObjects.clear();
71
+ this.staticTree.dirtyObjects.clear();
72
+
73
+ let startOffsetBodyPairNode;
74
+
75
+ // startOffset not advanced until following line, since there are circular references
76
+ this.world.pools.bvhNodes.fromArrayNoReferences(array, startOffset);
77
+ startOffset = this.world.pools.bvhNodes.fromArrayOnlyReferences(array, {
78
+ left: this.world.pools.bvhNodes,
79
+ right: this.world.pools.bvhNodes,
80
+ parent: this.world.pools.bvhNodes,
81
+ objects: this.world.pools.body,
82
+ }, startOffset);
83
+
84
+ startOffsetBodyPairNode = startOffset;
85
+ startOffset = this.world.pools.bodyPairNode.fromArrayNoReferences(array, startOffset);
86
+
87
+ this.world.pools.bodyPairEdge.fromArrayNoReferences(array, startOffset);
88
+ startOffset = this.world.pools.bodyPairEdge.fromArrayOnlyReferences(array, {
89
+ node: this.world.pools.bodyPairNode,
90
+ next: this.world.pools.bodyPairEdge,
91
+ prev: this.world.pools.bodyPairEdge,
92
+ }, startOffset);
93
+
94
+ this.world.pools.bodyPairNode.fromArrayOnlyReferences(array, {
95
+ bodyA: this.world.pools.body,
96
+ bodyB: this.world.pools.body,
97
+ edgeA: this.world.pools.bodyPairEdge,
98
+ edgeB: this.world.pools.bodyPairEdge,
99
+ }, startOffsetBodyPairNode);
100
+
101
+ const dynamicTreeRootIndex = array[startOffset++];
102
+ const dynamicTreeRootVersion = array[startOffset++];
103
+ const staticTreeRootIndex = array[startOffset++];
104
+ const staticTreeRootVersion = array[startOffset++];
105
+
106
+ if (dynamicTreeRootIndex >= 0) {
107
+ this.dynamicTree.root = this.world.pools.bvhNodes.array[dynamicTreeRootIndex];
108
+ if (dynamicTreeRootVersion !== this.dynamicTree.root.version) {
109
+ this.dynamicTree.root = null;
110
+ }
111
+ } else {
112
+ this.dynamicTree.root = null;
113
+ }
114
+
115
+ if (staticTreeRootIndex >= 0) {
116
+ this.staticTree.root = this.world.pools.bvhNodes.array[staticTreeRootIndex];
117
+ if (staticTreeRootVersion !== this.staticTree.root.version) {
118
+ this.staticTree.root = null;
119
+ }
120
+ } else {
121
+ this.staticTree.root = null;
122
+ }
123
+
124
+ return startOffset;
125
+ }
126
+
127
+ markDynamicBodyAsDirty(body: WithPool<Body>) {
128
+ this.dynamicTree.markObjectAsDirty(body);
129
+ }
130
+
131
+ updateDirtyBodies() {
132
+ for (const body of this.dynamicTree.dirtyObjects) {
133
+ this.dynamicTree.update(body, false);
134
+ }
135
+
136
+ for (const body of this.dynamicTree.dirtyObjects) {
137
+ this.pairs.destroyAllPairsOfOne(body as Body);
138
+ this._intersectTargetBody = body as Body;
139
+ this.dynamicTree.intersectBody(this._createPairCallback, body, false);
140
+ this.staticTree.intersectBody(this._createPairCallback, body, false);
141
+ }
142
+
143
+ this.dynamicTree.dirtyObjects.clear();
144
+ }
145
+
146
+ addDynamicBody(body: WithPool<Body>) {
147
+ this.dynamicTree.insert(body);
148
+ this._intersectTargetBody = body;
149
+ this.dynamicTree.intersectBody(this._createPairCallback, body);
150
+ this.staticTree.intersectBody(this._createPairCallback, body);
151
+ }
152
+
153
+ addKinematicBody(body: WithPool<Body>) {
154
+ this.dynamicTree.insert(body);
155
+ this._intersectTargetBody = body;
156
+ this.dynamicTree.intersectBody(this._createPairCallback, body);
157
+ this.staticTree.intersectBody(this._createPairCallback, body);
158
+ }
159
+
160
+ addStaticBody(body: WithPool<Body>) {
161
+ this.staticTree.insert(body);
162
+ this._intersectTargetBody = body;
163
+ this.dynamicTree.intersectBody(this._createPairCallback, body);
164
+ this.staticTree.intersectBody(this._createPairCallback, body);
165
+ }
166
+
167
+ removeDynamicBody(body: WithPool<Body>) {
168
+ this.dynamicTree.remove(body, true);
169
+ this.pairs.destroyAllPairsOfOne(body);
170
+ }
171
+
172
+ removeKinematicBody(body: WithPool<Body>) {
173
+ this.dynamicTree.remove(body, true);
174
+ this.pairs.destroyAllPairsOfOne(body);
175
+ }
176
+
177
+ removeStaticBody(body: WithPool<Body>) {
178
+ this.staticTree.remove(body, true);
179
+ this.pairs.destroyAllPairsOfOne(body);
180
+ }
181
+
182
+ *iteratePairs() {
183
+ this.updateDirtyBodies();
184
+ for (const node of this.pairs.iteratePairs()) {
185
+ // skip sleeping bodies
186
+ const bodyA = node.bodyA!;
187
+ const bodyB = node.bodyB!;
188
+
189
+ if (bodyA.type === BodyType.static && bodyB.type === BodyType.static) {
190
+ continue;
191
+ }
192
+
193
+ if (bodyA.type === BodyType.static && bodyB.type === BodyType.dynamic) {
194
+ if (bodyB.isSleeping) {
195
+ continue;
196
+ }
197
+ }
198
+
199
+ if (bodyA.type === BodyType.dynamic && bodyB.type === BodyType.static) {
200
+ if (bodyA.isSleeping) {
201
+ continue;
202
+ }
203
+ }
204
+
205
+ yield node;
206
+ }
207
+ }
208
+
209
+ visitBodiesWithCollision(bodyVisitor: BodyVisitor, aabb: Aabb, belongsToGroups: number, collidesWithGroups: number) {
210
+ // this.updateDirtyBodies();
211
+ this.dynamicTree.visitBodiesWithCollision(bodyVisitor, aabb, belongsToGroups, collidesWithGroups, false);
212
+ this.staticTree.visitBodiesWithCollision(bodyVisitor, aabb, belongsToGroups, collidesWithGroups, false);
213
+ }
214
+
215
+ visitBodiesWithCast(
216
+ bodyVisitor: BodyVisitor,
217
+ aabb: Aabb,
218
+ displacement: Vec3,
219
+ belongsToGroups: number,
220
+ collidesWithGroups: number
221
+ ) {
222
+ // this.updateDirtyBodies();
223
+ this.dynamicTree.visitBodiesWithCast(bodyVisitor, aabb, displacement, belongsToGroups, collidesWithGroups, false);
224
+ this.staticTree.visitBodiesWithCast(bodyVisitor, aabb, displacement, belongsToGroups, collidesWithGroups, false);
225
+ }
226
+
227
+ raycastBodies(bodyVisitor: BodyVisitor, ray: Ray, belongsToGroups: number, collidesWithGroups: number) {
228
+ // this.updateDirtyBodies();
229
+ this.dynamicTree.raycastBodies(bodyVisitor, ray, belongsToGroups, collidesWithGroups, false);
230
+ this.staticTree.raycastBodies(bodyVisitor, ray, belongsToGroups, collidesWithGroups, false);
231
+ }
232
+ }
233
+
234
+ export interface BodyVisitor {
235
+ shouldExit: boolean;
236
+ visit(body: Body): void;
237
+ }