Added Game core loop stepping support. Super-useful for debugging, and helped me track down the issue with jittery physics collision. Double-win!

This commit is contained in:
photonstorm
2014-01-29 17:10:13 +00:00
parent d51a37211c
commit 651858372c
25 changed files with 1268 additions and 438 deletions
+116 -19
View File
@@ -706,6 +706,9 @@ Phaser.Physics.Arcade.prototype = {
*/
collideSpriteVsTilemapLayer: function (sprite, tilemapLayer, collideCallback, processCallback, callbackContext) {
console.log('collideSpriteVsTilemapLayer x:', sprite.x, 'y:', sprite.y, 'body left:', sprite.body.left, 'right:', sprite.body.right);
this._mapData = tilemapLayer.getTiles(sprite.body.left, sprite.body.top, sprite.body.width, sprite.body.height, true);
if (this._mapData.length === 0)
@@ -839,6 +842,37 @@ Phaser.Physics.Arcade.prototype = {
},
/**
* Performs a rect intersection test against the two objects.
* Objects must expose properties: width, height, left, right, top, bottom.
* @method Phaser.Physics.Arcade#intersects
* @param {object} body - The Body to test.
* @param {object} tile - The Tile to test.
* @returns {boolean} Returns true if the objects intersect, otherwise false.
*/
tileIntersects: function (body, tile) {
if (body.width <= 0 || body.height <= 0 || tile.width <= 0 || tile.height <= 0)
{
return false;
}
// console.log('body: ', body.left, body.top, body.right, body.bottom);
// console.log('tile: ', tile.x, tile.y, tile.right, tile.bottom);
// console.log('intersect #1', body.right < tile.x, body.right, tile.x);
// console.log('intersect #2', body.bottom < tile.y, body.bottom, tile.y);
// console.log('intersect #3', body.left > tile.right, body.left, tile.right);
// console.log('intersect #4', body.top > tile.bottom, body.top, tile.bottom);
// return !(a.right < b.x || a.bottom < b.y || a.x > b.right || a.y > b.bottom);
result = !(body.right < tile.x || body.bottom < tile.y || body.left > tile.right || body.top > tile.bottom);
return result;
},
/**
* The core separation function to separate a physics body and an array of tiles.
* @method Phaser.Physics.Arcade#separateTiles
@@ -860,12 +894,13 @@ Phaser.Physics.Arcade.prototype = {
var tile;
var localOverlapX = 0;
var localOverlapY = 0;
var process = false;
for (var i = 0; i < tiles.length; i++)
{
tile = tiles[i];
if (this.intersects(body, tile))
if (this.tileIntersects(body, tile))
{
// They overlap. Any custom callbacks?
if (tile.tile.callback || tile.layer.callbacks[tile.tile.index])
@@ -883,55 +918,67 @@ Phaser.Physics.Arcade.prototype = {
}
}
if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight)
if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight && !body.blocked.left)
{
// LEFT
localOverlapX = body.left - tile.right;
console.log('STS left', localOverlapX, body.deltaX(), 'bt', body.left, tile.right);
if (localOverlapX >= body.deltaX())
{
body.blocked.left = true;
body.touching.left = true;
body.touching.none = false;
process = true;
}
}
else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft)
else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft && !body.blocked.right)
{
// RIGHT
localOverlapX = body.right - tile.x;
console.log('STS right', localOverlapX, body.deltaX(), 'bt', body.right, tile.x);
// Distance check
if (localOverlapX <= body.deltaX())
{
body.blocked.right = true;
body.touching.right = true;
body.touching.none = false;
process = true;
}
}
if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom)
if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom && !body.blocked.up)
{
// UP
localOverlapY = body.top - tile.bottom;
console.log('STS up', localOverlapY, body.deltaY(), 'bt', body.top, tile.bottom);
// Distance check
if (localOverlapY >= body.deltaY())
{
body.blocked.up = true;
body.touching.up = true;
body.touching.none = false;
process = true;
}
}
else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop)
else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop && !body.blocked.down)
{
// DOWN
localOverlapY = body.bottom - tile.y;
console.log('STS down', localOverlapY, body.deltaY(), 'bt', body.bottom, tile.y);
if (localOverlapY <= body.deltaY())
{
body.blocked.down = true;
body.touching.down = true;
body.touching.none = false;
process = true;
}
}
}
@@ -947,7 +994,14 @@ Phaser.Physics.Arcade.prototype = {
body.overlapY = localOverlapY;
}
return this.processTileSeparation(body);
if (process)
{
return this.processTileSeparation(body);
}
else
{
return false;
}
},
@@ -961,11 +1015,16 @@ Phaser.Physics.Arcade.prototype = {
separateTile: function (body, tile) {
// Can't separate two immovable objects (tiles are always immovable)
if (body.immovable || this.intersects(body, tile) === false)
if (body.immovable || this.tileIntersects(body, tile) === false)
{
console.log('fail');
console.log('body: ', body.left, body.top, body.right, body.bottom);
console.log('tile: ', tile.x, tile.y, tile.right, tile.bottom);
return false;
}
console.log('CHECK');
// They overlap. Any custom callbacks?
if (tile.tile.callback || tile.layer.callbacks[tile.tile.index])
{
@@ -986,62 +1045,87 @@ Phaser.Physics.Arcade.prototype = {
body.overlapX = 0;
body.overlapY = 0;
// Remember - this happens AFTER the body has been moved by the motion update, so it needs moving back again
if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight)
var process = false;
if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight && !body.blocked.left)
{
// LEFT
body.overlapX = body.left - tile.right;
if (body.overlapX >= body.deltaX())
console.log('ST left', body.overlapX, body.deltaX(), 'bt', body.left, tile.right);
if (body.overlapX <= body.deltaX())
{
// use touching instead of blocked?
console.log('pass');
body.blocked.left = true;
body.touching.left = true;
body.touching.none = false;
process = true;
}
}
else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft)
else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft && !body.blocked.right)
{
// RIGHT
body.overlapX = body.right - tile.x;
console.log(tile);
console.log('ST right', body.overlapX, body.deltaX(), 'bt', body.right, tile.x);
// Distance check
if (body.overlapX <= body.deltaX())
if (body.overlapX >= body.deltaX())
{
console.log('pass');
body.blocked.right = true;
body.touching.right = true;
body.touching.none = false;
process = true;
}
}
if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom)
if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom && !body.blocked.up)
{
// UP
body.overlapY = body.top - tile.bottom;
console.log('ST up', body.overlapY, body.deltaY(), 'bt', body.top, tile.bottom);
// Distance check
if (body.overlapY >= body.deltaY())
{
console.log('pass');
body.blocked.up = true;
body.touching.up = true;
body.touching.none = false;
process = true;
}
}
else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop)
else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop && !body.blocked.down)
{
// DOWN
body.overlapY = body.bottom - tile.y;
console.log('ST down', body.overlapY, body.deltaY(), 'bt', body.bottom, tile.y);
if (body.overlapY <= body.deltaY())
{
console.log('pass');
body.blocked.down = true;
body.touching.down = true;
body.touching.none = false;
process = true;
}
}
// Separate in a single sweep
return this.processTileSeparation(body);
if (process)
{
return this.processTileSeparation(body);
}
else
{
return false;
}
},
@@ -1057,13 +1141,26 @@ Phaser.Physics.Arcade.prototype = {
// Swap for a hit tile?
if (body.touching.none)
{
console.log('processTileSeparation QUIT');
return false;
}
body.x += body.overlapX;
body.y += body.overlapY;
console.log('pre processTileSeparation', body.x, body.y);
if (body.touching.left || body.touching.right || body.blocked.left || body.blocked.right)
{
body.x -= body.overlapX;
}
if (body.touching.up || body.touching.down || body.blocked.up || body.blocked.down)
{
body.y -= body.overlapY;
}
console.log('post processTileSeparation', body.x, body.y, body.right);
body.setBlockFlag(body.blocked.left, body.blocked.right, body.blocked.up, body.blocked.down, body.overlapX, body.overlapY);
// body.setBlockFlag(body.blocked.left, body.blocked.right, body.blocked.up, body.blocked.down, body.overlapX, body.overlapY);
// body.reboundCheck(true, true, true);
return true;
+137 -106
View File
@@ -55,13 +55,13 @@ Phaser.Physics.Arcade.Body = function (sprite) {
* @property {number} preX - The previous x position of the physics body.
* @readonly
*/
this.preX = sprite.x;
this.preX = sprite.world.x;
/**
* @property {number} preY - The previous y position of the physics body.
* @readonly
*/
this.preY = sprite.y;
this.preY = sprite.world.y;
/**
* @property {number} preRotation - The previous rotation of the physics body.
@@ -333,7 +333,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.overlapY = 0;
// Set-up the default shape
this.setRectangle(sprite.width, sprite.height, -sprite._cache.halfWidth, -sprite._cache.halfHeight);
this.setRectangle(sprite.width, sprite.height, 0, 0);
};
@@ -350,11 +350,11 @@ Phaser.Physics.Arcade.Body.prototype = {
*/
setCircle: function (radius, offsetX, offsetY) {
if (typeof offsetX === 'undefined') { offsetX = 0; }
if (typeof offsetY === 'undefined') { offsetY = 0; }
if (typeof offsetX === 'undefined') { offsetX = this.sprite._cache.halfWidth; }
if (typeof offsetY === 'undefined') { offsetY = this.sprite._cache.halfHeight; }
this.type = Phaser.Physics.Arcade.CIRCLE;
this.shape = new SAT.Circle(new SAT.Vector(this.sprite.center.x, this.sprite.center.y), radius);
this.shape = new SAT.Circle(new SAT.Vector(this.sprite.x, this.sprite.y), radius);
this.polygon = null;
this.offset.setTo(offsetX, offsetY);
@@ -379,7 +379,7 @@ Phaser.Physics.Arcade.Body.prototype = {
if (typeof translateY === 'undefined') { translateY = -this.sprite._cache.halfHeight; }
this.type = Phaser.Physics.Arcade.RECT;
this.shape = new SAT.Box(new SAT.Vector(this.sprite.center.x, this.sprite.center.y), width, height);
this.shape = new SAT.Box(new SAT.Vector(this.sprite.world.x, this.sprite.world.y), width, height);
this.polygon = this.shape.toPolygon();
this.polygon.translate(translateX, translateY);
@@ -446,8 +446,11 @@ Phaser.Physics.Arcade.Body.prototype = {
this.preX = this.x;
this.preY = this.y;
this.x = this.sprite.center.x + this.offset.x;
this.y = this.sprite.center.y + this.offset.y;
// this.x = this.sprite.center.x + this.offset.x;
// this.y = this.sprite.center.y + this.offset.y;
this.x = this.sprite.world.x + this.offset.x;
this.y = this.sprite.world.y + this.offset.y;
if (this.allowRotation)
{
@@ -462,26 +465,32 @@ Phaser.Physics.Arcade.Body.prototype = {
this.updateBounds();
// if (this.blocked.left && this.blockFlags[0] !== this.left)
// {
// this.blocked.left = false;
// }
if (this.sprite.debug)
{
console.log('Body postUpdate x:', this.x, 'y:', this.y, 'left:', this.left, 'right:', this.right);
}
// if (this.blocked.right && this.blockFlags[1] !== this.right)
// {
// this.blocked.right = false;
// }
// if (this.blocked.up && this.blockFlags[2] !== this.top)
// {
// this.blocked.up = false;
// }
if (this.blocked.left && this.blockFlags[0] !== this.left)
{
this.blocked.left = false;
}
// if (this.blocked.down && this.blockFlags[3] !== this.bottom)
// {
// console.log('reset down block flag', this.blockFlags[3], this.bottom);
// this.blocked.down = false;
// }
if (this.blocked.right && this.blockFlags[1] !== this.right)
{
this.blocked.right = false;
}
if (this.blocked.up && this.blockFlags[2] !== this.top)
{
this.blocked.up = false;
}
if (this.blocked.down && this.blockFlags[3] !== this.bottom)
{
// console.log('reset down block flag', this.blockFlags[3], this.bottom);
this.blocked.down = false;
}
this.blocked.left = false;
this.blocked.right = false;
@@ -508,26 +517,32 @@ Phaser.Physics.Arcade.Body.prototype = {
setBlockFlag: function (left, right, up, down, x, y) {
this.updateBounds();
if (left)
{
this.blockFlags[0] = this.left + x;
this.blockFlags[0] = this.left;
// this.blockFlags[0] = this.left + x;
// console.log('left flag set to', this.blockFlags[0]);
}
else if (right)
{
this.blockFlags[1] = this.right + y;
this.blockFlags[1] = this.right;
// this.blockFlags[1] = this.right + x;
// console.log('right flag set to', this.blockFlags[1]);
}
if (up)
{
this.blockFlags[2] = this.top + y;
this.blockFlags[2] = this.top;
// this.blockFlags[2] = this.top + y;
// this.blockFlags[2] = this.top;
// console.log('up flag set to', this.blockFlags[2]);
}
else if (down)
{
this.blockFlags[3] = this.bottom + y;
this.blockFlags[3] = this.bottom;
// this.blockFlags[3] = this.bottom + y;
// this.blockFlags[3] = this.bottom;
// console.log('down flag set to', this.blockFlags[3]);
}
@@ -551,53 +566,15 @@ Phaser.Physics.Arcade.Body.prototype = {
}
else
{
this.left = this.polygon.pos.x - this.polygon.points[0].x;
this.right = this.polygon.pos.x + this.polygon.points[0].x;
this.top = this.polygon.pos.y - this.polygon.points[0].y;
this.bottom = this.polygon.pos.y + this.polygon.points[0].y;
var temp;
for (var i = 1, len = this.polygon.points.length; i < len; i++)
{
// Left
temp = this.polygon.pos.x - this.polygon.points[i].x;
if (temp < this.left)
{
this.left = temp;
}
// Right
temp = this.polygon.pos.x + this.polygon.points[i].x;
if (temp > this.right)
{
this.right = temp;
}
// Top
temp = this.polygon.pos.y - this.polygon.points[i].y;
if (temp < this.top)
{
this.top = temp;
}
// Bottom
temp = this.polygon.pos.y + this.polygon.points[i].y;
if (temp > this.bottom)
{
this.bottom = temp;
}
}
this.width = this.right - this.left;
this.height = this.bottom - this.top;
this.left = Phaser.Math.minProperty('x', this.polygon.points) + this.polygon.pos.x;
this.right = Phaser.Math.maxProperty('x', this.polygon.points) + this.polygon.pos.x;
this.top = Phaser.Math.minProperty('y', this.polygon.points) + this.polygon.pos.y;
this.bottom = Phaser.Math.maxProperty('y', this.polygon.points) + this.polygon.pos.y;
}
this.width = this.right - this.left;
this.height = this.bottom - this.top;
},
/**
@@ -906,7 +883,7 @@ Phaser.Physics.Arcade.Body.prototype = {
return this.customSeparateCallback.call(this.customSeparateContext, this, response);
}
console.log(this.sprite.name, 'collided with', body.sprite.name, response);
// console.log(this.sprite.name, 'collided with', body.sprite.name, response);
this._distances[0] = body.right - this.x; // Distance of B to face on left side of A
this._distances[1] = this.right - body.x; // Distance of B to face on right side of A
@@ -918,12 +895,12 @@ Phaser.Physics.Arcade.Body.prototype = {
// Which is smaller? Left or Right?
if (this._distances[0] < this._distances[1])
{
console.log(this.sprite.name, 'collided on the LEFT with', body.sprite.name, response);
// console.log(this.sprite.name, 'collided on the LEFT with', body.sprite.name, response);
this.hitLeft(body, response);
}
else if (this._distances[1] < this._distances[0])
{
console.log(this.sprite.name, 'collided on the RIGHT with', body.sprite.name, response);
// console.log(this.sprite.name, 'collided on the RIGHT with', body.sprite.name, response);
this.hitRight(body, response);
}
}
@@ -932,12 +909,12 @@ Phaser.Physics.Arcade.Body.prototype = {
// Which is smaller? Top or Bottom?
if (this._distances[2] < this._distances[3])
{
console.log(this.sprite.name, 'collided on the TOP with', body.sprite.name, response);
// console.log(this.sprite.name, 'collided on the TOP with', body.sprite.name, response);
this.hitTop(body, response);
}
else if (this._distances[3] < this._distances[2])
{
console.log(this.sprite.name, 'collided on the BOTTOM with', body.sprite.name, response);
// console.log(this.sprite.name, 'collided on the BOTTOM with', body.sprite.name, response);
this.hitBottom(body, response);
}
}
@@ -964,17 +941,17 @@ Phaser.Physics.Arcade.Body.prototype = {
hitLeft: function (body, response) {
// We know that Body is overlapping with This on the left hand side (deltaX < 0 = moving left, > 0 = moving right)
if (body.speed > 0 && (body.deltaX() <= 0 || (body.deltaX() > 0 && !this.checkCollision.left)))
{
return;
}
// if (body.speed > 0 && (body.deltaX() <= 0 || (body.deltaX() > 0 && !this.checkCollision.left)))
// {
// return;
// }
if (this.collideCallback && !this.collideCallback.call(this.collideCallbackContext, Phaser.LEFT, this, body, response))
{
return;
}
if (this.immovable || this.blocked.right || this.touching.right)
if (!this.moves || this.immovable || this.blocked.right || this.touching.right)
{
body.give(this, response);
}
@@ -1012,11 +989,11 @@ Phaser.Physics.Arcade.Body.prototype = {
hitRight: function (body, response) {
// We know that Body is overlapping with This on the right hand side (deltaX < 0 = moving left, > 0 = moving right)
if (body.speed > 0 && (body.deltaX() >= 0 || (body.deltaX() < 0 && !this.checkCollision.right)))
{
console.log('bail 1', body.deltaX());
return;
}
// if (body.speed > 0 && (body.deltaX() >= 0 || (body.deltaX() < 0 && !this.checkCollision.right)))
// {
// console.log('bail 1', body.deltaX());
// return;
// }
if (this.collideCallback && !this.collideCallback.call(this.collideCallbackContext, Phaser.RIGHT, this, body))
{
@@ -1024,7 +1001,7 @@ Phaser.Physics.Arcade.Body.prototype = {
return;
}
if (this.immovable || this.blocked.left || this.touching.left)
if (!this.moves || this.immovable || this.blocked.left || this.touching.left)
{
body.give(this, response);
}
@@ -1062,17 +1039,19 @@ Phaser.Physics.Arcade.Body.prototype = {
hitTop: function (body, response) {
// We know that Body is overlapping with This on the bottom side (deltaY < 0 = moving up, > 0 = moving down)
if (body.speed > 0 && (body.deltaY() <= 0 || (body.deltaY() > 0 && !this.checkCollision.up)))
{
return;
}
// if (body.speed > 0 && (body.deltaY() <= 0 || (body.deltaY() > 0 && !this.checkCollision.up)))
// if (body.speed > 0 && (body.deltaY() <= 0 || (body.deltaY() > 0 && !this.checkCollision.up)))
// {
// console.log('bail', body.sprite.name, body.deltaY());
// return;
// }
if (this.collideCallback && !this.collideCallback.call(this.collideCallbackContext, Phaser.UP, this, body))
{
return;
}
if (this.immovable || this.blocked.down || this.touching.down)
if (!this.moves || this.immovable || this.blocked.down || this.touching.down)
{
body.give(this, response);
}
@@ -1110,17 +1089,17 @@ Phaser.Physics.Arcade.Body.prototype = {
hitBottom: function (body, response) {
// We know that Body is overlapping with This on the bottom side (deltaY < 0 = moving up, > 0 = moving down)
if (body.speed > 0 && (body.deltaY() >= 0 || (body.deltaY() < 0 && !this.checkCollision.down)))
{
return;
}
// if (body.speed > 0 && (body.deltaY() >= 0 || (body.deltaY() < 0 && !this.checkCollision.down)))
// {
// return;
// }
if (this.collideCallback && !this.collideCallback.call(this.collideCallbackContext, Phaser.DOWN, this, body))
{
return;
}
if (this.immovable || this.blocked.up || this.touching.up)
if (!this.moves || this.immovable || this.blocked.up || this.touching.up)
{
body.give(this, response);
}
@@ -1162,13 +1141,14 @@ Phaser.Physics.Arcade.Body.prototype = {
{
this.x += this._dx;
this.velocity.x += this._temp.x;
console.log('x added', this._dx);
}
if ((this._dy < 0 && !this.blocked.up && !this.touching.up) || (this._dy > 0 && !this.blocked.down && !this.touching.down))
{
this.y += this._dy;
this.velocity.y += this._temp.y;
// console.log('y added', this._dy);
console.log('y added', this._dy);
}
if (this.velocity.x > this.maxVelocity.x)
@@ -1225,8 +1205,37 @@ Phaser.Physics.Arcade.Body.prototype = {
this.facing = Phaser.DOWN;
}
this.sprite.x = this.x + (this.sprite.x - this.sprite.center.x) - this.offset.x;
this.sprite.y = this.y + (this.sprite.y - this.sprite.center.y) - this.offset.y;
this.updateBounds();
if (this.sprite.debug)
{
console.log('Body postUpdate x:', this.x, 'y:', this.y, 'left:', this.left, 'right:', this.right);
}
// this.sprite.x = this.x + (this.sprite.x - this.sprite.center.x) - this.offset.x;
// this.sprite.y = this.y + (this.sprite.y - this.sprite.center.y) - this.offset.y;
if (this.sprite.name === 'mushroom')
{
// console.log('old x', this.preX, 'new x', this.x, 'delta', this.deltaX());
// console.log('old y', this.preY, 'new y', this.y);
}
this.sprite.x = this.x - this.offset.x;
this.sprite.y = this.y - this.offset.y;
this.sprite.worldTransform[2] = this.x - this.offset.x;
this.sprite.worldTransform[5] = this.y - this.offset.y;
// this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
// this.sprite.x = this.x + (this.sprite.world.x - this.game.camera.x) - this.offset.x;
// this.sprite.y = this.y + (this.sprite.world.y - this.game.camera.y) - this.offset.y;
// this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
if (this.allowRotation)
{
@@ -1269,7 +1278,16 @@ Phaser.Physics.Arcade.Body.prototype = {
* @return {number} The delta value. Positive if the motion was to the right, negative if to the left.
*/
deltaX: function () {
return this.x - this.preX;
if (this.moves)
{
return this.x - this.preX;
}
else
{
return this.sprite.deltaX;
}
},
/**
@@ -1279,7 +1297,16 @@ Phaser.Physics.Arcade.Body.prototype = {
* @return {number} The delta value. Positive if the motion was downwards, negative if upwards.
*/
deltaY: function () {
return this.y - this.preY;
if (this.moves)
{
return this.y - this.preY;
}
else
{
return this.sprite.deltaY;
}
},
/**
@@ -1334,6 +1361,8 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "x", {
this.polygon.pos.x = value;
}
// this.updateBounds();
}
});
@@ -1376,6 +1405,8 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "y", {
this.polygon.pos.y = value;
}
// this.updateBounds();
}
});