@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
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Isometry } from "../../math/isometry";
|
|
2
|
+
import { Aabb } from "../../shape/Aabb";
|
|
3
|
+
import { HeightMap } from "../../shape/HeightMap";
|
|
4
|
+
import { ConvexShape } from "../../shape/Shape";
|
|
5
|
+
import { PenetrationDepthModule } from "../gjk/PenetrationDepthModule";
|
|
6
|
+
import { BackFaceMode, CollisionCollector, CollisionSettings } from "./collide";
|
|
7
|
+
import { HeightMapCollider } from "../HeightMapCollider";
|
|
8
|
+
import { Body } from "../../physics/Body";
|
|
9
|
+
import { Vec3 } from "../../math/vec3";
|
|
10
|
+
|
|
11
|
+
const isometryWorldToB = /*@__PURE__*/ Isometry.create();
|
|
12
|
+
const isometryAToB = /*@__PURE__*/ Isometry.create();
|
|
13
|
+
const shapeABoundsInBSpace = /*@__PURE__*/ Aabb.create();
|
|
14
|
+
|
|
15
|
+
const heightMapCollider = /*@__PURE__*/ new HeightMapCollider();
|
|
16
|
+
|
|
17
|
+
export function collideConvexVsHeightMap(
|
|
18
|
+
penetrationDepthModule: PenetrationDepthModule,
|
|
19
|
+
collector: CollisionCollector,
|
|
20
|
+
shapeA: ConvexShape,
|
|
21
|
+
shapeB: HeightMap,
|
|
22
|
+
isometryA: Isometry,
|
|
23
|
+
isometryB: Isometry,
|
|
24
|
+
settings: CollisionSettings,
|
|
25
|
+
bodyA: Body | null,
|
|
26
|
+
bodyB: Body | null,
|
|
27
|
+
initialDirection?: Vec3,
|
|
28
|
+
subShapeIdA: number = 0,
|
|
29
|
+
subShapeIdB: number = 0
|
|
30
|
+
) {
|
|
31
|
+
// TODO: don't hardcode this option
|
|
32
|
+
settings.backFaceMode = BackFaceMode.CollideWithBackFaces;
|
|
33
|
+
|
|
34
|
+
heightMapCollider.triangleCollider.initialize(
|
|
35
|
+
penetrationDepthModule,
|
|
36
|
+
shapeA,
|
|
37
|
+
isometryA,
|
|
38
|
+
isometryB,
|
|
39
|
+
settings,
|
|
40
|
+
collector,
|
|
41
|
+
undefined,
|
|
42
|
+
bodyA,
|
|
43
|
+
bodyB
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
isometryWorldToB.matrix.invertMatrix(isometryB.matrix);
|
|
47
|
+
isometryAToB.matrix.multiplyMatrices(isometryWorldToB.matrix, isometryA.matrix);
|
|
48
|
+
shapeABoundsInBSpace.copy(shapeA.computedAabb);
|
|
49
|
+
shapeABoundsInBSpace.transform(isometryAToB);
|
|
50
|
+
shapeABoundsInBSpace.expand(settings.maxSeparation);
|
|
51
|
+
heightMapCollider.initialize(shapeABoundsInBSpace, bodyA, bodyB);
|
|
52
|
+
shapeB.walkHeightMap(heightMapCollider);
|
|
53
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Isometry } from "../../math/isometry";
|
|
2
|
+
import { Aabb } from "../../shape/Aabb";
|
|
3
|
+
import { ConvexShape } from "../../shape/Shape";
|
|
4
|
+
import { PenetrationDepthModule } from "../gjk/PenetrationDepthModule";
|
|
5
|
+
import { ActiveEdgeMode, BackFaceMode, CollectFacesMode, CollisionCollector, CollisionSettings } from "./collide";
|
|
6
|
+
import { TriangleMesh } from "../../shape/TriangleMesh";
|
|
7
|
+
import { Triangle } from "../../shape/Triangle";
|
|
8
|
+
import { TriangleCollider2 } from "../TriangleCollider2";
|
|
9
|
+
import { Body } from "../../physics/Body";
|
|
10
|
+
import { Vec3 } from "../../math/vec3";
|
|
11
|
+
|
|
12
|
+
const isometryWorldToB = /*@__PURE__*/ Isometry.create();
|
|
13
|
+
const isometryAToB = /*@__PURE__*/ Isometry.create();
|
|
14
|
+
const shapeABoundsInBSpace = /*@__PURE__*/ Aabb.create();
|
|
15
|
+
const triangleCollider = /*@__PURE__*/ new TriangleCollider2();
|
|
16
|
+
|
|
17
|
+
function onHit(triangle: Triangle): boolean {
|
|
18
|
+
triangleCollider.collide(triangle, triangle.activeEdges, triangle.subShapeId, undefined, undefined, false);
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function collideConvexVsTriangleMesh(
|
|
23
|
+
penetrationDepthModule: PenetrationDepthModule,
|
|
24
|
+
collector: CollisionCollector,
|
|
25
|
+
shapeA: ConvexShape,
|
|
26
|
+
shapeB: TriangleMesh,
|
|
27
|
+
isometryA: Isometry,
|
|
28
|
+
isometryB: Isometry,
|
|
29
|
+
settings: CollisionSettings,
|
|
30
|
+
bodyA: Body | null,
|
|
31
|
+
bodyB: Body | null,
|
|
32
|
+
initialDirection?: Vec3,
|
|
33
|
+
subShapeIdA: number = 0,
|
|
34
|
+
subShapeIdB: number = 0
|
|
35
|
+
): void {
|
|
36
|
+
// TODO: don't hardcode this option
|
|
37
|
+
settings.backFaceMode = BackFaceMode.IgnoreBackFaces;
|
|
38
|
+
settings.collectFacesMode = CollectFacesMode.CollectFaces;
|
|
39
|
+
settings.activeEdgeMode = ActiveEdgeMode.CollideWithAll;
|
|
40
|
+
|
|
41
|
+
triangleCollider.initialize(
|
|
42
|
+
penetrationDepthModule,
|
|
43
|
+
shapeA,
|
|
44
|
+
isometryA,
|
|
45
|
+
isometryB,
|
|
46
|
+
settings,
|
|
47
|
+
collector,
|
|
48
|
+
subShapeIdA,
|
|
49
|
+
bodyA,
|
|
50
|
+
bodyB
|
|
51
|
+
);
|
|
52
|
+
isometryWorldToB.matrix.invertMatrix(isometryB.matrix);
|
|
53
|
+
isometryAToB.matrix.multiplyMatrices(isometryWorldToB.matrix, isometryA.matrix);
|
|
54
|
+
shapeABoundsInBSpace.copy(shapeA.computedAabb);
|
|
55
|
+
shapeABoundsInBSpace.transform(isometryAToB);
|
|
56
|
+
shapeABoundsInBSpace.expand(settings.maxSeparation);
|
|
57
|
+
shapeB.bvh!.intersectAabb(onHit, shapeABoundsInBSpace);
|
|
58
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Isometry } from "../../math/isometry";
|
|
2
|
+
import { Vec3 } from "../../math/vec3";
|
|
3
|
+
import { Aabb } from "../../shape/Aabb";
|
|
4
|
+
import { CompoundShape } from "../../shape/CompoundShape";
|
|
5
|
+
import { HeightMap } from "../../shape/HeightMap";
|
|
6
|
+
import { PenetrationDepthModule } from "../gjk/PenetrationDepthModule";
|
|
7
|
+
import { BackFaceMode, CollisionCollector, CollisionSettings } from "./collide";
|
|
8
|
+
import { HeightMapCollider } from "../HeightMapCollider";
|
|
9
|
+
import { Body } from "../../physics/Body";
|
|
10
|
+
import { ConvexShape } from "../../shape/Shape";
|
|
11
|
+
|
|
12
|
+
const isometryWorldToA = /*@__PURE__*/ Isometry.create();
|
|
13
|
+
const isometryBToA = /*@__PURE__*/ Isometry.create();
|
|
14
|
+
const shapeBBoundsInASpace = /*@__PURE__*/ Aabb.create();
|
|
15
|
+
|
|
16
|
+
const transformSubShapeToB = /*@__PURE__*/ Isometry.create();
|
|
17
|
+
const transformSubShapeToWorld = /*@__PURE__*/ Isometry.create();
|
|
18
|
+
|
|
19
|
+
const heightMapCollider = /*@__PURE__*/ new HeightMapCollider();
|
|
20
|
+
|
|
21
|
+
export function collideHeightMapVsCompound(
|
|
22
|
+
penetrationDepthModule: PenetrationDepthModule,
|
|
23
|
+
collector: CollisionCollector,
|
|
24
|
+
shapeA: HeightMap,
|
|
25
|
+
shapeB: CompoundShape,
|
|
26
|
+
isometryA: Isometry,
|
|
27
|
+
isometryB: Isometry,
|
|
28
|
+
settings: CollisionSettings,
|
|
29
|
+
bodyA: Body | null,
|
|
30
|
+
bodyB: Body | null,
|
|
31
|
+
initialDirection?: Vec3,
|
|
32
|
+
subShapeIdA: number = 0,
|
|
33
|
+
subShapeIdB: number = 0
|
|
34
|
+
) {
|
|
35
|
+
// TODO: don't hardcode this option
|
|
36
|
+
settings.backFaceMode = BackFaceMode.CollideWithBackFaces;
|
|
37
|
+
|
|
38
|
+
let index = 0;
|
|
39
|
+
for (const { shape, transform } of shapeB.shapes) {
|
|
40
|
+
const translation = transform.position;
|
|
41
|
+
const rotation = transform.rotation;
|
|
42
|
+
|
|
43
|
+
// set the isometries
|
|
44
|
+
transformSubShapeToB.fromRotationAndTranslation(rotation, translation);
|
|
45
|
+
transformSubShapeToWorld.matrix.multiplyMatrices(isometryB.matrix, transformSubShapeToB.matrix);
|
|
46
|
+
|
|
47
|
+
heightMapCollider.triangleCollider.initialize(
|
|
48
|
+
penetrationDepthModule,
|
|
49
|
+
shape as ConvexShape,
|
|
50
|
+
transformSubShapeToWorld,
|
|
51
|
+
isometryA,
|
|
52
|
+
settings,
|
|
53
|
+
collector,
|
|
54
|
+
index
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
isometryWorldToA.matrix.invertMatrix(isometryA.matrix);
|
|
58
|
+
|
|
59
|
+
isometryBToA.matrix.multiplyMatrices(transformSubShapeToWorld.matrix, isometryWorldToA.matrix);
|
|
60
|
+
|
|
61
|
+
shapeBBoundsInASpace.copy((shape as ConvexShape).computedAabb);
|
|
62
|
+
shapeBBoundsInASpace.transform(isometryBToA);
|
|
63
|
+
shapeBBoundsInASpace.expand(settings.maxSeparation);
|
|
64
|
+
heightMapCollider.initialize(shapeBBoundsInASpace, bodyB, bodyA);
|
|
65
|
+
shapeA.walkHeightMap(heightMapCollider);
|
|
66
|
+
|
|
67
|
+
index++;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Isometry } from "../../math/isometry";
|
|
2
|
+
import { Vec3 } from "../../math/vec3";
|
|
3
|
+
import { Aabb } from "../../shape/Aabb";
|
|
4
|
+
import { HeightMap } from "../../shape/HeightMap";
|
|
5
|
+
import { ConvexShape } from "../../shape/Shape";
|
|
6
|
+
import { PenetrationDepthModule } from "../gjk/PenetrationDepthModule";
|
|
7
|
+
import { BackFaceMode, CollisionCollector, CollisionSettings } from "./collide";
|
|
8
|
+
import { HeightMapCollider } from "../HeightMapCollider";
|
|
9
|
+
import { Body } from "../../physics/Body";
|
|
10
|
+
|
|
11
|
+
const isometryWorldToA = /*@__PURE__*/ Isometry.create();
|
|
12
|
+
const isometryBToA = /*@__PURE__*/ Isometry.create();
|
|
13
|
+
const shapeBBoundsInASpace = /*@__PURE__*/ Aabb.create();
|
|
14
|
+
|
|
15
|
+
const heightMapCollider = /*@__PURE__*/ new HeightMapCollider();
|
|
16
|
+
|
|
17
|
+
export function collideHeightMapVsConvex(
|
|
18
|
+
penetrationDepthModule: PenetrationDepthModule,
|
|
19
|
+
collector: CollisionCollector,
|
|
20
|
+
shapeA: HeightMap,
|
|
21
|
+
shapeB: ConvexShape,
|
|
22
|
+
isometryA: Isometry,
|
|
23
|
+
isometryB: Isometry,
|
|
24
|
+
settings: CollisionSettings,
|
|
25
|
+
bodyA: Body | null,
|
|
26
|
+
bodyB: Body | null,
|
|
27
|
+
initialDirection?: Vec3,
|
|
28
|
+
subShapeIdA: number = 0,
|
|
29
|
+
subShapeIdB: number = 0
|
|
30
|
+
) {
|
|
31
|
+
// TODO: don't hardcode this option
|
|
32
|
+
settings.backFaceMode = BackFaceMode.CollideWithBackFaces;
|
|
33
|
+
|
|
34
|
+
heightMapCollider.triangleCollider.initialize(
|
|
35
|
+
penetrationDepthModule,
|
|
36
|
+
shapeB,
|
|
37
|
+
isometryB,
|
|
38
|
+
isometryA,
|
|
39
|
+
settings,
|
|
40
|
+
collector,
|
|
41
|
+
undefined,
|
|
42
|
+
bodyB,
|
|
43
|
+
bodyA
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
isometryWorldToA.matrix.invertMatrix(isometryA.matrix);
|
|
47
|
+
isometryBToA.matrix.multiplyMatrices(isometryWorldToA.matrix, isometryB.matrix);
|
|
48
|
+
shapeBBoundsInASpace.copy(shapeB.computedAabb);
|
|
49
|
+
shapeBBoundsInASpace.transform(isometryBToA);
|
|
50
|
+
shapeBBoundsInASpace.expand(settings.maxSeparation);
|
|
51
|
+
heightMapCollider.initialize(shapeBBoundsInASpace, bodyB, bodyA);
|
|
52
|
+
shapeA.walkHeightMap(heightMapCollider);
|
|
53
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Isometry } from "../../math/isometry";
|
|
2
|
+
import { squared } from "../../math/scalar";
|
|
3
|
+
import { Vec3 } from "../../math/vec3";
|
|
4
|
+
import { Body } from "../../physics/Body";
|
|
5
|
+
import { Sphere } from "../../shape/Sphere";
|
|
6
|
+
import { PenetrationDepthModule } from "../gjk/PenetrationDepthModule";
|
|
7
|
+
import { CollisionCollector, CollisionResult, CollisionSettings } from "./collide";
|
|
8
|
+
|
|
9
|
+
const result = CollisionResult.create({
|
|
10
|
+
faceA: {
|
|
11
|
+
numVertices: 0,
|
|
12
|
+
buffer: { pool: new Vec3.Pool(32), maxLength: 32 },
|
|
13
|
+
},
|
|
14
|
+
faceB: {
|
|
15
|
+
numVertices: 0,
|
|
16
|
+
buffer: { pool: new Vec3.Pool(32), maxLength: 32 },
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const positionA = /*@__PURE__*/ Vec3.create();
|
|
21
|
+
const positionB = /*@__PURE__*/ Vec3.create();
|
|
22
|
+
const ab = /*@__PURE__*/ Vec3.create();
|
|
23
|
+
|
|
24
|
+
// TODO: use settings
|
|
25
|
+
// TODO: use initialDirection
|
|
26
|
+
export function collideSphereVsSphere(
|
|
27
|
+
penetrationDepthModule: PenetrationDepthModule,
|
|
28
|
+
collector: CollisionCollector,
|
|
29
|
+
shapeA: Sphere,
|
|
30
|
+
shapeB: Sphere,
|
|
31
|
+
isometryA: Isometry,
|
|
32
|
+
isometryB: Isometry,
|
|
33
|
+
settings: CollisionSettings,
|
|
34
|
+
bodyA: Body | null,
|
|
35
|
+
bodyB: Body | null,
|
|
36
|
+
initialDirection?: Vec3,
|
|
37
|
+
subShapeIdA: number = 0,
|
|
38
|
+
subShapeIdB: number = 0
|
|
39
|
+
) {
|
|
40
|
+
result.reset();
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
result.bodyA = bodyA;
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
result.bodyB = bodyB;
|
|
45
|
+
|
|
46
|
+
// get positions
|
|
47
|
+
isometryA.matrix.getTranslation(positionA);
|
|
48
|
+
isometryB.matrix.getTranslation(positionB);
|
|
49
|
+
|
|
50
|
+
// get distance
|
|
51
|
+
ab.subtractVectors(positionB, positionA);
|
|
52
|
+
|
|
53
|
+
// exit if no contact
|
|
54
|
+
if (ab.squaredLength() > squared(shapeA.radius + shapeB.radius)) {
|
|
55
|
+
result.hasContact = false;
|
|
56
|
+
collector.addMiss();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// set results
|
|
61
|
+
result.hasContact = true;
|
|
62
|
+
// @ts-ignore
|
|
63
|
+
result.bodyA = bodyA;
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
result.bodyB = bodyB;
|
|
66
|
+
result.subShapeIdA = subShapeIdA;
|
|
67
|
+
result.subShapeIdB = subShapeIdB;
|
|
68
|
+
result.normalA.normalizeVector(ab);
|
|
69
|
+
result.normalB.negateVector(result.normalA);
|
|
70
|
+
result.contactPointA.addScaledToVector(positionA, result.normalA, shapeA.radius);
|
|
71
|
+
result.contactPointB.addScaledToVector(positionB, result.normalB, shapeB.radius);
|
|
72
|
+
result.momentArmA.subtractVectors(result.contactPointA, positionA);
|
|
73
|
+
result.momentArmB.subtractVectors(result.contactPointB, positionB);
|
|
74
|
+
result.penetration = shapeA.radius + shapeB.radius - ab.length();
|
|
75
|
+
|
|
76
|
+
// normalize the normals
|
|
77
|
+
result.normalA.normalizeVector(result.normalA);
|
|
78
|
+
result.normalB.normalizeVector(result.normalB);
|
|
79
|
+
|
|
80
|
+
collector.addHit(result);
|
|
81
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Isometry } from "../../math/isometry";
|
|
2
|
+
import { Vec3 } from "../../math/vec3";
|
|
3
|
+
import { PenetrationDepthModule } from "../gjk/PenetrationDepthModule";
|
|
4
|
+
import { CollisionCollector, CollisionSettings } from "./collide";
|
|
5
|
+
import { CompoundShape } from "../../shape/CompoundShape";
|
|
6
|
+
import { TriangleMesh } from "../../shape/TriangleMesh";
|
|
7
|
+
import { collideTriangleMeshVsConvex } from "./collideTriangleMeshVsConvex";
|
|
8
|
+
import { Body } from "../../physics/Body";
|
|
9
|
+
import { ConvexShape } from "../..";
|
|
10
|
+
|
|
11
|
+
const transformSubShapeToB = /*@__PURE__*/ Isometry.create();
|
|
12
|
+
const transformSubShapeToWorld = /*@__PURE__*/ Isometry.create();
|
|
13
|
+
|
|
14
|
+
// the results are in world space
|
|
15
|
+
export function collideTriangleMeshVsCompound(
|
|
16
|
+
penetrationDepthModule: PenetrationDepthModule,
|
|
17
|
+
collector: CollisionCollector,
|
|
18
|
+
shapeA: TriangleMesh,
|
|
19
|
+
shapeB: CompoundShape,
|
|
20
|
+
isometryA: Isometry,
|
|
21
|
+
isometryB: Isometry,
|
|
22
|
+
settings: CollisionSettings,
|
|
23
|
+
bodyA: Body | null,
|
|
24
|
+
bodyB: Body | null,
|
|
25
|
+
initialDirection?: Vec3,
|
|
26
|
+
subShapeIdA: number = 0,
|
|
27
|
+
subShapeIdB: number = 0
|
|
28
|
+
): void {
|
|
29
|
+
// throw new Error("Not implemented");
|
|
30
|
+
|
|
31
|
+
let index = 0;
|
|
32
|
+
for (const { shape, transform } of shapeB.shapes) {
|
|
33
|
+
const translation = transform.position;
|
|
34
|
+
const rotation = transform.rotation;
|
|
35
|
+
|
|
36
|
+
// set the isometries
|
|
37
|
+
transformSubShapeToB.fromRotationAndTranslation(rotation, translation);
|
|
38
|
+
transformSubShapeToWorld.matrix.multiplyMatrices(isometryB.matrix, transformSubShapeToB.matrix);
|
|
39
|
+
|
|
40
|
+
// collide the shapes. TODO: this should be convex vs convex for now, but may be expanded in the future
|
|
41
|
+
collideTriangleMeshVsConvex(
|
|
42
|
+
penetrationDepthModule,
|
|
43
|
+
collector,
|
|
44
|
+
shapeA,
|
|
45
|
+
shape as ConvexShape,
|
|
46
|
+
isometryA,
|
|
47
|
+
transformSubShapeToWorld,
|
|
48
|
+
settings,
|
|
49
|
+
bodyA,
|
|
50
|
+
bodyB,
|
|
51
|
+
initialDirection,
|
|
52
|
+
0,
|
|
53
|
+
index
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
index++;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Isometry } from "../../math/isometry";
|
|
2
|
+
import { Aabb } from "../../shape/Aabb";
|
|
3
|
+
import { ConvexShape } from "../../shape/Shape";
|
|
4
|
+
import { PenetrationDepthModule } from "../gjk/PenetrationDepthModule";
|
|
5
|
+
import { ActiveEdgeMode, BackFaceMode, CollectFacesMode, CollisionCollector, CollisionSettings } from "./collide";
|
|
6
|
+
import { TriangleMesh } from "../../shape/TriangleMesh";
|
|
7
|
+
import { Triangle } from "../../shape/Triangle";
|
|
8
|
+
import { TriangleCollider2 } from "../TriangleCollider2";
|
|
9
|
+
import { Body } from "../../physics/Body";
|
|
10
|
+
import { Vec3 } from "../../math/vec3";
|
|
11
|
+
|
|
12
|
+
const isometryWorldToA = /*@__PURE__*/ Isometry.create();
|
|
13
|
+
const isometryBToA = /*@__PURE__*/ Isometry.create();
|
|
14
|
+
const shapeBBoundsInASpace = /*@__PURE__*/ Aabb.create();
|
|
15
|
+
const triangleCollider = /*@__PURE__*/ new TriangleCollider2();
|
|
16
|
+
|
|
17
|
+
function onHit(triangle: Triangle): boolean {
|
|
18
|
+
triangleCollider.collide(triangle, triangle.activeEdges, triangle.subShapeId, undefined, undefined, true);
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function collideTriangleMeshVsConvex(
|
|
23
|
+
penetrationDepthModule: PenetrationDepthModule,
|
|
24
|
+
collector: CollisionCollector,
|
|
25
|
+
shapeA: TriangleMesh,
|
|
26
|
+
shapeB: ConvexShape,
|
|
27
|
+
isometryA: Isometry,
|
|
28
|
+
isometryB: Isometry,
|
|
29
|
+
settings: CollisionSettings,
|
|
30
|
+
bodyA: Body | null,
|
|
31
|
+
bodyB: Body | null,
|
|
32
|
+
initialDirection?: Vec3,
|
|
33
|
+
subShapeIdA: number = 0,
|
|
34
|
+
subShapeIdB: number = 0
|
|
35
|
+
): void {
|
|
36
|
+
// TODO: don't hardcode this option
|
|
37
|
+
settings.backFaceMode = BackFaceMode.IgnoreBackFaces;
|
|
38
|
+
settings.collectFacesMode = CollectFacesMode.CollectFaces;
|
|
39
|
+
settings.activeEdgeMode = ActiveEdgeMode.CollideWithAll;
|
|
40
|
+
|
|
41
|
+
triangleCollider.initialize(
|
|
42
|
+
penetrationDepthModule,
|
|
43
|
+
shapeB,
|
|
44
|
+
isometryB,
|
|
45
|
+
isometryA,
|
|
46
|
+
settings,
|
|
47
|
+
collector,
|
|
48
|
+
subShapeIdB,
|
|
49
|
+
bodyB,
|
|
50
|
+
bodyA
|
|
51
|
+
);
|
|
52
|
+
isometryWorldToA.matrix.invertMatrix(isometryA.matrix);
|
|
53
|
+
isometryBToA.matrix.multiplyMatrices(isometryWorldToA.matrix, isometryB.matrix);
|
|
54
|
+
shapeBBoundsInASpace.copy(shapeB.computedAabb);
|
|
55
|
+
shapeBBoundsInASpace.transform(isometryBToA);
|
|
56
|
+
shapeBBoundsInASpace.expand(settings.maxSeparation);
|
|
57
|
+
shapeA.bvh!.intersectAabb(onHit, shapeBBoundsInASpace);
|
|
58
|
+
}
|