@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,186 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createClass,
|
|
3
|
+
LazyReferenceType,
|
|
4
|
+
MonomorphType,
|
|
5
|
+
NumberType,
|
|
6
|
+
PropertyDefinitionMap,
|
|
7
|
+
PropertyDefinitionReference,
|
|
8
|
+
ReferenceListType,
|
|
9
|
+
} from "monomorph";
|
|
10
|
+
import { Triangle } from "./Triangle";
|
|
11
|
+
import { Aabb } from "./Aabb";
|
|
12
|
+
import { Vec3 } from "../math/vec3";
|
|
13
|
+
import { Mat3 } from "../math/mat3";
|
|
14
|
+
import { ShapeCollectionInterface, ShapeType } from "./Shape";
|
|
15
|
+
import type { World } from "../world";
|
|
16
|
+
import { Isometry } from "../math/isometry";
|
|
17
|
+
import { Face } from "../physics/manifold/Face";
|
|
18
|
+
import { Mat4 } from "../math/mat4";
|
|
19
|
+
import { Quat } from "../math/quat";
|
|
20
|
+
import { TriangleMeshBvhTree } from "../physics/broadphase/TriangleMeshBvhTree";
|
|
21
|
+
import { Ray } from "./Ray";
|
|
22
|
+
|
|
23
|
+
const isometry = /*@__PURE__*/ Isometry.create();
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Callback function for walking over the triangles of a triangle mesh.
|
|
27
|
+
* @param triangle The triangle that is being visited.
|
|
28
|
+
* @param activeEdges The number of active edges.
|
|
29
|
+
* @param triangleId The id of the triangle.
|
|
30
|
+
* @returns returns a stopIteration flag. return true to stop iterating, false to continue.
|
|
31
|
+
*/
|
|
32
|
+
const triangleMeshProps = {
|
|
33
|
+
computedCenterOfMass: MonomorphType(Vec3, undefined, true),
|
|
34
|
+
computedVolume: NumberType(0.0, true),
|
|
35
|
+
computedAabb: MonomorphType(Aabb, undefined, true),
|
|
36
|
+
translation: MonomorphType(Vec3),
|
|
37
|
+
inertia: MonomorphType(Mat3, undefined, true),
|
|
38
|
+
vertexPositions: ReferenceListType(Vec3),
|
|
39
|
+
faceIndices: ReferenceListType(Vec3),
|
|
40
|
+
triangles: ReferenceListType(Triangle),
|
|
41
|
+
copyForDiff: LazyReferenceType((() => TriangleMesh) as () => never) as PropertyDefinitionReference<
|
|
42
|
+
TriangleMesh | null,
|
|
43
|
+
true
|
|
44
|
+
>,
|
|
45
|
+
bvh: LazyReferenceType((() => TriangleMeshBvhTree) as () => never) as PropertyDefinitionReference<
|
|
46
|
+
TriangleMeshBvhTree | null,
|
|
47
|
+
true
|
|
48
|
+
>,
|
|
49
|
+
} as const satisfies PropertyDefinitionMap;
|
|
50
|
+
|
|
51
|
+
const afterConstructorCode = `
|
|
52
|
+
this.world = null;
|
|
53
|
+
`;
|
|
54
|
+
|
|
55
|
+
export class TriangleMesh
|
|
56
|
+
extends createClass<TriangleMesh, typeof triangleMeshProps>(triangleMeshProps, { afterConstructorCode })
|
|
57
|
+
implements ShapeCollectionInterface
|
|
58
|
+
{
|
|
59
|
+
type: ShapeType.triangleMesh = ShapeType.triangleMesh;
|
|
60
|
+
|
|
61
|
+
declare world: World | null;
|
|
62
|
+
|
|
63
|
+
// TODO: for direct broadphase queries
|
|
64
|
+
intersectsAabb(aabb: Aabb): boolean {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// TODO: for character controller
|
|
69
|
+
computeSurfaceNormal(out: Vec3, inLocalSurfacePosition: Vec3, subShapeId?: number): void {
|
|
70
|
+
//
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// TODO: for physics. this currently just uses convex hull's method
|
|
74
|
+
computeInertiaTensor(out: Mat3, mass: number): void {
|
|
75
|
+
out.multiplyMatrixByScalar(this.inertia, mass / this.computedVolume);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
computeInverseInertiaTensor(out: Mat3, mass: number): void {
|
|
79
|
+
this.computeInertiaTensor(out, mass);
|
|
80
|
+
out.invert();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
computeWorldBounds(out: Aabb, translation: Vec3, rotation: Quat) {
|
|
84
|
+
// TODO: apply rotation if shape's center of mass is not at the origin?
|
|
85
|
+
isometry.fromRotationAndTranslation(rotation, translation);
|
|
86
|
+
out.transformAabb(this.computedAabb, isometry);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// TODO: for narrowphase
|
|
90
|
+
computeSupportingFace(
|
|
91
|
+
out: Face,
|
|
92
|
+
subShapeID: number,
|
|
93
|
+
direction: Vec3,
|
|
94
|
+
scale: number,
|
|
95
|
+
centerOfMassTransform: Mat4
|
|
96
|
+
): void {
|
|
97
|
+
out.clear();
|
|
98
|
+
|
|
99
|
+
// scaling is not implemented yet
|
|
100
|
+
if (scale !== 1.0) {
|
|
101
|
+
throw new Error("scaling is not implemented yet");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const triangle = this.triangles!.getAtIndex(subShapeID)!;
|
|
105
|
+
|
|
106
|
+
out.pushVertex(triangle.a);
|
|
107
|
+
out.pushVertex(triangle.b);
|
|
108
|
+
out.pushVertex(triangle.c);
|
|
109
|
+
out.transform(centerOfMassTransform);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
createTriangulatedVerticesAndNormalsArray(): [Float32Array, Float32Array, Float32Array] {
|
|
113
|
+
const positions: number[] = [];
|
|
114
|
+
const normals: number[] = [];
|
|
115
|
+
const indices: number[] = [];
|
|
116
|
+
|
|
117
|
+
for (const position of this.vertexPositions!) {
|
|
118
|
+
positions.push(position.x, position.y, position.z);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
for (const index of this.faceIndices!) {
|
|
122
|
+
indices.push(index.x, index.y, index.z);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (const triangle of this.triangles!) {
|
|
126
|
+
normals.push(triangle.normal.x, triangle.normal.y, triangle.normal.z);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return [new Float32Array(positions), new Float32Array(normals), new Float32Array(indices)];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
iterateTriangles(onHit: (triangle: Triangle) => boolean): void {
|
|
133
|
+
for (const triangle of this.triangles) {
|
|
134
|
+
if (onHit(triangle)) {
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
iterateNearbyTriangles(shapeBounds: Aabb, onHit: (triangle: Triangle) => boolean): void {
|
|
141
|
+
this.bvh!.intersectAabb(onHit, shapeBounds);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
hasChanged() {
|
|
145
|
+
// TODO: we don't currently support changes for this shape
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
commitChanges() {
|
|
150
|
+
if (this.hasChanged()) {
|
|
151
|
+
updateShape(this);
|
|
152
|
+
// this.world?.updateBodyProperties();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function updateVolume(shape: TriangleMesh) {
|
|
158
|
+
// recomputing volume for this shape not supported yet
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function updateLocalBounds(shape: TriangleMesh) {
|
|
162
|
+
shape.computedAabb.min.fromArray([Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY]);
|
|
163
|
+
shape.computedAabb.max.fromArray([Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY]);
|
|
164
|
+
for (const vertex of shape.vertexPositions) {
|
|
165
|
+
shape.computedAabb.min.minVectors(shape.computedAabb.min, vertex);
|
|
166
|
+
shape.computedAabb.max.maxVectors(shape.computedAabb.max, vertex);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function updateCopyForDiff(shape: TriangleMesh) {
|
|
171
|
+
if (shape.copyForDiff) {
|
|
172
|
+
shape.copyForDiff.copy(shape);
|
|
173
|
+
shape.copyForDiff.copyForDiff = null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function updateCenterOfMass(shape: TriangleMesh) {
|
|
178
|
+
// recomputing center of mass for this shape not supported yet
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function updateShape(shape: TriangleMesh) {
|
|
182
|
+
updateCopyForDiff(shape);
|
|
183
|
+
updateCenterOfMass(shape);
|
|
184
|
+
updateVolume(shape);
|
|
185
|
+
updateLocalBounds(shape);
|
|
186
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Tuple4<T> = [T, T, T, T];
|