Jump to content

Collisions within a group


Nechro
 Share

Recommended Posts

I have a group of 'GreenSquares' and I'm wondering how I can check whether any GreenSquare within the group is touching another GreenSquare.

 

I'm also wondering if there's a way I could check if a single GreenSquare is touching not just one, but two other GreenSquares.

 

Any advice at all would be appreciated.

Link to comment
Share on other sites

If you're using Arcade physics, you can do an overlap test on the group:

function update() {  game.physics.arcade.overlap(GreenSquares, null, function(GreenSquare1, GreenSquare2) {    console.log("Touching: ", GreenSquare1, GreenSquare2);  });}

Unfortunately, each overlap/collision callback is discreet and between two objects, so you'd need to calculate this yourself manually, maybe with a counter (untested code):

function update() {  // Set the touchCount (a custom property we'll use to count touches) for each square to 0  GreenSquares.setAll("touchCount", 0, false, false, 0, true);  game.physics.arcade.overlap(GreenSquares, null, function(GreenSquare1, GreenSquare2) {    // Increment the touchCount for the first square involved in this overlap test    GreenSquare1.touchCount++;    console.log("Touching: ", GreenSquare1, GreenSquare2);  });  // Log an array of all squares touching 2 other squares  console.log(getSquaresTouching(2));}function getSquaresTouching(count) {  // Filter the group children array to return only those with a touchCount of <count> and return it  return GreenSquares.children.filter(function(GreenSquare) { return GreenSquare.touchCount === count; })}
Link to comment
Share on other sites

Thanks Lewster, I was thinking of something like that but had no idea how to implement it in phaser. Unfortunately I've moved from arcade physics to p2 so I'm not sure how much it will apply as there isn't an explicit collide function that I can get the callback from.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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