Advertisement
morrtz

distance & bearing

May 19th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.34 KB | None | 0 0
  1. function getUnitPos(unitName)
  2.   local unit = Unit.getByName(unitName)
  3.   local unitPos = nil
  4.   if unit ~= nil then
  5.     unitPos = unit:getPosition()
  6.   end
  7.   return unitPos
  8. end
  9.  
  10. function getDistance(unit1, unit2)
  11.   local dist = nil
  12.   local pos1 = getUnitPos(unit1)
  13.   local pos2 = getUnitPos(unit2)
  14.   if pos1 ~= nil and pos2 ~= nil then
  15.     local px = pos1.p.x
  16.     local pz = pos1.p.z
  17.     local zx = pos2.p.x
  18.     local zz = pos2.p.z
  19.     dist = math.sqrt( (px - zx)^2 + (pz - zz)^2 )
  20.   end
  21.   return dist
  22. end
  23.  
  24. function getBRA(unit1, unit2)
  25.   local BRA = nil
  26.   local pos1 = getUnitPos(unit1)
  27.   local pos2 = getUnitPos(unit2)
  28.   if pos1 ~= nil and pos2 ~= nil then
  29.     local px = pos1.p.x
  30.     local pz = pos1.p.z
  31.     local zx = pos2.p.x
  32.     local zz = pos2.p.z
  33.     local dx = zx - px
  34.     local dz = zz - pz
  35.     BRA = math.atan2( dz, dx ) * 180 / math.pi
  36.     if BRA < 0 then
  37.     BRA = 360 + BRA
  38.     end
  39.   end
  40.   return BRA
  41. end
  42.  
  43. local dx = zx - px
  44.     local dz = zz - pz
  45.     -- http://stackoverflow.com/questions/1311049/how-to-map-atan2-to-degrees-0-360
  46.     dir = math.atan2( dz, dx ) * 180 / math.pi
  47.     if dir < 0 then
  48.       dir = 360 + dir
  49.     end
  50.  
  51. local dist = getDistance("Pilot #1", "Pilot #2")
  52. trigger.action.outText('Distance: ' .. dist, 5)
  53. local BRA = geBRA("Pilot #1", "Pilot #2")
  54. trigger.action.outText('BRA: ' .. BRA, 5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement