David Posted May 19, 2014 Share Posted May 19, 2014 I have turrets that need to react only to things within a certain distance. Of course, this could be done simply by checking the distance of every targetable object with every turret, but considering the amount of things I want to have on screen, the number of checks could get quite high. My current implementation uses an invisible "detection range" sprite with a custom collision processor in order to take advantage of the quad tree I'm sure this isn't a unique problem so I wanted to see how others handled this. Did you go the collision route? Did you just do a simple distance check against all objects without any real performance issues? Did you do something else that I'm not thinking of? Link to comment Share on other sites More sharing options...
Heppell08 Posted May 19, 2014 Share Posted May 19, 2014 Use distanceBetween. It is(was) in phaser because I used it myself in a test project.I'm pretty certain this should still be in phaser too.Load up phaser in your editor of choice and search for distanceBetween. I'm not sure if its in arcade/P2 or ninja but it might still be there.I say might because I built that game in 1.1.4 phaser and use distanceBetween myself. Link to comment Share on other sites More sharing options...
lewster32 Posted May 19, 2014 Share Posted May 19, 2014 Phaser.Physics.Arcade.distanceBetween still exists, however it seems to be a bit of a holdover from old versions what I can see. Phaser.Point.distance also does the same, except it explicitly takes Phaser.Point objects (if you're using TypeScript to enforce types) like so:turret.reactionDistance = 50; // distance in pixelsif (Phaser.Point.distance(turret.position, target.position) <= turret.reactionDistance) { turret.react();} Link to comment Share on other sites More sharing options...
XekeDeath Posted May 19, 2014 Share Posted May 19, 2014 I used a Phaser.Circle as the range, and checked the x/y position of other elements with the contains(x,y) function. lewster32 1 Link to comment Share on other sites More sharing options...
lewster32 Posted May 20, 2014 Share Posted May 20, 2014 Neat, I didn't know about Phaser.Circle.contains - looks like this is more efficient as well, as it does a fast rectangular bounds check first, and doesn't require a square route calculation. Link to comment Share on other sites More sharing options...
Recommended Posts