Other great resources: Official JS API docs, Scripting Forum
distBetweenTwoPoints()
Returns the straight-line distance between two points on the map...
This article is in need of technical review - returned distance seems to have been in world coords in 3.1.0 and earlier.
Syntax
var returnValue = distBetweenTwoPoints(x1, y1, x2, y2);Parameters
Parameter | Type | Mandatory | Description | Game version |
|---|---|---|---|---|
x1 | Number | The x co-ordinate of the first point | 3.1 Beta 1 | |
y1 | Number | The y co-ordinate of the first point | 3.1 Beta 1 | |
x2 | Number | The x co-ordinate of the second point | 3.1 Beta 1 | |
y2 | Number | The y co-ordinate of the second point | 3.1 Beta 1 |
Return value
Value | Type | Description | Game version |
|---|---|---|---|
<distance> | Number | The distance between point x1,y1 and x2,y2 Most likely in world coordinates in 3.1.0 and earlier. | 3.1 Beta 1 |
<error> | Error | Invalid parameters | 3.1 Beta 1 |
Notes
This function doesn't take in to account the path that a droid has to take to get to the destination point, it just calculates distance in a straight line.
If there are terrain obstacles like a lakes or cliffs between the two points the function completely ignores them. As a result, the actual distance could be much longer because a droid might have to take a very long root to reach it's destination. In other words, something might seem close but your droid might have to travel half way round the map to get to it!
Example
Find nearest oil resource
function findNearestOilTo(gameObj) {
var nearest = {dist:Number.MAX_VALUE, oil:null};
var oils = enumFeature(-1, "OilResource");
var dist;
oils.forEach( function(oil) {
dist = distanceBetweenTwoPoints( oil.x, oil.y, gameObj.x, gameObj.y );
if ( dist < nearest.dist ) {
nearest.dist = dist;
nearst.oil = oil;
}
} );
return nearest.oil;
}