Skip to content

Commit 9dcd356

Browse files
committed
Added new X axis functions
1 parent 1f07eaa commit 9dcd356

3 files changed

Lines changed: 465 additions & 93 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var CollisionInfo = require('./CollisionInfo');
8+
var CONST = require('./const');
9+
10+
/**
11+
* Takes a CollisionInfo object and tests to see if the two bodies are still intersecting / touching.
12+
*
13+
* @function Phaser.Physics.Arcade.CheckOverlapX
14+
* @since 3.17.0
15+
*
16+
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate.
17+
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body to separate.
18+
*
19+
* @return {boolean}
20+
*/
21+
var CheckOverlapX = function (collisionInfo)
22+
{
23+
collisionInfo = CollisionInfo.update(collisionInfo);
24+
25+
var face = collisionInfo.face;
26+
var body1 = collisionInfo.body1;
27+
var body2 = collisionInfo.body2;
28+
29+
if (face === CONST.FACING_LEFT)
30+
{
31+
// console.log('CheckOverlapX leftFace from', body.gameObject.name, 'body1 is', body1.gameObject.name, 'touching', collisionInfo.touching, 'inter', collisionInfo.intersects, 'oy', collisionInfo.overlapY);
32+
33+
body1.setTouchingLeft();
34+
body2.setTouchingRight();
35+
36+
body1.setBlockedLeft();
37+
body2.setBlockedRight();
38+
}
39+
else if (face === CONST.FACING_RIGHT)
40+
{
41+
// console.log('CheckOverlapX rightFace from', body.gameObject.name, 'body1 is', body1.gameObject.name);
42+
43+
body1.setTouchingRight();
44+
body2.setTouchingLeft();
45+
46+
body1.setBlockedRight();
47+
body2.setBlockedLeft();
48+
}
49+
50+
if (body1.isWorldBlockedRight())
51+
{
52+
body2.setHardBlockedRight();
53+
}
54+
else if (body2.isWorldBlockedRight())
55+
{
56+
body1.setHardBlockedRight();
57+
}
58+
59+
if (body1.isWorldBlockedLeft())
60+
{
61+
body2.setHardBlockedLeft();
62+
}
63+
else if (body2.isWorldBlockedLeft())
64+
{
65+
body1.setHardBlockedLeft();
66+
}
67+
68+
return collisionInfo.touching;
69+
};
70+
71+
module.exports = CheckOverlapX;

src/physics/arcade/GetOverlapX.js

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,70 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var CollisionInfo = require('./CollisionInfo');
78
var CONST = require('./const');
89

910
/**
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+
*
1212
*
1313
* @function Phaser.Physics.Arcade.GetOverlapX
14-
* @since 3.0.0
14+
* @since 3.17.0
1515
*
1616
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate.
1717
* @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).
2020
*
21-
* @return {number} The amount of overlap.
21+
* @return {CollisionInfo} A Collision Info object.
2222
*/
2323
var GetOverlapX = function (body1, body2, overlapOnly, bias)
2424
{
25-
var overlap = 0;
26-
var maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + bias;
25+
if (overlapOnly === undefined) { overlapOnly = false; }
26+
if (bias === undefined) { bias = 0; }
2727

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);
3829

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)
4433
{
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');
5035

51-
if (body2.physicsType === CONST.STATIC_BODY)
36+
if (collisionInfo.body1 === body1)
5237
{
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);
5643

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+
}
6148
}
6249
}
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)
7451
{
75-
body1.touching.none = false;
76-
body1.touching.left = true;
52+
// console.log('GetOverlapX rightFace');
7753

78-
body2.touching.none = false;
79-
body2.touching.right = true;
80-
81-
if (body2.physicsType === CONST.STATIC_BODY)
54+
if (collisionInfo.body1 === body1)
8255
{
83-
body1.blocked.none = false;
84-
body1.blocked.left = true;
85-
}
56+
body1.setTouchingRight();
57+
body2.setTouchingLeft();
8658

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+
}
9166
}
9267
}
9368
}
9469

95-
return overlap;
70+
return collisionInfo;
9671
};
9772

9873
module.exports = GetOverlapX;

0 commit comments

Comments
 (0)