spinter Posted May 18, 2013 Posted May 18, 2013 how can I obtain a distance from triger with script? ====VIAF==== Spinter 155° "Pantere Nere" TsSimComms My Site aiupgrade.net
TeTeT Posted May 18, 2013 Posted May 18, 2013 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
spinter Posted May 18, 2013 Author Posted May 18, 2013 I can not. I need to extract laa distance between me and another helicopter for example! ====VIAF==== Spinter 155° "Pantere Nere" TsSimComms My Site aiupgrade.net
spinter Posted May 18, 2013 Author Posted May 18, 2013 with getPlayerPos() not funcional ====VIAF==== Spinter 155° "Pantere Nere" TsSimComms My Site aiupgrade.net
TeTeT Posted May 18, 2013 Posted May 18, 2013 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")
spinter Posted May 18, 2013 Author Posted May 18, 2013 (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 May 18, 2013 by spinter ====VIAF==== Spinter 155° "Pantere Nere" TsSimComms My Site aiupgrade.net
TeTeT Posted May 19, 2013 Posted May 19, 2013 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)
spinter Posted May 19, 2013 Author Posted May 19, 2013 fantastic thx!!!!!!!!!! ====VIAF==== Spinter 155° "Pantere Nere" TsSimComms My Site aiupgrade.net
spinter Posted May 19, 2013 Author Posted May 19, 2013 help me!! I need an excuse even such a thing as I can find the direction to be taken by a united to the other? ====VIAF==== Spinter 155° "Pantere Nere" TsSimComms My Site aiupgrade.net
xcom Posted May 19, 2013 Posted May 19, 2013 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) [sIGPIC][/sIGPIC] BuddySpike Website | Live Map & Statistics BuddySpike Twitch Channel Buddyspike Discord Buddyspike Facebook
spinter Posted May 19, 2013 Author Posted May 19, 2013 (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 May 19, 2013 by spinter ====VIAF==== Spinter 155° "Pantere Nere" TsSimComms My Site aiupgrade.net
TeTeT Posted May 19, 2013 Posted May 19, 2013 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
spinter Posted May 19, 2013 Author Posted May 19, 2013 thanks you've been a great! ====VIAF==== Spinter 155° "Pantere Nere" TsSimComms My Site aiupgrade.net
Recommended Posts