For those of you who haven't been following along, take a peek at this demo HERE

var enemyArray:Array = new Array();

function enterFrameLoop(event):void
{ var closestEnemy:Number = 25000000; for (var i:int = 0; i < enemyArray.length; i++) { var currentEnemy:Object = enemyArray[i]; var dist:Number = Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2);

if (dist < closestEnemy) { closestEnemy = dist;

//use currentEnemy for whatever you want } }
}

last edited May 28 2014 07:33 pm by weirdlike

For those of you who haven't been following along, take a peek at this demo [url=http://armorgames.com/community/thread/12297297/as3classeseasy-level-editor]HERE[/url]
var enemyArray:Array = new Array();
function enterFrameLoop(event):void
{
var closestEnemy:Number = 25000000;
for (var i:int = 0; i < enemyArray.length; i++)
{
var currentEnemy:Object = enemyArray[i];
var dist:Number = Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2);
if (dist < closestEnemy)
{
closestEnemy = dist;
//use currentEnemy for whatever you want
}
}
}

Considering you don't really need the euclidean distance specifically, why don't you just compare distÂ² instead? It saves you the potentially expensive square root when you have a ton of enemies.

Considering you don't really need the euclidean distance specifically, why don't you just compare distÂ² instead? It saves you the potentially expensive square root when you have a ton of enemies.

See, for your function, you don't necessarily need to calculate the exact distance between objects. All you really need, is a value that's small for close enemies and large for enemies that are far away.

Keeping this in mind, you notice that you don't need to use the sqrt-operation, because Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2) alone is still going to be small for close objects and big for far-away objects.

Mind you, if you need to use the exact distance for something else later on, you'd use your example. But just to test which object is closest, the square root is not needed.

Sure, let me try.\r\n\r\nSee, for your function, you don't necessarily need to calculate the exact distance between objects. All you really need, is a value that's small for close enemies and large for enemies that are far away.\r\n\r\nKeeping this in mind, you notice that you don't need to use the sqrt-operation, because Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2) alone is still going to be small for close objects and big for far-away objects.\r\n\r\nMind you, if you need to use the exact distance for something else later on, you'd use your example. But just to test which object is closest, the square root is not needed.

tested and it does work, one thing to note tho is the max distance to check for will have to be increased in order to keep the same range.

Here are the results:

var original:Number = Math.sqrt(Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2));

var new:Number = Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2);

180.47636964433872 - original 32571.719999999987 - new

Thanks for the tip @KentyBK I will have to do some more testing to see the full effect.

tested and it does work, one thing to note tho is the max distance to check for will have to be increased in order to keep the same range. \r\n\r\nHere are the results:\r\n\r\nvar original:Number = Math.sqrt(Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2));\r\n\r\nvar new:Number = Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2);\r\n\r\n180.47636964433872 - original\r\n32571.719999999987 - new\r\n\r\n[img]http://i.imgur.com/CPRuQ9v.jpg[/img]\r\n\r\nThanks for the tip [url=http://armorgames.com/user/KentyBK]@KentyBK[/url] I will have to do some more testing to see the full effect.

Then I suggest using 25,000,000 as the max, i.e 5000Â².

I mean, if you're comparing without the square root, it only makes sense to square the range instead.

Then I suggest using 25,000,000 as the max, i.e 5000Â².\r\n\r\nI mean, if you're comparing without the square root, it only makes sense to square the range instead.

Well it would appear that I was able to increase the max enemy amount on my game there by about 20 units by eliminating that extra bit.

Edited the original post

Well it would appear that I was able to increase the max enemy amount on my game there by about 20 units by eliminating that extra bit. \r\n\r\nEdited the original post ;)

You can make it slightly faster by doing this and save on one iteration: (Although trivial gain)

var currentEnemy:Object = enemyArray[i];

var closestEnemy:Number = Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2);

for (var i:int = 1; i < enemyArray.length; i++) { currentEnemy:Object = enemyArray[i]; dist:Number = Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2);

Although in larger scale applications where you are dealing in the thousands or even millions, you need to index and make grids on the map.

You can make it slightly faster by doing this and save on one iteration: (Although trivial gain)\r\n\r\n\r\n var currentEnemy:Object = enemyArray[i];\r\n\r\n var closestEnemy:Number = Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2);\r\n\r\n for (var i:int = 1; i < enemyArray.length; i++)\r\n {\r\n currentEnemy:Object = enemyArray[i];\r\n dist:Number = Math.pow(hero.x-currentEnemy.x,2)+Math.pow(hero.y-currentEnemy.y,2);\r\n\r\n\r\n\r\nAlthough in larger scale applications where you are dealing in the thousands or even millions, you need to index and make grids on the map.