Closest Object / Vehicle / Ship to a point? - ED Forums
 


Notices

Reply
 
Thread Tools Display Modes
Old 02-14-2020, 09:59 AM   #1
johnv2pt0
Member
 
johnv2pt0's Avatar
 
Join Date: Nov 2010
Posts: 662
Default Closest Object / Vehicle / Ship to a point?

How do I get the closest object from a table of units to a specific point?
johnv2pt0 is offline   Reply With Quote
Old 02-14-2020, 07:13 PM   #2
Hardcard
Member
 
Hardcard's Avatar
 
Join Date: May 2013
Posts: 745
Default

Code:
local Unit_Table = { "Unit_1" , "Unit_2", "Unit_3" } -- Build a table with the chosen unit names 

local Reference_point = { x = x value , y = 0 , z = z value } -- 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)

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 ) -- Formula to calculate 2D coordinate distance + rounding
          
          table.insert(Distance_Table, Distance2D)
       end
   end
   
   local Threshold = 1000000000 -- Define a large maximum distance threshold to compare to 
   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])  -- This variable will cointain the unit object that's closest to the reference point (if any)
      local Closest_Name = Closest_Object:getName()   -- This variable will contain its name
      local Closest_Distance = Distance_Table[Closest_Index]  -- This variable will contain its distance from the reference point in meters
   
      trigger.action.outText("Closest unit = "..Closest_Name.."\nDistance (meters) = "..Closest_Distance, 10)  -- You can then include the info in a text message, if you want to
   end

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

Last edited by Hardcard; 02-14-2020 at 07:20 PM.
Hardcard is offline   Reply With Quote
Old 02-14-2020, 09:05 PM   #3
johnv2pt0
Member
 
johnv2pt0's Avatar
 
Join Date: Nov 2010
Posts: 662
Default

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?
johnv2pt0 is offline   Reply With Quote
Old 02-15-2020, 07:37 AM   #4
Hardcard
Member
 
Hardcard's Avatar
 
Join Date: May 2013
Posts: 745
Default

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 ), 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...00&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
__________________

Last edited by Hardcard; 02-15-2020 at 08:18 AM.
Hardcard is offline   Reply With Quote
Old 02-16-2020, 05:53 PM   #5
johnv2pt0
Member
 
johnv2pt0's Avatar
 
Join Date: Nov 2010
Posts: 662
Default

Roger, thanks HC!
johnv2pt0 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 10:41 AM. vBulletin Skin by ForumMonkeys. Powered by vBulletin®.
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.