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. |
var returnValue = distBetweenTwoPoints(x1, y1, x2, y2); |
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 |
Value | Type | Description | Game version |
---|---|---|---|
<distance> | Number | The distance between point x1,y1 and x2,y2
| 3.1 Beta 1 |
<error> | Error | Invalid parameters | 3.1 Beta 1 |
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!
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; } |
Availability Requires:
|
ContentsJump to: |
See alsoRelated articles:
|