Jump to content

Collision detection of two groups


Bertram
 Share

Recommended Posts

I can't seem to figure this one out... I have two groups, and I want to detect when they are colliding. But the test alert function is never triggered.

 

I thought this should be possible?

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update});var testa, testb;function preload() {    game.load.image('block', 'test.png');}function create() {    game.physics.startSystem(Phaser.Physics.ARCADE);		        testa = game.add.group();		game.physics.enable(testa, Phaser.Physics.ARCADE);		testa.enableBody = true;		testa.create(400,100,'block');		testb = game.add.group();		game.physics.enable(testb, Phaser.Physics.ARCADE);		testb.enableBody = true;		testb.create(400,200,'block');				cursors = game.input.keyboard.createCursorKeys();				cursors.up.onDown.add(function(){			testb.y -=10;		}, this);	}function update() {     game.physics.arcade.collide(testa, testb, test);   }function test(){    alert("HIT");}
Link to comment
Share on other sites

May be because of the specifics of the physics engine? When using collide - objects are automatically separated. When using overlap - they does not. Additionally you are using direct movement setting object's y property, instead of using velocity.

Link to comment
Share on other sites

  • 3 weeks later...

@stasuss is correct.  Your collisions will not work properly if you directly manipulate the position vs setting the velocity or acceleration.

 

Here's a link to a solution someone provided for a similar issue someone was having (they were tweening, but in the end this is just directly manipulating the position value).

Also you can see Rich's explanation here:

 

...if you are tweening the sprite x/y values directly then its entirely possible you end up translating the sprite directly into another one and the collision code never picks this up, because it checks velocity and position and then separates based on those values, but tweening them directly would break the separation.

 

Another possible solution could be using come from using customSeparateX/Y.  However, I haven't actually used this yet.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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