From AGI Wiki
Jump to: navigation, search

The distance command calculates the distance between two screen objects, and stores the result in a variable.


distance(obj oA,obj oB, var vC);


Variable vC is set to distance between objects oA and oB. The distance calculated in NOT the shortest straight line between the two objects. Instead, AGI uses the following formula:

d = Abs(y1 - y2) + Abs((x1 + w1 / 2) - (x2 + w2 / 2))


d = distance
x1, y1, w1 = horizontal position, vertical position and width of first object
x2, y2, w2 = horizontal position, vertical position and width of second object

Basically, this means that AGI calculates the distance by adding the difference in horizontal position of the objects' center points to the difference in the objects' vertical positions. (When calculating the center points, results round down, i.e. if width is 13, then width / 2 = 6, not 7).

If either object is not on screen (or not initialized), distance is set to 255. If both objects are visible, and the distance is 255 or greater, the result is set to 254.

Possible Errors



[ object 0 is at (110, 100); width is 6
[ object 1 is at (98, 161); width is 13
distance(o0, 01, v50);  [ v50 now equals 70

Technical Information

Required Interpreter Version: Available in all AGI versions.
Byte-Code Value: 69 (0x45 hex)

See Also

Screen Object/View Commands