Jump to content

Closest Object / Vehicle / Ship to a point?


johnv2pt0

Recommended Posts

local Unit_Table = { "[color="red"]Unit_1[/color]" , "[color="red"]Unit_2[/color]", "[color="Red"]Unit_3[/color]" } [color="Blue"]-- Build a table with the chosen unit names [/color]

local Reference_point = { x = [color="red"]x value[/color] , y = 0 , z = [color="red"]z value[/color] } [color="blue"]-- You can either define the reference coordinate like this or get the x and z values from another object using :getPoint().x and :getPoint().z (y isn't needed)[/color]

local Distance_Table = {}    
      
  for i, Unit_Name in ipairs( Unit_Table ) do   
      
      if Unit.getByName(Unit_Name) and Unit.getByName(Unit_Name):isExist() then 
         
         local Unit_Object = Unit.getByName(Unit_Name)
         local Unit_point = Unit_Object:getPoint()
         
         local Distance2D = math.floor( ( ( Reference_point.x - Unit_point.x ) ^ 2 + ( Reference_point.z - Unit_point.z ) ^ 2 ) ^ 0.5, 0 ) [color="blue"]-- Formula to calculate 2D coordinate distance + rounding[/color]
         
         table.insert(Distance_Table, Distance2D)
      end
  end
  
  local Threshold = 1000000000 [color="blue"]-- Define a large maximum distance threshold to compare to [/color]
  local Closest_Index = 0
 
  for k = 1 , #Distance_Table do
      
      local asNum = Distance_Table[k]
 
      if ( asNum and asNum < Threshold ) then
          Threshold = asNum
          Closest_Index = k
      end
  end
  
  if Unit.getByName(Unit_Table[Closest_Index]) then
  
     local Closest_Object = Unit.getByName(Unit_Table[Closest_Index])  [color="blue"]-- This variable will cointain the unit object that's closest to the reference point (if any)[/color]
     local Closest_Name = Closest_Object:getName()   [color="blue"]-- This variable will contain its name[/color]
     local Closest_Distance = Distance_Table[Closest_Index]  [color="blue"]-- This variable will contain its distance from the reference point in meters[/color]
  
     trigger.action.outText("Closest unit = "..Closest_Name.."\nDistance (meters) = "..Closest_Distance, 10)  [color="blue"]-- You can then include the info in a text message, if you want to[/color]
  end

 

 

Alternatively, you can save yourself some pain and use MOOSE or MIST functions which already do this stuff for you ;)


Edited by Hardcard
Link to comment
Share on other sites

Yikes. ...it hurts so good.

 

Thank you, I was looking at Mist and didn't see what I needed, but now I see mist.utils.get2DDist, so with your example above and that I think I can muddle my way through.

 

With MOOSE I guess I should look at core coordinate? Do you know of a script out there that has some of these things I could go rummaging through to learn from?

Link to comment
Share on other sites

I'm guessing that mist.utils.get2DDist uses a similar method to what I posted above. You can always look for that function inside the MIST file and see what it does.

 

MOOSE definitely uses almost the same method I posted above (since that's where I took the general idea from :D), but it only uses it for airbases and parking spots, not units (AFAIK).

 

To answer your question, yes, Core - Coordinate is the place to go for this kind of stuff, specifically, you're looking for :Get2DDistance() or :Get3DDistance()

 

As for practical MOOSE examples, here's a little script that I wrote to get the player's distance to a specific unit (the second function calculates and returns the distance):https://forums.eagle.ru/showpost.php?p=4111000&postcount=3

 

 

However, note that all these will only give you the distance between two coordinates, they won't tell you which object is closest to a specific coordinate... in order to get that, you'll need to use the script I posted in my first reply.

 

Now, I don't know about MIST, but, like I said, MOOSE doesn't have any built-in method to calculate what you're asking for... it only has methods to find the closest airbase, parking spot and road point (AFAIK).

 

Maybe I should talk to Pikey and Franky about this... we should be able to add this kind of functionality to MOOSE :thumbup:


Edited by Hardcard
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...