diff --git a/examples/assets/sprites/fruitnveg32wh37.png b/examples/assets/sprites/fruitnveg32wh37.png index 5d371cbf..4f835727 100644 Binary files a/examples/assets/sprites/fruitnveg32wh37.png and b/examples/assets/sprites/fruitnveg32wh37.png differ diff --git a/examples/assets/sprites/fruitnveg64wh37.png b/examples/assets/sprites/fruitnveg64wh37.png new file mode 100644 index 00000000..33e28193 Binary files /dev/null and b/examples/assets/sprites/fruitnveg64wh37.png differ diff --git a/examples/collision_sprite_vs_group.php b/examples/collision_sprite_vs_group.php new file mode 100644 index 00000000..4f2fac73 --- /dev/null +++ b/examples/collision_sprite_vs_group.php @@ -0,0 +1,100 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/examples/collision_sprite_vs_sprite_custom_process.php b/examples/collision_sprite_vs_sprite_custom_process.php index fe67c09a..f691580c 100644 --- a/examples/collision_sprite_vs_sprite_custom_process.php +++ b/examples/collision_sprite_vs_sprite_custom_process.php @@ -30,20 +30,25 @@ // This will check Sprite vs. Sprite collision using a custom process callback - sprite1 = game.add.sprite(50, 200, 'atari'); + sprite1 = game.add.sprite(0, 200, 'atari'); sprite1.name = 'atari'; - sprite1.body.velocity.x = 100; + // We'll use a random velocity here so we can test it in our processHandler + sprite1.body.velocity.x = 50 + Math.random() * 100; + // This tells phaser to not use the built-in body separation, instead you should handle it in your process callback (see below) + sprite1.body.customSeparateX = true; - sprite2 = game.add.sprite(700, 220, 'mushroom'); + sprite2 = game.add.sprite(750, 220, 'mushroom'); sprite2.name = 'mushroom'; - sprite2.body.velocity.x = -100; + // We'll use a random velocity here so we can test it in our processHandler + sprite2.body.velocity.x = -(50 + Math.random() * 100); + // This tells phaser to not use the built-in body separation, instead you should handle it in your process callback (see below) + sprite2.body.customSeparateX = true; } function update() { - // object1, object2, collideCallback, processCallback, callbackContext - game.physics.collide(sprite1, sprite2, collisionHandler, null, this); + game.physics.collide(sprite1, sprite2, collisionHandler, processHandler, this); } @@ -53,7 +58,23 @@ // For example you could test for velocity, health, etc. // If you want the collision to be deemed successful this function must return true. // In which case the collisionHandler will be called, otherwise it won't. - // Note: the objects will have already collided and separated by this point. + + // Note: the objects will have already been separated by this point unless you have set + // their customSeparateX/Y flags to true. If you do that it's up to you to handle separation. + + // Whichever one is going fastest wins, the other dies :) + if (obj1.body.velocity.x > Math.abs(obj2.body.velocity.x)) + { + obj2.kill(); + obj1.body.velocity.x = 0; + } + else + { + obj1.kill(); + obj2.body.velocity.x = 0; + } + + return true; } diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 6750ae3e..1497024f 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -167,6 +167,9 @@ Phaser.Sprite = function (game, x, y, key, frame) { // Set-up the physics body this.body = new Phaser.Physics.Arcade.Body(this); + this.velocity = this.body.velocity; + this.acceleration = this.body.acceleration; + // World bounds check this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds); this.inWorldThreshold = 0; diff --git a/src/physics/arcade/ArcadePhysics.js b/src/physics/arcade/ArcadePhysics.js index 94025a5a..9b737e96 100644 --- a/src/physics/arcade/ArcadePhysics.js +++ b/src/physics/arcade/ArcadePhysics.js @@ -424,11 +424,12 @@ Phaser.Physics.Arcade.prototype = { */ separate: function (body1, body2) { - if (this.separateX(body1, body2) || this.separateY(body1, body2)) + this._result = (this.separateX(body1, body2) || this.separateY(body1, body2)); + + if (this._result) { body1.postUpdate(); body2.postUpdate(); - this._result = true; } }, @@ -496,12 +497,21 @@ Phaser.Physics.Arcade.prototype = { // 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; @@ -510,6 +520,7 @@ Phaser.Physics.Arcade.prototype = { 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; } @@ -595,12 +606,21 @@ Phaser.Physics.Arcade.prototype = { // 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; @@ -609,6 +629,7 @@ Phaser.Physics.Arcade.prototype = { 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; } @@ -616,6 +637,7 @@ Phaser.Physics.Arcade.prototype = { { 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())) { @@ -626,6 +648,7 @@ Phaser.Physics.Arcade.prototype = { { 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())) { diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index b8242bbb..9ebe509e 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -49,6 +49,17 @@ Phaser.Physics.Arcade.Body = function (sprite) { this.allowRotation = true; this.allowGravity = true; + // These two flags allow you to disable the custom separation that takes place + // Used in combination with your own collision processHandler you can create whatever + // type of collision response you need. + this.customSeparateX = false; + this.customSeparateY = false; + + // When this body collides with another the amount of overlap is stored in here + // These values are useful if you want to provide your own custom separation logic. + this.overlapX = 0; + this.overlapY = 0; + this.collideWorldBounds = false; this.lastX = sprite.x;