Skip to content

Commit 932f70c

Browse files
committed
Removed logging and handled bounds collision
1 parent 89e33ae commit 932f70c

1 file changed

Lines changed: 91 additions & 44 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 91 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ var Body = new Class({
8989
*/
9090
this.debugShowVelocity = world.defaults.debugShowVelocity;
9191

92+
/**
93+
* Whether the Body's blocked faces are drawn to the debug display.
94+
*
95+
* @name Phaser.Physics.Arcade.Body#debugShowVelocity
96+
* @type {boolean}
97+
* @since 3.17.0
98+
*/
99+
this.debugShowBlocked = true;
100+
92101
/**
93102
* The color of this Body on the debug display.
94103
*
@@ -672,7 +681,7 @@ var Body = new Class({
672681
* @type {Phaser.Physics.Arcade.Types.ArcadeBodyCollision}
673682
* @since 3.0.0
674683
*/
675-
this.blocked = { none: true, up: false, down: false, left: false, right: false, x: 0, y: 0 };
684+
this.blocked = { none: true, up: false, down: false, left: false, right: false };
676685

677686
/**
678687
* Whether this Body is colliding with a tile or the world boundary.
@@ -681,7 +690,7 @@ var Body = new Class({
681690
* @type {Phaser.Physics.Arcade.Types.ArcadeBodyCollision}
682691
* @since 3.17.0
683692
*/
684-
this.worldBlocked = { up: false, down: false, left: false, right: false };
693+
this.worldBlocked = { none: true, up: false, down: false, left: false, right: false };
685694

686695
/**
687696
* Whether to automatically synchronize this Body's dimensions to the dimensions of its Game Object's visual bounds.
@@ -891,6 +900,7 @@ var Body = new Class({
891900
touching.left = false;
892901
touching.right = false;
893902

903+
worldBlocked.none = true;
894904
worldBlocked.left = false;
895905
worldBlocked.right = false;
896906
worldBlocked.up = false;
@@ -949,59 +959,53 @@ var Body = new Class({
949959
*/
950960
update: function (delta)
951961
{
952-
console.log(this.gameObject.name, 'upd');
953-
954962
if (this.moves)
955963
{
956964
this.world.updateMotion(this, delta);
957965

958966
var velocity = this.velocity;
959967

960-
var nx = velocity.x * delta;
961-
var ny = velocity.y * delta;
962-
963-
if (nx !== 0)
964-
{
965-
this.position.x += this.getMoveX(nx);
966-
}
967-
968-
if (ny !== 0)
969-
{
970-
this.position.y += this.getMoveY(ny);
971-
}
972-
973-
this.updateCenter();
974-
975-
this.angle = Math.atan2(velocity.y, velocity.x);
976-
this.speed = Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
968+
this.position.x += this.getMoveX(velocity.x * delta);
969+
this.position.y += this.getMoveY(velocity.y * delta);
977970
}
978971

979-
// Calculate the delta
980-
this._dx = this.position.x - this.prev.x;
981-
this._dy = this.position.y - this.prev.y;
972+
var worldBlocked = this.worldBlocked;
982973

983974
// World Bounds check
984-
if (this.collideWorldBounds)
975+
if (this.collideWorldBounds && !worldBlocked.none)
985976
{
986-
var bx = (this.worldBounce) ? -this.worldBounce.x : -this.bounce.x;
987-
var by = (this.worldBounce) ? -this.worldBounce.y : -this.bounce.y;
988-
var worldBlocked = this.worldBlocked;
977+
var worldBounds = this.world.bounds;
978+
979+
var bx = (this.worldBounce) ? this.worldBounce.x : this.bounce.x;
980+
var by = (this.worldBounce) ? this.worldBounce.y : this.bounce.y;
989981

990-
if ((worldBlocked.left || worldBlocked.right))
982+
if (bx !== 0)
991983
{
992-
if (bx !== 0)
984+
// Reverse the velocity for the bounce and flip the delta
985+
velocity.x *= -bx;
986+
987+
if (worldBlocked.left)
988+
{
989+
this.x = worldBounds.x + (1 * bx);
990+
}
991+
else if (worldBlocked.right)
993992
{
994-
// Reverse the velocity for the bounce and flip the delta
995-
velocity.x *= bx;
993+
this.right = worldBounds.right - (1 * bx);
996994
}
997995
}
998996

999-
if ((worldBlocked.up || worldBlocked.down))
997+
if (by !== 0)
1000998
{
1001-
if (by !== 0)
999+
// Reverse the velocity for the bounce and flip the delta
1000+
velocity.y *= -by;
1001+
1002+
if (worldBlocked.up)
1003+
{
1004+
this.y = worldBounds.y + (1 * by);
1005+
}
1006+
else if (worldBlocked.down)
10021007
{
1003-
// Reverse the velocity for the bounce and flip the delta
1004-
velocity.y *= by;
1008+
this.bottom = worldBounds.bottom - (1 * by);
10051009
}
10061010
}
10071011

@@ -1011,6 +1015,15 @@ var Body = new Class({
10111015
}
10121016
}
10131017

1018+
// Calculate the delta
1019+
this._dx = this.position.x - this.prev.x;
1020+
this._dy = this.position.y - this.prev.y;
1021+
1022+
this.updateCenter();
1023+
1024+
this.angle = Math.atan2(velocity.y, velocity.x);
1025+
this.speed = Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
1026+
10141027
// Now the update will throw collision checks at the Body
10151028
// And finally we'll integrate the new position back to the Sprite in postUpdate
10161029
},
@@ -1025,16 +1038,16 @@ var Body = new Class({
10251038
*/
10261039
postUpdate: function ()
10271040
{
1028-
// var dx = this.position.x - this.prev.x;
1029-
// var dy = this.position.y - this.prev.y;
1041+
var dx = this.position.x - this.prev.x;
1042+
var dy = this.position.y - this.prev.y;
10301043

10311044
var gameObject = this.gameObject;
10321045

1033-
var px = gameObject.x + gameObject.scaleX * (this.offset.x - gameObject.displayOriginX);
1034-
var py = gameObject.y + gameObject.scaleY * (this.offset.y - gameObject.displayOriginY);
1046+
// var px = gameObject.x + gameObject.scaleX * (this.offset.x - gameObject.displayOriginX);
1047+
// var py = gameObject.y + gameObject.scaleY * (this.offset.y - gameObject.displayOriginY);
10351048

1036-
var dx = this.position.x - px;
1037-
var dy = this.position.y - py;
1049+
// var dx = this.position.x - px;
1050+
// var dy = this.position.y - py;
10381051

10391052
if (this.moves)
10401053
{
@@ -1144,6 +1157,7 @@ var Body = new Class({
11441157

11451158
if (set)
11461159
{
1160+
worldBlocked.none = false;
11471161
this.updateCenter();
11481162
}
11491163

@@ -1524,6 +1538,39 @@ var Body = new Class({
15241538
}
15251539
}
15261540

1541+
if (this.debugShowBlocked)
1542+
{
1543+
var thickness = graphic.defaultStrokeWidth * 4;
1544+
1545+
// Top Left
1546+
var x1 = pos.x;
1547+
var y1 = pos.y;
1548+
1549+
// Top Right
1550+
var x2 = this.right;
1551+
var y2 = y1;
1552+
1553+
// Bottom Left
1554+
var x3 = x1;
1555+
var y3 = this.bottom - thickness;
1556+
1557+
// Bottom Right
1558+
var x4 = x2;
1559+
var y4 = y3;
1560+
1561+
var blocked = this.blocked;
1562+
1563+
if (blocked.up)
1564+
{
1565+
graphic.lineStyle(thickness, 0xff0000).lineBetween(x1, y1, x2, y2);
1566+
}
1567+
1568+
if (blocked.down)
1569+
{
1570+
graphic.lineStyle(thickness, 0xff0000).lineBetween(x3, y3, x4, y4);
1571+
}
1572+
}
1573+
15271574
if (this.debugShowVelocity)
15281575
{
15291576
graphic.lineStyle(graphic.defaultStrokeWidth, this.world.defaults.velocityDebugColor, 1);
@@ -1830,9 +1877,9 @@ var Body = new Class({
18301877
{
18311878
var blocked = this.blocked;
18321879

1833-
if (amount < 0 && blocked.up || amount > 0 && blocked.down)
1880+
if (amount === 0 || amount < 0 && blocked.up || amount > 0 && blocked.down)
18341881
{
1835-
// If it's already blocked, it can't go anywhere
1882+
// If it's already blocked, or zero, it can't go anywhere
18361883
return 0;
18371884
}
18381885

0 commit comments

Comments
 (0)