Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Check Sprites/Objects in range


Tarion
 Share

Recommended Posts

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

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

  • 2 years later...

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

 Share

  • Recently Browsing   0 members

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