Collision detection of two groups

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.physics.enable(testa, Phaser.Physics.ARCADE);		testa.enableBody = true;		testa.create(400,100,'block');		testb =;		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");}

Share this post

Link to post
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.

Share this post

Link to post
Share on other sites

@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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.