From fc0e239719d836e706ea7bcd5f5cfcbb5832627e Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 26 Mar 2014 10:48:30 +0000 Subject: [PATCH] ArcadePhysics.Body preUpdate has been modified to stop Sprites with non-1 scaling from gaining delta and moving off the screen (fix #644). Lots of ArcadePhysics.World methods have been marked as private where they shouldn't be called directly (separateX, etc) --- README.md | 7 +++++-- src/gameobjects/Sprite.js | 16 ++++++++++++++++ src/physics/arcade/Body.js | 16 ++++++++++++---- src/physics/arcade/World.js | 37 +++++++++++++++++++++++-------------- 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c7358c479..24351175f 100644 --- a/README.md +++ b/README.md @@ -63,17 +63,20 @@ Version 2.0.2 - "Ghealdan" - -in active development- Bug Fixes * Tween.generateData would enter an eternal loop if the total resulted in a float. Now wrapped in Math.floor. +* ArcadePhysics.Body preUpdate has been modified to stop Sprites with non-1 scaling from gaining delta and moving off the screen (fix #644). Updated -* The Build your First Phaser Game Tutorial has been updated for Phaser 2.x +* The "Build your First Phaser Game" Tutorial has been updated for Phaser 2 * Line.fromSprite now sets "fromCenter" to false by default as Sprite.center is deprecated in 2.x. Documentation and Examples updated to reflect this. +* All the documentation has been re-published for 2.0.2. +* Lots of ArcadePhysics.World methods have been marked as private where they shouldn't be called directly (separateX, etc) New Features -* n/a +* Sprite.overlap lets you quickly check to see if the bounds of two display objects are intersecting or not, without having to use a physics system. There is an extensive [Migration Guide](https://github.com/photonstorm/phaser/blob/master/resources/Migration%20Guide.md) available for those converting from Phaser 1.x to 2.x. In the guide we detail the API breaking changes and approach to our new physics system. diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 945f6f929..2641da1b0 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -681,6 +681,22 @@ Phaser.Sprite.prototype.play = function (name, frameRate, loop, killOnComplete) }; +/** +* Checks to see if the bounds of this Sprite overlaps with the bounds of the given Display Object, which can be a Sprite, Image, TileSprite or anything that extends those such as a Button. +* This check ignores the Sprites hitArea property and runs a Sprite.getBounds comparison on both objects to determine the result. +* Therefore it's relatively expensive to use in large quantities (i.e. with lots of Sprites at a high frequency), but should be fine for low-volume testing where physics isn't required. +* +* @method Phaser.Sprite#overlap +* @memberof Phaser.Sprite +* @param {Phaser.Sprite|Phaser.Image|Phaser.TileSprite|Phaser.Button|PIXI.DisplayObject} displayObject - The display object to check against. +* @return {boolean} True if the bounds of this Sprite intersects at any point with the bounds of the given display object. +*/ +Phaser.Sprite.prototype.overlap = function (displayObject) { + + return Phaser.Rectangle.intersects(this.getBounds(), displayObject.getBounds()); + +}; + /** * 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/Body.js b/src/physics/arcade/Body.js index 2a4a240f0..66374a1d0 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -292,6 +292,12 @@ Phaser.Physics.Arcade.Body = function (sprite) { */ this.tilePadding = new Phaser.Point(); + /** + * @property {boolean} _reset - Internal cache var. + * @private + */ + this._reset = true; + /** * @property {number} _sx - Internal cache var. * @private @@ -341,11 +347,9 @@ Phaser.Physics.Arcade.Body.prototype = { this._sy = asy; this.center.setTo(this.position.x + this.halfWidth, this.position.y + this.halfHeight); - return true; + this._reset = true; } - return false; - }, /** @@ -376,13 +380,15 @@ Phaser.Physics.Arcade.Body.prototype = { this.embedded = false; + this.updateBounds(); + this.position.x = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x; this.position.y = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y; this.rotation = this.sprite.angle; this.preRotation = this.rotation; - if (this.updateBounds() || this.sprite._cache[4] === 1) + if (this._reset || this.sprite._cache[4] === 1) { this.prev.x = this.position.x; this.prev.y = this.position.y; @@ -412,6 +418,8 @@ Phaser.Physics.Arcade.Body.prototype = { } } + this._reset = false; + }, /** diff --git a/src/physics/arcade/World.js b/src/physics/arcade/World.js index 2baef41e5..ad007036f 100644 --- a/src/physics/arcade/World.js +++ b/src/physics/arcade/World.js @@ -340,7 +340,7 @@ Phaser.Physics.Arcade.prototype = { * @param {function} [overlapCallback=null] - An optional callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you specified them. * @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then overlapCallback will only be called if processCallback returns true. * @param {object} [callbackContext] - The context in which to run the callbacks. - * @returns {boolean} True if an overlap occured otherwise false. + * @return {boolean} True if an overlap occured otherwise false. */ overlap: function (object1, object2, overlapCallback, processCallback, callbackContext) { @@ -381,7 +381,7 @@ Phaser.Physics.Arcade.prototype = { * @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them. * @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them. * @param {object} [callbackContext] - The context in which to run the callbacks. - * @returns {boolean} True if a collision occured otherwise false. + * @return {boolean} True if a collision occured otherwise false. */ collide: function (object1, object2, collideCallback, processCallback, callbackContext) { @@ -696,8 +696,8 @@ Phaser.Physics.Arcade.prototype = { /** * An internal function. Use Phaser.Physics.Arcade.collide instead. * - * @method Phaser.Physics.Arcade#collideGroupVsTilemapLayer * @private + * @method Phaser.Physics.Arcade#collideGroupVsTilemapLayer * @param {Phaser.Group} group - The Group to check. * @param {Phaser.TilemapLayer} tilemapLayer - The layer to check. * @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them. @@ -725,13 +725,14 @@ Phaser.Physics.Arcade.prototype = { /** * The core separation function to separate two physics bodies. * + * @private * @method Phaser.Physics.Arcade#separate * @param {Phaser.Physics.Arcade.Body} body1 - The first Body object to separate. * @param {Phaser.Physics.Arcade.Body} body2 - The second Body object to separate. * @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this function is set then the sprites will only be collided if it returns true. * @param {object} [callbackContext] - The context in which to run the process callback. * @param {boolean} overlapOnly - Just run an overlap or a full collision. - * @returns {boolean} Returns true if the bodies collided, otherwise false. + * @return {boolean} Returns true if the bodies collided, otherwise false. */ separate: function (body1, body2, processCallback, callbackContext, overlapOnly) { @@ -803,11 +804,13 @@ Phaser.Physics.Arcade.prototype = { /** * The core separation function to separate two physics bodies on the x axis. + * + * @private * @method Phaser.Physics.Arcade#separateX * @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate. * @param {Phaser.Physics.Arcade.Body} body2 - The Body object to separate. * @param {boolean} overlapOnly - If true the bodies will only have their overlap data set, no separation or exchange of velocity will take place. - * @returns {boolean} Returns true if the bodies were separated, otherwise false. + * @return {boolean} Returns true if the bodies were separated, otherwise false. */ separateX: function (body1, body2, overlapOnly) { @@ -916,11 +919,13 @@ Phaser.Physics.Arcade.prototype = { /** * The core separation function to separate two physics bodies on the y axis. + * + * @private * @method Phaser.Physics.Arcade#separateY * @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate. * @param {Phaser.Physics.Arcade.Body} body2 - The Body object to separate. * @param {boolean} overlapOnly - If true the bodies will only have their overlap data set, no separation or exchange of velocity will take place. - * @returns {boolean} Returns true if the bodies were separated, otherwise false. + * @return {boolean} Returns true if the bodies were separated, otherwise false. */ separateY: function (body1, body2, overlapOnly) { @@ -1042,10 +1047,12 @@ Phaser.Physics.Arcade.prototype = { /** * The core separation function to separate a physics body and a tile. + * + * @private * @method Phaser.Physics.Arcade#separateTile * @param {Phaser.Physics.Arcade.Body} body - The Body object to separate. * @param {Phaser.Tile} tile - The tile to collide against. - * @returns {boolean} Returns true if the body was separated, otherwise false. + * @return {boolean} Returns true if the body was separated, otherwise false. */ separateTile: function (i, body, tile) { @@ -1146,11 +1153,11 @@ Phaser.Physics.Arcade.prototype = { /** * Check the body against the given tile on the X axis. * + * @private * @method Phaser.Physics.Arcade#tileCheckX - * @protected * @param {Phaser.Physics.Arcade.Body} body - The Body object to separate. * @param {Phaser.Tile} tile - The tile to check. - * @returns {number} The amount of separation that occured. + * @return {number} The amount of separation that occured. */ tileCheckX: function (body, tile) { @@ -1195,11 +1202,11 @@ Phaser.Physics.Arcade.prototype = { /** * Check the body against the given tile on the Y axis. * + * @private * @method Phaser.Physics.Arcade#tileCheckY - * @protected * @param {Phaser.Physics.Arcade.Body} body - The Body object to separate. * @param {Phaser.Tile} tile - The tile to check. - * @returns {number} The amount of separation that occured. + * @return {number} The amount of separation that occured. */ tileCheckY: function (body, tile) { @@ -1243,11 +1250,12 @@ Phaser.Physics.Arcade.prototype = { /** * Internal function to process the separation of a physics body from a tile. + * + * @private * @method Phaser.Physics.Arcade#processTileSeparationX - * @protected * @param {Phaser.Physics.Arcade.Body} body - The Body object to separate. * @param {number} x - The x separation amount. - * @returns {boolean} Returns true as a pass-thru to the separateTile method. + * @return {boolean} Returns true as a pass-thru to the separateTile method. */ processTileSeparationX: function (body, x) { @@ -1275,8 +1283,9 @@ Phaser.Physics.Arcade.prototype = { /** * Internal function to process the separation of a physics body from a tile. + * + * @private * @method Phaser.Physics.Arcade#processTileSeparationY - * @protected * @param {Phaser.Physics.Arcade.Body} body - The Body object to separate. * @param {number} y - The y separation amount. */