@perplexdotgg/bounce 1.0.0 → 1.0.2
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/build/bounce.d.ts +39501 -0
- package/build/bounce.js +17166 -0
- package/package.json +1 -1
- package/src/builders/ConvexHullBuilder.ts +0 -437
- package/src/builders/ConvexHullBuilder2d.ts +0 -344
- package/src/builders/ConvexHullBuilder3d.ts +0 -1689
- package/src/builders/HeightMapBuilder.ts +0 -414
- package/src/builders/TriangleMeshBuilder.ts +0 -92
- package/src/collision/CastShapesModule.ts +0 -184
- package/src/collision/CollideShapesModule.ts +0 -152
- package/src/collision/HeightMapCaster.ts +0 -38
- package/src/collision/HeightMapCollider.ts +0 -33
- package/src/collision/TriangleCaster.ts +0 -249
- package/src/collision/TriangleCollider.ts +0 -308
- package/src/collision/TriangleCollider2.ts +0 -379
- package/src/collision/activeEdge.ts +0 -146
- package/src/collision/cast/cast.ts +0 -139
- package/src/collision/cast/castCompoundVsCompound.ts +0 -59
- package/src/collision/cast/castCompoundVsConvex.ts +0 -116
- package/src/collision/cast/castConvexVsCompound.ts +0 -123
- package/src/collision/cast/castConvexVsConvex.ts +0 -213
- package/src/collision/cast/castConvexVsHeightMap.ts +0 -73
- package/src/collision/cast/castConvexVsTriangleMesh.ts +0 -56
- package/src/collision/cast/castRayVsCompound.ts +0 -44
- package/src/collision/cast/castRayVsConvex.ts +0 -45
- package/src/collision/cast/castRayVsHeightMap.ts +0 -58
- package/src/collision/cast/castRayVsTriangleMesh.ts +0 -58
- package/src/collision/closestPoints/closestPoints.ts +0 -23
- package/src/collision/closestPoints/computeBarycentricCoordinates2d.ts +0 -32
- package/src/collision/closestPoints/computeBarycentricCoordinates3d.ts +0 -81
- package/src/collision/closestPoints/computeClosestPointOnLine.ts +0 -30
- package/src/collision/closestPoints/computeClosestPointOnTetrahedron.ts +0 -96
- package/src/collision/closestPoints/computeClosestPointOnTriangle.ts +0 -195
- package/src/collision/closestPoints/isOriginOutsideOfPlane.ts +0 -25
- package/src/collision/closestPoints/isOriginOutsideOfTrianglePlanes.ts +0 -72
- package/src/collision/collide/collide.ts +0 -146
- package/src/collision/collide/collideCompoundVsCompound.ts +0 -60
- package/src/collision/collide/collideCompoundVsConvex.ts +0 -59
- package/src/collision/collide/collideCompoundVsHeightMap.ts +0 -73
- package/src/collision/collide/collideCompoundVsTriangleMesh.ts +0 -56
- package/src/collision/collide/collideConvexVsCompound.ts +0 -57
- package/src/collision/collide/collideConvexVsConvex.ts +0 -225
- package/src/collision/collide/collideConvexVsConvexImp.ts +0 -236
- package/src/collision/collide/collideConvexVsHeightMap.ts +0 -53
- package/src/collision/collide/collideConvexVsTriangleMesh.ts +0 -58
- package/src/collision/collide/collideHeightMapVsCompound.ts +0 -69
- package/src/collision/collide/collideHeightMapVsConvex.ts +0 -53
- package/src/collision/collide/collideSphereVsSphere.ts +0 -81
- package/src/collision/collide/collideTriangleMeshVsCompound.ts +0 -58
- package/src/collision/collide/collideTriangleMeshVsConvex.ts +0 -58
- package/src/collision/epa/EpaConvexHullBuilder.ts +0 -397
- package/src/collision/epa/StaticArray.ts +0 -154
- package/src/collision/epa/TriangleFactory.ts +0 -32
- package/src/collision/epa/arrays.ts +0 -99
- package/src/collision/epa/binaryHeap.ts +0 -82
- package/src/collision/epa/structs.ts +0 -227
- package/src/collision/gjk/GjkModule.ts +0 -864
- package/src/collision/gjk/PenetrationDepthModule.ts +0 -493
- package/src/collision/gjk/SupportPoints.ts +0 -50
- package/src/collision/imp/MinkowskiDifference.ts +0 -36
- package/src/collision/imp/computeExploredDistanceLowerUpperBound.ts +0 -40
- package/src/collision/imp/finalizeImpResult.ts +0 -69
- package/src/collision/imp/findContactImp.ts +0 -196
- package/src/collision/imp/imp.ts +0 -28
- package/src/collision/imp/incrementalMinimumDistanceExploreDirection.ts +0 -207
- package/src/collision/mpr/findPortal.ts +0 -152
- package/src/collision/mpr/mpr.ts +0 -29
- package/src/collision/mpr/updatePortal.ts +0 -52
- package/src/constraints/BaseConstraint.ts +0 -50
- package/src/constraints/ConstraintOptions.ts +0 -22
- package/src/constraints/ConstraintSolver.ts +0 -119
- package/src/constraints/DistanceConstraint.ts +0 -229
- package/src/constraints/FixedConstraint.ts +0 -203
- package/src/constraints/HingeConstraint.ts +0 -460
- package/src/constraints/PointConstraint.ts +0 -108
- package/src/constraints/components/AngleComponent.ts +0 -226
- package/src/constraints/components/AxisComponent.ts +0 -263
- package/src/constraints/components/HingeComponent.ts +0 -215
- package/src/constraints/components/Motor.ts +0 -36
- package/src/constraints/components/PointConstraintComponent.ts +0 -179
- package/src/constraints/components/RotationEulerComponent.ts +0 -139
- package/src/constraints/components/Spring.ts +0 -30
- package/src/constraints/components/SpringComponent.ts +0 -71
- package/src/constraints/types.ts +0 -6
- package/src/helpers.ts +0 -147
- package/src/index.ts +0 -50
- package/src/math/BasicTransform.ts +0 -19
- package/src/math/NumberValue.ts +0 -13
- package/src/math/isometry.ts +0 -64
- package/src/math/mat3.ts +0 -529
- package/src/math/mat4.ts +0 -588
- package/src/math/quat.ts +0 -193
- package/src/math/scalar.ts +0 -81
- package/src/math/tensor.ts +0 -17
- package/src/math/vec3.ts +0 -589
- package/src/math/vec4.ts +0 -10
- package/src/physics/Body.ts +0 -581
- package/src/physics/CollisionFilter.ts +0 -52
- package/src/physics/SleepModule.ts +0 -163
- package/src/physics/broadphase/BodyPairsModule.ts +0 -363
- package/src/physics/broadphase/BvhModule.ts +0 -237
- package/src/physics/broadphase/BvhTree.ts +0 -803
- package/src/physics/broadphase/ConstraintPairsModule.ts +0 -385
- package/src/physics/broadphase/TriangleMeshBvhTree.ts +0 -379
- package/src/physics/manifold/ContactManifold.ts +0 -227
- package/src/physics/manifold/ContactManifoldModule.ts +0 -623
- package/src/physics/manifold/Face.ts +0 -119
- package/src/physics/manifold/ManifoldCache.ts +0 -116
- package/src/physics/manifold/clipping/clipPolyVsEdge.ts +0 -131
- package/src/physics/manifold/clipping/clipPolyVsPlane.ts +0 -73
- package/src/physics/manifold/clipping/clipPolyVsPoly.ts +0 -72
- package/src/physics/narrowphase/CollideBodiesModule.ts +0 -755
- package/src/physics/solver/ContactConstraintModule.ts +0 -659
- package/src/physics/solver/ManifoldConstraint.ts +0 -420
- package/src/physics/solver/estimateCollisionResponse.ts +0 -146
- package/src/shape/Aabb.ts +0 -400
- package/src/shape/Box.ts +0 -231
- package/src/shape/Capsule.ts +0 -332
- package/src/shape/CompoundShape.ts +0 -288
- package/src/shape/Convex.ts +0 -130
- package/src/shape/ConvexHull.ts +0 -423
- package/src/shape/Cylinder.ts +0 -313
- package/src/shape/HeightMap.ts +0 -511
- package/src/shape/Line.ts +0 -14
- package/src/shape/Plane.ts +0 -116
- package/src/shape/Ray.ts +0 -81
- package/src/shape/Segment.ts +0 -25
- package/src/shape/Shape.ts +0 -77
- package/src/shape/Sphere.ts +0 -181
- package/src/shape/TransformedShape.ts +0 -51
- package/src/shape/Triangle.ts +0 -122
- package/src/shape/TriangleMesh.ts +0 -186
- package/src/types.ts +0 -1
- package/src/world.ts +0 -1335
- package/tests/BodyPairsModule.test.ts +0 -71
- package/tests/BvhTree.test.ts +0 -406
- package/tests/test.md +0 -642
- package/tests/vec3.test.ts +0 -12
package/src/shape/Ray.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { createClass, MonomorphType, NumberType, PropertyDefinitionMap } from "monomorph";
|
|
2
|
-
import { Vec3 } from "../math/vec3";
|
|
3
|
-
import { Aabb } from "./Aabb";
|
|
4
|
-
import { Isometry } from "../math/isometry";
|
|
5
|
-
import { Triangle } from "./Triangle";
|
|
6
|
-
|
|
7
|
-
const rayProps = {
|
|
8
|
-
origin: MonomorphType(Vec3),
|
|
9
|
-
direction: MonomorphType(Vec3),
|
|
10
|
-
length: NumberType(0.0),
|
|
11
|
-
} as const satisfies PropertyDefinitionMap;
|
|
12
|
-
|
|
13
|
-
export class Ray extends createClass<Ray, typeof rayProps>(rayProps) {
|
|
14
|
-
intersectsAabb(aabb: Aabb): boolean {
|
|
15
|
-
return aabb.intersectsRay(this);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
transform(isometry: Isometry) {
|
|
19
|
-
this.origin.transformVectorFromMat4(this.origin, isometry.matrix);
|
|
20
|
-
isometry.matrix.multiply3x3(this.direction, this.direction);
|
|
21
|
-
return this;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
transformRay(ray: Ray, isometry: Isometry) {
|
|
25
|
-
this.origin.transformVectorFromMat4(ray.origin, isometry.matrix);
|
|
26
|
-
isometry.matrix.multiply3x3(this.direction, ray.direction);
|
|
27
|
-
return this;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* algorithm: https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
|
32
|
-
*/
|
|
33
|
-
intersectsTriangle(outResult: { fraction: number; hit: boolean }, triangle: Triangle) {
|
|
34
|
-
ray_vector.copy(this.direction);
|
|
35
|
-
|
|
36
|
-
edge1.subtractVectors(triangle.b, triangle.a);
|
|
37
|
-
edge2.subtractVectors(triangle.c, triangle.a);
|
|
38
|
-
ray_cross_e2.crossVectors(ray_vector, edge2);
|
|
39
|
-
const det = edge1.dot(ray_cross_e2);
|
|
40
|
-
|
|
41
|
-
if (det > -epsilon && det < epsilon) {
|
|
42
|
-
outResult.hit = false; // parallel
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const inv_det = 1.0 / det;
|
|
47
|
-
s.subtractVectors(this.origin, triangle.a);
|
|
48
|
-
const u = inv_det * s.dot(ray_cross_e2);
|
|
49
|
-
|
|
50
|
-
if (u < -epsilon || u > 1 + epsilon) {
|
|
51
|
-
outResult.hit = false;
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
s_cross_e1.crossVectors(s, edge1);
|
|
56
|
-
const v = inv_det * ray_vector.dot(s_cross_e1);
|
|
57
|
-
|
|
58
|
-
if (v < -epsilon || u + v > 1 + epsilon) {
|
|
59
|
-
outResult.hit = false;
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const t = inv_det * edge2.dot(s_cross_e1);
|
|
64
|
-
|
|
65
|
-
if (t > epsilon && t <= this.length + epsilon) {
|
|
66
|
-
outResult.fraction = t / this.length;
|
|
67
|
-
outResult.hit = true;
|
|
68
|
-
} else {
|
|
69
|
-
// line hit but outside the ray segment
|
|
70
|
-
outResult.hit = false;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const epsilon = 1e-6;
|
|
76
|
-
const edge1 = /*@__PURE__*/ Vec3.create();
|
|
77
|
-
const edge2 = /*@__PURE__*/ Vec3.create();
|
|
78
|
-
const ray_cross_e2 = /*@__PURE__*/ Vec3.create();
|
|
79
|
-
const s = /*@__PURE__*/ Vec3.create();
|
|
80
|
-
const s_cross_e1 = /*@__PURE__*/ Vec3.create();
|
|
81
|
-
const ray_vector = /*@__PURE__*/ Vec3.create();
|
package/src/shape/Segment.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { createClass, MonomorphType, PropertyDefinitionMap } from "monomorph";
|
|
2
|
-
import { Vec3 } from "../math/vec3";
|
|
3
|
-
import { Ray } from "./Ray";
|
|
4
|
-
|
|
5
|
-
const segmentProps = {
|
|
6
|
-
pointA: MonomorphType(Vec3),
|
|
7
|
-
pointB: MonomorphType(Vec3),
|
|
8
|
-
} as const satisfies PropertyDefinitionMap;
|
|
9
|
-
|
|
10
|
-
export class Segment extends createClass<Segment, typeof segmentProps>(segmentProps) {
|
|
11
|
-
computeCenter(out: Vec3) {
|
|
12
|
-
out.addVectors(this.pointA, this.pointB);
|
|
13
|
-
out.scaleVector(out, 0.5);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
computeHalfExtents(out: Vec3) {
|
|
17
|
-
out.subtractVectors(this.pointB, this.pointA);
|
|
18
|
-
out.scaleVector(out, 0.5);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
setFromRay(ray: Ray) {
|
|
22
|
-
this.pointA.copy(ray.origin);
|
|
23
|
-
this.pointB.addScaledToVector(ray.origin, ray.direction, ray.length);
|
|
24
|
-
}
|
|
25
|
-
}
|
package/src/shape/Shape.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { Aabb } from "./Aabb";
|
|
2
|
-
import { Face } from "../physics/manifold/Face";
|
|
3
|
-
import { Mat3 } from "../math/mat3";
|
|
4
|
-
import { Mat4 } from "../math/mat4";
|
|
5
|
-
import { Quat } from "../math/quat";
|
|
6
|
-
import { Vec3 } from "../math/vec3";
|
|
7
|
-
import { Sphere } from "./Sphere";
|
|
8
|
-
import { Box } from "./Box";
|
|
9
|
-
import { SupportMode, SupportShapeWithConvexRadius } from "./Convex";
|
|
10
|
-
import { CompoundShape } from "./CompoundShape";
|
|
11
|
-
import { HeightMap } from "./HeightMap";
|
|
12
|
-
import ConvexHull from "./ConvexHull";
|
|
13
|
-
import { Cylinder } from "./Cylinder";
|
|
14
|
-
import { TriangleMesh } from "./TriangleMesh";
|
|
15
|
-
import { Capsule } from "./Capsule";
|
|
16
|
-
import { WithPool } from "monomorph";
|
|
17
|
-
|
|
18
|
-
export const enum ShapeType {
|
|
19
|
-
box,
|
|
20
|
-
capsule,
|
|
21
|
-
compoundShape,
|
|
22
|
-
convexHull,
|
|
23
|
-
cylinder,
|
|
24
|
-
heightMap,
|
|
25
|
-
sphere,
|
|
26
|
-
triangleMesh,
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type ConvexShape = Box | Capsule | ConvexHull | Cylinder | Sphere;
|
|
30
|
-
export type ShapeCollection = CompoundShape | HeightMap | TriangleMesh;
|
|
31
|
-
export type Shape = ConvexShape | ShapeCollection;
|
|
32
|
-
export type StaticShape = Shape;
|
|
33
|
-
export type DynamicShape = Exclude<Shape, HeightMap>;
|
|
34
|
-
export type KinematicShape = Exclude<Shape, HeightMap>;
|
|
35
|
-
|
|
36
|
-
export type ConvexShapeInterface = {
|
|
37
|
-
type: ShapeType.box | ShapeType.capsule | ShapeType.convexHull | ShapeType.cylinder | ShapeType.sphere;
|
|
38
|
-
|
|
39
|
-
// for transform-dependent shape properties
|
|
40
|
-
computeSupportShape(mode: SupportMode, scale: number): SupportShapeWithConvexRadius;
|
|
41
|
-
computeSupportingFace(
|
|
42
|
-
out: Face,
|
|
43
|
-
subShapeID: number,
|
|
44
|
-
direction: Vec3,
|
|
45
|
-
scale: number,
|
|
46
|
-
centerOfMassTransform: Mat4
|
|
47
|
-
): void;
|
|
48
|
-
computeInverseInertiaTensor(out: Mat3, mass: number): void;
|
|
49
|
-
computeWorldBounds(out: Aabb, translation: Vec3, rotation: Quat): void;
|
|
50
|
-
computeSurfaceNormal(out: Vec3, inLocalSurfacePosition: Vec3, subShapeId?: number): void;
|
|
51
|
-
|
|
52
|
-
// for "diffing"
|
|
53
|
-
copyForDiff: WithPool<ConvexShape> | ConvexShape | null;
|
|
54
|
-
commitChanges(): void;
|
|
55
|
-
hasChanged(): boolean;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export type ShapeCollectionInterface = {
|
|
59
|
-
type: ShapeType.compoundShape | ShapeType.heightMap | ShapeType.triangleMesh;
|
|
60
|
-
|
|
61
|
-
// for transform-dependent shape properties
|
|
62
|
-
computeSupportingFace(
|
|
63
|
-
out: Face,
|
|
64
|
-
subShapeID: number,
|
|
65
|
-
direction: Vec3,
|
|
66
|
-
scale: number,
|
|
67
|
-
centerOfMassTransform: Mat4
|
|
68
|
-
): void;
|
|
69
|
-
computeInverseInertiaTensor(out: Mat3, mass: number): void;
|
|
70
|
-
computeWorldBounds(out: Aabb, translation: Vec3, rotation: Quat): void;
|
|
71
|
-
computeSurfaceNormal(out: Vec3, inLocalSurfacePosition: Vec3, subShapeId?: number): void;
|
|
72
|
-
|
|
73
|
-
// for "diffing"
|
|
74
|
-
copyForDiff: ShapeCollection | null;
|
|
75
|
-
commitChanges(): void;
|
|
76
|
-
hasChanged(): boolean;
|
|
77
|
-
};
|
package/src/shape/Sphere.ts
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createClass,
|
|
3
|
-
LazyReferenceType,
|
|
4
|
-
MonomorphType,
|
|
5
|
-
NumberType,
|
|
6
|
-
PropertyDefinitionMap,
|
|
7
|
-
PropertyDefinitionReference,
|
|
8
|
-
} from "monomorph";
|
|
9
|
-
import { Vec3 } from "../math/vec3";
|
|
10
|
-
import { Quat } from "../math/quat";
|
|
11
|
-
import { Face } from "../physics/manifold/Face";
|
|
12
|
-
import { Mat4 } from "../math/mat4";
|
|
13
|
-
import { Mat3 } from "../math/mat3";
|
|
14
|
-
import { Aabb } from "./Aabb";
|
|
15
|
-
import { SupportMode, SupportShapeWithConvexRadius } from "./Convex";
|
|
16
|
-
import { ConvexShapeInterface, ShapeType } from "./Shape";
|
|
17
|
-
import type { World } from "../world";
|
|
18
|
-
|
|
19
|
-
const sphereNoConvexProps = {
|
|
20
|
-
radius: NumberType(0.0),
|
|
21
|
-
} as const satisfies PropertyDefinitionMap;
|
|
22
|
-
|
|
23
|
-
export class SphereNoConvex
|
|
24
|
-
extends createClass<SphereNoConvex, typeof sphereNoConvexProps>(sphereNoConvexProps)
|
|
25
|
-
implements SupportShapeWithConvexRadius
|
|
26
|
-
{
|
|
27
|
-
getConvexRadius(): number {
|
|
28
|
-
return this.radius;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
computeSupport(out: Vec3, direction: Vec3): void {
|
|
32
|
-
out.zero();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const sphereWithConvexProps = {
|
|
37
|
-
radius: NumberType(0.0),
|
|
38
|
-
} as const satisfies PropertyDefinitionMap;
|
|
39
|
-
|
|
40
|
-
export class SphereWithConvex
|
|
41
|
-
extends createClass<SphereWithConvex, typeof sphereWithConvexProps>(sphereWithConvexProps)
|
|
42
|
-
implements SupportShapeWithConvexRadius
|
|
43
|
-
{
|
|
44
|
-
getConvexRadius(): number {
|
|
45
|
-
return 0;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
computeSupport(out: Vec3, direction: Vec3): void {
|
|
49
|
-
const directionLength = direction.length();
|
|
50
|
-
if (directionLength > 0) {
|
|
51
|
-
out.scaleVector(direction, this.radius / directionLength);
|
|
52
|
-
} else {
|
|
53
|
-
out.zero();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const sphereProps = {
|
|
59
|
-
computedCenterOfMass: MonomorphType(Vec3, undefined, true),
|
|
60
|
-
computedVolume: NumberType(0.0, true),
|
|
61
|
-
computedAabb: MonomorphType(Aabb, undefined, true),
|
|
62
|
-
radius: NumberType(1.0),
|
|
63
|
-
copyForDiff: LazyReferenceType((() => Sphere) as () => never) as PropertyDefinitionReference<Sphere | null, true>,
|
|
64
|
-
sphereNoConvex: MonomorphType(SphereNoConvex, undefined, true),
|
|
65
|
-
sphereWithConvex: MonomorphType(SphereWithConvex, undefined, true),
|
|
66
|
-
} as const satisfies PropertyDefinitionMap;
|
|
67
|
-
|
|
68
|
-
const afterConstructorCode = `
|
|
69
|
-
this.world = null;
|
|
70
|
-
`;
|
|
71
|
-
|
|
72
|
-
export class Sphere
|
|
73
|
-
extends createClass<Sphere, typeof sphereProps>(sphereProps, { afterConstructorCode })
|
|
74
|
-
implements ConvexShapeInterface
|
|
75
|
-
{
|
|
76
|
-
type: ShapeType.sphere = ShapeType.sphere;
|
|
77
|
-
|
|
78
|
-
declare world: World | null;
|
|
79
|
-
|
|
80
|
-
computeSupportShape(mode: SupportMode, scale: number): SupportShapeWithConvexRadius {
|
|
81
|
-
const scaled_radius = this.radius * scale;
|
|
82
|
-
|
|
83
|
-
switch (mode) {
|
|
84
|
-
case SupportMode.IncludeConvexRadius: {
|
|
85
|
-
this.sphereWithConvex.radius = scaled_radius;
|
|
86
|
-
return this.sphereWithConvex;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
case SupportMode.ExcludeConvexRadius: {
|
|
90
|
-
this.sphereNoConvex.radius = scaled_radius;
|
|
91
|
-
return this.sphereNoConvex;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
default: {
|
|
95
|
-
throw new Error(`Invalid support mode ${mode}`);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
computeSupportingFace(
|
|
101
|
-
out: Face,
|
|
102
|
-
subShapeID: number,
|
|
103
|
-
direction: Vec3,
|
|
104
|
-
scale: number,
|
|
105
|
-
centerOfMassTransform: Mat4
|
|
106
|
-
): void {
|
|
107
|
-
// no faces for a sphere
|
|
108
|
-
out.clear();
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
computeInertiaTensor(out: Mat3, mass: number) {
|
|
112
|
-
// assumptions:
|
|
113
|
-
// 1. sphere's center-of-mass is at origin
|
|
114
|
-
// 2. sphere is not transformed
|
|
115
|
-
// 3. sphere has uniform density = 1
|
|
116
|
-
const inertia = (2 / 5) * mass * this.radius * this.radius;
|
|
117
|
-
out.fromArray([inertia, 0, 0, 0, inertia, 0, 0, 0, inertia]);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
computeInverseInertiaTensor(out: Mat3, mass: number) {
|
|
121
|
-
this.computeInertiaTensor(out, mass);
|
|
122
|
-
out.invert();
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
computeWorldBounds(out: Aabb, translation: Vec3, rotation: Quat): void {
|
|
126
|
-
// TODO: apply rotation if shape's center of mass is not at the origin?
|
|
127
|
-
out.translateAabb(this.computedAabb, translation);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
hasChanged() {
|
|
131
|
-
return this.copyForDiff !== null && this.radius !== this.copyForDiff.radius;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
commitChanges() {
|
|
135
|
-
if (this.hasChanged()) {
|
|
136
|
-
updateShape(this);
|
|
137
|
-
// this.world?.updateBodyProperties();
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
computeSurfaceNormal(out: Vec3, inLocalSurfacePosition: Vec3, subShapeId?: number): void {
|
|
142
|
-
// get the vector pointing from the center to the surface point
|
|
143
|
-
out.subtractVectors(inLocalSurfacePosition, this.computedCenterOfMass);
|
|
144
|
-
out.normalize();
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const oldCreate = Sphere.create;
|
|
149
|
-
Sphere.create = function () {
|
|
150
|
-
const shape = oldCreate.apply(this, arguments as any);
|
|
151
|
-
updateShape(shape);
|
|
152
|
-
return shape;
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
function updateVolume(shape: Sphere) {
|
|
156
|
-
shape.computedVolume = (4 / 3) * Math.PI * shape.radius * shape.radius * shape.radius;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function updateLocalBounds(shape: Sphere) {
|
|
160
|
-
// TODO: apply rotation if shape's center of mass is not at the origin?
|
|
161
|
-
shape.computedAabb.min.addScalarToVector(shape.computedCenterOfMass, -shape.radius);
|
|
162
|
-
shape.computedAabb.max.addScalarToVector(shape.computedCenterOfMass, +shape.radius);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function updateCopyForDiff(shape: Sphere) {
|
|
166
|
-
if (shape.copyForDiff) {
|
|
167
|
-
shape.copyForDiff.copy(shape);
|
|
168
|
-
shape.copyForDiff.copyForDiff = null;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function updateCenterOfMass(shape: Sphere) {
|
|
173
|
-
shape.computedCenterOfMass.zero();
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function updateShape(shape: Sphere) {
|
|
177
|
-
updateCopyForDiff(shape);
|
|
178
|
-
updateCenterOfMass(shape);
|
|
179
|
-
updateVolume(shape);
|
|
180
|
-
updateLocalBounds(shape);
|
|
181
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createClass,
|
|
3
|
-
MonomorphType,
|
|
4
|
-
NumberType,
|
|
5
|
-
PropertyDefinition,
|
|
6
|
-
PropertyDefinitionMap,
|
|
7
|
-
ReferenceType,
|
|
8
|
-
} from "monomorph";
|
|
9
|
-
import { Shape, ShapeType } from "./Shape";
|
|
10
|
-
import { Sphere } from "./Sphere";
|
|
11
|
-
import { Box } from "./Box";
|
|
12
|
-
import { BasicTransform } from "../math/BasicTransform";
|
|
13
|
-
import ConvexHull from "./ConvexHull";
|
|
14
|
-
import { Cylinder } from "./Cylinder";
|
|
15
|
-
import { Capsule } from "./Capsule";
|
|
16
|
-
|
|
17
|
-
const transformedShapeProps = {
|
|
18
|
-
shapeType: NumberType(ShapeType.box) as PropertyDefinition<ShapeType, true>,
|
|
19
|
-
// we need to do "as 'shape' ..." otherwise they are just 'string' and not the actual specific keys
|
|
20
|
-
['shape' + ShapeType.box as 'shape0']: ReferenceType(Box, undefined, true),
|
|
21
|
-
['shape' + ShapeType.capsule as 'shape1']: ReferenceType(Capsule, undefined, true),
|
|
22
|
-
// compoundShape not supported
|
|
23
|
-
['shape' + ShapeType.convexHull as 'shape3']: ReferenceType(ConvexHull, undefined, true),
|
|
24
|
-
['shape' + ShapeType.cylinder as 'shape4']: ReferenceType(Cylinder, undefined, true),
|
|
25
|
-
// heightMap not supported
|
|
26
|
-
['shape' + ShapeType.sphere as 'shape6']: ReferenceType(Sphere, undefined, true),
|
|
27
|
-
// triangleMesh not supported
|
|
28
|
-
transform: MonomorphType(BasicTransform),
|
|
29
|
-
} as const satisfies PropertyDefinitionMap;
|
|
30
|
-
|
|
31
|
-
const afterConstructorCode = `
|
|
32
|
-
this.world = null;
|
|
33
|
-
`;
|
|
34
|
-
|
|
35
|
-
export class TransformedShape extends createClass<TransformedShape, typeof transformedShapeProps>(
|
|
36
|
-
transformedShapeProps,
|
|
37
|
-
{ afterConstructorCode }
|
|
38
|
-
) {
|
|
39
|
-
get shape(): Shape | null {
|
|
40
|
-
return this['shape' + this.shapeType as keyof typeof this] as Shape | null;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
set shape(value: Shape) {
|
|
44
|
-
(this['shape' + this.shapeType as keyof typeof this] as (Shape | null)) = null;
|
|
45
|
-
(this['shape' + value.type as keyof typeof this] as (Shape | null)) = value;
|
|
46
|
-
this.shapeType = value.type;
|
|
47
|
-
// this.computeMassProperties();
|
|
48
|
-
// computeCenterOfMassPosition(this.computedCenterOfMassPosition, this);
|
|
49
|
-
// this.computeWorldBounds(this.computedBounds);
|
|
50
|
-
}
|
|
51
|
-
}
|
package/src/shape/Triangle.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { createClass, MonomorphType, NumberType, PropertyDefinitionMap } from "monomorph";
|
|
2
|
-
import { Vec3 } from "../math/vec3";
|
|
3
|
-
import { Mat4 } from "../math/mat4";
|
|
4
|
-
import { Aabb } from "./Aabb";
|
|
5
|
-
import { SupportMode, SupportShapeWithConvexRadius } from "./Convex";
|
|
6
|
-
import { Face } from "../physics/manifold/Face";
|
|
7
|
-
import { Ray } from "./Ray";
|
|
8
|
-
|
|
9
|
-
const triangleProps = {
|
|
10
|
-
computedBounds: MonomorphType(Aabb, undefined, true),
|
|
11
|
-
normal: MonomorphType(Vec3, undefined, true),
|
|
12
|
-
a: MonomorphType(Vec3),
|
|
13
|
-
b: MonomorphType(Vec3),
|
|
14
|
-
c: MonomorphType(Vec3),
|
|
15
|
-
subShapeId: NumberType(0, true),
|
|
16
|
-
activeEdges: NumberType(0, true),
|
|
17
|
-
// aabb: { struct: Aabb, readOnly: true },
|
|
18
|
-
// activeEdges: { struct: Uint32, optional: true },
|
|
19
|
-
// translation: { struct: Vec3, optional: true },
|
|
20
|
-
} as const satisfies PropertyDefinitionMap;
|
|
21
|
-
|
|
22
|
-
const ab = /*@__PURE__*/ Vec3.create();
|
|
23
|
-
const ac = /*@__PURE__*/ Vec3.create();
|
|
24
|
-
|
|
25
|
-
export class Triangle extends createClass<Triangle, typeof triangleProps>(triangleProps) {
|
|
26
|
-
// type: ShapeType.triangle = ShapeType.triangle;
|
|
27
|
-
|
|
28
|
-
computeNormal(out: Vec3): void {
|
|
29
|
-
ab.subtractVectors(this.b, this.a);
|
|
30
|
-
ac.subtractVectors(this.c, this.a);
|
|
31
|
-
out.crossVectors(ab, ac);
|
|
32
|
-
out.normalize();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
computeCentroid(out: Vec3): void {
|
|
36
|
-
out
|
|
37
|
-
.addVectors(this.a, this.b)
|
|
38
|
-
.addVector(this.c)
|
|
39
|
-
.scale(1 / 3);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
computeUnnormalizedNormal(out: Vec3): void {
|
|
43
|
-
ab.subtractVectors(this.b, this.a);
|
|
44
|
-
ac.subtractVectors(this.c, this.a);
|
|
45
|
-
out.crossVectors(ab, ac);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
computeSurfaceNormal(out: Vec3, inLocalSurfacePosition: Vec3, subShapeId?: number): void {
|
|
49
|
-
// TODO: take into account surface position for backfaces?
|
|
50
|
-
this.computeNormal(out);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
transform(isometry: Mat4) {
|
|
54
|
-
this.a.transformByMat4(isometry);
|
|
55
|
-
this.b.transformByMat4(isometry);
|
|
56
|
-
this.c.transformByMat4(isometry);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
computeLocalBounds(out: Aabb) {
|
|
60
|
-
out.min.x = Math.min(this.a.x, this.b.x, this.c.x);
|
|
61
|
-
out.min.y = Math.min(this.a.y, this.b.y, this.c.y);
|
|
62
|
-
out.min.z = Math.min(this.a.z, this.b.z, this.c.z);
|
|
63
|
-
|
|
64
|
-
out.max.x = Math.max(this.a.x, this.b.x, this.c.x);
|
|
65
|
-
out.max.y = Math.max(this.a.y, this.b.y, this.c.y);
|
|
66
|
-
out.max.z = Math.max(this.a.z, this.b.z, this.c.z);
|
|
67
|
-
|
|
68
|
-
out.computeCentroid(out.centroid);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
computeSupport(out: Vec3, direction: Vec3): void {
|
|
72
|
-
const dotA = this.a.dot(direction);
|
|
73
|
-
const dotB = this.b.dot(direction);
|
|
74
|
-
const dotC = this.c.dot(direction);
|
|
75
|
-
|
|
76
|
-
if (dotA > dotB) {
|
|
77
|
-
if (dotA > dotC) {
|
|
78
|
-
out.copy(this.a);
|
|
79
|
-
} else {
|
|
80
|
-
out.copy(this.c);
|
|
81
|
-
}
|
|
82
|
-
} else {
|
|
83
|
-
if (dotB > dotC) {
|
|
84
|
-
out.copy(this.b);
|
|
85
|
-
} else {
|
|
86
|
-
out.copy(this.c);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
getConvexRadius(): number {
|
|
92
|
-
return 0;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
computeSupportShape(mode: SupportMode, scale: number): SupportShapeWithConvexRadius {
|
|
96
|
-
// TODO: account for mode and scale not being supported like the other shapes
|
|
97
|
-
// 1. either adjust the method signature to make mode and scale optional
|
|
98
|
-
// 2. or add support for mode and scale
|
|
99
|
-
return this;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
toObject() {
|
|
103
|
-
return {
|
|
104
|
-
a: this.a.toObject(),
|
|
105
|
-
b: this.b.toObject(),
|
|
106
|
-
c: this.c.toObject(),
|
|
107
|
-
normal: this.normal.toObject(),
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
computeSupportingFace(out: Face, subShapeID: number, direction: Vec3, scale: number, centerOfMassTransform: Mat4) {
|
|
112
|
-
// TODO: prescale transform
|
|
113
|
-
|
|
114
|
-
// TODO: flip triangle if scaled inside out
|
|
115
|
-
|
|
116
|
-
out.clear();
|
|
117
|
-
out.pushVertex(this.a);
|
|
118
|
-
out.pushVertex(this.b);
|
|
119
|
-
out.pushVertex(this.c);
|
|
120
|
-
out.transform(centerOfMassTransform);
|
|
121
|
-
}
|
|
122
|
-
}
|