Skip to content

Commit c819599

Browse files
committed
Use of a Game Object bound to a body is now optional
1 parent 811ed0e commit c819599

3 files changed

Lines changed: 200 additions & 129 deletions

File tree

src/physics/arcade/BaseBody.js

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,21 @@ var BaseBody = new Class({
2929

3030
initialize:
3131

32-
function StaticBody (world, gameObject, bodyType)
32+
function BaseBody (world, gameObject, bodyType, x, y, width, height)
3333
{
34-
var width = (gameObject.width) ? gameObject.width : 64;
35-
var height = (gameObject.height) ? gameObject.height : 64;
34+
if (width === undefined) { width = 64; }
35+
if (height === undefined) { height = 64; }
36+
if (x === undefined) { x = 0; }
37+
if (y === undefined) { y = 0; }
38+
39+
if (gameObject)
40+
{
41+
width = gameObject.width;
42+
height = gameObject.height;
43+
44+
x = gameObject.x - gameObject.displayOriginX;
45+
y = gameObject.y - gameObject.displayOriginY;
46+
}
3647

3748
/**
3849
* The Arcade Physics simulation this Body belongs to.
@@ -45,9 +56,12 @@ var BaseBody = new Class({
4556

4657
/**
4758
* The Game Object this Body belongs to.
59+
*
60+
* As of Phaser 3.17 this can be null in order to create a Body that isn't bound to a parent,
61+
* but that still collides and overlaps within the physics world.
4862
*
4963
* @name Phaser.Physics.Arcade.Body#gameObject
50-
* @type {Phaser.GameObjects.GameObject}
64+
* @type {?Phaser.GameObjects.GameObject}
5165
* @since 3.0.0
5266
*/
5367
this.gameObject = gameObject;
@@ -62,14 +76,16 @@ var BaseBody = new Class({
6276
*/
6377
this.enable = true;
6478

79+
var defaults = world.defaults;
80+
6581
/**
6682
* Whether the Body's boundary is drawn to the debug display.
6783
*
6884
* @name Phaser.Physics.Arcade.Body#debugShowBody
6985
* @type {boolean}
7086
* @since 3.0.0
7187
*/
72-
this.debugShowBody = world.defaults.debugShowBody;
88+
this.debugShowBody = defaults.debugShowBody;
7389

7490
/**
7591
* Whether the Body's velocity is drawn to the debug display.
@@ -78,7 +94,7 @@ var BaseBody = new Class({
7894
* @type {boolean}
7995
* @since 3.0.0
8096
*/
81-
this.debugShowVelocity = world.defaults.debugShowVelocity;
97+
this.debugShowVelocity = defaults.debugShowVelocity;
8298

8399
/**
84100
* Whether the Body's blocked faces are drawn to the debug display.
@@ -87,7 +103,7 @@ var BaseBody = new Class({
87103
* @type {boolean}
88104
* @since 3.17.0
89105
*/
90-
this.debugShowBlocked = world.defaults.debugShowBlocked;
106+
this.debugShowBlocked = defaults.debugShowBlocked;
91107

92108
/**
93109
* The color of this Body on the debug display.
@@ -96,7 +112,7 @@ var BaseBody = new Class({
96112
* @type {integer}
97113
* @since 3.0.0
98114
*/
99-
this.debugBodyColor = world.defaults.bodyDebugColor;
115+
this.debugBodyColor = defaults.bodyDebugColor;
100116

101117
/**
102118
* Whether this Body's boundary is circular (`true`) or rectangular (`false`).
@@ -141,7 +157,7 @@ var BaseBody = new Class({
141157
* @type {Phaser.Math.Vector2}
142158
* @since 3.0.0
143159
*/
144-
this.position = new Vector2(gameObject.x - gameObject.displayOriginX, gameObject.y - gameObject.displayOriginY);
160+
this.position = new Vector2(x, y);
145161

146162
/**
147163
* The position of this Body during the previous step.
@@ -150,7 +166,7 @@ var BaseBody = new Class({
150166
* @type {Phaser.Math.Vector2}
151167
* @since 3.17.0
152168
*/
153-
this.prev = new Vector2(gameObject.x - gameObject.displayOriginX, gameObject.y - gameObject.displayOriginY);
169+
this.prev = new Vector2(x, y);
154170

155171
/**
156172
* The width of the Static Body's boundary, in pixels.
@@ -200,7 +216,7 @@ var BaseBody = new Class({
200216
* @type {Phaser.Math.Vector2}
201217
* @since 3.0.0
202218
*/
203-
this.center = new Vector2(gameObject.x + this.halfWidth, gameObject.y + this.halfHeight);
219+
this.center = new Vector2(x + this.halfWidth, y + this.halfHeight);
204220

205221
/**
206222
* Whether this Body's position is affected by gravity (local or world).
@@ -455,9 +471,12 @@ var BaseBody = new Class({
455471
var x = pos.x + this.halfWidth;
456472
var y = pos.y + this.halfHeight;
457473

458-
var worldBlockedColor = this.world.defaults.worldBlockedDebugColor;
459-
var blockedColor = this.world.defaults.blockedDebugColor;
460-
var sleepColor = this.world.defaults.sleepDebugColor;
474+
var defaults = this.world.defaults;
475+
476+
var worldBlockedColor = defaults.worldBlockedDebugColor;
477+
var blockedColor = defaults.blockedDebugColor;
478+
var sleepColor = defaults.sleepDebugColor;
479+
var checkCollision = this.checkCollision;
461480

462481
// var thickness = graphic.defaultStrokeWidth;
463482

@@ -489,44 +508,56 @@ var BaseBody = new Class({
489508
if (this.debugShowBody)
490509
{
491510
// Top
492-
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
493-
494-
if (blocked.up || worldBlocked.up || hardBlocked.up)
511+
if (checkCollision.up)
495512
{
496-
color = (worldBlocked.up || hardBlocked.up) ? worldBlockedColor : blockedColor;
497-
}
513+
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
498514

499-
graphic.lineStyle(thickness, color).lineBetween(x1, y1 + halfThickness, x2, y2 + halfThickness);
515+
if (blocked.up || worldBlocked.up || hardBlocked.up)
516+
{
517+
color = (worldBlocked.up || hardBlocked.up) ? worldBlockedColor : blockedColor;
518+
}
519+
520+
graphic.lineStyle(thickness, color).lineBetween(x1, y1 + halfThickness, x2, y2 + halfThickness);
521+
}
500522

501523
// Bottom
502-
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
503-
504-
if (blocked.down || worldBlocked.down || hardBlocked.down)
524+
if (checkCollision.down)
505525
{
506-
color = (worldBlocked.down || hardBlocked.down) ? worldBlockedColor : blockedColor;
507-
}
526+
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
508527

509-
graphic.lineStyle(thickness, color).lineBetween(x3, y3 - halfThickness, x4, y4 - halfThickness);
528+
if (blocked.down || worldBlocked.down || hardBlocked.down)
529+
{
530+
color = (worldBlocked.down || hardBlocked.down) ? worldBlockedColor : blockedColor;
531+
}
532+
533+
graphic.lineStyle(thickness, color).lineBetween(x3, y3 - halfThickness, x4, y4 - halfThickness);
534+
}
510535

511536
// Left
512-
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
513-
514-
if (blocked.left || worldBlocked.left)
537+
if (checkCollision.left)
515538
{
516-
color = (worldBlocked.left) ? worldBlockedColor : blockedColor;
517-
}
539+
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
518540

519-
graphic.lineStyle(thickness, color).lineBetween(x1 + halfThickness, y1, x3 + halfThickness, y3);
541+
if (blocked.left || worldBlocked.left)
542+
{
543+
color = (worldBlocked.left) ? worldBlockedColor : blockedColor;
544+
}
545+
546+
graphic.lineStyle(thickness, color).lineBetween(x1 + halfThickness, y1, x3 + halfThickness, y3);
547+
}
520548

521549
// Right
522-
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
523-
524-
if (blocked.right || worldBlocked.right)
550+
if (checkCollision.right)
525551
{
526-
color = (worldBlocked.right) ? worldBlockedColor : blockedColor;
527-
}
552+
color = (this.sleeping) ? sleepColor : this.debugBodyColor;
528553

529-
graphic.lineStyle(thickness, color).lineBetween(x2 - halfThickness, y2, x4 - halfThickness, y4);
554+
if (blocked.right || worldBlocked.right)
555+
{
556+
color = (worldBlocked.right) ? worldBlockedColor : blockedColor;
557+
}
558+
559+
graphic.lineStyle(thickness, color).lineBetween(x2 - halfThickness, y2, x4 - halfThickness, y4);
560+
}
530561
}
531562

532563
// if (this.isCircle)
@@ -536,7 +567,7 @@ var BaseBody = new Class({
536567

537568
if (this.debugShowVelocity)
538569
{
539-
graphic.lineStyle(graphic.defaultStrokeWidth, this.world.defaults.velocityDebugColor, 1);
570+
graphic.lineStyle(thickness, defaults.velocityDebugColor, 1);
540571
graphic.lineBetween(x, y, x + this.velocity.x / 2, y + this.velocity.y / 2);
541572
}
542573
},
@@ -955,6 +986,8 @@ var BaseBody = new Class({
955986
*/
956987
setGameObject: function (gameObject, update)
957988
{
989+
if (update === undefined) { update = true; }
990+
958991
if (gameObject && gameObject !== this.gameObject)
959992
{
960993
// Remove this body from the old game object

0 commit comments

Comments
 (0)