Skip to content

Commit 9db9511

Browse files
committed
Working on new blocked flag setting
1 parent e27196c commit 9db9511

1 file changed

Lines changed: 73 additions & 21 deletions

File tree

src/physics/arcade/SeparateY.js

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

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

910
/**
1011
* Separates two overlapping bodies on the Y-axis (vertically).
1112
*
12-
* Separation involves moving two overlapping bodies so they don't overlap anymore and adjusting their velocities based on their mass. This is a core part of collision detection.
13+
* Separation involves moving two overlapping bodies so they don't overlap anymore
14+
* and adjusting their velocities based on their mass. This is a core part of collision detection.
1315
*
14-
* The bodies won't be separated if there is no vertical overlap between them, if they are static, or if either one uses custom logic for its separation.
16+
* The bodies won't be separated if there is no vertical overlap between them, if they are static,
17+
* or if either one uses custom logic for its separation.
1518
*
1619
* @function Phaser.Physics.Arcade.SeparateY
1720
* @since 3.0.0
1821
*
19-
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate.
22+
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate. This is our priority body.
2023
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body to separate.
2124
* @param {boolean} overlapOnly - If `true`, the bodies will only have their overlap data set and no separation will take place.
2225
* @param {number} bias - A value to add to the delta value during overlap checking. Used to prevent sprite tunneling.
@@ -25,25 +28,39 @@ var GetOverlapY = require('./GetOverlapY');
2528
*/
2629
var SeparateY = function (body1, body2, overlapOnly, bias)
2730
{
31+
// console.log('SeparateY start', body1.gameObject.name, body2.gameObject.name);
32+
2833
var overlap = GetOverlapY(body1, body2, overlapOnly, bias);
2934

35+
var velocity1 = body1.velocity;
36+
var velocity2 = body2.velocity;
37+
38+
var body1Immovable = (body1.physicsType === CONST.STATIC_BODY || body1.immovable);
39+
var body2Immovable = (body2.physicsType === CONST.STATIC_BODY || body2.immovable);
40+
3041
// Can't separate two immovable bodies, or a body with its own custom separation logic
31-
if (overlapOnly || overlap === 0 || (body1.immovable && body2.immovable) || body1.customSeparateY || body2.customSeparateY)
42+
if (overlapOnly || (body1Immovable && body2Immovable) || body1.customSeparateY || body2.customSeparateY)
3243
{
33-
// return true if there was some overlap, otherwise false
44+
// return true if there was some overlap, otherwise false.
3445
return (overlap !== 0) || (body1.embedded && body2.embedded);
3546
}
3647

37-
// Adjust their positions and velocities accordingly (if there was any overlap)
38-
var v1 = body1.velocity.y;
39-
var v2 = body2.velocity.y;
48+
// Adjust their positions and velocities accordingly based on the amount of overlap
49+
var v1 = velocity1.y;
50+
var v2 = velocity2.y;
4051

41-
if (!body1.immovable && !body2.immovable)
52+
// At this point, the velocity from gravity, world rebounds, etc has been factored in.
53+
// The body is moving the direction it wants to, but may be blocked.
54+
55+
if (!body1Immovable && !body2Immovable)
4256
{
57+
// Neither body is immovable, so they get an equal amount of separation and a new velocity based on mass
58+
// Share the overlap equally if both bodies are unblocked
59+
4360
overlap *= 0.5;
4461

45-
body1.y -= overlap;
46-
body2.y += overlap;
62+
body1.y -= body1.getMoveY(overlap, true);
63+
body2.y += body2.getMoveY(overlap, true);
4764

4865
var nv1 = Math.sqrt((v2 * v2 * body2.mass) / body1.mass) * ((v2 > 0) ? 1 : -1);
4966
var nv2 = Math.sqrt((v1 * v1 * body1.mass) / body2.mass) * ((v1 > 0) ? 1 : -1);
@@ -52,32 +69,67 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
5269
nv1 -= avg;
5370
nv2 -= avg;
5471

55-
body1.velocity.y = avg + nv1 * body1.bounce.y;
56-
body2.velocity.y = avg + nv2 * body2.bounce.y;
72+
velocity1.y = avg + nv1 * body1.bounce.y;
73+
velocity2.y = avg + nv2 * body2.bounce.y;
5774
}
58-
else if (!body1.immovable)
75+
else if (!body1Immovable)
5976
{
60-
body1.y -= overlap;
61-
body1.velocity.y = v2 - v1 * body1.bounce.y;
77+
// Body2 is immovable, but Body1 can move, so it gets all the separation unless blocked
78+
79+
body1.y -= body1.getMoveY(overlap, true);
80+
81+
velocity1.y = v2 - v1 * body1.bounce.y;
6282

6383
// This is special case code that handles things like horizontal moving platforms you can ride
6484
if (body2.moves)
6585
{
66-
body1.x += (body2.x - body2.prev.x) * body2.friction.x;
86+
body1.x += body1.getMoveX((body2.deltaX()) * body2.friction.x, true);
6787
}
6888
}
69-
else
89+
else if (!body2Immovable)
7090
{
71-
body2.y += overlap;
72-
body2.velocity.y = v1 - v2 * body2.bounce.y;
91+
// Body1 is immovable, but Body2 can move, so it gets all the separation unless blocked
92+
body2.y += body2.getMoveY(overlap, true);
93+
94+
velocity2.y = v1 - v2 * body2.bounce.y;
7395

7496
// This is special case code that handles things like horizontal moving platforms you can ride
7597
if (body1.moves)
7698
{
77-
body2.x += (body1.x - body1.prev.x) * body1.friction.x;
99+
body2.x += body2.getMoveX((body1.deltaX()) * body1.friction.x, true);
78100
}
79101
}
80102

103+
// It's possible both bodies were equally immovable, in which case neither of them have moved
104+
105+
/*
106+
var blocked1 = body1.blocked;
107+
var blocked2 = body2.blocked;
108+
109+
// Set flags
110+
if (overlap < 0)
111+
{
112+
// body1 was moving down
113+
body1.setTouchingDown();
114+
body2.setTouchingUp();
115+
116+
if (blocked2.down)
117+
{
118+
body1.setBlockedDown();
119+
120+
// If we're still moving, but blocked, and the velocity hasn't inversed (i.e. rebounded) we should zero it.
121+
if (velocity1.y > 0)
122+
{
123+
velocity1.y = 0;
124+
}
125+
}
126+
}
127+
else
128+
{
129+
// body1 was moving up
130+
}
131+
*/
132+
81133
// If we got this far then there WAS overlap, and separation is complete, so return true
82134
return true;
83135
};

0 commit comments

Comments
 (0)