Jump to content

Get position


Recommended Posts

I am building a escort mission, where you are escorting a C130. I want a radio item (F10) that sends a text message about the C130 position relative yours. How exactlyis is that done?

 

I have tried getposition in do script, but I don´t seem to get it. I know nothing about scripting, so easy instructions please :)

 

Unit name of the C130: C130

Unit name of the player: Player

 

I have done a radio item add, that set flag 999 to true, or 1.

Then when 999 is true, the action is "do script". What exactly should I enter? (if it is possible at all)

Link to comment
Share on other sites

Nothing happens.

 

try this:

 

local group = Group.getByName('Your unit group name')

local unitPos = nil

local units = group:getUnits()

if units then

unitPos = Unit.getByName(units[1]:getName()):getPosition()

end

i5-9600K@4.8GHz z390 Aorus Pro 32Gb DDR4 1080ti Rift CV1 Warthog on Virpil base

Link to comment
Share on other sites

No, nothing.

 

Where should I put the text?

 

My unit name is Player. The C130 unit name is C130.

 

Is this correct?

 

local group = Group.getByName('Player')

local unitPos = nil

local units = group:getUnits()

if units then

unitPos = Unit.getByName(units[1]:getName()):getPosition()

end

Link to comment
Share on other sites

no, this is correct:

 

local group = Group.getByName('C130')

local unitPos = nil

local units = group:getUnits()

if units then

unitPos = Unit.getByName(units[1]:getName()):getPosition()

end

i5-9600K@4.8GHz z390 Aorus Pro 32Gb DDR4 1080ti Rift CV1 Warthog on Virpil base

Link to comment
Share on other sites

Still nothing. What exactly shall happen?

 

I want a text message to appear, with the C130 position

 

Ah, then you need to add an extra line (also, I assume 'C130' is a name of the group the unit belongs to):

 

local group = Group.getByName('C130')

local unitPos = nil

local units = group:getUnits()

if units then

unitPos = Unit.getByName(units[1]:getName()):getPosition()

end

trigger.action.outText("Position X is: "..unitPos.x.z, 5)

i5-9600K@4.8GHz z390 Aorus Pro 32Gb DDR4 1080ti Rift CV1 Warthog on Virpil base

Link to comment
Share on other sites

It works, thanks :)

 

But is it possible to use heading and range from your aircraft, or relative from bullseye? Now it says something like 0.5343242345 and that doesn´t say much to me :)

 

I knew that would be your next step :), but was kinda lazy to dig into the code... Ok, so here what you need to do:

 

- get position with the code I gave you

- get position of your own aircraft (same method, but use the name of your aircraft's group)

- convert positions above from Vec3 to Vec2 coordinates

- calculate a heading and distance using functions below

 

Conversion from Vec3 to Vec2 example:

local myVec2Position = {x = myVec3Position.p.x, y = myVec3Position.p.z}

 

These functions will calculate distance and heading between two positions respectively using Vec2 positions:

 

function GetDistance(Vec2a, Vec2b)

local deltax = Vec2b.x - Vec2a.x

local deltay = Vec2b.y - Vec2a.y

return math.sqrt(math.pow(deltax, 2) + math.pow(deltay, 2))

end

 

function GetHeadingBetween(Vec2a, Vec2b)

local deltax = Vec2b.x - Vec2a.x

local deltay = Vec2b.y - Vec2a.y

if (deltax > 0) and (deltay == 0) then

return 360

elseif (deltax > 0) and (deltay > 0) then

return math.deg(math.atan(deltay / deltax))

elseif (deltax == 0) and (deltay > 0) then

return 90

elseif (deltax < 0) and (deltay > 0) then

return 90 - math.deg(math.atan(deltax / deltay))

elseif (deltax < 0) and (deltay == 0) then

return 180

elseif (deltax < 0) and (deltay < 0) then

return 180 + math.deg(math.atan(deltay / deltax))

elseif (deltax == 0) and (deltay < 0) then

return 270

elseif (deltax > 0) and (deltay < 0) then

return 270 - math.deg(math.atan(deltax / deltay))

end

end

i5-9600K@4.8GHz z390 Aorus Pro 32Gb DDR4 1080ti Rift CV1 Warthog on Virpil base

Link to comment
Share on other sites

This is a hell of a lot cleaner to get a bearing between 2 objects:

 

function getBearing(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ)
    local bearing = math.atan2(obj2PosZ - obj1PosZ, obj2PosX - obj1PosX)
    if bearing < 0 then
         bearing = bearing + 2 * math.pi
    end
    bearing = bearing * 180 / math.pi
    return bearing    -- degrees
end

-16AGR- 16th Air Guards Regiment is always looking for pilots - http://www.16agr.com

 

EWRS - Early Warning Radar Script

 

Specs:

 

 

Gigabyte Sniper Z5-S

Intel i5-4670k 3.4GHz OC'd 3.9GHz w/ Thermaltake 120mm Water 3.0 Pro Liquid CPU Cooler

16GB RAM

Gigabyte GTX 1080

TM Hotas Warthog: SN: 06976

Saitek Pro Flight Combat Rudder Pedals

TrackIR5 with TrackClipPro & Oculus Rift

2x 28" 4k UHD Monitors (3840x2160 each) + 1280x1024

 

 

Link to comment
Share on other sites

Thanks for the replies. But I can´t get it to work. Remember that I´m a newbie with scripting. When pressing F10 and then activate the script I want a text message to appear with the information (distance and heading to the C130 from me/or Bulls eye). I have tried both Ruskybeavers and Steggles versions.

 

 

Is this correct?:

 

local group = Group.getByName('C130')

local unitPos = nil

local units = group:getUnits()

if units then

unitPos = Unit.getByName(units[1]:getName()):getPosition()

end

 

local group = Group.getByName('Player')

local unitPos = nil

local units = group:getUnits()

if units then

unitPos = Unit.getByName(units[1]:getName()):getPosition()

end

 

local myVec2Position = {x = myVec3Position.p.x, y = myVec3Position.p.z}

 

function GetDistance(Vec2a, Vec2b)

local deltax = Vec2b.x - Vec2a.x

local deltay = Vec2b.y - Vec2a.y

return math.sqrt(math.pow(deltax, 2) + math.pow(deltay, 2))

end

 

function GetHeadingBetween(Vec2a, Vec2b)

local deltax = Vec2b.x - Vec2a.x

local deltay = Vec2b.y - Vec2a.y

if (deltax > 0) and (deltay == 0) then

return 360

elseif (deltax > 0) and (deltay > 0) then

return math.deg(math.atan(deltay / deltax))

elseif (deltax == 0) and (deltay > 0) then

return 90

elseif (deltax < 0) and (deltay > 0) then

return 90 - math.deg(math.atan(deltax / deltay))

elseif (deltax < 0) and (deltay == 0) then

return 180

elseif (deltax < 0) and (deltay < 0) then

return 180 + math.deg(math.atan(deltay / deltax))

elseif (deltax == 0) and (deltay < 0) then

return 270

elseif (deltax > 0) and (deltay < 0) then

return 270 - math.deg(math.atan(deltax / deltay))

end

end

 

 

 

Or the same start but with Steggles code.

Link to comment
Share on other sites

No, if that would be your final script - it is far from being correct. It seems like you need to get familiar with some basic programming concepts, i.e. how to call functions, how to pass variables etc etc.

i5-9600K@4.8GHz z390 Aorus Pro 32Gb DDR4 1080ti Rift CV1 Warthog on Virpil base

Link to comment
Share on other sites

Here you go. Make sure you set the PILOT names for the units in the editor. You will have to format the output too cause it has a heap of decimal places.

 

function getBearing(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ)
   local bearing = math.atan2(obj2PosZ - obj1PosZ, obj2PosX - obj1PosX)
   if bearing < 0 then
       bearing = bearing + 2 * math.pi
   end
   bearing = bearing * 180 / math.pi
   return bearing    -- degrees
end

function getDistance(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ)
   local xDiff = obj1PosX - obj2PosX
   local yDiff = obj1PosZ - obj2PosZ
   return math.sqrt(xDiff * xDiff + yDiff * yDiff) -- meters
end


local unit = Unit.getByName('C130') -- Set Pilot name (not group name) of the C130 in the editor to C130
local unitPos = unit:getPosition()

local player = Unit.getByName('Player') -- Set Pilot name (not group name) of player slot to Player
local playerPos = player:getPosition()

local distance = getDistance(playerPos.p.x, playerPos.p.z, unitPos.p.x, unitPos.p.z)
local bearing = getBearing(playerPos.p.x, playerPos.p.z, unitPos.p.x, unitPos.p.z) --bearing from playerPos to unitPos

trigger.action.outText("BRA: C130 " .. bearing .. " degrees for " .. distance .. " meters.",10)

 

Hint: If you want the units altitude too, its unitPos.p.y and its in meters


Edited by Steggles

-16AGR- 16th Air Guards Regiment is always looking for pilots - http://www.16agr.com

 

EWRS - Early Warning Radar Script

 

Specs:

 

 

Gigabyte Sniper Z5-S

Intel i5-4670k 3.4GHz OC'd 3.9GHz w/ Thermaltake 120mm Water 3.0 Pro Liquid CPU Cooler

16GB RAM

Gigabyte GTX 1080

TM Hotas Warthog: SN: 06976

Saitek Pro Flight Combat Rudder Pedals

TrackIR5 with TrackClipPro & Oculus Rift

2x 28" 4k UHD Monitors (3840x2160 each) + 1280x1024

 

 

Link to comment
Share on other sites

Yeah, it works. Thanks alot.:thumbup:

 

How do I format the output?

 

http://lua-users.org/wiki/StringLibraryTutorial

 

Look at the string.format() function.

 

Something like this:

trigger.action.outText("BRA: C130 " .. string.format("%03d",bearing) .. " degrees for " .. string.format("%8.1f",distance) .. " meters.",10)

 

You can of course apply some maths to change meters to NM or KM etc before displaying as well if you wanted.

-16AGR- 16th Air Guards Regiment is always looking for pilots - http://www.16agr.com

 

EWRS - Early Warning Radar Script

 

Specs:

 

 

Gigabyte Sniper Z5-S

Intel i5-4670k 3.4GHz OC'd 3.9GHz w/ Thermaltake 120mm Water 3.0 Pro Liquid CPU Cooler

16GB RAM

Gigabyte GTX 1080

TM Hotas Warthog: SN: 06976

Saitek Pro Flight Combat Rudder Pedals

TrackIR5 with TrackClipPro & Oculus Rift

2x 28" 4k UHD Monitors (3840x2160 each) + 1280x1024

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...