Skip to content

Commit fe6664e

Browse files
committed
Fixed the Body collide issues and optimised the process at the same time. Now the QuadTree appears to work perfectly as a result. Bonus!
1 parent bb5d8ca commit fe6664e

7 files changed

Lines changed: 116 additions & 44 deletions

File tree

examples/anchor1.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,29 @@ function preload() {
2424
function create() {
2525

2626
player = game.add.sprite(400, 300, 'ship');
27+
player.alpha = 0.2;
28+
player.body.collideWorldBounds = true;
29+
player.body.bounce.setTo(1, 1);
30+
31+
// The body offset is from the ANCHOR point of the Sprite, not the top-left (or center, etc)
32+
player.body.setSize(60, 60, 0, 0);
2733
player.anchor.setTo(0.5, 0.5);
34+
player.body.velocity.x = 100;
2835

2936
}
3037

3138
function update() {
39+
// console.log(Math.floor(player.center.x), Math.floor(player.center.y));
3240
player.angle++;
41+
player.scale.x += 0.005;
42+
player.scale.y += 0.005;
3343
}
3444

3545
function render() {
36-
game.debug.renderSpriteCorners(player,true,true);
37-
game.debug.renderRectangle(player.body.bounds);
46+
// game.debug.renderSpriteCorners(player,true,true);
47+
game.debug.renderRectangle(player.body);
48+
game.debug.renderPixel(400, 300, 'rgb(255,0,0)');
49+
game.debug.renderPoint(player.center, 'rgb(255,254,0)');
3850
}
3951

4052
})();

examples/body3.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ function update() {
5656
function render() {
5757

5858
game.debug.renderSpriteCorners(bunny, true, true);
59-
game.debug.renderRectangle(bunny.body.bounds);
60-
game.debug.renderRectangle(wall.body.bounds);
59+
game.debug.renderRectangle(bunny.body);
60+
game.debug.renderRectangle(wall.body);
6161

6262
}
6363

examples/invaders.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,22 @@ function preload() {
2525
function create() {
2626

2727
player = game.add.sprite(400, 500, 'ship');
28-
// player.anchor.setTo(0.5, 0.5);
28+
player.anchor.setTo(0.5, 0.5);
2929

3030
aliens = game.add.group();
3131

3232
for (var y = 0; y < 4; y++)
3333
{
3434
for (var x = 0; x < 10; x++)
3535
{
36-
aliens.create(x * 48, y * 64, 'alien');
36+
aliens.create(x * 48, y * 50, 'alien');
3737
}
3838
}
3939

4040
aliens.x = 100;
41+
aliens.y = 50;
4142

42-
var tween = game.add.tween(aliens).to({x: 200}, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
43+
var tween = game.add.tween(aliens).to({x: 200}, 3000, Phaser.Easing.Linear.None, true, 0, 1000, true);
4344
tween.onComplete.add(descend, this);
4445

4546
}

examples/quadtree2.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function create() {
3939

4040
ship = game.add.sprite(400, 400, 'ship');
4141
ship.body.collideWorldBounds = true;
42-
ship.body.bounce.setTo(0.5, 0.5);
42+
ship.body.bounce.setTo(1, 1);
4343

4444
}
4545

@@ -64,14 +64,14 @@ function update() {
6464
}
6565

6666
game.physics.collideGroup(aliens);
67-
// total = game.physics.overlap(ship);
67+
total = game.physics.overlap(ship);
6868

6969
}
7070

7171
function render() {
7272

7373
game.debug.renderQuadTree(game.physics.quadTree);
74-
game.debug.renderRectangle(ship.body.bounds);
74+
game.debug.renderRectangle(ship.body);
7575

7676
// game.debug.renderText('total: ' + total.length, 32, 50);
7777

src/math/QuadTree.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Phaser.QuadTree.prototype = {
104104
// if we have subnodes ...
105105
if (this.nodes[0] != null)
106106
{
107-
index = this.getIndex(body.bounds);
107+
index = this.getIndex(body);
108108

109109
if (index !== -1)
110110
{
@@ -126,7 +126,7 @@ Phaser.QuadTree.prototype = {
126126
// Add objects to subnodes
127127
while (i < this.objects.length)
128128
{
129-
index = this.getIndex(this.objects[i].bounds);
129+
index = this.getIndex(this.objects[i]);
130130

131131
if (index !== -1)
132132
{
@@ -192,7 +192,7 @@ Phaser.QuadTree.prototype = {
192192

193193
var returnObjects = this.objects;
194194

195-
sprite.body.quadTreeIndex = this.getIndex(sprite.body.bounds);
195+
sprite.body.quadTreeIndex = this.getIndex(sprite.body);
196196

197197
// Temp store for the node IDs this sprite is in, we can use this for fast elimination later
198198
sprite.body.quadTreeIDs.push(this.ID);

src/physics/arcade/Body.js

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Phaser.Physics.Arcade.Body = function (sprite) {
55

66
this.offset = new Phaser.Point;
77

8-
// the top-left of the Body
98
this.x = sprite.x;
109
this.y = sprite.y;
1110

@@ -19,8 +18,6 @@ Phaser.Physics.Arcade.Body = function (sprite) {
1918
this.halfWidth = Math.floor(sprite.currentFrame.sourceSizeW / 2);
2019
this.halfHeight = Math.floor(sprite.currentFrame.sourceSizeH / 2);
2120

22-
this.bounds = new Phaser.Rectangle(sprite.x, sprite.y, this.width, this.height);
23-
2421
// Scale value cache
2522
this._sx = sprite.scale.x;
2623
this._sy = sprite.scale.y;
@@ -69,8 +66,6 @@ Phaser.Physics.Arcade.Body.prototype = {
6966
this.height = this.sourceHeight * scaleY;
7067
this.halfWidth = Math.floor(this.width / 2);
7168
this.halfHeight = Math.floor(this.height / 2);
72-
this.bounds.width = this.width;
73-
this.bounds.height = this.height;
7469
this._sx = scaleX;
7570
this._sy = scaleY;
7671
}
@@ -94,19 +89,16 @@ Phaser.Physics.Arcade.Body.prototype = {
9489

9590
this.lastX = this.x;
9691
this.lastY = this.y;
97-
98-
this.x = this.sprite.x - this.offset.x + (this.sprite.anchor.x * this.width));
99-
this.y = this.sprite.y - this.offset.y + (this.sprite.anchor.y * this.height));
10092
this.rotation = this.sprite.angle;
10193

94+
this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
95+
this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
96+
10297
if (this.moves)
10398
{
10499
this.game.physics.updateMotion(this);
105100
}
106101

107-
this.bounds.x = this.x;
108-
this.bounds.y = this.y;
109-
110102
if (this.collideWorldBounds)
111103
{
112104
this.checkWorldBounds();
@@ -120,44 +112,52 @@ Phaser.Physics.Arcade.Body.prototype = {
120112
}
121113

122114
// Adjust the sprite based on all of the above, so the x/y coords will be correct going into the State update
123-
// this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
124-
// this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
125-
// this.sprite.x = this.x;
126-
// this.sprite.y = this.y;
115+
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
116+
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
117+
118+
if (this.allowRotation)
119+
{
120+
this.sprite.angle = this.rotation;
121+
}
122+
123+
},
124+
125+
/*
126+
postUpdate: function () {
127+
128+
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
129+
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
127130
128131
if (this.allowRotation)
129132
{
130-
// this.sprite.angle = this.rotation;
133+
this.sprite.angle = this.rotation;
131134
}
132135
133136
},
137+
*/
134138

135139
checkWorldBounds: function () {
136140

137-
if (this.bounds.x < this.game.world.bounds.x)
141+
if (this.x < this.game.world.bounds.x)
138142
{
139143
this.x = this.game.world.bounds.x;
140-
this.velocity.x *= -1;
141-
this.velocity.x *= this.bounce.x;
144+
this.velocity.x *= -this.bounce.x;
142145
}
143-
else if (this.bounds.right > this.game.world.bounds.right)
146+
else if (this.right > this.game.world.bounds.right)
144147
{
145148
this.x = this.game.world.bounds.right - this.width;
146-
this.velocity.x *= -1;
147-
this.velocity.x *= this.bounce.x;
149+
this.velocity.x *= -this.bounce.x;
148150
}
149151

150-
if (this.bounds.y < this.game.world.bounds.y)
152+
if (this.y < this.game.world.bounds.y)
151153
{
152154
this.y = this.game.world.bounds.y;
153-
this.velocity.y *= -1;
154-
this.velocity.y *= this.bounce.y;
155+
this.velocity.y *= -this.bounce.y;
155156
}
156-
else if (this.bounds.bottom > this.game.world.bounds.bottom)
157+
else if (this.bottom > this.game.world.bounds.bottom)
157158
{
158159
this.y = this.game.world.bounds.bottom - this.height;
159-
this.velocity.y *= -1;
160-
this.velocity.y *= this.bounce.y;
160+
this.velocity.y *= -this.bounce.y;
161161
}
162162

163163
},
@@ -173,8 +173,6 @@ Phaser.Physics.Arcade.Body.prototype = {
173173
this.height = this.sourceHeight * this._sy;
174174
this.halfWidth = Math.floor(this.width / 2);
175175
this.halfHeight = Math.floor(this.height / 2);
176-
this.bounds.width = this.width;
177-
this.bounds.height = this.height;
178176
this.offset.setTo(offsetX, offsetY);
179177

180178
},
@@ -195,4 +193,60 @@ Phaser.Physics.Arcade.Body.prototype = {
195193
return this.y - this.lastY;
196194
}
197195

198-
};
196+
};
197+
198+
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
199+
200+
/**
201+
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
202+
* @method bottom
203+
* @return {Number}
204+
**/
205+
get: function () {
206+
return this.y + this.height;
207+
},
208+
209+
/**
210+
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
211+
* @method bottom
212+
* @param {Number} value
213+
**/
214+
set: function (value) {
215+
if(value <= this.y) {
216+
this.height = 0;
217+
} else {
218+
this.height = (this.y - value);
219+
}
220+
},
221+
enumerable: true,
222+
configurable: true
223+
});
224+
225+
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", {
226+
227+
/**
228+
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
229+
* However it does affect the width property.
230+
* @method right
231+
* @return {Number}
232+
**/
233+
get: function () {
234+
return this.x + this.width;
235+
},
236+
237+
/**
238+
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
239+
* However it does affect the width property.
240+
* @method right
241+
* @param {Number} value
242+
**/
243+
set: function (value) {
244+
if(value <= this.x) {
245+
this.width = 0;
246+
} else {
247+
this.width = this.x + value;
248+
}
249+
},
250+
enumerable: true,
251+
configurable: true
252+
});

src/utils/Debug.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Phaser.Utils.Debug.prototype = {
2828
renderShadow: true,
2929
currentX: 0,
3030
currentY: 0,
31+
currentAlpha: 1,
3132
context: null,
3233

3334
/**
@@ -55,16 +56,20 @@ Phaser.Utils.Debug.prototype = {
5556
this.currentColor = color;
5657
}
5758

59+
this.currentAlpha = this.context.globalAlpha;
60+
5861
this.context.save();
5962
this.context.setTransform(1, 0, 0, 1, 0, 0);
6063
this.context.fillStyle = color;
6164
this.context.font = this.font;
65+
this.context.globalAlpha = 1;
6266

6367
},
6468

6569
stop: function () {
6670

6771
this.context.restore();
72+
this.context.globalAlpha = this.currentAlpha;
6873

6974
},
7075

0 commit comments

Comments
 (0)