Skip to content

Commit

Permalink
The way the collision process callback works has changed significantl…
Browse files Browse the repository at this point in the history
…y and now works as originally intended.

The World level quadtree is no longer created, they are now built and ripped down each time you collide a Group, this helps collision accuracy.
Bodies are no longer added to a world quadtree, so have had all of their quadtree properties removed such as skipQuadtree, quadTreeIndex, etc.
QuadTree.populate - you can pass it a Group and it'll automatically insert all of the children ready for inspection.
Removed ArcadePhysics binding to the QuadTree, so it can now be used independantly of the physics system.
  • Loading branch information
photonstorm committed Jan 14, 2014
1 parent 17af96d commit 011d2d8
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 625 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Significant API changes:
* Tween.onStartCallback and onCompleteCallback have been removed to avoid confusion. You should use the onStart, onLoop and onComplete events instead.
* Button.forceOut default value has changed from true to false, so Buttons will revert to an Up state (if set) when pressed and released.
* Body.drag has been removed. Please use the new Body.friction value instead (which is a number value, not a Point object)
* The way the collision process callback works has changed significantly and now works as originally intended.
* The World level quadtree is no longer created, they are now built and ripped down each time you collide a Group, this helps collision accuracy.
* Bodies are no longer added to a world quadtree, so have had all of their quadtree properties removed such as skipQuadtree, quadTreeIndex, etc.


New features:
Expand Down Expand Up @@ -85,6 +88,7 @@ New features:
* Body.speed - the current speed of the body.
* Body.friction - This now replaces Body.drag and provides for a much smoother friction experience.
* Body.sleeping - A Physics Body can now be set to 'go to sleep' if the velocity drops between the given range (sleepMin and sleepMax) for the given period of sleepDuration (see the new examples).
* QuadTree.populate - you can pass it a Group and it'll automatically insert all of the children ready for inspection.


New Examples:
Expand Down Expand Up @@ -143,6 +147,7 @@ Updates:
* Added StateManager.getCurrentState to return the currently running State object (thanks Niondir)
* Removed the console.log redirect from Utils as it was messing with Firefox.
* Body.acceleration is now much smoother and less eratic at high speeds.
* Removed ArcadePhysics binding to the QuadTree, so it can now be used independantly of the physics system.


Bug Fixes:
Expand Down
65 changes: 65 additions & 0 deletions examples/collision/process callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });

function preload() {

game.load.image('atari', 'assets/sprites/atari130xe.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');

}

var sprite1;
var sprite2;

function create() {

game.stage.backgroundColor = '#2d2d2d';

// This will check Sprite vs. Sprite collision using a custom process callback
sprite1 = game.add.sprite(0, 200, 'atari');
sprite2 = game.add.sprite(750, 220, 'mushroom');

// We'll use random velocities so we can test it in our processCallback
sprite1.body.velocity.x = 50 + Math.random() * 100;
sprite2.body.velocity.x = -(50 + Math.random() * 100);

}

function update() {

game.physics.collide(sprite1, sprite2, collisionCallback, processCallback, this);

}

function processCallback (obj1, obj2) {

// This function can perform your own additional checks on the 2 objects that collided.
// For example you could test for velocity, health, etc.
// This function needs to return either true or false. If it returns true then collision carries on (separating the two objects).
// If it returns false the collision is assumed to have failed and aborts, no further checks or separation happen.

if (obj1.body.speed > obj2.body.speed)
{
return true;
}
else
{
return false;
}

}

function collisionCallback (obj1, obj2) {

game.stage.backgroundColor = '#992d2d';

}

function render() {

game.debug.renderText('The processCallback will only collide if sprite1 is going fastest.', 32, 32);
game.debug.renderText('Sprite 1 speed: ' + sprite1.body.speed, 32, 64);
game.debug.renderText('Sprite 2 speed: ' + sprite2.body.speed, 32, 96);

}

2 changes: 1 addition & 1 deletion examples/collision/sprite vs group.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ function collisionHandler (obj1, obj2) {

function render () {

game.debug.renderQuadTree(game.physics.quadTree);
// game.debug.renderQuadTree(game.physics.quadTree);

}
78 changes: 0 additions & 78 deletions examples/collision/sprite vs sprite custom.js

This file was deleted.

3 changes: 1 addition & 2 deletions examples/collision/sprite vs sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ function update() {

function collisionHandler (obj1, obj2) {

// The two sprites are colliding
game.stage.backgroundColor = '#992d2d';

console.log(obj1.name + ' collided with ' + obj2.name);

}
12 changes: 4 additions & 8 deletions examples/collision/vertical collision.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ function create() {
sprite1.body.velocity.y = 100;

// This adjusts the collision body size.
// 100x100 is the new width/height.
// 220x10 is the new width/height.
// See the offset bounding box for another example.
sprite1.body.setSize(220, 50, 0, 0);
sprite1.body.setSize(220, 10, 0, 0);

sprite2 = game.add.sprite(400, 500, 'mushroom');
sprite2 = game.add.sprite(400, 450, 'mushroom');
sprite2.name = 'mushroom';
sprite2.body.immovable = true;
// sprite2.body.velocity.x = -100;

}

Expand All @@ -42,17 +41,14 @@ function collisionHandler (obj1, obj2) {

game.stage.backgroundColor = '#992d2d';

console.log(obj1.name + ' collided with ' + obj2.name);

}

function render() {

game.debug.renderSpriteInfo(sprite1, 32, 32);
game.debug.renderSpriteCollision(sprite1, 32, 400);
game.debug.renderSpriteCollision(sprite1, 400, 32);

game.debug.renderSpriteBody(sprite1);
game.debug.renderSpriteBody(sprite2);

}

1 change: 1 addition & 0 deletions src/core/Group.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ Phaser.Group.prototype = {
* Allows you to call your own function on each member of this Group. You must pass the callback and context in which it will run.
* After the checkExists parameter you can add as many parameters as you like, which will all be passed to the callback along with the child.
* For example: Group.forEach(awardBonusGold, this, true, 100, 500)
* Note: Currently this will skip any children which are Groups themselves.
*
* @method Phaser.Group#forEach
* @param {function} callback - The function that will be called. Each child of the Group will be passed to it as its first parameter.
Expand Down
Loading

0 comments on commit 011d2d8

Please sign in to comment.