titmael Posted May 13, 2014 Share Posted May 13, 2014 Hi, I wonder if there is any tool in Phaser to find sprites in a square/circle (for example all sprite in a circle with radius 100px and coordinates x, y) ? Thx Link to comment Share on other sites More sharing options...
Videlais Posted May 13, 2014 Share Posted May 13, 2014 Hi, titmael. Have you look at the Phaser examples? Specifically, have you looked at the 'Overlap Without Physics' example? You can create a new Phaser.Rectangle object and use its functions to check for overlap and if a certain set of coordinates are within that rectangle. Worst case, you can do the math yourself, checking if some coordinates are within a calculated circle too. Link to comment Share on other sites More sharing options...
titmael Posted May 13, 2014 Author Share Posted May 13, 2014 Clever idea ! But that means I have to change my sprite body shape (and I don't want to ) ? Link to comment Share on other sites More sharing options...
Videlais Posted May 13, 2014 Share Posted May 13, 2014 Clever idea ! But that means I have to change my sprite body shape (and I don't want to ) ? Not necessarily. You can add something like a 'triggerBox' property that is a Phaser.Rectangle and use that instead.var spriteA = this.game.add.sprite(0,0, cacheKey);spriteA.triggerBox = new Phaser.Rectangle(x, y, width, height);var spriteB = this.game.add.sprite(0,0, cacheKey);And then, to check, something like the following:if(spriteA.triggerBox.intersects(spriteB.getBounds())) { // spriteB overlaps spriteA.triggerBox's rectangle} Link to comment Share on other sites More sharing options...
lewster32 Posted May 13, 2014 Share Posted May 13, 2014 The Phaser.Math.distance method could be used for finding objects within a radius. In basic use you'd just loop through all of the sprites you want to check and do a comparison between their positions and the center point to get how far away they are from it. It's a pretty fast method so it should work fine even with lots of sprites, but if you're keen on optimisation then you'll maybe want to look into 'area of interest' techniques, QuadTrees and so on.var centerPoint = new Phaser.Point(50, 50);var triggerDistance = 100;spriteGroup.forEachAlive(function(sprite) { if (Phaser.Math.distance(sprite.x, sprite.y, centerPoint.x, centerPoint.y) <= triggerDistance) { // do something to this sprite, as it lies within the trigger distance }}); Link to comment Share on other sites More sharing options...
titmael Posted May 14, 2014 Author Share Posted May 14, 2014 Thanks for your both solutions, I guess for little amount of sprites it's better to use the math way Link to comment Share on other sites More sharing options...
Recommended Posts