 # Distance calculation

CoppeliaSim can measure the minimum distance between two measurable entities in a flexible way. The calculation is an exact minimum distance calculation. [Minimum distance calculation between two manipulators]

Using the API function sim.checkDistance, one can easily calculate the minimum distance between entities, for instance between a robot and its environment, from within a child script, in each simulation step:

``` function sysCall_init() local robotBase=sim.getObject('/robotModelAlias') robotCollection=sim.createCollection(0) sim.addItemToCollection(robotCollection,sim.handle_tree,robotBase,0) end function sysCall_sensing() local result,distData,objectPair=sim.checkDistance(robotCollection,sim.handle_all) if result>0 then local txt='Robot clearance is '..distData txt=txt..'m, minimum distance object pair is '..getAsString(objectPair) print(txt) end end```

One can also display the measured minimum distance, as a colored segment in the scene:

``` function sysCall_init() local robotBase=sim.getObject('/robotModelAlias') robotCollection=sim.createCollection(0) sim.addItemToCollection(robotCollection,sim.handle_tree,robotBase,0) distanceSegment=sim.addDrawingObject(sim.drawing_lines,4,0,-1,1,{0,1,0}) end function sysCall_sensing() local result,distData,objectPair=sim.checkDistance(robotCollection,sim.handle_all) if result>0 then sim.addDrawingObjectItem(distanceSegment,nil) sim.addDrawingObjectItem(distanceSegment,distData) end end```

CoppeliaSim's distance calculation functionality is also available as stand-alone routines via the Coppelia geometric routines.