diff --git a/Docs/Screen Shots/phaser_sprite_bounds.png b/Docs/Screen Shots/phaser_sprite_bounds.png new file mode 100644 index 00000000..cda91f28 Binary files /dev/null and b/Docs/Screen Shots/phaser_sprite_bounds.png differ diff --git a/README.md b/README.md index 4a31cd0b..82e5eea7 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,11 @@ Version 1.0.6 (in progress) * New: When loading a Sprite Sheet you can now pass negative values for the frame sizes which specifies the number of rows/columns to load instead (thanks TheJare) * New: BitmapText now supports anchor and has fixed box dimensions (thanks TheJare) * Fixed bug where if a State contains an empty Preloader the Update will not be called (thanks TheJare) +* Added World.postUpdate - all sprite position changes, as a result of physics, happen here before the render. +* Complete overhaul of Physics.Arcade.Body - now significantly more stable and faster too. +* Updated ArcadePhysics.separateX/Y to use new body system - much better results now. +* QuadTree bug found in 1.0.5 now fixed. The QuadTree is updated properly now using worldTransform values. +* Several new examples added (cameras, tweens, etc) diff --git a/examples/camera/camera cull.php b/examples/camera/camera cull.php new file mode 100644 index 00000000..00b87050 --- /dev/null +++ b/examples/camera/camera cull.php @@ -0,0 +1,64 @@ + + + + + \ No newline at end of file diff --git a/examples/camera/moving the camera.php b/examples/camera/moving the camera.php new file mode 100644 index 00000000..7fc2c508 --- /dev/null +++ b/examples/camera/moving the camera.php @@ -0,0 +1,70 @@ + + + + + \ No newline at end of file diff --git a/examples/collision/vertical collision.php b/examples/collision/vertical collision.php new file mode 100644 index 00000000..a878458d --- /dev/null +++ b/examples/collision/vertical collision.php @@ -0,0 +1,72 @@ + + + + + \ No newline at end of file diff --git a/examples/tweens/chained tweens.php b/examples/tweens/chained tweens.php index cb77634f..1d01dd69 100644 --- a/examples/tweens/chained tweens.php +++ b/examples/tweens/chained tweens.php @@ -7,7 +7,7 @@ (function () { - var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render }); + var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create }); var p; @@ -19,23 +19,17 @@ function create() { - game.stage.backgroundColor = 0x337799; + game.stage.backgroundColor = 0x2d2d2d; - p = game.add.sprite(0, 0, 'diamond'); + p = game.add.sprite(100, 100, 'diamond'); - game.add.tween(p).to({ x: 700 }, 1000, Phaser.Easing.Linear.None, true) + game.add.tween(p).to({ x: 600 }, 2000, Phaser.Easing.Linear.None, true) .to({ y: 300 }, 1000, Phaser.Easing.Linear.None) - .to({ x: 0 }, 1000, Phaser.Easing.Linear.None) - .to({ y: 0 }, 1000, Phaser.Easing.Linear.None) + .to({ x: 100 }, 2000, Phaser.Easing.Linear.None) + .to({ y: 100 }, 1000, Phaser.Easing.Linear.None) .loop(); } - function update() { - } - - function render() { - } - })(); diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 160fc8f1..be395d24 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -210,9 +210,6 @@ Phaser.Sprite.prototype.preUpdate = function() { this._cache.dirty = true; } - // this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x); - // this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y); - if (this.visible) { this.renderOrderID = this.game.world.currentRenderOrderID++; @@ -299,10 +296,14 @@ Phaser.Sprite.prototype.postUpdate = function() { // The sprite is positioned in this call, after taking into consideration motion updates and collision this.body.postUpdate(); - this.position.x -= (this.game.world.camera.x * this.scrollFactor.x); - this.position.y -= (this.game.world.camera.y * this.scrollFactor.y); - this.x -= (this.game.world.camera.x * this.scrollFactor.x); - this.y -= (this.game.world.camera.y * this.scrollFactor.y); + this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x); + this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y); + + if (this.position.x != this._cache.x || this.position.y != this._cache.y) + { + this.position.x = this._cache.x; + this.position.y = this._cache.y; + } } } diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js index 54a279eb..ca7de4f6 100644 --- a/src/geom/Rectangle.js +++ b/src/geom/Rectangle.js @@ -648,7 +648,7 @@ Phaser.Rectangle.equals = function (a, b) { */ Phaser.Rectangle.intersection = function (a, b, out) { - out = out || new Phaser.Rectangle; + out = out || new Phaser.Rectangle; if (Phaser.Rectangle.intersects(a, b)) { diff --git a/src/physics/arcade/ArcadePhysics.js b/src/physics/arcade/ArcadePhysics.js index 6cba4a13..094b6a15 100644 --- a/src/physics/arcade/ArcadePhysics.js +++ b/src/physics/arcade/ArcadePhysics.js @@ -380,12 +380,6 @@ Phaser.Physics.Arcade.prototype = { this._result = (this.separateX(body1, body2) || this.separateY(body1, body2)); - // if (this._result) - // { - // body1.postUpdate(); - // body2.postUpdate(); - // } - }, /** @@ -396,106 +390,103 @@ Phaser.Physics.Arcade.prototype = { */ separateX: function (body1, body2) { - // Can't separate two immovable or non-existing bodys + // Can't separate two immovable bodies if (body1.immovable && body2.immovable) { return false; } - // First, get the two body deltas this._overlap = 0; - if (body1.deltaX() != body2.deltaX()) + // Check if the hulls actually overlap + if (Phaser.Rectangle.intersects(body1, body2)) { - // Check if the X hulls actually overlap + this._maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + this.OVERLAP_BIAS; - this._bounds1.setTo(body1.x - ((body1.deltaX() > 0) ? body1.deltaX() : 0), body1.lastY, body1.width + ((body1.deltaX() > 0) ? body1.deltaX() : -body1.deltaX()), body1.height); - this._bounds2.setTo(body2.x - ((body2.deltaX() > 0) ? body2.deltaX() : 0), body2.lastY, body2.width + ((body2.deltaX() > 0) ? body2.deltaX() : -body2.deltaX()), body2.height); - - if ((this._bounds1.right > this._bounds2.x) && (this._bounds1.x < this._bounds2.right) && (this._bounds1.bottom > this._bounds2.y) && (this._bounds1.y < this._bounds2.bottom)) + if (body1.deltaX() == 0 && body2.deltaX() == 0) { - this._maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + this.OVERLAP_BIAS; + // They overlap but neither of them are moving + body1.embedded = true; + body2.embedded = true; + } + else if (body1.deltaX() > body2.deltaX()) + { + // Body1 is moving right and/or Body2 is moving left + this._overlap = body1.x + body1.width - body2.x; - // If they did overlap (and can), figure out by how much and flip the corresponding flags - if (body1.deltaX() > body2.deltaX()) + if ((this._overlap > this._maxOverlap) || body1.allowCollision.right == false || body2.allowCollision.left == false) { - this._overlap = body1.x + body1.width - body2.x; - - if ((this._overlap > this._maxOverlap) || body1.allowCollision.right == false || body2.allowCollision.left == false) - { - this._overlap = 0; - } - else - { - body1.touching.right = true; - body2.touching.left = true; - } + this._overlap = 0; } - else if (body1.deltaX() < body2.deltaX()) + else { - this._overlap = body1.x - body2.width - body2.x; - - if ((-this._overlap > this._maxOverlap) || body1.allowCollision.left == false || body2.allowCollision.right == false) - { - this._overlap = 0; - } - else - { - body1.touching.left = true; - body2.touching.right = true; - } + body1.touching.right = true; + body2.touching.left = true; } } - } - - // Then adjust their positions and velocities accordingly (if there was any overlap) - if (this._overlap != 0) - { - body1.overlapX = this._overlap; - body2.overlapX = this._overlap; - - if (body1.customSeparateX || body2.customSeparateX) + else if (body1.deltaX() < body2.deltaX()) { + // Body1 is moving left and/or Body2 is moving right + this._overlap = body1.x - body2.width - body2.x; + + if ((-this._overlap > this._maxOverlap) || body1.allowCollision.left == false || body2.allowCollision.right == false) + { + this._overlap = 0; + } + else + { + body1.touching.left = true; + body2.touching.right = true; + } + } + + // Then adjust their positions and velocities accordingly (if there was any overlap) + if (this._overlap != 0) + { + body1.overlapX = this._overlap; + body2.overlapX = this._overlap; + + if (body1.customSeparateX || body2.customSeparateX) + { + return true; + } + + this._velocity1 = body1.velocity.x; + this._velocity2 = body2.velocity.x; + + if (!body1.immovable && !body2.immovable) + { + this._overlap *= 0.5; + + body1.x = body1.x - this._overlap; + body2.x += this._overlap; + + this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1); + this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1); + this._average = (this._newVelocity1 + this._newVelocity2) * 0.5; + this._newVelocity1 -= this._average; + this._newVelocity2 -= this._average; + + body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x; + body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x; + } + else if (!body1.immovable) + { + body1.x = body1.x - this._overlap; + body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x; + } + else if (!body2.immovable) + { + body2.x += this._overlap; + body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x; + } + return true; } - - this._velocity1 = body1.velocity.x; - this._velocity2 = body2.velocity.x; - - if (!body1.immovable && !body2.immovable) - { - this._overlap *= 0.5; - - body1.x = body1.x - this._overlap; - body2.x += this._overlap; - - this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1); - this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1); - this._average = (this._newVelocity1 + this._newVelocity2) * 0.5; - this._newVelocity1 -= this._average; - this._newVelocity2 -= this._average; - - body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x; - body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x; - } - else if (!body1.immovable) - { - body1.x = body1.x - this._overlap; - body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x; - } - else if (!body2.immovable) - { - body2.x += this._overlap; - body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x; - } - - return true; - } - else - { - return false; } + return false; + }, /** @@ -512,110 +503,110 @@ Phaser.Physics.Arcade.prototype = { return false; } - // First, get the two body deltas this._overlap = 0; - if (body1.deltaY() != body2.deltaY()) + // Check if the hulls actually overlap + if (Phaser.Rectangle.intersects(body1, body2)) { - // Check if the Y hulls actually overlap - this._bounds1.setTo(body1.x, body1.y - ((body1.deltaY() > 0) ? body1.deltaY() : 0), body1.width, body1.height + body1.deltaAbsY()); - this._bounds2.setTo(body2.x, body2.y - ((body2.deltaY() > 0) ? body2.deltaY() : 0), body2.width, body2.height + body2.deltaAbsY()); + this._maxOverlap = body1.deltaAbsY() + body2.deltaAbsY() + this.OVERLAP_BIAS; - if ((this._bounds1.right > this._bounds2.x) && (this._bounds1.x < this._bounds2.right) && (this._bounds1.bottom > this._bounds2.y) && (this._bounds1.y < this._bounds2.bottom)) + if (body1.deltaY() == 0 && body2.deltaY() == 0) { - this._maxOverlap = body1.deltaAbsY() + body2.deltaAbsY() + this.OVERLAP_BIAS; + // They overlap but neither of them are moving + body1.embedded = true; + body2.embedded = true; + } + else if (body1.deltaY() > body2.deltaY()) + { + // Body1 is moving down and/or Body2 is moving up + this._overlap = body1.y + body1.height - body2.y; - // If they did overlap (and can), figure out by how much and flip the corresponding flags - if (body1.deltaY() > body2.deltaY()) + if ((this._overlap > this._maxOverlap) || body1.allowCollision.down == false || body2.allowCollision.up == false) { - this._overlap = body1.y + body1.height - body2.y; - - if ((this._overlap > this._maxOverlap) || body1.allowCollision.down == false || body2.allowCollision.up == false) - { - this._overlap = 0; - } - else - { - body1.touching.down = true; - body2.touching.up = true; - } + this._overlap = 0; } - else if (body1.deltaY() < body2.deltaY()) + else { - this._overlap = body1.y - body2.height - body2.y; - - if ((-this._overlap > this._maxOverlap) || body1.allowCollision.up == false || body2.allowCollision.down == false) - { - this._overlap = 0; - } - else - { - body1.touching.up = true; - body2.touching.down = true; - } + body1.touching.down = true; + body2.touching.up = true; } } - } - - // Then adjust their positions and velocities accordingly (if there was any overlap) - if (this._overlap != 0) - { - body1.overlapY = this._overlap; - body2.overlapY = this._overlap; - - if (body1.customSeparateY || body2.customSeparateY) + else if (body1.deltaY() < body2.deltaY()) { + // Body1 is moving up and/or Body2 is moving down + this._overlap = body1.y - body2.height - body2.y; + + if ((-this._overlap > this._maxOverlap) || body1.allowCollision.up == false || body2.allowCollision.down == false) + { + this._overlap = 0; + } + else + { + body1.touching.up = true; + body2.touching.down = true; + } + } + + // Then adjust their positions and velocities accordingly (if there was any overlap) + if (this._overlap != 0) + { + body1.overlapY = this._overlap; + body2.overlapY = this._overlap; + + if (body1.customSeparateY || body2.customSeparateY) + { + return true; + } + + this._velocity1 = body1.velocity.y; + this._velocity2 = body2.velocity.y; + + if (!body1.immovable && !body2.immovable) + { + this._overlap *= 0.5; + + body1.y = body1.y - this._overlap; + body2.y += this._overlap; + + this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1); + this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1); + this._average = (this._newVelocity1 + this._newVelocity2) * 0.5; + this._newVelocity1 -= this._average; + this._newVelocity2 -= this._average; + + body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y; + body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y; + } + else if (!body1.immovable) + { + body1.y = body1.y - this._overlap; + body1.velocity.y = this._velocity2 - this._velocity1 * body1.bounce.y; + + // This is special case code that handles things like horizontal moving platforms you can ride + if (body2.active && body2.moves && (body1.deltaY() > body2.deltaY())) + { + body1.x += body2.x - body2.lastX; + } + } + else if (!body2.immovable) + { + body2.y += this._overlap; + body2.velocity.y = this._velocity1 - this._velocity2 * body2.bounce.y; + + // This is special case code that handles things like horizontal moving platforms you can ride + if (body1.sprite.active && body1.moves && (body1.deltaY() < body2.deltaY())) + { + body2.x += body1.x - body1.lastX; + } + } + return true; } - this._velocity1 = body1.velocity.y; - this._velocity2 = body2.velocity.y; - - if (!body1.immovable && !body2.immovable) - { - this._overlap *= 0.5; - - body1.y = body1.y - this._overlap; - body2.y += this._overlap; - - this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1); - this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1); - this._average = (this._newVelocity1 + this._newVelocity2) * 0.5; - this._newVelocity1 -= this._average; - this._newVelocity2 -= this._average; - - body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y; - body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y; - } - else if (!body1.immovable) - { - body1.y = body1.y - this._overlap; - body1.velocity.y = this._velocity2 - this._velocity1 * body1.bounce.y; - - // This is special case code that handles things like horizontal moving platforms you can ride - if (body2.active && body2.moves && (body1.deltaY() > body2.deltaY())) - { - body1.x += body2.x - body2.lastX; - } - } - else if (!body2.immovable) - { - body2.y += this._overlap; - body2.velocity.y = this._velocity1 - this._velocity2 * body2.bounce.y; - - // This is special case code that handles things like horizontal moving platforms you can ride - if (body1.sprite.active && body1.moves && (body1.deltaY() < body2.deltaY())) - { - body2.x += body1.x - body1.lastX; - } - } - - return true; - } - else - { - return false; } + + return false; + }, /** @@ -626,12 +617,11 @@ Phaser.Physics.Arcade.prototype = { */ separateTile: function (object, x, y, width, height, mass, collideLeft, collideRight, collideUp, collideDown, separateX, separateY) { - var separatedY = this.separateTileY(object.body, x, y, width, height, mass, collideUp, collideDown, separateY); var separatedX = this.separateTileX(object.body, x, y, width, height, mass, collideLeft, collideRight, separateX); + var separatedY = this.separateTileY(object.body, x, y, width, height, mass, collideUp, collideDown, separateY); if (separatedX || separatedY) { - object.body.postUpdate(); return true; } @@ -653,77 +643,67 @@ Phaser.Physics.Arcade.prototype = { return false; } - // First, get the object delta this._overlap = 0; - // console.log('separatedX', x, y, object.deltaX()); - - if (object.deltaX() != 0) + if (Phaser.Rectangle.intersectsRaw(object, x, x + width, y, y + height)) { - this._bounds1.setTo(object.x, object.y, object.width, object.height); + this._maxOverlap = object.deltaAbsX() + this.OVERLAP_BIAS; - if ((this._bounds1.right > x) && (this._bounds1.x < x + width) && (this._bounds1.bottom > y) && (this._bounds1.y < y + height)) + if (object.deltaX() == 0) { - // The hulls overlap, let's process it - this._maxOverlap = object.deltaAbsX() + this.OVERLAP_BIAS; - - // TODO - We need to check if we're already inside of the tile, i.e. jumping through an n-way tile - // in which case we didn't ought to separate because it'll look like tunneling - - if (object.deltaX() > 0) - { - // Going right ... - this._overlap = object.x + object.width - x; - - if ((this._overlap > this._maxOverlap) || !object.allowCollision.right || !collideLeft) - { - this._overlap = 0; - } - else - { - object.touching.right = true; - } - } - else if (object.deltaX() < 0) - { - // Going left ... - this._overlap = object.x - width - x; - - if ((-this._overlap > this._maxOverlap) || !object.allowCollision.left || !collideRight) - { - this._overlap = 0; - } - else - { - object.touching.left = true; - } - } + // Object is stuck inside a tile and not moving } - } - - // Then adjust their positions and velocities accordingly (if there was any overlap) - if (this._overlap != 0) - { - if (separate) + else if (object.deltaX() > 0) { - object.x = object.x - this._overlap; + // Going right ... + this._overlap = object.x + object.width - x; - if (object.bounce.x == 0) + if ((this._overlap > this._maxOverlap) || !object.allowCollision.right || !collideLeft) { - object.velocity.x = 0; + this._overlap = 0; } else { - object.velocity.x = -object.velocity.x * object.bounce.x; + object.touching.right = true; } } - return true; - } - else - { - return false; + else if (object.deltaX() < 0) + { + // Going left ... + this._overlap = object.x - width - x; + + if ((-this._overlap > this._maxOverlap) || !object.allowCollision.left || !collideRight) + { + this._overlap = 0; + } + else + { + object.touching.left = true; + } + } + + if (this._overlap != 0) + { + if (separate) + { + object.x = object.x - this._overlap; + + if (object.bounce.x == 0) + { + object.velocity.x = 0; + } + else + { + object.velocity.x = -object.velocity.x * object.bounce.x; + } + } + return true; + } + } + return false; + }, /** @@ -740,77 +720,66 @@ Phaser.Physics.Arcade.prototype = { return false; } - // First, get the object delta this._overlap = 0; - if (object.deltaY() != 0) + if (Phaser.Rectangle.intersectsRaw(object, x, x + width, y, y + height)) { - this._bounds1.setTo(object.x, object.y, object.width, object.height); + this._maxOverlap = object.deltaAbsY() + this.OVERLAP_BIAS; - if ((this._bounds1.right > x) && (this._bounds1.x < x + width) && (this._bounds1.bottom > y) && (this._bounds1.y < y + height)) + if (object.deltaY() == 0) { - // The hulls overlap, let's process it + // Object is stuck inside a tile and not moving + } + else if (object.deltaY() > 0) + { + // Going down ... + this._overlap = object.bottom - y; - // Not currently used, may need it so keep for now - this._maxOverlap = object.deltaAbsY() + this.OVERLAP_BIAS; - - // TODO - We need to check if we're already inside of the tile, i.e. jumping through an n-way tile - // in which case we didn't ought to separate because it'll look like tunneling - - if (object.deltaY() > 0) + // if (object.allowCollision.down && collideDown && this._overlap < this._maxOverlap) + if ((this._overlap > this._maxOverlap) || !object.allowCollision.down || !collideDown) { - // Going down ... - this._overlap = object.bottom - y; - - // if (object.allowCollision.down && collideDown && this._overlap < this._maxOverlap) - if ((this._overlap > this._maxOverlap) || !object.allowCollision.down || !collideDown) - { - this._overlap = 0; - } - else - { - object.touching.down = true; - } + this._overlap = 0; } else { - // Going up ... - this._overlap = object.y - height - y; - - if ((-this._overlap > this._maxOverlap) || !object.allowCollision.up || !collideUp) - { - this._overlap = 0; - } - else - { - object.touching.up = true; - } + object.touching.down = true; } } - } - - // Then adjust their positions and velocities accordingly (if there was any overlap) - if (this._overlap != 0) - { - if (separate) + else if (object.deltaY() < 0) { - object.y = object.y - this._overlap; + // Going up ... + this._overlap = object.y - height - y; - if (object.bounce.y == 0) + if ((-this._overlap > this._maxOverlap) || !object.allowCollision.up || !collideUp) { - object.velocity.y = 0; + this._overlap = 0; } else { - object.velocity.y = -object.velocity.y * object.bounce.y; + object.touching.up = true; } } - return true; - } - else - { - return false; + + if (this._overlap != 0) + { + if (separate) + { + object.y = object.y - this._overlap; + + if (object.bounce.y == 0) + { + object.velocity.y = 0; + } + else + { + object.velocity.y = -object.velocity.y * object.bounce.y; + } + } + return true; + } } + + return false; }, diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index d2aac936..7f64ad5b 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -9,8 +9,6 @@ Phaser.Physics.Arcade.Body = function (sprite) { this.y = sprite.y; this.preX = sprite.x; this.preY = sprite.y; - this.lastX = sprite.x; - this.lastY = sprite.y; // un-scaled original size this.sourceWidth = sprite.currentFrame.sourceSizeW; @@ -64,6 +62,9 @@ Phaser.Physics.Arcade.Body = function (sprite) { this.overlapX = 0; this.overlapY = 0; + // If a body is overlapping with another body, but neither of them are moving (maybe they spawned on-top of each other?) this is set to true + this.embedded = false; + this.collideWorldBounds = false; }; @@ -99,6 +100,8 @@ Phaser.Physics.Arcade.Body.prototype = { this.touching.left = false; this.touching.right = false; + this.embedded = false; + this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x; this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y; this.rotation = this.sprite.angle; @@ -106,12 +109,6 @@ Phaser.Physics.Arcade.Body.prototype = { this.x = this.preX; this.y = this.preY; - // There is a bug here in that the worldTransform values are what should be used, otherwise the quadTree gets the wrong rect given to it - // this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x; - // this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y; - // this.x = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x; - // this.y = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y; - if (this.moves) { this.game.physics.updateMotion(this); @@ -129,51 +126,24 @@ Phaser.Physics.Arcade.Body.prototype = { this.game.physics.quadTree.insert(this); } - if (this.allowRotation) - { - this.sprite.angle = this.rotation; - } - }, postUpdate: function () { if (this.deltaX() != 0) { - this.sprite.position.x += this.deltaX(); this.sprite.x += this.deltaX(); } if (this.deltaY() != 0) { - this.sprite.position.y += this.deltaY(); this.sprite.y += this.deltaY(); } - - // this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x); - // this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y); - - - // if (this.position.x != this._cache.x || this.position.y != this._cache.y) - // { - // this.position.x = this._cache.x; - // this.position.y = this._cache.y; - // this._cache.dirty = true; - // } - - - // Adjust the sprite based on all of the above, so the x/y coords will be correct going into the State update - // this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width); - // this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height); - - // this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width); - // this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height); - - // if (this.allowRotation) - // { - // this.sprite.angle = this.rotation; - // } + if (this.allowRotation) + { + this.sprite.angle = this.rotation; + } }, @@ -226,30 +196,25 @@ Phaser.Physics.Arcade.Body.prototype = { this.angularVelocity = 0; this.angularAcceleration = 0; - // this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x; - // this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y; - // this.lastX = this.x; - // this.lastY = this.y; + this.x = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x; + this.y = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y; + this.rotation = this.sprite.angle; }, deltaAbsX: function () { - // return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX()); return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX()); }, deltaAbsY: function () { - // return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY()); return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY()); }, deltaX: function () { - // return this.x - this.lastX; return this.x - this.preX; }, deltaY: function () { - // return this.y - this.lastY; return this.y - this.preY; } diff --git a/src/tilemap/Tile.js b/src/tilemap/Tile.js index 94795789..e4500526 100644 --- a/src/tilemap/Tile.js +++ b/src/tilemap/Tile.js @@ -121,18 +121,33 @@ Phaser.Tile.prototype = { this.collideUp = false; this.collideDown = false; - }, - - /** - * Returns a string representation of this object. - * @method toString - * @return {string} a string representation of the object. - **/ - toString: function () { - - // return "[{Tile (index=" + this.index + " collisions=" + this.allowCollisions + " width=" + this.width + " height=" + this.height + ")}]"; - return ''; - } -}; \ No newline at end of file +}; + +Object.defineProperty(Phaser.Tile.prototype, "bottom", { + + /** + * 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. + * @method bottom + * @return {Number} + **/ + get: function () { + return this.y + this.height; + } + +}); + +Object.defineProperty(Phaser.Tile.prototype, "right", { + + /** + * 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. + * However it does affect the width property. + * @method right + * @return {Number} + **/ + get: function () { + return this.x + this.width; + } + +}); diff --git a/src/tilemap/Tilemap.js b/src/tilemap/Tilemap.js index cae0ff92..fa695266 100644 --- a/src/tilemap/Tilemap.js +++ b/src/tilemap/Tilemap.js @@ -183,6 +183,8 @@ Phaser.Tilemap.prototype.parseTiledJSON = function (json, key) { */ Phaser.Tilemap.prototype.generateTiles = function (qty) { + console.log('generating', qty, 'tiles'); + for (var i = 0; i < qty; i++) { this.tiles.push(new Phaser.Tile(this.game, this, i, this.currentLayer.tileWidth, this.currentLayer.tileHeight));