(info) 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

ParameterTypeMandatoryDescriptionGame version
x1Number(tick)The x co-ordinate of the first point3.1 Beta 1
y1Number(tick)The y co-ordinate of the first point3.1 Beta 1
x2Number(tick)The x co-ordinate of the second point3.1 Beta 1
y2Number(tick)The y co-ordinate of the second point3.1 Beta 1

Return value

ValueTypeDescriptionGame version
<distance>Number

The distance between point x1,y1 and x2,y2

(warning) Most likely in world coordinates in 3.1.0 and earlier.

3.1 Beta 1
<error>ErrorInvalid parameters3.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;
}
Availability 3.1 B1+

Requires:

  • Warzone 3.1 Beta 1 and above.
Contents

Jump to:

See also

Related articles: