@ue-too/dynamics 0.5.2 → 0.7.1

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 (57) hide show
  1. package/README.md +7 -0
  2. package/benchmark.d.ts +47 -0
  3. package/collision-filter.d.ts +17 -0
  4. package/{.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/collision.d.ts → collision.d.ts} +23 -2
  5. package/dynamic-tree.d.ts +57 -0
  6. package/dynamics.tsbuildinfo +1 -0
  7. package/ecs-version/collision-system.d.ts +36 -0
  8. package/ecs-version/component.d.ts +40 -0
  9. package/ecs-version/index.d.ts +4 -0
  10. package/ecs-version/physics-system.d.ts +9 -0
  11. package/ecs-version/render-system.d.ts +8 -0
  12. package/index.d.ts +11 -0
  13. package/index.js +3165 -0
  14. package/index.js.map +1 -0
  15. package/package.json +10 -10
  16. package/pair-manager.d.ts +41 -0
  17. package/{dist/quadtree.d.ts → quadtree.d.ts} +6 -5
  18. package/{dist/rigidbody.d.ts → rigidbody.d.ts} +33 -4
  19. package/simple-benchmark-runner.d.ts +2 -0
  20. package/{.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/world.d.ts → world.d.ts} +20 -2
  21. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/collision.js +0 -330
  22. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/collision.js.map +0 -1
  23. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/constraint.js +0 -166
  24. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/constraint.js.map +0 -1
  25. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/dynamics.tsbuildinfo +0 -1
  26. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/index.d.ts +0 -3
  27. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/index.js +0 -4
  28. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/index.js.map +0 -1
  29. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/quadtree.d.ts +0 -26
  30. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/quadtree.js +0 -124
  31. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/quadtree.js.map +0 -1
  32. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/rigidbody.d.ts +0 -252
  33. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/rigidbody.js +0 -476
  34. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/rigidbody.js.map +0 -1
  35. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/world.js +0 -90
  36. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/world.js.map +0 -1
  37. package/dist/collision.d.ts +0 -36
  38. package/dist/constraint.d.ts +0 -35
  39. package/dist/dynamics.tsbuildinfo +0 -1
  40. package/dist/index.d.ts +0 -3
  41. package/dist/index.js +0 -925
  42. package/dist/index.js.map +0 -1
  43. package/dist/package.json +0 -22
  44. package/dist/world.d.ts +0 -29
  45. package/jest.config.js +0 -18
  46. package/project.json +0 -33
  47. package/rollup.config.js +0 -20
  48. package/src/collision.ts +0 -359
  49. package/src/constraint.ts +0 -245
  50. package/src/index.ts +0 -3
  51. package/src/quadtree.ts +0 -145
  52. package/src/rigidbody.ts +0 -640
  53. package/src/world.ts +0 -116
  54. package/test/rigidbody.test.ts +0 -10
  55. package/tsconfig.json +0 -21
  56. package/tsconfig.spec.json +0 -12
  57. /package/{.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/constraint.d.ts → constraint.d.ts} +0 -0
package/src/rigidbody.ts DELETED
@@ -1,640 +0,0 @@
1
- import { PointCal, Point } from "@ue-too/math";
2
-
3
- export interface RigidBody {
4
- center: Point;
5
- orientationAngle: number;
6
- linearVelocity: Point;
7
- angularVelocity: number;
8
- AABB: {min: Point, max: Point};
9
- mass: number;
10
- staticFrictionCoeff: number;
11
- momentOfInertia: number;
12
- step(deltaTime: number): void;
13
- isStatic(): boolean;
14
- isMovingStatic(): boolean;
15
- getMinMaxProjection(unitvector: Point): {min: number, max: number};
16
- getCollisionAxes(relativeBody: RigidBody): Point[];
17
- applyForce(force: Point): void;
18
- applyForceInOrientation(force: Point): void;
19
- // applyForceAtPoint(force: Point, point: Point): void;
20
- move(delta: Point): void;
21
- significantVertex(collisionNormal: Point): Point;
22
- getSignificantVertices(collisionNormal: Point): Point[];
23
- getNormalOfSignificantFace(collisionNormal: Point): Point;
24
- getAdjacentFaces(collisionNormal: Point): {startPoint: {coord: Point, index: number}, endPoint: {coord: Point, index: number}}[];
25
- }
26
-
27
- export interface VisualComponent{
28
- draw(ctx: CanvasRenderingContext2D): void;
29
- }
30
-
31
- export abstract class BaseRigidBody implements RigidBody{
32
-
33
- protected _center: Point;
34
- protected _mass: number = 50;
35
- protected _linearVelocity: Point;
36
- protected _angularVelocity: number; // in radians
37
- protected _orientationAngle: number = 0;
38
- protected linearAcceleartion: Point;
39
- protected force: Point;
40
- protected isStaticBody: boolean = false;
41
- protected _staticFrictionCoeff: number = 0.3;
42
- protected dynamicFrictionCoeff: number = 0.3;
43
- protected frictionEnabled: boolean = false;
44
- protected isMovingStaticBody: boolean = false;
45
- protected angularDampingFactor: number = 0.005;
46
-
47
-
48
- constructor(center: Point, _orientationAngle: number = 0, mass: number = 50, isStaticBody: boolean = false, frictionEnabled: boolean = false){
49
- this._center = center;
50
- this._orientationAngle = _orientationAngle;
51
- this._mass = mass;
52
- this.isStaticBody = isStaticBody;
53
- this.frictionEnabled = frictionEnabled;
54
- this.force = {x: 0, y: 0};
55
- this.linearAcceleartion = {x: 0, y: 0};
56
- this._linearVelocity = {x: 0, y: 0};
57
- this._angularVelocity = 0;
58
- }
59
-
60
- move(delta: Point): void {
61
- if (!this.isStatic()){
62
- this._center = PointCal.addVector(this._center, delta);
63
- }
64
- }
65
-
66
- rotateRadians(angle: number): void {
67
- this._orientationAngle += angle;
68
- }
69
-
70
- getCenter(): Point {
71
- return this._center;
72
- }
73
-
74
- getOrientationAngle(): number{
75
- return this._orientationAngle;
76
- }
77
-
78
- get angularVelocity(): number{
79
- return this._angularVelocity;
80
- }
81
-
82
- set angularVelocity(angularVelocity: number){
83
- this._angularVelocity = angularVelocity;
84
- }
85
-
86
- get orientationAngle(): number{
87
- return this._orientationAngle;
88
- }
89
-
90
- isStatic(): boolean{
91
- return this.isStaticBody;
92
- }
93
-
94
- isMovingStatic(): boolean {
95
- return this.isMovingStaticBody;
96
- }
97
-
98
- setLinearVelocity(linearVelocity: Point): void {
99
- this._linearVelocity = linearVelocity;
100
- }
101
-
102
- setMovingStatic(movingStatic: boolean):void {
103
- this.isMovingStaticBody = movingStatic;
104
- }
105
-
106
- setOrientationAngle(angle: number): void{
107
- this._orientationAngle = angle;
108
- }
109
-
110
- applyForce(force: Point): void {
111
- if (PointCal.magnitude(this.force) !== 0){
112
- this.force = PointCal.addVector(this.force, force);
113
- } else {
114
- this.force = force;
115
- }
116
- }
117
-
118
- applyForceInOrientation(force: Point | number): void {
119
- let forceTransformed: Point;
120
- if (typeof force === "number") {
121
- forceTransformed = PointCal.rotatePoint({x: force, y: 0}, this._orientationAngle);
122
- } else {
123
- forceTransformed = PointCal.rotatePoint(force, this._orientationAngle);
124
- }
125
- this.applyForce(forceTransformed);
126
- }
127
-
128
- step(deltaTime: number): void {
129
- if (this.frictionEnabled) {
130
- if (this.isStatic() ||
131
- (this.linearVelocity.x == 0 &&
132
- this.linearVelocity.y == 0 &&
133
- PointCal.magnitude(PointCal.subVector({x: this.force.x, y: this.force.y}, {x: 0, y: 0})) >= 0 &&
134
- PointCal.magnitude({x: this.force.x, y: this.force.y}) < this.staticFrictionCoeff * this.mass * 9.81)
135
- ) {
136
- if (this.force.z != undefined) {
137
- this.force = {x: 0, y: 0, z: this.force.z};
138
- } else {
139
- this.force = {x: 0, y: 0};
140
- }
141
- // return;
142
- } else {
143
- let kineticFrictionDirection = PointCal.multiplyVectorByScalar(PointCal.unitVector({x: this._linearVelocity.x, y: this._linearVelocity.y}), -1);
144
- let kineticFriction = PointCal.multiplyVectorByScalar(kineticFrictionDirection, this.dynamicFrictionCoeff * this.mass * 9.81);
145
- this.force = PointCal.addVector(this.force, kineticFriction);
146
- }
147
- }
148
- const angularDamping = this._angularVelocity != 0 ? this._angularVelocity > 0 ? -this.angularDampingFactor : this.angularDampingFactor : 0;
149
- // console.log("angular velocity", this._angularVelocity);
150
- // console.log("angular damping", angularDamping);
151
- if (Math.abs(this._angularVelocity) < Math.abs(angularDamping)) {
152
- this._angularVelocity = 0;
153
- } else {
154
- this._angularVelocity += angularDamping;
155
- }
156
- this._orientationAngle += this._angularVelocity * deltaTime;
157
- if (PointCal.magnitude({x: this._linearVelocity.x, y: this._linearVelocity.y}) < PointCal.magnitude(PointCal.divideVectorByScalar(PointCal.multiplyVectorByScalar(this.force, deltaTime), this.mass))){
158
- if (this._linearVelocity.z != undefined) {
159
- this._linearVelocity = {x: 0, y: 0, z: this._linearVelocity.z};
160
- } else {
161
- this._linearVelocity = {x: 0, y: 0};
162
- }
163
- }
164
- const gravitationalForce = -9.81 * this._mass;
165
- this.force = PointCal.addVector(this.force, {x: 0, y: 0, z: gravitationalForce});
166
- this._linearVelocity = PointCal.addVector(this._linearVelocity, PointCal.divideVectorByScalar(PointCal.multiplyVectorByScalar(this.force, deltaTime), this.mass));
167
- this._center = PointCal.addVector(this._center, PointCal.multiplyVectorByScalar(this._linearVelocity, deltaTime));
168
- if (this._center.z != undefined && this._center.z < 0) {
169
- this._center.z = 0;
170
- }
171
- this.force = {x: 0, y: 0};
172
- }
173
-
174
- get center(): Point {
175
- return this._center;
176
- }
177
-
178
- set center(dest: Point){
179
- this._center = dest;
180
- }
181
-
182
- get linearVelocity(): Point {
183
- return this._linearVelocity;
184
- }
185
-
186
- set linearVelocity(dest: Point){
187
- this._linearVelocity = dest;
188
- }
189
-
190
- get mass(): number{
191
- return this._mass;
192
- }
193
-
194
- get staticFrictionCoeff(): number{
195
- return this._staticFrictionCoeff;
196
- }
197
-
198
- set staticFrictionCoeff(coeff: number){
199
- this._staticFrictionCoeff = coeff;
200
- }
201
-
202
- abstract get momentOfInertia(): number;
203
- abstract getMinMaxProjection(unitvector: Point): {min: number, max: number};
204
- abstract getCollisionAxes(relativeBody: RigidBody): Point[];
205
- abstract get AABB(): {min: Point, max: Point};
206
- abstract significantVertex(collisionNormal: Point): Point;
207
- abstract getSignificantVertices(collisionNormal: Point): Point[];
208
- abstract getNormalOfSignificantFace(collisionNormal: Point): Point;
209
- abstract getAdjacentFaces(collisionNormal: Point): {startPoint: {coord: Point, index: number}, endPoint: {coord: Point, index: number}}[];
210
- }
211
-
212
- export class VisaulCircleBody implements VisualComponent, RigidBody {
213
-
214
- private _circle: Circle;
215
- private _context: CanvasRenderingContext2D;
216
-
217
- constructor(center: Point = {x: 0, y: 0}, radius: number, drawingContext: CanvasRenderingContext2D, _orientationAngle: number = 0, mass: number = 50, isStatic: boolean = false, frictionEnabled: boolean = true) {
218
- this._circle = new Circle(center, radius, _orientationAngle, mass, isStatic, frictionEnabled);
219
- this._context = drawingContext;
220
- }
221
-
222
- draw(ctx: CanvasRenderingContext2D): void {
223
- ctx.beginPath();
224
- ctx.arc(this._circle.center.x, this._circle.center.y, this._circle.radius, 0, 2 * Math.PI);
225
- ctx.stroke();
226
- }
227
-
228
- step(deltaTime: number): void {
229
- this._circle.step(deltaTime);
230
- this.draw(this._context);
231
- }
232
-
233
- isStatic(): boolean {
234
- return this._circle.isStatic();
235
- }
236
-
237
- isMovingStatic(): boolean {
238
- return this._circle.isMovingStatic();
239
- }
240
-
241
- getMinMaxProjection(unitvector: Point): { min: number; max: number; } {
242
- return this._circle.getMinMaxProjection(unitvector);
243
- }
244
-
245
- getCollisionAxes(relativeBody: RigidBody): Point[] {
246
- return this._circle.getCollisionAxes(relativeBody);
247
- }
248
-
249
- applyForce(force: Point): void {
250
- this._circle.applyForce(force);
251
- }
252
-
253
- get AABB(): { min: Point; max: Point; } {
254
- return this._circle.AABB;
255
- }
256
-
257
- getMass(): number {
258
- return this._circle.mass;
259
- }
260
-
261
- applyForceInOrientation(force: Point): void {
262
- this._circle.applyForceInOrientation(force);
263
- }
264
-
265
- move(delta: Point): void {
266
- this._circle.move(delta);
267
- }
268
-
269
- getSignificantVertices(collisionNormal: Point): Point[] {
270
- return this._circle.getSignificantVertices(collisionNormal);
271
- }
272
-
273
- get center(): Point {
274
- return this._circle.center;
275
- }
276
-
277
- set center(dest: Point){
278
- this._circle.center = dest;
279
- }
280
-
281
- get linearVelocity(): Point {
282
- return this._circle.linearVelocity;
283
- }
284
-
285
- set linearVelocity(dest: Point){
286
- this._circle.linearVelocity = dest;
287
- }
288
-
289
- get orientationAngle(){
290
- return this._circle.orientationAngle;
291
- }
292
-
293
- significantVertex(collisionNormal: Point): Point {
294
- return this._circle.significantVertex(collisionNormal);
295
- }
296
-
297
- set angularVelocity(angularVelocity: number){
298
- this._circle.angularVelocity = angularVelocity;
299
- }
300
-
301
- get angularVelocity(): number{
302
- return this._circle.angularVelocity;
303
- }
304
-
305
- get mass(): number{
306
- return this._circle.mass;
307
- }
308
-
309
- getNormalOfSignificantFace(collisionNormal: Point): Point {
310
- return this._circle.getNormalOfSignificantFace(collisionNormal);
311
- }
312
-
313
- get staticFrictionCoeff(): number{
314
- return this._circle.staticFrictionCoeff;
315
- }
316
-
317
- set staticFrictionCoeff(coeff: number){
318
- this._circle.staticFrictionCoeff = coeff;
319
- }
320
-
321
- getAdjacentFaces(collisionNormal: Point): {startPoint: {coord: Point, index: number}, endPoint: {coord: Point, index: number}}[] {
322
- return this._circle.getAdjacentFaces(collisionNormal);
323
- }
324
-
325
- get momentOfInertia(): number {
326
- return this._circle.momentOfInertia;
327
- }
328
-
329
- }
330
-
331
- export class VisualPolygonBody implements VisualComponent, RigidBody {
332
-
333
- private _polygon: Polygon;
334
- private _context: CanvasRenderingContext2D;
335
-
336
- constructor(center: Point = {x: 0, y: 0}, vertices: Point[], drawingContext: CanvasRenderingContext2D, _orientationAngle: number = 0, mass: number = 50, isStatic: boolean = false, frictionEnabled: boolean = true) {
337
- this._polygon = new Polygon(center, vertices, _orientationAngle, mass, isStatic, frictionEnabled);
338
- this._context = drawingContext;
339
- }
340
-
341
- draw(ctx: CanvasRenderingContext2D): void {
342
- ctx.beginPath();
343
- let vertices = this._polygon.getVerticesAbsCoord();
344
- ctx.moveTo(vertices[0].x, vertices[0].y);
345
- vertices.forEach(vertex => {
346
- ctx.lineTo(vertex.x, vertex.y);
347
- });
348
- ctx.lineTo(vertices[0].x, vertices[0].y);
349
- ctx.stroke();
350
- // ctx.beginPath();
351
- // ctx.rect(this._polygon.AABB.min.x, this._polygon.AABB.min.y, this._polygon.AABB.max.x - this._polygon.AABB.min.x, this._polygon.AABB.max.y - this._polygon.AABB.min.y);
352
- // ctx.stroke();
353
- }
354
-
355
- step(deltaTime: number): void {
356
- this._polygon.step(deltaTime);
357
- this.draw(this._context);
358
- }
359
-
360
- isStatic(): boolean {
361
- return this._polygon.isStatic();
362
- }
363
-
364
- isMovingStatic(): boolean {
365
- return this._polygon.isMovingStatic();
366
- }
367
-
368
- getMinMaxProjection(unitvector: Point): { min: number; max: number; } {
369
- return this._polygon.getMinMaxProjection(unitvector);
370
- }
371
-
372
- getCollisionAxes(relativeBody: RigidBody): Point[] {
373
- return this._polygon.getCollisionAxes(relativeBody);
374
- }
375
-
376
- applyForce(force: Point): void {
377
- this._polygon.applyForce(force);
378
- }
379
-
380
- applyForceInOrientation(force: Point): void {
381
- this._polygon.applyForceInOrientation(force);
382
- }
383
-
384
- setLinearVelocity(linearVelocity: Point): void {
385
- this._polygon.setLinearVelocity(linearVelocity);
386
- }
387
-
388
- move(delta: Point): void {
389
- this._polygon.move(delta);
390
- }
391
-
392
- get center(): Point {
393
- return this._polygon.center;
394
- }
395
-
396
- set center(dest: Point){
397
- this._polygon.center = dest;
398
- }
399
-
400
- get linearVelocity(): Point {
401
- return this._polygon.linearVelocity;
402
- }
403
-
404
- set linearVelocity(dest: Point){
405
- this._polygon.linearVelocity = dest;
406
- }
407
-
408
- get angularVelocity(): number{
409
- return this._polygon.angularVelocity;
410
- }
411
-
412
- set angularVelocity(angularVelocity: number){
413
- this._polygon.angularVelocity = angularVelocity;
414
- }
415
-
416
- get orientationAngle(): number{
417
- return this._polygon.orientationAngle;
418
- }
419
-
420
- significantVertex(collisionNormal: Point): Point {
421
- return this._polygon.significantVertex(collisionNormal);
422
- }
423
-
424
- getSignificantVertices(collisionNormal: Point): Point[] {
425
- return this._polygon.getSignificantVertices(collisionNormal);
426
- }
427
-
428
- get AABB(): {min: Point, max: Point}{
429
- return this._polygon.AABB;
430
- }
431
-
432
- get mass(): number{
433
- return this._polygon.mass;
434
- }
435
-
436
- get staticFrictionCoeff(): number{
437
- return this._polygon.staticFrictionCoeff;
438
- }
439
-
440
- set staticFrictionCoeff(coeff: number){
441
- this._polygon.staticFrictionCoeff = coeff;
442
- }
443
-
444
- get momentOfInertia(): number {
445
- return this._polygon.momentOfInertia;
446
- }
447
-
448
- getNormalOfSignificantFace(collisionNormal: Point): Point {
449
- return this._polygon.getNormalOfSignificantFace(collisionNormal);
450
- }
451
-
452
- getAdjacentFaces(collisionNormal: Point): {startPoint: {coord: Point, index: number}, endPoint: {coord: Point, index: number}}[] {
453
- return this._polygon.getAdjacentFaces(collisionNormal);
454
- }
455
- }
456
-
457
- export class Polygon extends BaseRigidBody {
458
-
459
- private vertices: Point[];
460
- private _momentOfInertia: number;
461
-
462
- constructor(center: Point = {x: 0, y: 0}, vertices: Point[], _orientationAngle: number = 0, mass: number = 50, isStatic: boolean = false, frictionEnabled: boolean = true) {
463
- super(center, _orientationAngle, mass, isStatic, frictionEnabled);
464
- this.vertices = vertices;
465
- this.step = this.step.bind(this);
466
-
467
- let numerator = this.vertices.reduce((acc, vertex, index) => {
468
- let nextPointIndex = index < this.vertices.length - 1 ? index + 1 : 0;
469
- let nextPoint = this.vertices[nextPointIndex];
470
- let crossProduct = PointCal.crossProduct(nextPoint, vertex);
471
- return acc + PointCal.magnitude(crossProduct) * (PointCal.dotProduct(vertex, vertex) + PointCal.dotProduct(vertex, nextPoint) + PointCal.dotProduct(nextPoint, nextPoint));
472
- }, 0);
473
-
474
- let denomiator = this.vertices.reduce((acc, vertex, index) => {
475
- return acc + PointCal.magnitude(PointCal.crossProduct(this.vertices[index < this.vertices.length - 1 ? index + 1 : 0], vertex));
476
- }, 0);
477
-
478
-
479
- this._momentOfInertia = this._mass * numerator / (6 * denomiator);
480
- }
481
-
482
-
483
- getVerticesAbsCoord(): Point[]{
484
- return this.vertices.map(vertex=>{
485
- return PointCal.addVector(this._center, PointCal.rotatePoint(vertex, this._orientationAngle));
486
- });
487
- }
488
-
489
- getCollisionAxes(relativeBody: RigidBody): Point[] {
490
- return this.getVerticesAbsCoord().map((vertex, index, absVertices)=>{
491
- let vector = PointCal.subVector(vertex, absVertices[absVertices.length - 1]);
492
- if (index > 0){
493
- vector = PointCal.subVector(vertex, absVertices[index - 1]);
494
- }
495
- return PointCal.unitVector(PointCal.rotatePoint(vector, Math.PI / 2));
496
- });
497
- }
498
-
499
- getMinMaxProjection(unitvector: Point): { min: number; max: number; } {
500
- let vertices = this.getVerticesAbsCoord();
501
-
502
- let projections = vertices.map( vertex => {
503
- return PointCal.dotProduct(vertex, unitvector);
504
- });
505
-
506
-
507
- return {min: Math.min(...projections), max: Math.max(...projections)};
508
- }
509
-
510
- get AABB(): { min: Point; max: Point; } {
511
- let points = this.getVerticesAbsCoord();
512
- let xCoords = points.map(vertex => vertex.x);
513
- let yCoords = points.map(vertex => vertex.y);
514
- return {min: {x: Math.min(...xCoords), y: Math.min(...yCoords)}, max: {x: Math.max(...xCoords), y: Math.max(...yCoords)}};
515
- }
516
-
517
- significantVertex(collisionNormal: Point): Point {
518
- let vertices = this.getVerticesAbsCoord();
519
- let verticesProjected = vertices.map(vertex => PointCal.dotProduct(vertex, collisionNormal));
520
- let maxIndex = verticesProjected.indexOf(Math.max(...verticesProjected));
521
- return vertices[maxIndex];
522
- }
523
-
524
- getSignificantVertices(collisionNormal: Point): Point[]{
525
- let vertices = this.getVerticesAbsCoord();
526
- let verticesProjected = vertices.map(vertex => PointCal.dotProduct(vertex, collisionNormal));
527
- let maxIndex = verticesProjected.indexOf(Math.max(...verticesProjected));
528
- const tipVertex = vertices[maxIndex];
529
- let prevPointIndex = maxIndex > 0 ? maxIndex - 1 : vertices.length - 1;
530
- let nextPointIndex = maxIndex < vertices.length - 1 ? maxIndex + 1 : 0;
531
- const prevPoint = vertices[prevPointIndex];
532
- const nextPoint = vertices[nextPointIndex];
533
- const prevPointProjected = PointCal.dotProduct(prevPoint, collisionNormal);
534
- const nextPointProjected = PointCal.dotProduct(nextPoint, collisionNormal);
535
- if (prevPointProjected > nextPointProjected) {
536
- return [prevPoint, tipVertex];
537
- } else {
538
- return [tipVertex, nextPoint];
539
- }
540
- }
541
-
542
- getNormalOfSignificantFace(collisionNormal: Point): Point{
543
- const vertices = this.getSignificantVertices(collisionNormal);
544
- const direction = PointCal.unitVectorFromA2B(vertices[0], vertices[1]);
545
- return PointCal.rotatePoint(direction, -Math.PI / 2);
546
- }
547
-
548
- getAdjacentFaces(collisionNormal: Point): {startPoint: {coord: Point, index: number}, endPoint: {coord: Point, index: number}}[]{
549
- let vertices = this.getVerticesAbsCoord();
550
- let verticesProjected = vertices.map(vertex => PointCal.dotProduct(vertex, collisionNormal));
551
- let maxIndex = verticesProjected.indexOf(Math.max(...verticesProjected));
552
- const tipVertex = vertices[maxIndex];
553
- let prevPointIndex = maxIndex > 0 ? maxIndex - 1 : vertices.length - 1;
554
- let nextPointIndex = maxIndex < vertices.length - 1 ? maxIndex + 1 : 0;
555
- const prevPoint = vertices[prevPointIndex];
556
- const nextPoint = vertices[nextPointIndex];
557
- const prevPointProjected = PointCal.dotProduct(prevPoint, collisionNormal);
558
- const nextPointProjected = PointCal.dotProduct(nextPoint, collisionNormal);
559
- const adjacentFaces: Point[] = [];
560
- const adjacentFacesWithIndex: {startPoint: {coord: Point, index: number}, endPoint: {coord: Point, index: number}}[] = [];
561
- if (prevPointProjected > nextPointProjected) {
562
- adjacentFaces.push(prevPoint, tipVertex);
563
- adjacentFacesWithIndex.push({startPoint: {coord: prevPoint, index: prevPointIndex}, endPoint: {coord: tipVertex, index: maxIndex}});
564
-
565
- // the nextface is the next face
566
- adjacentFacesWithIndex.unshift({startPoint: {coord: tipVertex, index: maxIndex}, endPoint: {coord: nextPoint, index: nextPointIndex}});
567
- // need to get the previous face of the previous face
568
- let prevPrevPointIndex = prevPointIndex > 0 ? prevPointIndex - 1 : vertices.length - 1;
569
- adjacentFacesWithIndex.unshift({startPoint: {coord: vertices[prevPrevPointIndex], index: prevPrevPointIndex}, endPoint: {coord: prevPoint, index: prevPointIndex}});
570
- } else {
571
- adjacentFaces.push(tipVertex, nextPoint);
572
- adjacentFacesWithIndex.push({startPoint: {coord: tipVertex, index: maxIndex}, endPoint: {coord: nextPoint, index: nextPointIndex}});
573
-
574
- // need to get the next face of the next face
575
- let nextNextPointIndex = nextPointIndex < vertices.length - 1 ? nextPointIndex + 1 : 0;
576
- adjacentFacesWithIndex.unshift({startPoint: {coord: nextPoint, index: nextPointIndex}, endPoint: {coord: vertices[nextNextPointIndex], index: nextNextPointIndex}})
577
-
578
- // the prevoius face is the previous face
579
- adjacentFacesWithIndex.unshift({startPoint: {coord: prevPoint, index: prevPointIndex}, endPoint: {coord: tipVertex, index: maxIndex}});
580
- }
581
-
582
- return adjacentFacesWithIndex;
583
- }
584
-
585
- get momentOfInertia(): number {
586
- return this._momentOfInertia;
587
- }
588
-
589
- }
590
-
591
- export class Circle extends BaseRigidBody {
592
-
593
- private _radius: number;
594
- private _momentOfInertia: number;
595
-
596
- constructor(center: Point = {x: 0, y: 0}, radius: number, _orientationAngle: number = 0, mass: number = 50, isStatic: boolean = false, frictionEnabled: boolean = true) {
597
- super(center, _orientationAngle, mass, isStatic, frictionEnabled);
598
- this._radius = radius;
599
- this.step = this.step.bind(this);
600
- this._momentOfInertia = this._mass * this._radius * this._radius / 2;
601
- }
602
-
603
- getMinMaxProjection(unitvector: Point): { min: number; max: number; } {
604
- let PositiveFurthest = PointCal.addVector(this._center, PointCal.multiplyVectorByScalar(unitvector, this._radius));
605
- let NegativeFurthest = PointCal.addVector(this._center, PointCal.multiplyVectorByScalar(unitvector, -this._radius));
606
- return {min: PointCal.dotProduct(NegativeFurthest, unitvector), max: PointCal.dotProduct(PositiveFurthest, unitvector)};
607
- }
608
-
609
- getCollisionAxes(relativeBody: RigidBody): Point[] {
610
- return [PointCal.unitVector(PointCal.subVector(relativeBody.center, this._center))];
611
- }
612
-
613
- get AABB(): { min: Point; max: Point; } {
614
- return {min: PointCal.subVector(this._center, {x: this._radius, y: this._radius}), max: PointCal.addVector(this._center, {x: this._radius, y: this._radius})};
615
- }
616
-
617
- significantVertex(collisionNormal: Point): Point {
618
- return PointCal.addVector(this._center, PointCal.multiplyVectorByScalar(collisionNormal, this._radius));
619
- }
620
-
621
- get radius(): number {
622
- return this._radius;
623
- }
624
-
625
- getSignificantVertices(collisionNormal: Point): Point[]{
626
- return [PointCal.addVector(this._center, PointCal.multiplyVectorByScalar(collisionNormal, this._radius))];
627
- }
628
-
629
- getNormalOfSignificantFace(collisionNormal: Point): Point{
630
- return PointCal.unitVector(collisionNormal);
631
- }
632
-
633
- getAdjacentFaces(collisionNormal: Point): {startPoint: {coord: Point, index: number}, endPoint: {coord: Point, index: number}}[]{
634
- return [];
635
- }
636
-
637
- get momentOfInertia(): number {
638
- return this._momentOfInertia;
639
- }
640
- }