|
4 | 4 | * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
5 | 5 | */ |
6 | 6 |
|
| 7 | +var CollisionInfo = require('./CollisionInfo'); |
7 | 8 | var CONST = require('./const'); |
8 | 9 |
|
9 | 10 | /** |
10 | | - * Calculates and returns the horizontal overlap between two arcade physics bodies and sets their properties |
11 | | - * accordingly, including: `touching.left`, `touching.right`, `touching.none` and `overlapX'. |
| 11 | + * |
12 | 12 | * |
13 | 13 | * @function Phaser.Physics.Arcade.GetOverlapX |
14 | | - * @since 3.0.0 |
| 14 | + * @since 3.17.0 |
15 | 15 | * |
16 | 16 | * @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate. |
17 | 17 | * @param {Phaser.Physics.Arcade.Body} body2 - The second Body to separate. |
18 | | - * @param {boolean} overlapOnly - Is this an overlap only check, or part of separation? |
19 | | - * @param {number} bias - A value added to the delta values during collision checks. Increase it to prevent sprite tunneling(sprites passing through another instead of colliding). |
| 18 | + * @param {boolean} [overlapOnly] - Is this an overlap only check, or part of separation? |
| 19 | + * @param {number} [bias] - A value added to the delta values during collision checks. Increase it to prevent sprite tunneling (sprites passing through each other instead of colliding). |
20 | 20 | * |
21 | | - * @return {number} The amount of overlap. |
| 21 | + * @return {CollisionInfo} A Collision Info object. |
22 | 22 | */ |
23 | 23 | var GetOverlapX = function (body1, body2, overlapOnly, bias) |
24 | 24 | { |
25 | | - var overlap = 0; |
26 | | - var maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + bias; |
| 25 | + if (overlapOnly === undefined) { overlapOnly = false; } |
| 26 | + if (bias === undefined) { bias = 0; } |
27 | 27 |
|
28 | | - if (body1._dx === 0 && body2._dx === 0) |
29 | | - { |
30 | | - // They overlap but neither of them are moving |
31 | | - body1.embedded = true; |
32 | | - body2.embedded = true; |
33 | | - } |
34 | | - else if (body1._dx > body2._dx) |
35 | | - { |
36 | | - // Body1 is moving right and / or Body2 is moving left |
37 | | - overlap = body1.right - body2.x; |
| 28 | + var collisionInfo = CollisionInfo.get(body1, body2, overlapOnly, bias); |
38 | 29 |
|
39 | | - if ((overlap > maxOverlap && !overlapOnly) || body1.checkCollision.right === false || body2.checkCollision.left === false) |
40 | | - { |
41 | | - overlap = 0; |
42 | | - } |
43 | | - else |
| 30 | + if (!overlapOnly) |
| 31 | + { |
| 32 | + if (collisionInfo.face === CONST.FACING_LEFT) |
44 | 33 | { |
45 | | - body1.touching.none = false; |
46 | | - body1.touching.right = true; |
47 | | - |
48 | | - body2.touching.none = false; |
49 | | - body2.touching.left = true; |
| 34 | + // console.log('GetOverlapX leftFace'); |
50 | 35 |
|
51 | | - if (body2.physicsType === CONST.STATIC_BODY) |
| 36 | + if (collisionInfo.body1 === body1) |
52 | 37 | { |
53 | | - body1.blocked.none = false; |
54 | | - body1.blocked.right = true; |
55 | | - } |
| 38 | + body1.setTouchingLeft(); |
| 39 | + body2.setTouchingRight(); |
| 40 | + |
| 41 | + body1.setBlockedLeft(collisionInfo); |
| 42 | + body2.setBlockedRight(collisionInfo); |
56 | 43 |
|
57 | | - if (body1.physicsType === CONST.STATIC_BODY) |
58 | | - { |
59 | | - body2.blocked.none = false; |
60 | | - body2.blocked.left = true; |
| 44 | + if (body2.isWorldBlockedLeft()) |
| 45 | + { |
| 46 | + body1.setHardBlockedLeft(); |
| 47 | + } |
61 | 48 | } |
62 | 49 | } |
63 | | - } |
64 | | - else if (body1._dx < body2._dx) |
65 | | - { |
66 | | - // Body1 is moving left and/or Body2 is moving right |
67 | | - overlap = body1.x - body2.width - body2.x; |
68 | | - |
69 | | - if ((-overlap > maxOverlap && !overlapOnly) || body1.checkCollision.left === false || body2.checkCollision.right === false) |
70 | | - { |
71 | | - overlap = 0; |
72 | | - } |
73 | | - else |
| 50 | + else if (collisionInfo.face === CONST.FACING_RIGHT) |
74 | 51 | { |
75 | | - body1.touching.none = false; |
76 | | - body1.touching.left = true; |
| 52 | + // console.log('GetOverlapX rightFace'); |
77 | 53 |
|
78 | | - body2.touching.none = false; |
79 | | - body2.touching.right = true; |
80 | | - |
81 | | - if (body2.physicsType === CONST.STATIC_BODY) |
| 54 | + if (collisionInfo.body1 === body1) |
82 | 55 | { |
83 | | - body1.blocked.none = false; |
84 | | - body1.blocked.left = true; |
85 | | - } |
| 56 | + body1.setTouchingRight(); |
| 57 | + body2.setTouchingLeft(); |
86 | 58 |
|
87 | | - if (body1.physicsType === CONST.STATIC_BODY) |
88 | | - { |
89 | | - body2.blocked.none = false; |
90 | | - body2.blocked.right = true; |
| 59 | + body1.setBlockedRight(collisionInfo); |
| 60 | + body2.setBlockedLeft(collisionInfo); |
| 61 | + |
| 62 | + if (body2.isWorldBlockedRight()) |
| 63 | + { |
| 64 | + body1.setHardBlockedRight(); |
| 65 | + } |
91 | 66 | } |
92 | 67 | } |
93 | 68 | } |
94 | 69 |
|
95 | | - return overlap; |
| 70 | + return collisionInfo; |
96 | 71 | }; |
97 | 72 |
|
98 | 73 | module.exports = GetOverlapX; |
0 commit comments