dacha 0.17.2 → 0.18.0-alpha.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/build/contrib/components/collider/index.d.ts +6 -2
- package/build/contrib/components/collider/index.js +7 -2
- package/build/contrib/components/rigid-body/index.d.ts +29 -17
- package/build/contrib/components/rigid-body/index.js +64 -21
- package/build/contrib/events/index.d.ts +10 -75
- package/build/contrib/events/index.js +0 -36
- package/build/contrib/systems/physics-system/index.d.ts +0 -1
- package/build/contrib/systems/physics-system/index.js +6 -11
- package/build/contrib/systems/physics-system/subsystems/collision-broadcast/collision.d.ts +5 -3
- package/build/contrib/systems/physics-system/subsystems/collision-broadcast/collision.js +7 -5
- package/build/contrib/systems/physics-system/subsystems/collision-broadcast/index.d.ts +4 -6
- package/build/contrib/systems/physics-system/subsystems/collision-broadcast/index.js +20 -17
- package/build/contrib/systems/physics-system/subsystems/collision-detection/index.d.ts +12 -9
- package/build/contrib/systems/physics-system/subsystems/collision-detection/index.js +107 -93
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/box-box/check-boxes-intersection.d.ts +10 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/box-box/check-boxes-intersection.js +36 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/box-box/utils.d.ts +23 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/box-box/utils.js +143 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/box-circle/check-box-and-circle-intersection.d.ts +9 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/box-circle/check-box-and-circle-intersection.js +47 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/circle-circle/check-circles-intersection.d.ts +12 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/circle-circle/check-circles-intersection.js +49 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/index.d.ts +2 -2
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/index.js +3 -3
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/tests/helpers.d.ts +17 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/tests/helpers.js +66 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/utils.d.ts +4 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/utils.js +9 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/reorientation-checkers/check-collider.js +3 -0
- package/build/contrib/systems/physics-system/subsystems/collision-detection/types.d.ts +14 -5
- package/build/contrib/systems/physics-system/subsystems/collision-detection/types.js +0 -3
- package/build/contrib/systems/physics-system/subsystems/constraint-solver/index.d.ts +7 -10
- package/build/contrib/systems/physics-system/subsystems/constraint-solver/index.js +117 -79
- package/build/contrib/systems/physics-system/subsystems/index.d.ts +0 -1
- package/build/contrib/systems/physics-system/subsystems/index.js +0 -1
- package/build/contrib/systems/physics-system/subsystems/physics/index.d.ts +3 -9
- package/build/contrib/systems/physics-system/subsystems/physics/index.js +57 -93
- package/build/contrib/systems/physics-system/types.d.ts +9 -0
- package/build/engine/math-lib/vector/ops.d.ts +3 -3
- package/build/engine/math-lib/vector/ops.js +8 -5
- package/build/engine/math-lib/vector/vector2.d.ts +22 -7
- package/build/engine/math-lib/vector/vector2.js +29 -5
- package/build/events/index.d.ts +2 -2
- package/build/events/index.js +1 -1
- package/package.json +1 -1
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/check-box-and-circle-intersection.d.ts +0 -16
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/check-box-and-circle-intersection.js +0 -80
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/check-boxes-intersection.d.ts +0 -6
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/check-boxes-intersection.js +0 -72
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/check-circles-intersection.d.ts +0 -11
- package/build/contrib/systems/physics-system/subsystems/collision-detection/intersection-checkers/check-circles-intersection.js +0 -39
- package/build/contrib/systems/physics-system/subsystems/collision-solver/index.d.ts +0 -10
- package/build/contrib/systems/physics-system/subsystems/collision-solver/index.js +0 -50
|
@@ -89,7 +89,7 @@ export declare class VectorOps {
|
|
|
89
89
|
*/
|
|
90
90
|
static dotProduct(point: Point, vector: Vector2): number;
|
|
91
91
|
/**
|
|
92
|
-
*
|
|
92
|
+
* Returns the closest point on an edge segment to the given point.
|
|
93
93
|
*
|
|
94
94
|
* @param {Point} point - Point to project. Should have properties `x` and `y`.
|
|
95
95
|
* @param {Edge} edge - Edge to project onto, defined by two endpoints `point1` and `point2`.
|
|
@@ -100,10 +100,10 @@ export declare class VectorOps {
|
|
|
100
100
|
* ```typescript
|
|
101
101
|
* const point = { x: 3, y: 4 };
|
|
102
102
|
* const edge = { point1: { x: 0, y: 0 }, point2: { x: 10, y: 0 } };
|
|
103
|
-
* const projected = VectorOps.
|
|
103
|
+
* const projected = VectorOps.getClosestPointOnEdge(point, edge);
|
|
104
104
|
* ```
|
|
105
105
|
*/
|
|
106
|
-
static
|
|
106
|
+
static getClosestPointOnEdge(point: Point, edge: Edge): Point;
|
|
107
107
|
/**
|
|
108
108
|
* Determines if a point is inside a polygon.
|
|
109
109
|
*
|
|
@@ -87,7 +87,7 @@ export class VectorOps {
|
|
|
87
87
|
return point.x * vector.x + point.y * vector.y;
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
|
-
*
|
|
90
|
+
* Returns the closest point on an edge segment to the given point.
|
|
91
91
|
*
|
|
92
92
|
* @param {Point} point - Point to project. Should have properties `x` and `y`.
|
|
93
93
|
* @param {Edge} edge - Edge to project onto, defined by two endpoints `point1` and `point2`.
|
|
@@ -98,15 +98,18 @@ export class VectorOps {
|
|
|
98
98
|
* ```typescript
|
|
99
99
|
* const point = { x: 3, y: 4 };
|
|
100
100
|
* const edge = { point1: { x: 0, y: 0 }, point2: { x: 10, y: 0 } };
|
|
101
|
-
* const projected = VectorOps.
|
|
101
|
+
* const projected = VectorOps.getClosestPointOnEdge(point, edge);
|
|
102
102
|
* ```
|
|
103
103
|
*/
|
|
104
|
-
static
|
|
104
|
+
static getClosestPointOnEdge(point, edge) {
|
|
105
105
|
const abVector = new Vector2(edge.point2.x - edge.point1.x, edge.point2.y - edge.point1.y);
|
|
106
106
|
const apVector = new Vector2(point.x - edge.point1.x, point.y - edge.point1.y);
|
|
107
|
-
const dotProduct = VectorOps.dotProduct(apVector, abVector);
|
|
108
107
|
const lengthSquared = abVector.x * abVector.x + abVector.y * abVector.y;
|
|
109
|
-
|
|
108
|
+
if (lengthSquared === 0) {
|
|
109
|
+
return edge.point1;
|
|
110
|
+
}
|
|
111
|
+
const dotProduct = VectorOps.dotProduct(apVector, abVector);
|
|
112
|
+
const t = Math.max(0, Math.min(1, dotProduct / lengthSquared));
|
|
110
113
|
return {
|
|
111
114
|
x: edge.point1.x + t * abVector.x,
|
|
112
115
|
y: edge.point1.y + t * abVector.y,
|
|
@@ -15,28 +15,43 @@ export declare class Vector2 {
|
|
|
15
15
|
*/
|
|
16
16
|
constructor(x: number, y: number);
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
18
|
+
* Returns the Euclidean length of the vector.
|
|
19
|
+
*
|
|
19
20
|
* @returns Magnitude of the vector
|
|
20
21
|
*/
|
|
21
22
|
get magnitude(): number;
|
|
22
23
|
/**
|
|
23
|
-
* Adds
|
|
24
|
+
* Adds another vector to the current vector in place.
|
|
25
|
+
*
|
|
24
26
|
* @param vector - Vector to add
|
|
27
|
+
* @returns The current vector after the addition
|
|
25
28
|
*/
|
|
26
|
-
add(vector: Vector2):
|
|
29
|
+
add(vector: Vector2): Vector2;
|
|
27
30
|
/**
|
|
28
|
-
* Multiplies the current vector by
|
|
31
|
+
* Multiplies the current vector by a scalar in place.
|
|
32
|
+
*
|
|
29
33
|
* @param number - Number to multiply by
|
|
34
|
+
* @returns The current vector after scaling
|
|
30
35
|
*/
|
|
31
|
-
multiplyNumber(number: number):
|
|
36
|
+
multiplyNumber(number: number): Vector2;
|
|
32
37
|
/**
|
|
33
|
-
*
|
|
38
|
+
* Normalizes the current vector to unit length in place.
|
|
39
|
+
*
|
|
40
|
+
* If the vector magnitude is zero, it remains unchanged.
|
|
41
|
+
*
|
|
42
|
+
* @returns The current vector after normalization
|
|
43
|
+
*/
|
|
44
|
+
normalize(): Vector2;
|
|
45
|
+
/**
|
|
46
|
+
* Checks whether another vector has the same coordinates.
|
|
47
|
+
*
|
|
34
48
|
* @param vector - Vector to compare with
|
|
35
49
|
* @returns True if the vectors are equal, false otherwise
|
|
36
50
|
*/
|
|
37
51
|
equals(vector: Vector2): boolean;
|
|
38
52
|
/**
|
|
39
|
-
* Creates a new vector with the same
|
|
53
|
+
* Creates a new vector with the same coordinates.
|
|
54
|
+
*
|
|
40
55
|
* @returns A new vector with the same x and y coordinates
|
|
41
56
|
*/
|
|
42
57
|
clone(): Vector2;
|
|
@@ -18,30 +18,53 @@ export class Vector2 {
|
|
|
18
18
|
this.y = y;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Returns the Euclidean length of the vector.
|
|
22
|
+
*
|
|
22
23
|
* @returns Magnitude of the vector
|
|
23
24
|
*/
|
|
24
25
|
get magnitude() {
|
|
25
26
|
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
|
|
26
27
|
}
|
|
27
28
|
/**
|
|
28
|
-
* Adds
|
|
29
|
+
* Adds another vector to the current vector in place.
|
|
30
|
+
*
|
|
29
31
|
* @param vector - Vector to add
|
|
32
|
+
* @returns The current vector after the addition
|
|
30
33
|
*/
|
|
31
34
|
add(vector) {
|
|
32
35
|
this.x += vector.x;
|
|
33
36
|
this.y += vector.y;
|
|
37
|
+
return this;
|
|
34
38
|
}
|
|
35
39
|
/**
|
|
36
|
-
* Multiplies the current vector by
|
|
40
|
+
* Multiplies the current vector by a scalar in place.
|
|
41
|
+
*
|
|
37
42
|
* @param number - Number to multiply by
|
|
43
|
+
* @returns The current vector after scaling
|
|
38
44
|
*/
|
|
39
45
|
multiplyNumber(number) {
|
|
40
46
|
this.x *= number;
|
|
41
47
|
this.y *= number;
|
|
48
|
+
return this;
|
|
42
49
|
}
|
|
43
50
|
/**
|
|
44
|
-
*
|
|
51
|
+
* Normalizes the current vector to unit length in place.
|
|
52
|
+
*
|
|
53
|
+
* If the vector magnitude is zero, it remains unchanged.
|
|
54
|
+
*
|
|
55
|
+
* @returns The current vector after normalization
|
|
56
|
+
*/
|
|
57
|
+
normalize() {
|
|
58
|
+
const magnitude = this.magnitude;
|
|
59
|
+
if (magnitude === 0) {
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
this.multiplyNumber(1 / magnitude);
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks whether another vector has the same coordinates.
|
|
67
|
+
*
|
|
45
68
|
* @param vector - Vector to compare with
|
|
46
69
|
* @returns True if the vectors are equal, false otherwise
|
|
47
70
|
*/
|
|
@@ -49,7 +72,8 @@ export class Vector2 {
|
|
|
49
72
|
return this.x === vector.x && this.y === vector.y;
|
|
50
73
|
}
|
|
51
74
|
/**
|
|
52
|
-
* Creates a new vector with the same
|
|
75
|
+
* Creates a new vector with the same coordinates.
|
|
76
|
+
*
|
|
53
77
|
* @returns A new vector with the same x and y coordinates
|
|
54
78
|
*/
|
|
55
79
|
clone() {
|
package/build/events/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { AddActor, RemoveActor, LoadScene, EnterScene, ExitScene, DestroyScene, SceneLoaded, SceneEntered, SceneExited, SceneDestroyed, } from '../engine/events';
|
|
2
2
|
export type { AddActorEvent, RemoveActorEvent, LoadSceneEvent, EnterSceneEvent, ExitSceneEvent, DestroySceneEvent, SceneLoadedEvent, SceneEnteredEvent, SceneExitedEvent, SceneDestroyedEvent, } from '../engine/events';
|
|
3
|
-
export { GameStatsUpdate, KeyboardInput, MouseInput, CollisionEnter, CollisionStay, CollisionLeave,
|
|
4
|
-
export type { GameStatsUpdateEvent, KeyboardInputEvent, MouseInputEvent, KeyboardControlEvent, MouseControlEvent, CollisionEnterEvent, CollisionStayEvent, CollisionLeaveEvent,
|
|
3
|
+
export { GameStatsUpdate, KeyboardInput, MouseInput, CollisionEnter, CollisionStay, CollisionLeave, PlayAudio, StopAudio, SetAudioVolume, } from '../contrib/events';
|
|
4
|
+
export type { GameStatsUpdateEvent, KeyboardInputEvent, MouseInputEvent, KeyboardControlEvent, MouseControlEvent, CollisionEnterEvent, CollisionStayEvent, CollisionLeaveEvent, SetAudioGroupVolumeEvent, SetAudioSourceVolumeEvent, } from '../contrib/events';
|
package/build/events/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { AddActor, RemoveActor, LoadScene, EnterScene, ExitScene, DestroyScene, SceneLoaded, SceneEntered, SceneExited, SceneDestroyed, } from '../engine/events';
|
|
2
|
-
export { GameStatsUpdate, KeyboardInput, MouseInput, CollisionEnter, CollisionStay, CollisionLeave,
|
|
2
|
+
export { GameStatsUpdate, KeyboardInput, MouseInput, CollisionEnter, CollisionStay, CollisionLeave, PlayAudio, StopAudio, SetAudioVolume, } from '../contrib/events';
|
package/package.json
CHANGED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { CollisionEntry, Intersection } from '../types';
|
|
2
|
-
/**
|
|
3
|
-
* Checks box and circle colliders at the intersection.
|
|
4
|
-
* The main target is to check two possible scenarios:
|
|
5
|
-
* - circle lies inside box
|
|
6
|
-
* - circle intersects one of the boxe's edges
|
|
7
|
-
* Steps of the algorithm:
|
|
8
|
-
* 1. Find the nearest edge to circle center and check wether it intersects with circle or not
|
|
9
|
-
* For each edge three points should be considered: corners and circle center projection
|
|
10
|
-
* 2. Determine is the circle center lies inside of the box or not.
|
|
11
|
-
* This affects how we should compute mtv distance
|
|
12
|
-
* 3. If circle doesn't have any intersection with boxe's edges
|
|
13
|
-
* and circle center lies outside of the box – return false.
|
|
14
|
-
* Otherwise compute mtv vectors considering relative position of circle and box centers
|
|
15
|
-
*/
|
|
16
|
-
export declare const checkBoxAndCircleIntersection: (arg1: CollisionEntry, arg2: CollisionEntry) => Intersection | false;
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Collider } from '../../../../../components';
|
|
2
|
-
import { MathOps, Vector2, VectorOps } from '../../../../../../engine/math-lib';
|
|
3
|
-
const getMtvs = (axis, overlap, point1, point2) => {
|
|
4
|
-
axis.multiplyNumber((1 / axis.magnitude) * overlap);
|
|
5
|
-
const positiveX = Math.abs(axis.x);
|
|
6
|
-
const negativeX = -Math.abs(axis.x);
|
|
7
|
-
const positiveY = Math.abs(axis.y);
|
|
8
|
-
const negativeY = -Math.abs(axis.y);
|
|
9
|
-
return {
|
|
10
|
-
mtv1: new Vector2(point1.x < point2.x ? negativeX : positiveX, point1.y < point2.y ? negativeY : positiveY),
|
|
11
|
-
mtv2: new Vector2(point2.x > point1.x ? positiveX : negativeX, point2.y > point1.y ? positiveY : negativeY),
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Checks box and circle colliders at the intersection.
|
|
16
|
-
* The main target is to check two possible scenarios:
|
|
17
|
-
* - circle lies inside box
|
|
18
|
-
* - circle intersects one of the boxe's edges
|
|
19
|
-
* Steps of the algorithm:
|
|
20
|
-
* 1. Find the nearest edge to circle center and check wether it intersects with circle or not
|
|
21
|
-
* For each edge three points should be considered: corners and circle center projection
|
|
22
|
-
* 2. Determine is the circle center lies inside of the box or not.
|
|
23
|
-
* This affects how we should compute mtv distance
|
|
24
|
-
* 3. If circle doesn't have any intersection with boxe's edges
|
|
25
|
-
* and circle center lies outside of the box – return false.
|
|
26
|
-
* Otherwise compute mtv vectors considering relative position of circle and box centers
|
|
27
|
-
*/
|
|
28
|
-
export const checkBoxAndCircleIntersection = (arg1, arg2) => {
|
|
29
|
-
let box;
|
|
30
|
-
let circle;
|
|
31
|
-
if (arg1.actor.getComponent(Collider).type === 'box') {
|
|
32
|
-
box = arg1.geometry;
|
|
33
|
-
circle = arg2.geometry;
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
box = arg2.geometry;
|
|
37
|
-
circle = arg1.geometry;
|
|
38
|
-
}
|
|
39
|
-
let isIntersection = false;
|
|
40
|
-
let minDistance = Infinity;
|
|
41
|
-
let minDistanceAxis;
|
|
42
|
-
const { center: circleCenter } = circle;
|
|
43
|
-
for (const edge of box.edges) {
|
|
44
|
-
const projectedPoint = VectorOps.projectPointToEdge(circleCenter, edge);
|
|
45
|
-
const minX = Math.min(edge.point1.x, edge.point2.x);
|
|
46
|
-
const maxX = Math.max(edge.point1.x, edge.point2.x);
|
|
47
|
-
const minY = Math.min(edge.point1.y, edge.point2.y);
|
|
48
|
-
const maxY = Math.max(edge.point1.y, edge.point2.y);
|
|
49
|
-
const isPointOnEdge = projectedPoint.x >= minX
|
|
50
|
-
&& projectedPoint.x <= maxX
|
|
51
|
-
&& projectedPoint.y >= minY
|
|
52
|
-
&& projectedPoint.y <= maxY;
|
|
53
|
-
const distanceProjection = isPointOnEdge ? MathOps.getDistanceBetweenTwoPoints(circleCenter.x, projectedPoint.x, circleCenter.y, projectedPoint.y) : Infinity;
|
|
54
|
-
const distance1 = MathOps.getDistanceBetweenTwoPoints(circleCenter.x, edge.point1.x, circleCenter.y, edge.point1.y);
|
|
55
|
-
const distance2 = MathOps.getDistanceBetweenTwoPoints(circleCenter.x, edge.point2.x, circleCenter.y, edge.point2.y);
|
|
56
|
-
const isInsideCircle = Math.min(distanceProjection, distance1, distance2) < circle.radius;
|
|
57
|
-
isIntersection = isIntersection || isInsideCircle;
|
|
58
|
-
if (isPointOnEdge && distanceProjection < minDistance) {
|
|
59
|
-
minDistance = distanceProjection;
|
|
60
|
-
minDistanceAxis = edge.normal.clone();
|
|
61
|
-
}
|
|
62
|
-
if (distance1 < minDistance) {
|
|
63
|
-
minDistance = distance1;
|
|
64
|
-
minDistanceAxis = minDistance !== 0
|
|
65
|
-
? new Vector2(edge.point1.x - circleCenter.x, edge.point1.y - circleCenter.y)
|
|
66
|
-
: edge.normal.clone();
|
|
67
|
-
}
|
|
68
|
-
if (distance2 < minDistance) {
|
|
69
|
-
minDistance = distance2;
|
|
70
|
-
minDistanceAxis = minDistance !== 0
|
|
71
|
-
? new Vector2(edge.point2.x - circleCenter.x, edge.point2.y - circleCenter.y)
|
|
72
|
-
: edge.normal.clone();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
const isInsidePolygon = VectorOps.isPointInPolygon(circleCenter, box.edges);
|
|
76
|
-
if (!isIntersection && !isInsidePolygon) {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
return getMtvs(minDistanceAxis, isInsidePolygon ? circle.radius + minDistance : circle.radius - minDistance, arg1.geometry.center, arg2.geometry.center);
|
|
80
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { CollisionEntry, Intersection } from '../types';
|
|
2
|
-
/**
|
|
3
|
-
* Checks boxes colliders at the intersection.
|
|
4
|
-
* The SAT (separating axis theorem) is used to determine an intersection and mtvs.
|
|
5
|
-
*/
|
|
6
|
-
export declare const checkBoxesIntersection: (arg1: CollisionEntry, arg2: CollisionEntry) => Intersection | false;
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { Vector2, VectorOps } from '../../../../../../engine/math-lib';
|
|
2
|
-
const projectPolygon = (polygon, axisVector) => {
|
|
3
|
-
const initialProjectionValue = VectorOps.dotProduct(polygon.edges[0].point1, axisVector);
|
|
4
|
-
const projection = {
|
|
5
|
-
min: initialProjectionValue,
|
|
6
|
-
max: initialProjectionValue,
|
|
7
|
-
};
|
|
8
|
-
for (let i = 1; i < polygon.edges.length; i += 1) {
|
|
9
|
-
const projectionValue = VectorOps.dotProduct(polygon.edges[i].point1, axisVector);
|
|
10
|
-
if (projectionValue < projection.min) {
|
|
11
|
-
projection.min = projectionValue;
|
|
12
|
-
}
|
|
13
|
-
else if (projectionValue > projection.max) {
|
|
14
|
-
projection.max = projectionValue;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return projection;
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* Checks boxes colliders at the intersection.
|
|
21
|
-
* The SAT (separating axis theorem) is used to determine an intersection and mtvs.
|
|
22
|
-
*/
|
|
23
|
-
export const checkBoxesIntersection = (arg1, arg2) => {
|
|
24
|
-
let overlap = Infinity;
|
|
25
|
-
let normal;
|
|
26
|
-
const geometry1 = arg1.geometry;
|
|
27
|
-
const geometry2 = arg2.geometry;
|
|
28
|
-
// Consider arg1 box normals as axes
|
|
29
|
-
for (const edge of geometry1.edges) {
|
|
30
|
-
const axis = edge.normal;
|
|
31
|
-
const aProjection = projectPolygon(geometry1, axis);
|
|
32
|
-
const bProjection = projectPolygon(geometry2, axis);
|
|
33
|
-
const aDistance = aProjection.min - bProjection.max;
|
|
34
|
-
const bDistance = bProjection.min - aProjection.max;
|
|
35
|
-
if (aDistance > 0 || bDistance > 0) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
const axisOverlap = Math.min(Math.abs(aDistance), Math.abs(bDistance));
|
|
39
|
-
if (axisOverlap < overlap) {
|
|
40
|
-
overlap = axisOverlap;
|
|
41
|
-
normal = axis;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
// Consider arg2 box normals as axes
|
|
45
|
-
for (const edge of geometry2.edges) {
|
|
46
|
-
const axis = edge.normal;
|
|
47
|
-
const aProjection = projectPolygon(geometry1, axis);
|
|
48
|
-
const bProjection = projectPolygon(geometry2, axis);
|
|
49
|
-
const aDistance = aProjection.min - bProjection.max;
|
|
50
|
-
const bDistance = bProjection.min - aProjection.max;
|
|
51
|
-
if (aDistance > 0 || bDistance > 0) {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
const axisOverlap = Math.min(Math.abs(aDistance), Math.abs(bDistance));
|
|
55
|
-
if (axisOverlap < overlap) {
|
|
56
|
-
overlap = axisOverlap;
|
|
57
|
-
normal = axis;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const { x: xArg1, y: yArg1 } = geometry1.center;
|
|
61
|
-
const { x: xArg2, y: yArg2 } = geometry2.center;
|
|
62
|
-
const mtv = normal.clone();
|
|
63
|
-
mtv.multiplyNumber(overlap);
|
|
64
|
-
const positiveX = Math.abs(mtv.x);
|
|
65
|
-
const negativeX = -Math.abs(mtv.x);
|
|
66
|
-
const positiveY = Math.abs(mtv.y);
|
|
67
|
-
const negativeY = -Math.abs(mtv.y);
|
|
68
|
-
return {
|
|
69
|
-
mtv1: new Vector2(xArg1 < xArg2 ? negativeX : positiveX, yArg1 < yArg2 ? negativeY : positiveY),
|
|
70
|
-
mtv2: new Vector2(xArg2 > xArg1 ? positiveX : negativeX, yArg2 > yArg1 ? positiveY : negativeY),
|
|
71
|
-
};
|
|
72
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { CollisionEntry, Intersection } from '../types';
|
|
2
|
-
/**
|
|
3
|
-
* Checks circles at the intersection.
|
|
4
|
-
* Steps of the alghorith:
|
|
5
|
-
* 1. Calculate distance between circles centers.
|
|
6
|
-
* 2. If distance greater or equal to summ of circles radiuses then is no intersection.
|
|
7
|
-
* 3. If distance is zero then circles centers lie at the same point, so just X axis used for mtv.
|
|
8
|
-
* 4. If distance less than summ of circles radiuses and it's non-zero
|
|
9
|
-
* then circles centers used to get the axis.
|
|
10
|
-
*/
|
|
11
|
-
export declare const checkCirclesIntersection: (arg1: CollisionEntry, arg2: CollisionEntry) => Intersection | false;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { Vector2 } from '../../../../../../engine/math-lib';
|
|
2
|
-
/**
|
|
3
|
-
* Checks circles at the intersection.
|
|
4
|
-
* Steps of the alghorith:
|
|
5
|
-
* 1. Calculate distance between circles centers.
|
|
6
|
-
* 2. If distance greater or equal to summ of circles radiuses then is no intersection.
|
|
7
|
-
* 3. If distance is zero then circles centers lie at the same point, so just X axis used for mtv.
|
|
8
|
-
* 4. If distance less than summ of circles radiuses and it's non-zero
|
|
9
|
-
* then circles centers used to get the axis.
|
|
10
|
-
*/
|
|
11
|
-
export const checkCirclesIntersection = (arg1, arg2) => {
|
|
12
|
-
const { radius: rArg1 } = arg1.geometry;
|
|
13
|
-
const { radius: rArg2 } = arg2.geometry;
|
|
14
|
-
const { x: xArg1, y: yArg1 } = arg1.geometry.center;
|
|
15
|
-
const { x: xArg2, y: yArg2 } = arg2.geometry.center;
|
|
16
|
-
const x = xArg1 - xArg2;
|
|
17
|
-
const y = yArg1 - yArg2;
|
|
18
|
-
const distance = Math.sqrt((x ** 2) + (y ** 2));
|
|
19
|
-
if (distance >= rArg1 + rArg2) {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
if (distance === 0) {
|
|
23
|
-
return {
|
|
24
|
-
mtv1: new Vector2(rArg1 + rArg2, 0),
|
|
25
|
-
mtv2: new Vector2(-(rArg1 + rArg2), 0),
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
const mtv = new Vector2(x, y);
|
|
29
|
-
const overlap = rArg1 + rArg2 - distance;
|
|
30
|
-
mtv.multiplyNumber((1 / distance) * overlap);
|
|
31
|
-
const positiveX = Math.abs(mtv.x);
|
|
32
|
-
const negativeX = -Math.abs(mtv.x);
|
|
33
|
-
const positiveY = Math.abs(mtv.y);
|
|
34
|
-
const negativeY = -Math.abs(mtv.y);
|
|
35
|
-
return {
|
|
36
|
-
mtv1: new Vector2(xArg1 < xArg2 ? negativeX : positiveX, yArg1 < yArg2 ? negativeY : positiveY),
|
|
37
|
-
mtv2: new Vector2(xArg2 < xArg1 ? negativeX : positiveX, yArg2 < yArg1 ? negativeY : positiveY),
|
|
38
|
-
};
|
|
39
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { SceneSystemOptions } from '../../../../../engine/system';
|
|
2
|
-
export declare class CollisionSolver {
|
|
3
|
-
private scene;
|
|
4
|
-
private gravity;
|
|
5
|
-
constructor(options: SceneSystemOptions);
|
|
6
|
-
destroy(): void;
|
|
7
|
-
private handleCollision;
|
|
8
|
-
private validateCollision;
|
|
9
|
-
private addReactionForce;
|
|
10
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { Vector2 } from '../../../../../engine/math-lib';
|
|
2
|
-
import { RigidBody } from '../../../../components/rigid-body';
|
|
3
|
-
import { Collision, AddForce, StopMovement } from '../../../../events';
|
|
4
|
-
import { RIGID_BODY_TYPE } from '../../consts';
|
|
5
|
-
const REACTION_FORCE_VECTOR_X = 0;
|
|
6
|
-
const REACTION_FORCE_VECTOR_Y = -1;
|
|
7
|
-
export class CollisionSolver {
|
|
8
|
-
scene;
|
|
9
|
-
gravity;
|
|
10
|
-
constructor(options) {
|
|
11
|
-
const { scene, gravity } = options;
|
|
12
|
-
this.scene = scene;
|
|
13
|
-
this.gravity = gravity;
|
|
14
|
-
this.scene.addEventListener(Collision, this.handleCollision);
|
|
15
|
-
}
|
|
16
|
-
destroy() {
|
|
17
|
-
this.scene.removeEventListener(Collision, this.handleCollision);
|
|
18
|
-
}
|
|
19
|
-
handleCollision = (event) => {
|
|
20
|
-
const { actor1, actor2, mtv1 } = event;
|
|
21
|
-
if (!this.validateCollision(actor1, actor2)) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
this.addReactionForce(actor1, mtv1);
|
|
25
|
-
};
|
|
26
|
-
validateCollision(actor1, actor2) {
|
|
27
|
-
const rigidBody1 = actor1.getComponent(RigidBody);
|
|
28
|
-
const rigidBody2 = actor2.getComponent(RigidBody);
|
|
29
|
-
if (!rigidBody1 || !rigidBody2) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
if (rigidBody2.type === RIGID_BODY_TYPE.STATIC) {
|
|
33
|
-
return !rigidBody1.ghost && !rigidBody2.ghost;
|
|
34
|
-
}
|
|
35
|
-
return !rigidBody1.ghost && !rigidBody1.isPermeable
|
|
36
|
-
&& !rigidBody2.ghost && !rigidBody2.isPermeable;
|
|
37
|
-
}
|
|
38
|
-
addReactionForce(actor, mtv) {
|
|
39
|
-
const rigidBody = actor.getComponent(RigidBody);
|
|
40
|
-
const { useGravity, mass } = rigidBody;
|
|
41
|
-
if (useGravity && mtv.y && Math.sign(mtv.y) === -1 && !mtv.x) {
|
|
42
|
-
const reactionForce = new Vector2(REACTION_FORCE_VECTOR_X, REACTION_FORCE_VECTOR_Y);
|
|
43
|
-
reactionForce.multiplyNumber(mass * this.gravity);
|
|
44
|
-
actor.dispatchEventImmediately(AddForce, {
|
|
45
|
-
value: reactionForce,
|
|
46
|
-
});
|
|
47
|
-
actor.dispatchEventImmediately(StopMovement);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|