Skip to content

Commit e1f9e31

Browse files
committed
Added setTouching and setBlocked methods + refactored the way world bounds collision is done
1 parent ab1d39b commit e1f9e31

1 file changed

Lines changed: 223 additions & 31 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 223 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ var Body = new Class({
383383
* @type {boolean}
384384
* @default false
385385
* @since 3.0.0
386-
* @see Phaser.Physics.Arcade.World#worldboundsEvent
386+
* @see Phaser.Physics.Arcade.Events#WORLD_BOUNDS
387387
*/
388388
this.onWorldBounds = false;
389389

@@ -394,7 +394,7 @@ var Body = new Class({
394394
* @type {boolean}
395395
* @default false
396396
* @since 3.0.0
397-
* @see Phaser.Physics.Arcade.World#collideEvent
397+
* @see Phaser.Physics.Arcade.Events#COLLIDE
398398
*/
399399
this.onCollide = false;
400400

@@ -405,7 +405,7 @@ var Body = new Class({
405405
* @type {boolean}
406406
* @default false
407407
* @since 3.0.0
408-
* @see Phaser.Physics.Arcade.World#overlapEvent
408+
* @see Phaser.Physics.Arcade.Events#OVERLAP
409409
*/
410410
this.onOverlap = false;
411411

@@ -930,7 +930,7 @@ var Body = new Class({
930930
* The results are synced back to the Game Object in `postUpdate`.
931931
*
932932
* @method Phaser.Physics.Arcade.Body#update
933-
* @fires Phaser.Physics.Arcade.World#worldbounds
933+
* @fires Phaser.Physics.Arcade.Events#WORLD_BOUNDS
934934
* @since 3.0.0
935935
*
936936
* @param {number} delta - The delta time, in seconds, elapsed since the last frame.
@@ -952,18 +952,47 @@ var Body = new Class({
952952

953953
this.angle = Math.atan2(vy, vx);
954954
this.speed = Math.sqrt(vx * vx + vy * vy);
955+
}
956+
957+
// Calculate the delta
958+
this._dx = this.position.x - this.prev.x;
959+
this._dy = this.position.y - this.prev.y;
955960

956-
// Now the update will throw collision checks at the Body
957-
// And finally we'll integrate the new position back to the Sprite in postUpdate
961+
// World Bounds check
962+
if (this.collideWorldBounds && this.checkWorldBounds())
963+
{
964+
var bx = (this.worldBounce) ? -this.worldBounce.x : -this.bounce.x;
965+
var by = (this.worldBounce) ? -this.worldBounce.y : -this.bounce.y;
966+
var blocked = this.blocked;
967+
968+
if (blocked.left || blocked.right)
969+
{
970+
if (bx !== 0)
971+
{
972+
// Reverse the velocity for the bounce and flip the delta
973+
this.velocity.x *= bx;
974+
this._dx *= -1;
975+
}
976+
}
977+
978+
if (blocked.up || blocked.down)
979+
{
980+
if (by !== 0)
981+
{
982+
// Reverse the velocity for the bounce and flip the delta
983+
this.velocity.y *= by;
984+
this._dy *= -1;
985+
}
986+
}
958987

959-
if (this.collideWorldBounds && this.checkWorldBounds() && this.onWorldBounds)
988+
if (this.onWorldBounds)
960989
{
961990
this.world.emit(Events.WORLD_BOUNDS, this, this.blocked.up, this.blocked.down, this.blocked.left, this.blocked.right);
962991
}
963992
}
964993

965-
this._dx = this.position.x - this.prev.x;
966-
this._dy = this.position.y - this.prev.y;
994+
// Now the update will throw collision checks at the Body
995+
// And finally we'll integrate the new position back to the Sprite in postUpdate
967996
},
968997

969998
/**
@@ -1057,41 +1086,40 @@ var Body = new Class({
10571086
var pos = this.position;
10581087
var bounds = this.world.bounds;
10591088
var check = this.world.checkCollision;
1089+
var set = false;
10601090

1061-
var bx = (this.worldBounce) ? -this.worldBounce.x : -this.bounce.x;
1062-
var by = (this.worldBounce) ? -this.worldBounce.y : -this.bounce.y;
1063-
1064-
if (pos.x < bounds.x && check.left)
1091+
if (pos.x <= bounds.x && check.left)
10651092
{
1093+
set = true;
10661094
pos.x = bounds.x;
1067-
this.velocity.x *= bx;
1068-
this.blocked.left = true;
1069-
this.blocked.none = false;
1095+
this.setBlockedLeft();
10701096
}
1071-
else if (this.right > bounds.right && check.right)
1097+
else if (this.right >= bounds.right && check.right)
10721098
{
1099+
set = true;
10731100
pos.x = bounds.right - this.width;
1074-
this.velocity.x *= bx;
1075-
this.blocked.right = true;
1076-
this.blocked.none = false;
1101+
this.setBlockedRight();
10771102
}
10781103

1079-
if (pos.y < bounds.y && check.up)
1104+
if (pos.y <= bounds.y && check.up)
10801105
{
1106+
set = true;
10811107
pos.y = bounds.y;
1082-
this.velocity.y *= by;
1083-
this.blocked.up = true;
1084-
this.blocked.none = false;
1108+
this.setBlockedUp();
10851109
}
1086-
else if (this.bottom > bounds.bottom && check.down)
1110+
else if (this.bottom >= bounds.bottom && check.down)
10871111
{
1112+
set = true;
10881113
pos.y = bounds.bottom - this.height;
1089-
this.velocity.y *= by;
1090-
this.blocked.down = true;
1091-
this.blocked.none = false;
1114+
this.setBlockedDown();
1115+
}
1116+
1117+
if (set)
1118+
{
1119+
this.updateCenter();
10921120
}
10931121

1094-
return !this.blocked.none;
1122+
return set;
10951123
},
10961124

10971125
/**
@@ -1241,6 +1269,24 @@ var Body = new Class({
12411269
this.updateCenter();
12421270
},
12431271

1272+
zeroX: function ()
1273+
{
1274+
this.velocity.x = 0;
1275+
this.prev.x = this.position.x;
1276+
this._dx = 0;
1277+
1278+
return this;
1279+
},
1280+
1281+
zeroY: function ()
1282+
{
1283+
this.velocity.y = 0;
1284+
this.prev.y = this.position.y;
1285+
this._dy = 0;
1286+
1287+
return this;
1288+
},
1289+
12441290
/**
12451291
* Sets acceleration, velocity, and speed to zero.
12461292
*
@@ -1348,7 +1394,7 @@ var Body = new Class({
13481394
*/
13491395
deltaAbsX: function ()
13501396
{
1351-
return (this._dx > 0) ? this._dx : -this._dx;
1397+
return Math.abs(this._dx);
13521398
},
13531399

13541400
/**
@@ -1361,7 +1407,7 @@ var Body = new Class({
13611407
*/
13621408
deltaAbsY: function ()
13631409
{
1364-
return (this._dy > 0) ? this._dy : -this._dy;
1410+
return Math.abs(this._dy);
13651411
},
13661412

13671413
/**
@@ -1640,6 +1686,152 @@ var Body = new Class({
16401686
return this;
16411687
},
16421688

1689+
setTouchingUp: function ()
1690+
{
1691+
var touching = this.touching;
1692+
1693+
touching.up = true;
1694+
touching.none = false;
1695+
1696+
return this;
1697+
},
1698+
1699+
setTouchingDown: function ()
1700+
{
1701+
var touching = this.touching;
1702+
1703+
touching.down = true;
1704+
touching.none = false;
1705+
1706+
return this;
1707+
},
1708+
1709+
setTouchingLeft: function ()
1710+
{
1711+
var touching = this.touching;
1712+
1713+
touching.left = true;
1714+
touching.none = false;
1715+
1716+
return this;
1717+
},
1718+
1719+
setTouchingRight: function ()
1720+
{
1721+
var touching = this.touching;
1722+
1723+
touching.right = true;
1724+
touching.none = false;
1725+
1726+
return this;
1727+
},
1728+
1729+
setBlockedUp: function ()
1730+
{
1731+
var blocked = this.blocked;
1732+
1733+
blocked.up = true;
1734+
blocked.none = false;
1735+
1736+
return this;
1737+
},
1738+
1739+
setBlockedDown: function ()
1740+
{
1741+
var blocked = this.blocked;
1742+
1743+
blocked.down = true;
1744+
blocked.none = false;
1745+
1746+
return this;
1747+
},
1748+
1749+
setBlockedLeft: function ()
1750+
{
1751+
var blocked = this.blocked;
1752+
1753+
blocked.left = true;
1754+
blocked.none = false;
1755+
1756+
return this;
1757+
},
1758+
1759+
setBlockedRight: function ()
1760+
{
1761+
var blocked = this.blocked;
1762+
1763+
blocked.right = true;
1764+
blocked.none = false;
1765+
1766+
return this;
1767+
},
1768+
1769+
getMoveX: function (amount, setBlocked)
1770+
{
1771+
if (setBlocked === undefined) { setBlocked = false; }
1772+
1773+
var blocked = this.blocked;
1774+
1775+
if (amount < 0 && blocked.left || amount > 0 && blocked.right)
1776+
{
1777+
// If it's already blocked, it can't go anywhere
1778+
return 0;
1779+
}
1780+
1781+
if (this.checkWorldBounds)
1782+
{
1783+
var pos = this.position;
1784+
var bounds = this.world.bounds;
1785+
var check = this.world.checkCollision;
1786+
1787+
if (amount < 0 && check.left && pos.x + amount < bounds.x)
1788+
{
1789+
this.setBlockedLeft();
1790+
return amount - ((pos.x + amount) - bounds.x);
1791+
}
1792+
else if (amount > 0 && check.right && this.right + amount > bounds.right)
1793+
{
1794+
this.setBlockedRight();
1795+
return amount - ((this.right + amount) - bounds.right);
1796+
}
1797+
}
1798+
1799+
return amount;
1800+
},
1801+
1802+
getMoveY: function (amount, setBlocked)
1803+
{
1804+
if (setBlocked === undefined) { setBlocked = false; }
1805+
1806+
var blocked = this.blocked;
1807+
1808+
if (amount < 0 && blocked.up || amount > 0 && blocked.down)
1809+
{
1810+
// If it's already blocked, it can't go anywhere
1811+
return 0;
1812+
}
1813+
1814+
if (this.checkWorldBounds)
1815+
{
1816+
var pos = this.position;
1817+
var bounds = this.world.bounds;
1818+
var check = this.world.checkCollision;
1819+
1820+
if (amount < 0 && check.up && pos.y + amount < bounds.y)
1821+
{
1822+
this.setBlockedUp();
1823+
return amount - ((pos.y + amount) - bounds.y);
1824+
}
1825+
else if (amount > 0 && check.down && this.bottom + amount > bounds.bottom)
1826+
{
1827+
this.setBlockedDown();
1828+
return amount - ((this.bottom + amount) - bounds.bottom);
1829+
}
1830+
}
1831+
1832+
return amount;
1833+
},
1834+
16431835
/**
16441836
* Sets the Body's acceleration.
16451837
*

0 commit comments

Comments
 (0)