Skip to content

Commit 0005cf5

Browse files
committed
Resolved bounds sleep issue
Now to create collision info objects for sleep settling.
1 parent 6a14504 commit 0005cf5

4 files changed

Lines changed: 142 additions & 88 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 101 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -996,10 +996,8 @@ var Body = new Class({
996996
{
997997
this.checkWorldBounds();
998998
}
999-
else
1000-
{
1001-
this.updateCenter();
1002-
}
999+
1000+
this.updateCenter();
10031001

10041002
// Reset deltas (world bounds checks have no effect on this)
10051003
this.prev.x = this.x;
@@ -1013,18 +1011,19 @@ var Body = new Class({
10131011
{
10141012
this.sleeping = true;
10151013

1016-
console.log(this.gameObject.name, 'put to sleep on frame', this.world._frame);
1014+
console.log(this.gameObject.name, 'put to sleep on frame', this.world._frame, 'force?', forceY, 'at', this.y);
10171015

10181016
this.velocity.set(0);
10191017
this.prevVelocity.set(0);
10201018
this.speed = 0;
10211019

1022-
// this.forcePosition = true;
1023-
1020+
var blocked = this.blocked;
10241021
var worldBlocked = this.worldBlocked;
10251022

10261023
if (forceY && !worldBlocked.none)
10271024
{
1025+
console.log(this.gameObject.name, 'sleeping and fixed to world bounds');
1026+
10281027
var worldBounds = this.world.bounds;
10291028

10301029
if (worldBlocked.down)
@@ -1038,6 +1037,21 @@ var Body = new Class({
10381037
this.forcePosition = 2;
10391038
}
10401039
}
1040+
else if (forceY && !blocked.none)
1041+
{
1042+
console.log(this.gameObject.name, 'sleeping and fixed to blocker bounds');
1043+
1044+
if (blocked.down)
1045+
{
1046+
// this.bottom = worldBounds.bottom;
1047+
this.forcePosition = 2;
1048+
}
1049+
else if (blocked.up)
1050+
{
1051+
// this.y = worldBounds.y;
1052+
this.forcePosition = 2;
1053+
}
1054+
}
10411055
}
10421056
},
10431057

@@ -1126,7 +1140,7 @@ var Body = new Class({
11261140
{
11271141
this.checkWorldRebound();
11281142
}
1129-
1143+
11301144
if (this.forcePosition !== 2)
11311145
{
11321146
position.x += this.getMoveX(velocity.x * delta);
@@ -1227,8 +1241,14 @@ var Body = new Class({
12271241
else
12281242
{
12291243
velocity.y *= -by;
1244+
12301245
console.log(this.gameObject.name, 'rebounded up', newVelocityY, gravityY, 'frame', this.world._frame);
12311246

1247+
if (this.forcePosition === 2)
1248+
{
1249+
this.forcePosition = 0;
1250+
}
1251+
12321252
if (this.onWorldBounds)
12331253
{
12341254
this.world.emit(Events.WORLD_BOUNDS, this, worldBlocked.up, worldBlocked.down, worldBlocked.left, worldBlocked.right);
@@ -1251,8 +1271,14 @@ var Body = new Class({
12511271
else
12521272
{
12531273
velocity.y *= -by;
1274+
12541275
console.log(this.gameObject.name, 'rebounded down', newVelocityY, gravityY, 'frame', this.world._frame);
12551276

1277+
if (this.forcePosition === 2)
1278+
{
1279+
this.forcePosition = 0;
1280+
}
1281+
12561282
if (this.onWorldBounds)
12571283
{
12581284
this.world.emit(Events.WORLD_BOUNDS, this, worldBlocked.up, worldBlocked.down, worldBlocked.left, worldBlocked.right);
@@ -1276,8 +1302,14 @@ var Body = new Class({
12761302
else
12771303
{
12781304
velocity.y *= -by;
1305+
12791306
console.log(this.gameObject.name, 'rebounded zero-g', newVelocityY, velocity.y);
12801307

1308+
if (this.forcePosition === 2)
1309+
{
1310+
this.forcePosition = 0;
1311+
}
1312+
12811313
if (this.onWorldBounds)
12821314
{
12831315
this.world.emit(Events.WORLD_BOUNDS, this, worldBlocked.up, worldBlocked.down, worldBlocked.left, worldBlocked.right);
@@ -1353,7 +1385,7 @@ var Body = new Class({
13531385

13541386
if (this.forcePosition !== 0)
13551387
{
1356-
console.log(this.world._frame, this.gameObject.name, 'forcePosition', this.y);
1388+
console.log(this.world._frame, this.gameObject.name, 'forcePosition', this.y, 'type', this.forcePosition);
13571389

13581390
gameObject.x = this.x;
13591391
gameObject.y = this.y;
@@ -1436,6 +1468,7 @@ var Body = new Class({
14361468

14371469
if (this._sleep >= this.sleepIterations)
14381470
{
1471+
console.log('slept by checkSleep');
14391472
this.sleep();
14401473
}
14411474
}
@@ -1468,41 +1501,21 @@ var Body = new Class({
14681501
* @method Phaser.Physics.Arcade.Body#checkWorldBounds
14691502
* @since 3.0.0
14701503
*
1471-
* @return {boolean} True if this Body is colliding with the world boundary.
1504+
* @return {boolean} True if this Body is touching over intersecting with the world boundary.
14721505
*/
14731506
checkWorldBounds: function ()
14741507
{
1475-
var pos = this.position;
1476-
var bounds = this.world.bounds;
1477-
var check = this.world.checkCollision;
1478-
1479-
var forceY = undefined;
1480-
1481-
if (check.up && pos.y <= (bounds.y + 1))
1482-
{
1483-
if (this.y !== bounds.y && this.velocity.y <= 0)
1484-
{
1485-
forceY = bounds.y;
1486-
}
1508+
var worldBounds = this.world.bounds;
1509+
var worldCollision = this.world.checkCollision;
14871510

1488-
this.setWorldBlockedUp(forceY);
1489-
}
1490-
else if (check.down && this.bottom >= (bounds.bottom - 1))
1511+
if (worldCollision.up && this.y <= (worldBounds.y + 1))
14911512
{
1492-
if (this.bottom !== bounds.bottom && this.velocity.y >= 0)
1493-
{
1494-
forceY = bounds.bottom;
1495-
}
1496-
1497-
this.setWorldBlockedDown(forceY);
1513+
this.setWorldBlockedUp(true);
14981514
}
1499-
1500-
if (forceY)
1515+
else if (worldCollision.down && this.bottom >= (worldBounds.bottom - 1))
15011516
{
1502-
this.updateCenter();
1517+
this.setWorldBlockedDown(true);
15031518
}
1504-
1505-
return (forceY !== undefined);
15061519
},
15071520

15081521
/**
@@ -2185,13 +2198,18 @@ var Body = new Class({
21852198

21862199
this.setBlocker(by);
21872200

2188-
if (forceY && this.forcePosition !== 2)
2201+
// We don't reposition this body if it's already blocked on a face
2202+
if (!forceY || this.forcePosition === 2 || this.worldBlocked.down || this.worldBlocked.up)
21892203
{
2190-
if (IntersectsRect(this, by))
2191-
{
2192-
this.y = by.bottom;
2193-
this.forcePosition = 1;
2194-
}
2204+
return this;
2205+
}
2206+
2207+
if (IntersectsRect(this, by))
2208+
{
2209+
console.log(this.gameObject.name, 'setBlockedUp', by.y, 'check', forceY, this.forcePosition, this.isBlocked());
2210+
2211+
this.y = by.bottom;
2212+
this.forcePosition = 1;
21952213
}
21962214

21972215
return this;
@@ -2215,13 +2233,18 @@ var Body = new Class({
22152233
// GetOverlapY = calls this, setting 'true' for forcing Y
22162234
// SeparateY = calls this, not setting anything, so 'true' for forcing Y
22172235

2218-
if (forceY && this.forcePosition !== 2)
2236+
// We don't reposition this body if it's already blocked on a face
2237+
if (!forceY || this.forcePosition === 2 || this.worldBlocked.down || this.worldBlocked.up)
22192238
{
2220-
if (IntersectsRect(this, by))
2221-
{
2222-
this.bottom = by.y;
2223-
this.forcePosition = 1;
2224-
}
2239+
return this;
2240+
}
2241+
2242+
if (IntersectsRect(this, by))
2243+
{
2244+
console.log(this.gameObject.name, 'setBlockedDown', by.y, 'check', forceY, this.forcePosition, this.worldBlocked.down);
2245+
2246+
this.bottom = by.y;
2247+
this.forcePosition = 1;
22252248
}
22262249

22272250
return this;
@@ -2253,14 +2276,23 @@ var Body = new Class({
22532276

22542277
setWorldBlockedUp: function (forceY)
22552278
{
2279+
var worldBounds = this.world.bounds;
22562280
var worldBlocked = this.worldBlocked;
2281+
var worldCollision = this.world.checkCollision;
2282+
2283+
if (!worldCollision.up)
2284+
{
2285+
return;
2286+
}
22572287

22582288
worldBlocked.up = true;
22592289
worldBlocked.none = false;
22602290

2261-
if (forceY !== undefined && !this.wasBlocked.up)
2291+
if (forceY && this.y !== worldBounds.y)
2292+
// if (forceY && this.y < worldBounds.y)
2293+
// if (forceY)
22622294
{
2263-
this.y = forceY;
2295+
this.y = worldBounds.y;
22642296
this.forcePosition = 2;
22652297
}
22662298

@@ -2269,15 +2301,24 @@ var Body = new Class({
22692301

22702302
setWorldBlockedDown: function (forceY)
22712303
{
2304+
var worldBounds = this.world.bounds;
22722305
var worldBlocked = this.worldBlocked;
2306+
var worldCollision = this.world.checkCollision;
2307+
2308+
if (!worldCollision.down)
2309+
{
2310+
return;
2311+
}
22732312

22742313
worldBlocked.down = true;
22752314
worldBlocked.none = false;
22762315

2277-
if (forceY !== undefined && !this.wasBlocked.down)
2316+
if (forceY && this.bottom !== worldBounds.bottom)
2317+
// if (forceY && this.bottom > worldBounds.bottom)
2318+
// if (forceY)
22782319
{
22792320
console.log(this.gameObject.name, 'world blocked down + position');
2280-
this.bottom = forceY;
2321+
this.bottom = worldBounds.bottom;
22812322
this.forcePosition = 2;
22822323
}
22832324

@@ -2390,21 +2431,20 @@ var Body = new Class({
23902431

23912432
if (this.collideWorldBounds)
23922433
{
2393-
var pos = this.position;
2394-
var bounds = this.world.bounds;
2395-
var check = this.world.checkCollision;
2434+
var worldBounds = this.world.bounds;
2435+
var worldCollision = this.world.checkCollision;
23962436

2397-
if (amount < 0 && check.up && pos.y + amount < bounds.y)
2437+
if (amount < 0 && worldCollision.up && this.y + amount < worldBounds.y)
23982438
{
2399-
this.setWorldBlockedUp();
2439+
this.setWorldBlockedUp(true);
24002440

2401-
return amount - ((pos.y + amount) - bounds.y);
2441+
return amount - ((this.y + amount) - worldBounds.y);
24022442
}
2403-
else if (amount > 0 && check.down && this.bottom + amount > bounds.bottom)
2443+
else if (amount > 0 && worldCollision.down && this.bottom + amount > worldBounds.bottom)
24042444
{
2405-
this.setWorldBlockedDown();
2445+
this.setWorldBlockedDown(true);
24062446

2407-
return amount - ((this.bottom + amount) - bounds.bottom);
2447+
return amount - ((this.bottom + amount) - worldBounds.bottom);
24082448
}
24092449
}
24102450

src/physics/arcade/GetOverlapY.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ var GetOverlapY = function (body1, body2, overlapOnly, bias)
5959

6060
if (!overlapOnly)
6161
{
62+
console.log('GetOverlapY !topFace');
63+
6264
body1.setTouchingDown();
6365
body2.setTouchingUp();
6466

@@ -82,6 +84,8 @@ var GetOverlapY = function (body1, body2, overlapOnly, bias)
8284

8385
if (!overlapOnly)
8486
{
87+
console.log('GetOverlapY topFace');
88+
8589
body1.setTouchingUp();
8690
body2.setTouchingDown();
8791

0 commit comments

Comments
 (0)