Jump to content

Recommended Posts

Posted

You need to get the center point of the trigger and the point you want to get the distance to. I use the following function to get the distance to a point from the player pos. If player pos is not defined, nil is returned.

 

-- getDistance to player huey
function getDistance(point)
 local dist = nil
 local playerPos = getPlayerPos()
 if playerPos ~= nil then
   local px = playerPos.p.x
   local pz = playerPos.p.z
   local zx = point.x
   local zz = point.z
   dist = math.sqrt( (px - zx)^2 + (pz - zz)^2 )
 end
 return dist
end

Posted

You need to get the positions of the two helicopters. For example:

 

function getPlayerPos()
 local playerHelo = Unit.getByName("Player")
 local playerPos = nil
 if playerHelo ~= nil then
   playerPos = playerHelo:getPosition()
 end
 return playerPos 
end

 

If you need this for multiple units, I'd recommend to generalize this to:

 

function getUnitPos(unitName)
 local unit = Unit.getByName(unitName)
 local unitPos = nil
 if unit ~= nil then
   unitPos = unit:getPosition()
 end
 return unitPos
end

 

The getdistance then needs to call getUnitPos("Player") and getUnitPos("2nd helo name")

Posted (edited)

How can I put it in a function and make the call? 'm not very good with lua!

 

 

do

local dist = nil

local dist2 = nil

local unit = Unit.getByName('Pilot #1')

local avgx, avgy, avgz = 0, 0, 0

local unitPos = nil

if unit ~= nil then

unitPos = unit:getPosition()

avgx = avgx + unit:getPosition().p.x

avgy = avgy + unit:getPosition().p.y

avgz = avgz + unit:getPosition().p.z

end

 

 

local unit = StaticObject.getByName('Container')

local avgx2, avgy2, avgz2 = 0, 0, 0

local unitPos = nil

if unit ~= nil then

unitPos = unit:getPosition()

avgx2 = avgx2 + unit:getPosition().p.x

avgy2 = avgy2 + unit:getPosition().p.y

avgz2 = avgz2 + unit:getPosition().p.z

dist = math.sqrt( (avgx - avgx2)^2 + (avgz - avgz2)^2 )

trigger.action.outText('Distance: ' .. dist, 5)

end

 

end

Edited by spinter

====VIAF====

Spinter 155° "Pantere Nere"

 

TsSimComms

 

My Site aiupgrade.net

 

 

 

Posted

Untested, so check for spelling errors:

 

function getUnitPos(unitName)
 local unit = Unit.getByName(unitName)
 local unitPos = nil
 if unit ~= nil then
   unitPos = unit:getPosition()
 end
 return unitPos
end

function getDistance(unit1, unit2)
 local dist = nil
 local pos1 = getUnitPos(unit1)
 local pos2 = getUnitPos(unit2)
 if pos1 ~= nil and pos2 ~= nil then
   local px = pos1.p.x
   local pz = pos1.p.z
   local zx = pos2.p.x
   local zz = pos2.p.z
   dist = math.sqrt( (px - zx)^2 + (pz - zz)^2 )
 end
 return dist
end

local dist = getDistance("Pilot #1", "Container")
trigger.action.outText('Distance: ' .. dist, 5)

Posted

Try this out -

 


function getUnitPos(unitName)
 local unit = Unit.getByName(unitName)
 local unitPos = nil
 if unit ~= nil then
   unitPos = unit:getPosition()
 end
 return unitPos
end

function getDistance(unit1, unit2)
 local dist = nil
 local pos1 = getUnitPos(unit1)
 local pos2 = getUnitPos(unit2)
 if pos1 ~= nil and pos2 ~= nil then
   local px = pos1.p.x
   local pz = pos1.p.z
   local zx = pos2.p.x
   local zz = pos2.p.z
   dist = math.sqrt( (px - zx)^2 + (pz - zz)^2 )
 end
 return dist
end

function getBRA(unit1, unit2)
 local BRA = nil
 local pos1 = getUnitPos(unit1)
 local pos2 = getUnitPos(unit2)
 if pos1 ~= nil and pos2 ~= nil then
   local px = pos1.p.x
   local pz = pos1.p.z
   local zx = pos2.p.x
   local zz = pos2.p.z
   BRA = 90 + math.atan( (pz-zz)/(px-zx) )
 end
 return BRA
end

local dist = getDistance("Pilot #1", "Container")
trigger.action.outText('Distance: ' .. dist, 5)
local BRA = geBRA("Pilot #1", "Container")
trigger.action.outText('BRA: ' .. BRA, 5)

Posted (edited)
Try this out -

 


function getBRA(unit1, unit2)
 local BRA = nil
 local pos1 = getUnitPos(unit1)
 local pos2 = getUnitPos(unit2)
 if pos1 ~= nil and pos2 ~= nil then
   local px = pos1.p.x
   local pz = pos1.p.z
   local zx = pos2.p.x
   local zz = pos2.p.z
   BRA = 90 + math.atan( (pz-zz)/(px-zx) )
 end
 return BRA
end


local BRA = getBRA("Pilot #1", "Container")
trigger.action.outText('BRA: ' .. BRA, 5)

 

Sorry return me 90.08234834 to 90.9829283457827

Help please

Edited by spinter

====VIAF====

Spinter 155° "Pantere Nere"

 

TsSimComms

 

My Site aiupgrade.net

 

 

 

Posted

I have no clue on geometrics, but this worked for me:

 

    local dx = zx - px
   local dz = zz - pz
   -- http://stackoverflow.com/questions/1311049/how-to-map-atan2-to-degrees-0-360
   dir = math.atan2( dz, dx ) * 180 / math.pi
   if dir < 0 then
     dir = 360 + dir
   end

  • Recently Browsing   0 members

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