Jump to content

Detect collision and overlap?


casarock
 Share

Recommended Posts

Hi,

 

I want to detect a collision or better sayed overlapping of a sprite with a group. I wasn't able to overlap the objects using the game.physic.collide method. I always  get a collision and my "character" stops moving. I used a code inspiered by collision detection examples:

(function () {    var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });        function preload() {        game.load.image('phaser', 'phaser-dude.png');        game.load.spritesheet('veggies', 'fruitnveg64wh37.png', 64, 64);    }    var sprite;    var veggies;    function create() {        game.stage.backgroundColor = '#2d2d2d';        //  This will check Group vs. Group collision (bullets vs. veggies!)        veggies = game.add.group();        for (var i = 0; i < 50; i++)        {            var c = veggies.create(game.world.randomX, Math.random() * 500, 'veggies', game.rnd.integerInRange(0, 36));            c.name = 'veg' + i;            c.body.immovable = true;            //c.body.allowCollision = false;            //c.body.touching = { none: false, up: true, down: true, left: true, right: true };        }        sprite = game.add.sprite(400, 550, 'phaser');    }    function update() {        sprite.velocity.x = 0;        sprite.velocity.y = 0;        if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))        {            sprite.velocity.x = -200;        }        else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))        {            sprite.velocity.x = 200;        }        else if (game.input.keyboard.isDown(Phaser.Keyboard.UP))        {            sprite.velocity.y = -200;        }        else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))        {            sprite.velocity.y = 200;        }        game.physics.collide(sprite, veggies, collisionHandler, null, this);    }    function collisionHandler(sprite, veg) {        console.log(sprite, veg);    }})();

as you could see I've experimented with allowcollison (colision could be deactivated easily) and body.touching. 

 

It seems like I am doing it wrong. :) I am not able to detect overlapping/collision. Or do I have to implement this by my own routine? Before I start I just want to be sure if phaser could support me ;)

 

Thanks!

 

 

 

Link to comment
Share on other sites

Right - exactly as cang said. However I'm redoing the collision system at the moment and have included a straight 'overlaps' check with separation optional. The current quadtree implementation isn't correctly updated for objects in Groups. You can checkout the dev branch and you'll see the progress I've been making. I need to resolve some tilemap issues and then it will be part of the 1.0.7 release towards the end of next week.

Link to comment
Share on other sites

Sorry I wasn't clear enough.

 

I assume you want to do it on your veggie objects so it would be something like this.

        for (var i = 0; i < 50; i++)        {            var c = veggies.create(game.world.randomX, Math.random() * 500, 'veggies', game.rnd.integerInRange(0, 36));            c.name = 'veg' + i;            c.body.immovable = true;            c.body.customSeparateX = true;            c.body.customSeparateY = true;        }

Now when you call the collide method it won't perform that separation logic. :)

 

Edit: Ah didn't notice the person who asked the question was different than the OP. Anyway example still applies.

Link to comment
Share on other sites

  • 3 weeks later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...