EasyEB Posted February 26, 2017 Posted February 26, 2017 (edited) Hi! Is it possible to get a message to player with range between two groups? For knstance have a message for ten seconds saying "Blue1 is 2 nm from target" and "target" beeing group Red1. So,I'm still learning this. I guess what I need is a way to get the position of Blue1 (a) and Red1 (b) and then to measure that distance (n). And then make a message that says "[a] is [n] from target" Edited February 26, 2017 by EasyEB
brontolo Posted February 26, 2017 Posted February 26, 2017 take a look here: https://forums.eagle.ru/showthread.php?t=176715&highlight=unit+position&page=2 Brontolo VIAF
EasyEB Posted February 26, 2017 Author Posted February 26, 2017 (edited) That seems basically like it! Thanks! So, here's the code function getDistance(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ) local xDiff = obj1PosX - obj2PosX local yDiff = obj1PosZ - obj2PosZ return math.sqrt(xDiff * xDiff + yDiff * yDiff) -- meters end local unit1 = Unit.getByName('Unit 1') local unit1Pos = unit1:getPosition() local unit2 = Unit.getByName('Unit 2') local unit2Pos = unit2:getPosition() local distance = getDistance(unit2Pos.p.x, unit2Pos.p.z, unit1Pos.p.x, unit1Pos.p.z) trigger.action.outText("Unit 1 is " .. distance .. " from Unit 2.",10) This gives me a message about the distance in meters between "Unit 1" and "Unit 2". What I need now is 1: To get "Unit 1" unit name from the leader of a group called Red1. 2: To get "Unit 2" unit name from the leader of a group called Blue1. 3: To get the distance in nautical miles. I'm just thinking out loud here, but if anyone has the know how, please chime in! Update: Ok, so I had to ask my wife for help and she gave me this: function getDistance(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ) local xDiff = obj1PosX - obj2PosX local yDiff = obj1PosZ - obj2PosZ -- return math.sqrt(xDiff * xDiff + yDiff * yDiff) -- meters return math.sqrt(xDiff * xDiff + yDiff * yDiff)/1852 -- meters end local unit1 = Unit.getByName('Unit 1') local unit1Pos = unit1:getPosition() local unit2 = Unit.getByName('Unit 2') local unit2Pos = unit2:getPosition() local distance = getDistance(unit2Pos.p.x, unit2Pos.p.z, unit1Pos.p.x, unit1Pos.p.z) trigger.action.outText("Unit 1 is " .. distance .. " from Unit 2.",10) So now it's in nautical miles. But it's showing like ten decimals, and I want it to round off to just show one decimal. Update 2: Ok, so now I've got it down to one decimals. function round(num, numDecimalPlaces) local mult = 10^(numDecimalPlaces or 0) return math.floor(num * mult + 0.5) / mult end function getDistance(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ) local xDiff = obj1PosX - obj2PosX local yDiff = obj1PosZ - obj2PosZ local nautic = math.sqrt(xDiff * xDiff + yDiff * yDiff)/1852 -- position in nautic miles return round(nautic, 1) -- return math.sqrt(xDiff * xDiff + yDiff * yDiff)/1852 end local unit1 = Unit.getByName('Unit 1') local unit1Pos = unit1:getPosition() local unit2 = Unit.getByName('Unit 2') local unit2Pos = unit2:getPosition() local distance = getDistance(unit2Pos.p.x, unit2Pos.p.z, unit1Pos.p.x, unit1Pos.p.z) trigger.action.outText("Unit 1 is " .. distance .. " from Unit 2.",10) Update 3: Ok, so I went and got a beer and my wife gave me this: Using MIST to get lead position of groups Blue1 and Red1. function round(num, numDecimalPlaces) local mult = 10^(numDecimalPlaces or 0) return math.floor(num * mult + 0.5) / mult end function getDistance(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ) local xDiff = obj1PosX - obj2PosX local yDiff = obj1PosZ - obj2PosZ local nautic = math.sqrt(xDiff * xDiff + yDiff * yDiff)/1852 -- position in nautic miles return round(nautic, 1) end local red1Pos = mist.getLeadPos('Red1') local blue1Pos = mist.getLeadPos('Blue1') local distance = getDistance(blue1Pos.x, blue1Pos.z, red1Pos.x, red1Pos.z) trigger.action.outText("Red1 is " .. distance .. " nautical miles from Blue1.",10) Update 4: Ok, so now I want Blue1 to give their MGRS coordinates aswell. I need to find a way to convert the Vec3 from blue1pos to MGRS and get it into the message. Update 5: Ok, got it. This will result in a message where group Blue1 gives their MGRS coordinates, and hos far away they are from group Red1 in nautical miles, rounded off two one decimal. I think I will try to change mist.getLeadPos to mist.getAvgPos, but whatever. function round(num, numDecimalPlaces) local mult = 10^(numDecimalPlaces or 0) return math.floor(num * mult + 0.5) / mult end function getDistance(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ) local xDiff = obj1PosX - obj2PosX local yDiff = obj1PosZ - obj2PosZ local nautic = math.sqrt(xDiff * xDiff + yDiff * yDiff)/1852 -- position in nautic miles return round(nautic, 1) end local blue1Units = mist.makeUnitTable({'[g]Blue1'}) local blue1MGRS = mist.getMGRSString({units=blue1Units, acc = 5}) local red1Pos = mist.getLeadPos('Red1') local blue1Pos = mist.getLeadPos('Blue1') local distance = getDistance(blue1Pos.x, blue1Pos.z, red1Pos.x, red1Pos.z) trigger.action.outText("Blue1 current position is " .. blue1MGRS .. ", " .. distance .. " miles from target.",10) Update 6: So here is the same thing but using mist.getAvgPos instead of mist.getLeadPos. function round(num, numDecimalPlaces) local mult = 10^(numDecimalPlaces or 0) return math.floor(num * mult + 0.5) / mult end function getDistance(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ) local xDiff = obj1PosX - obj2PosX local yDiff = obj1PosZ - obj2PosZ local nautic = math.sqrt(xDiff * xDiff + yDiff * yDiff)/1852 -- position in nautic miles return round(nautic, 1) end local blue1Units = mist.makeUnitTable({'[g]Blue1'}) local blue1MGRS = mist.getMGRSString({units=blue1Units, acc = 5}) local red1Pos = mist.getAvgPos(mist.makeUnitTable({'[g]Red1'})) local blue1Pos = mist.getAvgPos(mist.makeUnitTable({'[g]Blue1'})) local distance = getDistance(blue1Pos.x, blue1Pos.z, red1Pos.x, red1Pos.z) trigger.action.outText("Blue1 current position is " .. blue1MGRS .. ", " .. distance .. " miles from target.",10) Edited February 26, 2017 by EasyEB
Recommended Posts