Jump to content

BitmapData Circle collision


mfcv
 Share

Recommended Posts

I'm drawing circles at random places anywhere across the game with this code in the create() function:

bmd = game.add.bitmapData(width, height);game.add.sprite(0, 0, bmd);

And this code is run when it's time to draw the expanding circle (radius and alpha is increasing until a certain point):

   bmd.clear();   bmd.circle(star.x, star.y, radius, 'rgba(122, 45, 255,' + alpha + ')');

I wanna check for collisions with other sprites, but only with the circle. Not with the bitmapData that is covering the whole screen for obvious reasons. How can I do that?

Link to comment
Share on other sites

You will probably have to write a custom function which uses a Phaser.Circle object and the Phaser.Circle.intersectsRectangle method to check against the body (which has the same properties as a rectangle) of each sprite. Something like this (however for your code I'd suggest making the circle just once, and then adjusting its radius property every time you want to do a check):

// create a circle (note the third parameter is diameter, not radius)var collisionCircle = new Phaser.Circle(star.x, star.y, radius * 2);bmd.clear();bmd.circle(star.x, star.y, radius, 'rgba(122, 45, 255,' + alpha + ')');spritesGroup.forEach(function(sprite) {  if (Phaser.Circle.intersectsRectangle(collisionCircle, sprite.body)) {    // this sprite is colliding with the circle  }});
Link to comment
Share on other sites

I have a feeling collision detection with an unlimited length trail would get quite CPU intensive, as it's essentially per-pixel collision detection, or you'd have to create some kind of grid of colliders and optimise it with a QuadTree.

 

If, instead of drawing the trail to a BitmapData object, you created a pool of small sprites behind the ship and you could then detect against those - but this obviously limits you if what you want is a permanent trail (sort of like Tron light cycles).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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