diff --git a/README.md b/README.md index 1aa768fb..0e56f6ac 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ Significant API changes: * Body.collideCallback allows you to set a callback that is fired whenever the Body is hit on any of its active faces. * Body.allowCollision has been renamed to Body.checkCollision. * Body.rebound is a boolean that controls if a body will exchange velocity on collision. Set to false to allow it to be 'pushed' (see new examples). +* Removed Body.deltaAbsX and deltaAbsY as they are no longer used internally. +* Body.screenX and screenY moved to getters, no longer calculated every frame. New features: diff --git a/examples/collision/sprite vs sprite.js b/examples/collision/sprite vs sprite.js index 01a50397..6a8edbd8 100644 --- a/examples/collision/sprite vs sprite.js +++ b/examples/collision/sprite vs sprite.js @@ -1,5 +1,5 @@ -var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update }); +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); function preload() { @@ -40,3 +40,12 @@ function collisionHandler (obj1, obj2) { game.stage.backgroundColor = '#992d2d'; } + +function render() { + + // game.debug.renderBodyInfo(sprite1, 16, 16); + + game.debug.renderPolygon(sprite1.body.polygons); + game.debug.renderPolygon(sprite2.body.polygons); + +} \ No newline at end of file diff --git a/examples/physics/mass velocity test.js b/examples/physics/mass velocity test.js index be3de3a1..824d79ab 100644 --- a/examples/physics/mass velocity test.js +++ b/examples/physics/mass velocity test.js @@ -15,6 +15,8 @@ function create() { aliens = game.add.group(); + game.physics.gravity.y = 100; + for (var i = 0; i < 50; i++) { var s = aliens.create(game.world.randomX, game.world.randomY, 'baddie'); @@ -27,6 +29,7 @@ function create() { } car = game.add.sprite(400, 300, 'car'); + car.name = 'car'; car.anchor.setTo(0.5, 0.5); car.body.collideWorldBounds = true; // car.body.bounce.setTo(0.8, 0.8); @@ -62,7 +65,17 @@ function update() { function render() { + for (var i = 0; i < aliens._container.children.length; i++) + { + game.debug.renderPolygon(aliens._container.children[i].body.polygons); + } + + game.debug.renderPolygon(car.body.polygons); + + // game.debug.renderBodyInfo(aliens._container.children[0], 32, 32); + game.debug.renderBodyInfo(aliens._container.children[0], 32, 32); + // game.debug.renderBodyInfo(car, 16, 24); - game.debug.renderBodyInfo(aliens.getFirstAlive(), 16, 24); + // game.debug.renderBodyInfo(aliens.getFirstAlive(), 16, 24); } diff --git a/examples/wip/platform.js b/examples/wip/platform.js index fc1a9ecc..b8a411ec 100644 --- a/examples/wip/platform.js +++ b/examples/wip/platform.js @@ -67,10 +67,8 @@ function create() { function launch() { - game.time._x = true; - - // sprite.body.velocity.x = -200; - // sprite.body.velocity.y = -200; + sprite.body.velocity.x = -200; + sprite.body.velocity.y = -200; } diff --git a/examples/wip/tween-relative.js b/examples/wip/tween-relative.js index 38fb243f..759f3067 100644 --- a/examples/wip/tween-relative.js +++ b/examples/wip/tween-relative.js @@ -29,17 +29,19 @@ function create() { function move() { + console.log('moving'); + if (sprite.x === 100) { // Here you'll notice we are using a relative value for the tween. // You can specify a number as a string with either + or - at the start of it. // When the tween starts it will take the sprites current X value and add +300 to it. - game.add.tween(sprite).to( { x: '+300' }, 2000, Phaser.Easing.Linear.None, true); + // game.add.tween(sprite).to( { x: '+300' }, 2000, Phaser.Easing.Linear.None, true); } else if (sprite.x === 400) { - game.add.tween(sprite).to( { x: '-300' }, 2000, Phaser.Easing.Linear.None, true); + // game.add.tween(sprite).to( { x: '-300' }, 2000, Phaser.Easing.Linear.None, true); } } @@ -48,10 +50,17 @@ function render() { if (sprite.x === 100 || sprite.x === 400) { - game.debug.renderText('Click sprite to tween', 32, 32); + // game.debug.renderText('Click sprite to tween', 32, 32); } game.debug.renderText('x: ' + arrowStart.x, arrowStart.x, arrowStart.y - 4); game.debug.renderText('x: ' + arrowEnd.x, arrowEnd.x, arrowEnd.y - 4); + game.debug.renderText('sprite.x: ' + sprite.x + ' deltaX: ' + sprite.deltaX, 32, 32); + game.debug.renderText('sprite.y: ' + sprite.y + ' deltaY: ' + sprite.deltaY, 32, 48); + + game.debug.renderPolygon(sprite.body.polygons); + + game.debug.renderPoint(sprite.center); + } diff --git a/src/core/World.js b/src/core/World.js index 9492b956..161b1952 100644 --- a/src/core/World.js +++ b/src/core/World.js @@ -77,7 +77,9 @@ Phaser.World.prototype.preUpdate = function () { if (currentNode['preUpdate'] && !currentNode.preUpdate()) { currentNode = currentNode.last._iNext; - } else { + } + else + { currentNode = currentNode._iNext; } @@ -106,7 +108,9 @@ Phaser.World.prototype.update = function () { if (currentNode['update'] && !currentNode.update()) { currentNode = currentNode.last._iNext; - } else { + } + else + { currentNode = currentNode._iNext; } diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index cda3fe0b..cf17ea54 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -507,7 +507,6 @@ Phaser.Sprite.prototype.updateAnimation = function() { this._cache.halfHeight = Math.floor(this._cache.height / 2); this._cache.dirty = true; - } }; @@ -993,6 +992,34 @@ Phaser.Sprite.prototype.play = function (name, frameRate, loop, killOnComplete) }; +/** +* Returns the delta x value. The difference between Sprite.x now and in the previous step. +* @name Phaser.Sprite#deltaX +* @property {number} deltaX - The delta value. Positive if the motion was to the right, negative if to the left. +* @readonly +*/ +Object.defineProperty(Phaser.Sprite.prototype, 'deltaX', { + + get: function() { + return this.world.x - this._cache.prevX; + } + +}); + +/** +* Returns the delta x value. The difference between Sprite.y now and in the previous step. +* @name Phaser.Sprite#deltaY +* @property {number} deltaY - The delta value. Positive if the motion was downwards, negative if upwards. +* @readonly +*/ +Object.defineProperty(Phaser.Sprite.prototype, 'deltaY', { + + get: function() { + return this.world.y - this._cache.prevY; + } + +}); + /** * Indicates the rotation of the Sprite, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation. * Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement player.angle = 450 is the same as player.angle = 90. diff --git a/src/physics/arcade/ArcadePhysics.js b/src/physics/arcade/ArcadePhysics.js index 72481934..570d4739 100644 --- a/src/physics/arcade/ArcadePhysics.js +++ b/src/physics/arcade/ArcadePhysics.js @@ -29,11 +29,6 @@ Phaser.Physics.Arcade = function (game) { */ this.gravity = new Phaser.Point(); - /** - * @property {Phaser.Rectangle} bounds - The bounds inside of which the physics world exists. Defaults to match the world bounds. - */ - // this.bounds = new Phaser.Rectangle(0, 0, game.world.width, game.world.height); - /** * @property {Phaser.QuadTree} quadTree - The world QuadTree. */ @@ -91,6 +86,12 @@ Phaser.Physics.Arcade = function (game) { */ this._dy = 0; + /** + * @property {Phaser.Point} _p - Internal cache var. + * @private + */ + this._p = new Phaser.Point(0, 0); + /** * @property {number} _gravityX - Internal cache var. * @private @@ -123,6 +124,12 @@ Phaser.Physics.Arcade.RECT = 0; */ Phaser.Physics.Arcade.CIRCLE = 1; +/** +* @constant +* @type {number} +*/ +Phaser.Physics.Arcade.POLYGON = 2; + Phaser.Physics.Arcade.prototype = { /** @@ -194,8 +201,9 @@ Phaser.Physics.Arcade.prototype = { // pos = pos + dt*(vel + temp/2) // vel = vel + temp - body.motionVelocity.x = (body.acceleration.x + this._gravityX) * this.game.time.physicsElapsed; - body.motionVelocity.y = (body.acceleration.y + this._gravityY) * this.game.time.physicsElapsed; + this._p.setTo((body.acceleration.x + this._gravityX) * this.game.time.physicsElapsed, (body.acceleration.y + this._gravityY) * this.game.time.physicsElapsed); + + return this._p; }, diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index 9ee95c67..eda52526 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -60,18 +60,6 @@ Phaser.Physics.Arcade.Body = function (sprite) { */ this.preRotation = sprite.angle; - /** - * @property {number} screenX - The x position of the physics body translated to screen space. - * @readonly - */ - this.screenX = sprite.x; - - /** - * @property {number} screenY - The y position of the physics body translated to screen space. - * @readonly - */ - this.screenY = sprite.y; - /** * @property {number} sourceWidth - The un-scaled original size. * @readonly @@ -109,11 +97,6 @@ Phaser.Physics.Arcade.Body = function (sprite) { */ this.center = new Phaser.Point(this.x + this.halfWidth, this.y + this.halfHeight); - /** - * @property {Phaser.Point} motionVelocity - The data from the updateMotion function. - */ - this.motionVelocity = new Phaser.Point(); - /** * @property {Phaser.Point} velocity - The velocity of the Body. */ @@ -134,11 +117,6 @@ Phaser.Physics.Arcade.Body = function (sprite) { */ this.angle = 0; - /** - * @property {number} minBounceVelocity - Optional minimum bounce velocity. - */ - this.minBounceVelocity = 0.1; - /** * @property {Phaser.Point} gravity - The gravity applied to the motion of the Body. This works in addition to any gravity set on the world. */ @@ -150,16 +128,16 @@ Phaser.Physics.Arcade.Body = function (sprite) { this.bounce = new Phaser.Point(); /** - * @property {Phaser.Point} minVelocity - When a body rebounds off another the minVelocity is checked, if the new velocity is lower than the minVelocity the body is stopped. + * @property {Phaser.Point} minVelocity - When a body rebounds off another or a wall the minVelocity is checked. If the new velocity is lower than minVelocity the body is stopped. * @default */ - this.minVelocity = new Phaser.Point(10, 10); + this.minVelocity = new Phaser.Point(5, 5); /** * @property {Phaser.Point} maxVelocity - The maximum velocity that the Body can reach. * @default */ - this.maxVelocity = new Phaser.Point(2000, 2000); + this.maxVelocity = new Phaser.Point(1000, 1000); /** * @property {number} angularVelocity - The angular velocity of the Body. @@ -290,11 +268,6 @@ Phaser.Physics.Arcade.Body = function (sprite) { */ this.blocked = { up: false, down: false, left: false, right: false }; - /** - * @property {Phaser.Point} blockedPoint - A Point object holding the blocked penetration distance. - */ - this.blockedPoint = new Phaser.Point(0, 0); - /** * @property {Phaser.Physics.Arcade.RECT|Phaser.Physics.Arcade.CIRCLE} type - The type of SAT Shape. */ @@ -311,6 +284,12 @@ Phaser.Physics.Arcade.Body = function (sprite) { */ this.polygons = this.shape.toPolygon(); + /** + * @property {Phaser.Point} _temp - Internal cache var. + * @private + */ + this._temp = null; + /** * @property {number} _dx - Internal cache var. * @private @@ -341,11 +320,6 @@ Phaser.Physics.Arcade.Body = function (sprite) { */ this._distances = [0, 0, 0, 0]; - this._x = 0; - this._y = 0; - - this._debug = 0; - }; Phaser.Physics.Arcade.Body.prototype = { @@ -401,15 +375,13 @@ Phaser.Physics.Arcade.Body.prototype = { */ preUpdate: function () { - this.screenX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x; - this.screenY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y; + this.preX = this.x; + this.preY = this.y; + this.preRotation = this.rotation; - this._x = this.sprite.x; - this._y = this.sprite.y; - this.preX = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x; - this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y; - - this.preRotation = this.sprite.angle; + this.x = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x; + this.y = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y; + this.rotation = this.sprite.angle; this.blocked.up = false; this.blocked.down = false; @@ -422,10 +394,6 @@ Phaser.Physics.Arcade.Body.prototype = { this.touching.left = false; this.touching.right = false; - this.x = this.preX; - this.y = this.preY; - this.rotation = this.preRotation; - this.speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y); this.angle = Math.atan2(this.velocity.y, this.velocity.x); @@ -434,11 +402,10 @@ Phaser.Physics.Arcade.Body.prototype = { if (this.collideWorldBounds) { this.checkWorldBounds(); + this.adjustWorldBounds(); } - this.game.physics.updateMotion(this); - - this.applyMotion(); + this.applyFriction(); } this.syncPosition(); @@ -455,62 +422,59 @@ Phaser.Physics.Arcade.Body.prototype = { if (this.x <= this.game.world.bounds.x) { - this.x += this.game.world.bounds.x - this.x; this.blocked.left = true; } else if (this.right >= this.game.world.bounds.right) { - this.x -= this.right - this.game.world.bounds.right; this.blocked.right = true; } if (this.y <= this.game.world.bounds.y) { - this.y += this.game.world.bounds.y - this.y; this.blocked.up = true; } else if (this.bottom >= this.game.world.bounds.bottom) { - this.y -= this.bottom - this.game.world.bounds.bottom; this.blocked.down = true; } -/* - if (this.x <= this.game.world.bounds.x) + }, + + /** + * Internal method used to adjust the position of the Body against the World Bounds. + * + * @method Phaser.Physics.Arcade#adjustWorldBounds + * @protected + */ + adjustWorldBounds: function () { + + if (this.x < this.game.world.bounds.x) { - this.blockedPoint.x = this.game.world.bounds.x - this.x; - this.touching.left = true; - // console.log('cw left', this.blockedPoint.x); + this.x += this.game.world.bounds.x - this.x; } - else if (this.right >= this.game.world.bounds.right) + else if (this.right > this.game.world.bounds.right) { - this.blockedPoint.x = this.right - this.game.world.bounds.right; - this.touching.right = true; - // console.log('cw right', this.blockedPoint.x); + this.x -= this.right - this.game.world.bounds.right; } - if (this.y <= this.game.world.bounds.y) + if (this.y < this.game.world.bounds.y) { - this.blockedPoint.y = this.game.world.bounds.y - this.y; - this.touching.up = true; - // console.log('cw up', this.blockedPoint.y); + this.y += this.game.world.bounds.y - this.y; } - else if (this.bottom >= this.game.world.bounds.bottom) + else if (this.bottom > this.game.world.bounds.bottom) { - this.blockedPoint.y = this.bottom - this.game.world.bounds.bottom; - this.touching.down = true; - // console.log('cw down', this.blockedPoint.y); + this.y -= this.bottom - this.game.world.bounds.bottom; } -*/ + }, /** * Internal method. * - * @method Phaser.Physics.Arcade#applyMotion + * @method Phaser.Physics.Arcade#applyFriction * @protected */ - applyMotion: function () { + applyFriction: function () { if (this.friction > 0 && this.acceleration.isZero()) { @@ -527,111 +491,6 @@ Phaser.Physics.Arcade.Body.prototype = { this.velocity.y = Math.sin(this.angle) * this.speed; } - if (this.blocked.left) - { - this.velocity.x *= -this.bounce.x; - this.reboundCheck(true, false); - - this._dx = this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2); - - if (this._dx > this.minBounceVelocity || this.getTotalGravityX() > 0) - { - this.x += this._dx; - this.velocity.x += this.motionVelocity.x; - } - else - { - this.preX = this.x; - this.velocity.x = 0; - } - } - else if (this.blocked.right) - { - this.velocity.x *= -this.bounce.x; - this.reboundCheck(true, false); - - this._dx = this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2); - - if (this._dx < -this.minBounceVelocity || this.getTotalGravityX() < 0) - { - this.x += this._dx; - this.velocity.x += this.motionVelocity.x; - } - else - { - this.preX = this.x; - this.velocity.x = 0; - } - } - else - { - this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2); - this.velocity.x += this.motionVelocity.x; - } - - // overlapX/Y values at this point will be penetration into the bounds and DELTA WILL BE ZERO - if (this.blocked.up) - { - this.velocity.y *= -this.bounce.y; - this.reboundCheck(false, true); - - this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2); - - if (this._dy > this.minBounceVelocity || this.getTotalGravityY() > 0) - { - this.y += this._dy; - this.velocity.y += this.motionVelocity.y; - } - else - { - this.preY = this.y; - this.velocity.y = 0; - } - } - else if (this.blocked.down) - { - this.velocity.y *= -this.bounce.y; - this.reboundCheck(false, true); - - this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2); - - if (this._dy < -this.minBounceVelocity || this.getTotalGravityY() < 0) - { - this.y += this._dy; - this.velocity.y += this.motionVelocity.y; - } - else - { - this.preY = this.y; - this.velocity.y = 0; - } - } - else - { - this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2); - this.velocity.y += this.motionVelocity.y; - } - - if (this.velocity.x > this.maxVelocity.x) - { - this.velocity.x = this.maxVelocity.x; - } - else if (this.velocity.x < -this.maxVelocity.x) - { - this.velocity.x = -this.maxVelocity.x; - } - - if (this.velocity.y > this.maxVelocity.y) - { - this.velocity.y = this.maxVelocity.y; - } - else if (this.velocity.y < -this.maxVelocity.y) - { - this.velocity.y = -this.maxVelocity.y; - } - - this.syncPosition(); - }, /** @@ -641,11 +500,17 @@ Phaser.Physics.Arcade.Body.prototype = { * @protected * @param {boolean} x - Check the X axis? * @param {boolean} y - Check the Y axis? + * @param {boolean} rebound - If true it will reverse the velocity on the given axis */ - reboundCheck: function (x, y) { + reboundCheck: function (x, y, rebound) { if (x) { + if (rebound) + { + this.velocity.x *= -this.bounce.x; + } + var gx = this.getTotalGravityX(); if (Math.abs(this.velocity.x) < this.minVelocity.x && (this.blocked.left && gx < 0 || this.blocked.right && gx > 0)) @@ -656,6 +521,11 @@ Phaser.Physics.Arcade.Body.prototype = { if (y) { + if (rebound) + { + this.velocity.y *= -this.bounce.y; + } + var gy = this.getTotalGravityY(); if (Math.abs(this.velocity.y) < this.minVelocity.y && (this.blocked.up && gy < 0 || this.blocked.down && gy > 0)) @@ -749,6 +619,8 @@ Phaser.Physics.Arcade.Body.prototype = { if (this.rebound) { this.processRebound(body); + this.reboundCheck(true, true, false); + body.reboundCheck(true, true, false); } }, @@ -768,6 +640,8 @@ Phaser.Physics.Arcade.Body.prototype = { if (this.rebound) { this.processRebound(body); + this.reboundCheck(true, true, false); + body.reboundCheck(true, true, false); } }, @@ -789,6 +663,8 @@ Phaser.Physics.Arcade.Body.prototype = { if (this.rebound) { this.exchange(body); + this.reboundCheck(true, true, false); + body.reboundCheck(true, true, false); } }, @@ -835,9 +711,6 @@ Phaser.Physics.Arcade.Body.prototype = { body.velocity.y = nv2; } - this.reboundCheck(true, true); - body.reboundCheck(true, true); - }, /** @@ -851,7 +724,7 @@ Phaser.Physics.Arcade.Body.prototype = { this.velocity.x = body.velocity.x - this.velocity.x * this.bounce.x; this.velocity.y = body.velocity.y - this.velocity.y * this.bounce.y; - this.reboundCheck(true, true); + this.reboundCheck(true, true, false); }, @@ -911,12 +784,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); } } @@ -925,16 +798,28 @@ 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); } } + if (this.collideWorldBounds) + { + // this.checkWorldBounds(); + this.adjustWorldBounds(); + } + + if (body.collideWorldBounds) + { + // body.checkWorldBounds(); + body.adjustWorldBounds(); + } + this.syncPosition(); body.syncPosition(); @@ -967,16 +852,15 @@ Phaser.Physics.Arcade.Body.prototype = { return; } - // This body isn't moving horizontally, so it was hit by something moving right - if (this.immovable || this.blocked.right) + if (this.immovable || this.blocked.right || this.touching.right) { body.give(this, response); } else { - if (body.immovable || body.blocked.left) + if (body.immovable || body.blocked.left || body.touching.left) { - // We take the full separation as what hit it isn't moveable + // We take the full separation this.take(body, response); } else @@ -989,12 +873,6 @@ Phaser.Physics.Arcade.Body.prototype = { this.touching.left = true; body.touching.right = true; - if (this.checkWorldBounds && this.right >= this.game.world.bounds.right) - { - this.blocked.right = true; - this.x -= this.right - this.game.world.bounds.right; - } - }, /** @@ -1014,24 +892,25 @@ Phaser.Physics.Arcade.Body.prototype = { // 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 (this.collideCallback && !this.collideCallback.call(this.collideCallbackContext, Phaser.RIGHT, this, body)) { + console.log('bail 2'); return; } - // This body isn't moving horizontally, so it was hit by something moving left - if (this.immovable || this.blocked.left) + if (this.immovable || this.blocked.left || this.touching.left) { body.give(this, response); } else { - if (body.immovable || body.blocked.right) + if (body.immovable || body.blocked.right || body.touching.right) { - // We take the full separation as what hit it isn't moveable + // We take the full separation this.take(body, response); } else @@ -1044,12 +923,6 @@ Phaser.Physics.Arcade.Body.prototype = { this.touching.right = true; body.touching.left = true; - if (this.checkWorldBounds && this.x <= this.game.world.bounds.x) - { - this.blocked.left = true; - this.x += this.game.world.bounds.x - this.x; - } - }, /** @@ -1077,16 +950,15 @@ Phaser.Physics.Arcade.Body.prototype = { return; } - // This body isn't moving vertically, so it was hit by something moving down - if (this.immovable || this.blocked.down) + if (this.immovable || this.blocked.down || this.touching.down) { body.give(this, response); } else { - if (body.immovable || body.blocked.up) + if (body.immovable || body.blocked.up || body.touching.up) { - // We take the full separation as what hit it isn't moveable + // We take the full separation this.take(body, response); } else @@ -1099,12 +971,6 @@ Phaser.Physics.Arcade.Body.prototype = { this.touching.up = true; body.touching.down = true; - if (this.checkWorldBounds && this.bottom >= this.game.world.bounds.bottom) - { - this.blocked.down = true; - this.y -= this.bottom - this.game.world.bounds.bottom; - } - }, /** @@ -1132,16 +998,15 @@ Phaser.Physics.Arcade.Body.prototype = { return; } - // This body isn't moving vertically, so it was hit by something moving up - if (this.immovable || this.blocked.up) + if (this.immovable || this.blocked.up || this.touching.up) { body.give(this, response); } else { - if (body.immovable || body.blocked.down) + if (body.immovable || body.blocked.down || body.touching.down) { - // We take the full separation as what hit it isn't moveable + // We take the full separation this.take(body, response); } else @@ -1154,12 +1019,6 @@ Phaser.Physics.Arcade.Body.prototype = { this.touching.down = true; body.touching.up = true; - if (this.checkWorldBounds && this.y <= this.game.world.bounds.y) - { - this.blocked.up = true; - this.y += this.game.world.bounds.y - this.y; - } - }, /** @@ -1181,6 +1040,53 @@ Phaser.Physics.Arcade.Body.prototype = { }, + /** + * Internal method. + * + * @method Phaser.Physics.Arcade#integrateVelocity + * @protected + */ + integrateVelocity: function () { + + this._temp = this.game.physics.updateMotion(this); + this._dx = this.game.time.physicsElapsed * (this.velocity.x + this._temp.x / 2); + this._dy = this.game.time.physicsElapsed * (this.velocity.y + this._temp.y / 2); + + // positive = RIGHT / DOWN + // negative = LEFT / UP + + if ((this._dx < 0 && !this.blocked.left && !this.touching.left) || (this._dx > 0 && !this.blocked.right && !this.touching.right)) + { + this.x += this._dx; + this.velocity.x += this._temp.x; + } + + 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; + } + + if (this.velocity.x > this.maxVelocity.x) + { + this.velocity.x = this.maxVelocity.x; + } + else if (this.velocity.x < -this.maxVelocity.x) + { + this.velocity.x = -this.maxVelocity.x; + } + + if (this.velocity.y > this.maxVelocity.y) + { + this.velocity.y = this.maxVelocity.y; + } + else if (this.velocity.y < -this.maxVelocity.y) + { + this.velocity.y = -this.maxVelocity.y; + } + + }, + /** * Internal method. This is called directly before the sprites are sent to the renderer and after the update function has finished. * @@ -1191,7 +1097,22 @@ Phaser.Physics.Arcade.Body.prototype = { if (this.moves) { - this.checkWorldBounds(); + if (this.bounce.x !== 0 && (this.blocked.left || this.blocked.right)) + { + this.reboundCheck(true, false, true); + } + + if (this.bounce.y !== 0 && (this.blocked.up || this.blocked.down)) + { + this.reboundCheck(false, true, true); + } + + this.integrateVelocity(); + + if (this.collideWorldBounds) + { + this.adjustWorldBounds(); + } this.syncPosition(); @@ -1213,22 +1134,9 @@ Phaser.Physics.Arcade.Body.prototype = { this.facing = Phaser.DOWN; } - this.sprite.x = this.sprite.worldTransform[2] = this.x - (this.preX - this._x); - this.sprite.y = this.sprite.worldTransform[5] = this.y - (this.preY - this._y); + this.sprite.x = this.sprite.worldTransform[2] = this.x - (this.preX - this.sprite.x); + this.sprite.y = this.sprite.worldTransform[5] = this.y - (this.preY - this.sprite.y); -/* - if ((this.deltaX() < 0 && !this.blocked.left) || (this.deltaX() > 0 && !this.blocked.right)) - { - this.sprite.x += this.deltaX(); - this.sprite.worldTransform[2] += this.deltaX(); - } - - if ((this.deltaY() < 0 && !this.blocked.up) || (this.deltaY() > 0 && !this.blocked.down)) - { - this.sprite.y += this.deltaY(); - this.sprite.worldTransform[5] += this.deltaY(); - } -*/ this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight); if (this.allowRotation) @@ -1308,26 +1216,6 @@ Phaser.Physics.Arcade.Body.prototype = { }, - /** - * Returns the absolute delta x value. - * - * @method Phaser.Physics.Arcade.Body#deltaAbsX - * @return {number} The absolute delta value. - */ - deltaAbsX: function () { - return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX()); - }, - - /** - * Returns the absolute delta y value. - * - * @method Phaser.Physics.Arcade.Body#deltaAbsY - * @return {number} The absolute delta value. - */ - deltaAbsY: function () { - return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY()); - }, - /** * Returns the delta x value. The difference between Body.x now and in the previous step. * @@ -1348,6 +1236,12 @@ Phaser.Physics.Arcade.Body.prototype = { return this.y - this.preY; }, + /** + * Returns the delta z value. The difference between Body.rotation now and in the previous step. + * + * @method Phaser.Physics.Arcade.Body#deltaZ + * @return {number} The delta value. + */ deltaZ: function () { return this.rotation - this.preRotation; } @@ -1427,3 +1321,37 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", { } }); + +/** +* @name Phaser.Physics.Arcade.Body#screenX +* @property {number} screenX - The x position of the physics body translated to screen space. +*/ +Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "screenX", { + + /** + * The x position of the physics body translated to screen space. + * @method screenX + * @return {number} + */ + get: function () { + return (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x; + } + +}); + +/** +* @name Phaser.Physics.Arcade.Body#screenY +* @property {number} screenY - The y position of the physics body translated to screen space. +*/ +Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "screenY", { + + /** + * The y position of the physics body translated to screen space. + * @method screenY + * @return {number} + */ + get: function () { + return (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y; + } + +}); diff --git a/src/utils/Debug.js b/src/utils/Debug.js index e063485f..52cad04d 100644 --- a/src/utils/Debug.js +++ b/src/utils/Debug.js @@ -452,17 +452,15 @@ Phaser.Utils.Debug.prototype = { color = color || 'rgb(255,255,255)'; - this.start(x, y, color, 220); + this.start(x, y, color, 210); this.splitline('x: ' + sprite.body.x.toFixed(2), 'y: ' + sprite.body.y.toFixed(2), 'width: ' + sprite.width, 'height: ' + sprite.height); - this.splitline('_x: ' + sprite.body._x.toFixed(2), '_y: ' + sprite.body._y.toFixed(2), 'wx: ' + sprite.worldTransform[2].toFixed(2), 'wy: ' + sprite.worldTransform[5].toFixed(2)); this.splitline('speed: ' + sprite.body.speed.toFixed(2), 'angle: ' + sprite.body.angle.toFixed(2), 'friction: ' + sprite.body.friction); this.splitline('blocked left: ' + sprite.body.blocked.left, 'right: ' + sprite.body.blocked.right, 'up: ' + sprite.body.blocked.up, 'down: ' + sprite.body.blocked.down); this.splitline('touching left: ' + sprite.body.touching.left, 'right: ' + sprite.body.touching.right, 'up: ' + sprite.body.touching.up, 'down: ' + sprite.body.touching.down); this.splitline('gravity x: ' + sprite.body.gravity.x, 'y: ' + sprite.body.gravity.y, 'world gravity x: ' + this.game.physics.gravity.x, 'y: ' + this.game.physics.gravity.y); this.splitline('acceleration x: ' + sprite.body.acceleration.x.toFixed(2), 'y: ' + sprite.body.acceleration.y.toFixed(2)); this.splitline('velocity x: ' + sprite.body.velocity.x.toFixed(2), 'y: ' + sprite.body.velocity.y.toFixed(2), 'deltaX: ' + sprite.body.deltaX().toFixed(2), 'deltaY: ' + sprite.body.deltaY().toFixed(2)); - this.splitline('motion x: ' + sprite.body.motionVelocity.x.toFixed(2), 'y: ' + sprite.body.motionVelocity.y.toFixed(2)); this.splitline('bounce x: ' + sprite.body.bounce.x.toFixed(2), 'y: ' + sprite.body.bounce.y.toFixed(2)); this.stop(); @@ -526,17 +524,15 @@ Phaser.Utils.Debug.prototype = { // 4 = scaleY // 5 = translateY - // this.line('id: ' + sprite._id); - // this.line('scale x: ' + sprite.worldTransform[0]); - // this.line('scale y: ' + sprite.worldTransform[4]); - // this.line('tx: ' + sprite.worldTransform[2]); - // this.line('ty: ' + sprite.worldTransform[5]); - // this.line('skew x: ' + sprite.worldTransform[3]); - // this.line('skew y: ' + sprite.worldTransform[1]); - this.line('deltaX: ' + sprite.body.deltaX()); - this.line('deltaY: ' + sprite.body.deltaY()); - // this.line('sdx: ' + sprite.deltaX()); - // this.line('sdy: ' + sprite.deltaY()); + this.line('id: ' + sprite._id); + this.line('scale x: ' + sprite.worldTransform[0]); + this.line('scale y: ' + sprite.worldTransform[4]); + this.line('tx: ' + sprite.worldTransform[2]); + this.line('ty: ' + sprite.worldTransform[5]); + this.line('skew x: ' + sprite.worldTransform[3]); + this.line('skew y: ' + sprite.worldTransform[1]); + this.line('sdx: ' + sprite.deltaX); + this.line('sdy: ' + sprite.deltaY); // this.line('inCamera: ' + this.game.renderer.spriteRenderer.inCamera(this.game.camera, sprite)); this.stop(); @@ -634,25 +630,6 @@ Phaser.Utils.Debug.prototype = { }, - renderGroupInfo: function (group, x, y, color) { - - if (this.context == null) - { - return; - } - - color = color || 'rgb(255, 255, 255)'; - - this.start(x, y, color); - - this.line('Group (size: ' + group.length + ')'); - this.line('x: ' + group.x); - this.line('y: ' + group.y); - - this.stop(); - - }, - /** * Renders Point coordinates in the given color. * @method Phaser.Utils.Debug#renderPointInfo