Tarion Posted January 5, 2014 Share Posted January 5, 2014 Hi,I'm going to build a little game. I'm looking for a ways to count objects in a specific area (around my sprite). Is there anything build in? Link to comment Share on other sites More sharing options...
Tarion Posted January 5, 2014 Author Share Posted January 5, 2014 I just put them into an array and itereate myself for now Link to comment Share on other sites More sharing options...
ragnarok Posted January 5, 2014 Share Posted January 5, 2014 Phaser has a QuadTree implementation (/phaser/docs/Phaser.QuadTree.html), which might be helpful with your problem.Haven't tried it yet, though. Should look something like that: var qTree = new Phaser.QuadTree(game.physics, 0, 0, game.world.x, game.world.y, enemy.length, 4, 1); qTree.insert(enemy[0].body); qTree.insert(enemy[1].body); qTree.insert(enemy[2].body);var near_enemies=qTree.retrieve(Phaser.Rectangle(player.x-50,player.y-50,player.width+50,player.height+50); The project that the phaser-QuadTree-code is (according to the docs) based upon has a quite understandable overview about how to generally use it...https://github.com/timohausmann/quadtree-js/...and a good linked article that explains what's going on if this is your first use of QuadTrees: http://gamedevelopment.tutsplus.com/tutorials/quick-tip-use-quadtrees-to-detect-likely-collisions-in-2d-space--gamedev-374 Link to comment Share on other sites More sharing options...
Tarion Posted January 6, 2014 Author Share Posted January 6, 2014 Thanks for the hint, but "retrieve(sprite: Object)" takes an sprite and checks only based on the sprites body size. So passing a rect would not work without further tweaking of the QuadTree but I will consider it when I get performance issues. Until then it would be nice, to provide some generic quadtree, that not only works on sprite but every object hat implements a IRectangle or something. If there is demand on that I would also write one for Phaser. Link to comment Share on other sites More sharing options...
XekeDeath Posted January 6, 2014 Share Posted January 6, 2014 You could use an invisible sprite to define the area you want to check in, and use physics.overlap to check other sprites inside it. samme 1 Link to comment Share on other sites More sharing options...
samme Posted August 6, 2016 Share Posted August 6, 2016 The invisible sprite method is good. I've also used var distanceBetween = Phaser.Physics.Arcade.prototype.distanceBetween; Phaser.Physics.Arcade.Body.prototype.distanceTo = function(target) { return distanceBetween(this.center, target.body.center); }; Phaser.Physics.Arcade.Body.prototype.isBeyond = function(range, target) { return this.distanceTo(target) > range; }; Phaser.Physics.Arcade.Body.prototype.isWithin = function(range, target) { return this.distanceTo(target) <= range; }; Link to comment Share on other sites More sharing options...
Recommended Posts