Skip to content

Commit bdde075

Browse files
committed
Implemented sleeping and much better debug drawing
1 parent ae93acc commit bdde075

1 file changed

Lines changed: 101 additions & 73 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 101 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var CircleContains = require('../../geom/circle/Contains');
88
var Class = require('../../utils/Class');
99
var CONST = require('./const');
1010
var Events = require('./events');
11-
var FuzzyEqual = require('../../math/fuzzy/Equal');
11+
var FuzzyLessThan = require('../../math/fuzzy/LessThan');
12+
var FuzzyGreaterThan = require('../../math/fuzzy/GreaterThan');
1213
var RadToDeg = require('../../math/RadToDeg');
1314
var Rectangle = require('../../geom/rectangle/Rectangle');
1415
var RectangleContains = require('../../geom/rectangle/Contains');
@@ -97,7 +98,7 @@ var Body = new Class({
9798
* @type {boolean}
9899
* @since 3.17.0
99100
*/
100-
this.debugShowBlocked = true;
101+
this.debugShowBlocked = world.defaults.debugShowBlocked;
101102

102103
/**
103104
* The color of this Body on the debug display.
@@ -287,14 +288,18 @@ var Body = new Class({
287288
this.velocity = new Vector2();
288289

289290
/**
290-
* The Body's previously calculated velocity, in pixels per second, in the previous frame.
291+
* Is the Body asleep?.
291292
*
292-
* @name Phaser.Physics.Arcade.Body#prevVelocity
293-
* @type {Phaser.Math.Vector2}
293+
* @name Phaser.Physics.Arcade.Body#sleeping
294+
* @type {boolean}
294295
* @readonly
295296
* @since 3.17.0
296297
*/
297-
this.prevVelocity = new Vector2();
298+
this.sleeping = false;
299+
300+
this._sleep = 0;
301+
302+
this.forcePosition = false;
298303

299304
/**
300305
* The Body's absolute maximum change in position, in pixels per step.
@@ -943,6 +948,7 @@ var Body = new Class({
943948
this.overlapY = 0;
944949

945950
this.embedded = false;
951+
this.forcePosition = false;
946952

947953
// Updates the transform values
948954
this.updateBounds();
@@ -1007,7 +1013,7 @@ var Body = new Class({
10071013
var worldBlocked = this.worldBlocked;
10081014

10091015
// World Bounds check
1010-
if (this.collideWorldBounds)
1016+
if (this.collideWorldBounds && !this.sleeping)
10111017
{
10121018
if (!worldBlocked.none)
10131019
{
@@ -1033,16 +1039,6 @@ var Body = new Class({
10331039
}
10341040
}
10351041

1036-
// Is the velocity flip flopping?
1037-
// if ( this._flipflopY >= this.relaxCount)
1038-
// {
1039-
// console.log('flipflop reset');
1040-
// velocity.y = 0;
1041-
// this._dy = 0;
1042-
// this._flipflopY = 0;
1043-
// this.prev.y = this.y;
1044-
// }
1045-
10461042
this.updateCenter();
10471043

10481044
this.angle = Math.atan2(velocity.y, velocity.x);
@@ -1067,7 +1063,7 @@ var Body = new Class({
10671063

10681064
var gameObject = this.gameObject;
10691065

1070-
if (this.moves)
1066+
if (this.moves && !this.sleeping)
10711067
{
10721068
var mx = this.deltaMax.x;
10731069
var my = this.deltaMax.y;
@@ -1096,8 +1092,16 @@ var Body = new Class({
10961092
}
10971093
}
10981094

1099-
gameObject.x += dx;
1100-
gameObject.y += dy;
1095+
if (this.forcePosition)
1096+
{
1097+
gameObject.x = this.x;
1098+
gameObject.y = this.y;
1099+
}
1100+
else
1101+
{
1102+
gameObject.x += dx;
1103+
gameObject.y += dy;
1104+
}
11011105
}
11021106

11031107
if (dx < 0)
@@ -1129,32 +1133,38 @@ var Body = new Class({
11291133
// Store collision flags
11301134
var wasTouching = this.wasTouching;
11311135
var touching = this.touching;
1132-
var prev = this.prev;
11331136

11341137
wasTouching.none = touching.none;
11351138
wasTouching.up = touching.up;
11361139
wasTouching.down = touching.down;
11371140
wasTouching.left = touching.left;
11381141
wasTouching.right = touching.right;
11391142

1140-
// var vx = this.velocity.x;
1141-
// var vy = this.velocity.y;
1142-
1143-
// if (this.blocked.down && window.track && window.track === this)
1144-
// {
1145-
// var diff = this.position.y - this.prev.y;
1143+
if (Math.abs(dy) < 1)
1144+
{
1145+
if (this._sleep < 60)
1146+
{
1147+
this._sleep++;
11461148

1147-
// console.log(diff);
1148-
// }
1149+
if (this._sleep >= 60)
1150+
{
1151+
this.sleeping = true;
1152+
}
1153+
}
1154+
}
1155+
else
1156+
{
1157+
if (this._sleep > 0)
1158+
{
1159+
this._sleep *= 0.5;
11491160

1150-
// if (this.velocity.y !== 0 && this.blocked.down && Math.abs(this.y - prev.y) < 0.3)
1151-
// {
1152-
// this._flipflopY++;
1153-
// }
1154-
// else
1155-
// {
1156-
// this._flipflopY = 0;
1157-
// }
1161+
if (this._sleep <= 0)
1162+
{
1163+
this.sleeping = false;
1164+
this._sleep = 0;
1165+
}
1166+
}
1167+
}
11581168

11591169
this.prev.x = this.position.x;
11601170
this.prev.y = this.position.y;
@@ -1190,22 +1200,23 @@ var Body = new Class({
11901200
worldBlocked.right = true;
11911201
}
11921202

1193-
if (check.up && pos.y < bounds.y)
1203+
if (check.up && pos.y <= bounds.y + 1)
11941204
{
11951205
set = true;
1196-
pos.y = bounds.y;
11971206
worldBlocked.up = true;
1207+
pos.y = bounds.y;
11981208
}
1199-
else if (check.down && this.bottom > bounds.bottom)
1209+
else if (check.down && this.bottom >= bounds.bottom - 1)
12001210
{
12011211
set = true;
1202-
pos.y = bounds.bottom - this.height;
12031212
worldBlocked.down = true;
1213+
pos.y = bounds.bottom - this.height;
12041214
}
12051215

12061216
if (set)
12071217
{
12081218
worldBlocked.none = false;
1219+
this.forcePosition = true;
12091220
this.updateCenter();
12101221
}
12111222

@@ -1572,63 +1583,80 @@ var Body = new Class({
15721583
var x = pos.x + this.halfWidth;
15731584
var y = pos.y + this.halfHeight;
15741585

1575-
if (this.debugShowBody)
1576-
{
1577-
graphic.lineStyle(graphic.defaultStrokeWidth, this.debugBodyColor);
1586+
var blockedColor = this.world.defaults.blockedDebugColor;
1587+
var sleepColor = this.world.defaults.sleepDebugColor;
15781588

1579-
if (this.isCircle)
1580-
{
1581-
graphic.strokeCircle(x, y, this.width / 2);
1582-
}
1583-
else
1584-
{
1585-
graphic.strokeRect(pos.x, pos.y, this.width, this.height);
1586-
}
1587-
}
1589+
var thickness = graphic.defaultStrokeWidth;
1590+
var halfThickness = thickness / 2;
15881591

1589-
if (this.debugShowBlocked)
1590-
{
1591-
var thickness = graphic.defaultStrokeWidth * 4;
1592+
// Top Left
1593+
var x1 = pos.x;
1594+
var y1 = pos.y;
1595+
1596+
// Top Right
1597+
var x2 = this.right;
1598+
var y2 = y1;
15921599

1593-
// Top Left
1594-
var x1 = pos.x;
1595-
var y1 = pos.y;
1600+
// Bottom Left
1601+
var x3 = x1;
1602+
var y3 = this.bottom;
15961603

1597-
// Top Right
1598-
var x2 = this.right;
1599-
var y2 = y1;
1604+
// Bottom Right
1605+
var x4 = x2;
1606+
var y4 = y3;
16001607

1601-
// Bottom Left
1602-
var x3 = x1;
1603-
var y3 = this.bottom;
1608+
var blocked = this.blocked;
16041609

1605-
// Bottom Right
1606-
var x4 = x2;
1607-
var y4 = y3;
1610+
var color;
16081611

1609-
var blocked = this.blocked;
1612+
if (this.debugShowBody)
1613+
{
1614+
// Top
1615+
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
16101616

16111617
if (blocked.up)
16121618
{
1613-
graphic.lineStyle(thickness, 0xff0000).lineBetween(x1, y1, x2, y2);
1619+
color = blockedColor;
16141620
}
16151621

1622+
graphic.lineStyle(thickness, color).lineBetween(x1, y1 + halfThickness, x2, y2 + halfThickness);
1623+
1624+
// Bottom
1625+
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
1626+
16161627
if (blocked.down)
16171628
{
1618-
graphic.lineStyle(thickness, 0xff0000).lineBetween(x3, y3, x4, y4);
1629+
color = blockedColor;
16191630
}
16201631

1632+
graphic.lineStyle(thickness, color).lineBetween(x3, y3 - halfThickness, x4, y4 - halfThickness);
1633+
1634+
// Left
1635+
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
1636+
16211637
if (blocked.left)
16221638
{
1623-
graphic.lineStyle(thickness, 0xff0000).lineBetween(x1, y1, x3, y3);
1639+
color = blockedColor;
16241640
}
16251641

1642+
graphic.lineStyle(thickness, color).lineBetween(x1 + halfThickness, y1, x3 + halfThickness, y3);
1643+
1644+
// Right
1645+
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
1646+
16261647
if (blocked.right)
16271648
{
1628-
graphic.lineStyle(thickness, 0xff0000).lineBetween(x2, y2, x4, y4);
1649+
color = blockedColor;
16291650
}
1651+
1652+
graphic.lineStyle(thickness, color).lineBetween(x2 - halfThickness, y2, x4 - halfThickness, y4);
16301653
}
16311654

1655+
// if (this.isCircle)
1656+
// {
1657+
// graphic.strokeCircle(x, y, this.width / 2);
1658+
// }
1659+
16321660
if (this.debugShowVelocity)
16331661
{
16341662
graphic.lineStyle(graphic.defaultStrokeWidth, this.world.defaults.velocityDebugColor, 1);

0 commit comments

Comments
 (0)