mirror of
https://github.com/wassname/phaser.git
synced 2026-08-11 11:23:06 +08:00
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. 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.
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -84,6 +84,6 @@ function collisionHandler (obj1, obj2) {
|
||||
|
||||
function render () {
|
||||
|
||||
game.debug.renderQuadTree(game.physics.quadTree);
|
||||
// game.debug.renderQuadTree(game.physics.quadTree);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, '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');
|
||||
sprite1.name = 'atari';
|
||||
// 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(750, 220, 'mushroom');
|
||||
sprite2.name = 'mushroom';
|
||||
// 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() {
|
||||
|
||||
game.physics.collide(sprite1, sprite2, collisionHandler, processHandler, this);
|
||||
|
||||
}
|
||||
|
||||
function processHandler (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.
|
||||
// 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 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;
|
||||
|
||||
}
|
||||
|
||||
function collisionHandler (obj1, obj2) {
|
||||
|
||||
game.stage.backgroundColor = '#992d2d';
|
||||
|
||||
console.log(obj1.name + ' collided with ' + obj2.name);
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user