distance: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
m (Text replacement - "↵ ↵|" to " |")
 
(113 intermediate revisions by 23 users not shown)
Line 1: Line 1:
[[Category:Scripting Commands]]
{{RV|type=command
[[Category:Scripting Commands OFP 1.97]]
[[Category:Scripting Commands OFP 1.46]]
[[Category:Scripting Commands ArmA]]


back to [[Scripting_Reference#D|COMREF]]
|game1= ofp
|version1= 1.00


<h2 style="color:#000066">'''''obj1'' distance ''obj2'''''</h2>
|game2= ofpe
|version2= 1.00


|game3= arma1
|version3= 1.00


'''Operand types:'''
|game4= arma2
|version4= 1.00


'''obj1:''' [[Object]]
|game5= arma2oa
|version5= 1.50


'''obj2:''' [[Object]]
|game6= tkoh
|version6= 1.00


'''Type of returned value:'''
|game7= arma3
|version7= 0.50


[[Number]]
|arg= global


'''Description:'''
|gr1= Math - Geometry


Compute distance between the two objects.
|descr= [[File:distance2D.jpg|400px|right]]
Returns a distance in meters between [[Object]]s, [[Position]]s or [[Location]]s.
{{Feature | important | If positions are supplied as arguments, the coordinates are treated as [[Position#PositionAGL|PositionAGL]]. If [[Position#Introduction|Position2D]] is supplied, z is assumed 0. If you need distance between two [[Position#Position3D|Position3D]] coordinates, use [[vectorDistance]].}}


|s1= param1 [[distance]] param2


'''Example:'''
|p1= param1: [[Object]] or [[Array]] in format [[Position#PositionAGL|PositionAGL]] or [[Position#Introduction|Position2D]]


_dist = [[player]] '''distance''' [[leader]] [[player]]
|p2= param2: [[Object]] or [[Array]] in format [[Position#PositionAGL|PositionAGL]] or [[Position#Introduction|Position2D]]
 
|r1= [[Number]] - distance in meters or 1e10 if distance cannot be calculated
 
|s2= location1 [[distance]] location2
 
|p21= location1: [[Location]] or [[Array]] in format [[Position]] or [[Position#Introduction|Position2D]]
 
|p22= location2: [[Location]] or [[Array]] in format [[Position]] or [[Position#Introduction|Position2D]]
 
|r2= [[Number]] - distance in meters or 1e10 if distance cannot be calculated
 
|s3= a [[distance]] b
|s3since= arma3 2.18
 
|p41= a: [[Number]]
|p42= b: [[Number]]
 
|r3= [[Number]] - distance c calculated as c = sqrt(a * a + b * b);
 
 
|x1= <sqf>_meters = player distance _object;</sqf>
 
|x2= <sqf>_meters = player distance [1,2,3];</sqf>
 
|x3= <sqf>_meters = [1,2,3] distance [4,5,6];</sqf>
 
|x4= <sqf>_meters = position player distance nearestLocation [position player, "hill"];</sqf>
 
|seealso= [[distanceSqr]] [[vectorDistance]] [[distance2D]]
}}
 
{{Note
|user= Sudden Death
|timestamp= 20070214164700
|text= Distance to position3D (array) does not work with {{ofp}}, only objects
 
Armed Assault:
position3D (array) and object works
}}
 
{{Note
|user= Rommel
|timestamp= 20100310075400
|text= This returns the map distance, not the vector distance, [0,0,0] distance [0,0,1] can be >1 or <1, not exactly 1; unlike the vector math.
}}
 
{{Note
|user= Igneous01
|timestamp= 20120803101500
|text= If a position in format [x, y, z] is provided for both arguments, distance also checks the z dimension as well. Ie:
player distance [0,0,200]
will return 200, if the player is at this position at ground/sea level.
}}
 
{{Note
|user= AgentRev01
|timestamp= 20140323025200
|text= When objects are supplied as arguments, distance is calculated from their model center (object modelToWorld [0,0,0]), and not the position returned by getPos/ATL/ASL.<br>
However, this is not relevant for units, as their model center matches their world position.
}}

Latest revision as of 13:45, 8 April 2024

Hover & click on the images for description

Description

Description:
distance2D.jpg

Returns a distance in meters between Objects, Positions or Locations.

If positions are supplied as arguments, the coordinates are treated as PositionAGL. If Position2D is supplied, z is assumed 0. If you need distance between two Position3D coordinates, use vectorDistance.
Groups:
Math - Geometry

Syntax 1

Syntax:
param1 distance param2
Parameters:
param1: Object or Array in format PositionAGL or Position2D
param2: Object or Array in format PositionAGL or Position2D
Return Value:
Number - distance in meters or 1e10 if distance cannot be calculated

Syntax 2

Syntax:
location1 distance location2
Parameters:
location1: Location or Array in format Position or Position2D
location2: Location or Array in format Position or Position2D
Return Value:
Number - distance in meters or 1e10 if distance cannot be calculated

Syntax 3

Syntax:
a distance b
Parameters:
a: Number
b: Number
Return Value:
Number - distance c calculated as c = sqrt(a * a + b * b);

Examples

Example 1:
_meters = player distance _object;
Example 2:
_meters = player distance [1,2,3];
Example 3:
_meters = [1,2,3] distance [4,5,6];
Example 4:
_meters = position player distance nearestLocation [position player, "hill"];

Additional Information

See also:
distanceSqr vectorDistance distance2D

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Sudden Death - c
Posted on Feb 14, 2007 - 16:47 (UTC)
Distance to position3D (array) does not work with Operation Flashpoint, only objects Armed Assault: position3D (array) and object works
Rommel - c
Posted on Mar 10, 2010 - 07:54 (UTC)
This returns the map distance, not the vector distance, [0,0,0] distance [0,0,1] can be >1 or <1, not exactly 1; unlike the vector math.
Igneous01 - c
Posted on Aug 03, 2012 - 10:15 (UTC)
If a position in format [x, y, z] is provided for both arguments, distance also checks the z dimension as well. Ie: player distance [0,0,200] will return 200, if the player is at this position at ground/sea level.
AgentRev01 - c
Posted on Mar 23, 2014 - 02:52 (UTC)
When objects are supplied as arguments, distance is calculated from their model center (object modelToWorld [0,0,0]), and not the position returned by getPos/ATL/ASL.
However, this is not relevant for units, as their model center matches their world position.