@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,81 @@
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();
@@ -0,0 +1,25 @@
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
+ }
@@ -0,0 +1,77 @@
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
+ };
@@ -0,0 +1,181 @@
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
+ }
@@ -0,0 +1,51 @@
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
+ }
@@ -0,0 +1,122 @@
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
+ }