AKA_Clutter Posted September 16, 2024 Posted September 16, 2024 Hi, Still pretty much a newbie with lua and DCS MSE. I am trying to write a function to get the position of a unit and of a zone, and then determine the distance and heading from the unit to the zone. I have gotten it to work, but though trial and error. I do it though using math.atan2(z,x) and then check to see if I need to add 2Pi to get it the correct quadrant. (check to see if I have a negative degree). As I said the code snippet below works, BUT math.atan2(z,x) doesn't work as advertised. I did read in the Lua 5.3 manual where the atan2 function has been "depreciated" and that math.atan(y, [x]) is supposed to correctly accout for the different quadrants. However, I couldn't get it to work either. My question is am I doing something wrong, or missing something basic? Is this an oddity within DCS? Thanks in advance. local function distanceBearing(uObject, landingZone) -- get coords for zone local lzTbl = trigger.misc.getZone(landingZone) local lzX = lzTbl.point.x local lzZ = lzTbl.point.z local lzY = lzTbl.point.y -- vec3 Object.getPoint(Class Self ) local uPoint = uObject:getPoint() local uPointX = uPoint.x local uPointZ = uPoint.z local uPointY = uPoint.y -- Calcualte delta x and z local dx = lzX - uPointX local dz = lzZ - uPointZ local uDistance = math.sqrt(dz*dz + dx*dx) * m2nm -- calculate bearing local uHeading = math.deg(math.atan2(dz,dx)) if uHeading > 0 then uHeading = uHeading --trigger.action.outText("Bearing 6 to inside if statment (then) " .. uBearing6, 10) else uHeading = math.deg(math.atan2(dz, dx)+2*math.pi) --trigger.action.outText("Bearing 6 to inside if statment (else) " .. uBearing6, 10) end trigger.action.outText("\n====================\n\n Distance and bearing to Hot LZ 1\n\n====================\n ", 10) trigger.action.outText("Distance to Hot_LZ_1: " .. uDistance, 10) trigger.action.outText("Heading to Hot_LZ_1: " .. uHeading, 10) end ---------------- AKA_Clutter Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080 FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.
Grimes Posted September 17, 2024 Posted September 17, 2024 Looking at the code and comparing to how the functions in mist work it looks like the math is correct. However there is an adjustment you will need to make to get accurate in cockpit bearings. Here is the mist function. Basically at map origin on the Z axis is the only place on the map where getting the bearing between two points would be 100% accurate. The further you move away from it the more error there will be. See the screenshot. If you were flying along the black like your cockpit would indicate 000. If you flew the cyan line, the bearing between the two points on the map, because it is flat, would be 0, but in the cockpit it'll show 10-11 ish degrees. The issue is most pronounced on the maps where map origin is offset heavily east/west. null function mist.getNorthCorrection(gPoint) --gets the correction needed for true north local point = mist.utils.deepCopy(gPoint) if not point.z then --Vec2; convert to Vec3 point.z = point.y point.y = 0 end local lat, lon = coord.LOtoLL(point) local north_posit = coord.LLtoLO(lat + 1, lon) return math.atan2(north_posit.z - point.z, north_posit.x - point.x) end 1 The right man in the wrong place makes all the difference in the world. Current Projects: Grayflag Server, Scripting Wiki Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread) SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum
AKA_Clutter Posted September 18, 2024 Author Posted September 18, 2024 @Grimes, Thanks. Took a while to figure out what why what I was computing didn't match the F10 map line. FInally it dawned on me. I havent yet tried to do the correct, that is the next step. Thanks. Still curious as to why neither atan or atan2 works as the lua manual states. ---------------- AKA_Clutter Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080 FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.
cfrag Posted September 18, 2024 Posted September 18, 2024 11 hours ago, AKA_Clutter said: Still curious as to why neither atan or atan2 works as the lua manual states. That's strange indeed, as (seemingly) it works for me. To get the bearing from Point A To B, I use function dcsCommon.bearingFromAtoB(A, B) -- coords in x, z local dx = B.x - A.x local dz = B.z - A.z local bearing = math.atan2(dz, dx) -- in radiants return bearing end which returns bearing in rads. To convert rads to degrees, simply multiply by 57.2958, i.e. local degrees = rads * 57.2958 -- 57.2958 = 180/pi Now, if the result is < 0 add 360, if it's greater than 360, subtract 360. function dcsCommon.bearingInDegreesFromAtoB(A, B) local bearing = dcsCommon.bearingFromAtoB(A, B) -- in rads bearing = math.floor(bearing * 57.2958) if bearing < 0 then bearing = bearing + 360 end if bearing > 360 then bearing = bearing - 360 end return bearing end Can you give an example where your code fails?
AKA_Clutter Posted September 18, 2024 Author Posted September 18, 2024 (edited) Hi @cfrag As usual, I was far less than clear. The function math.atan2(z,x) does work for me just as you explained. It is just not the way I expected based on the lua 5.1 manual. Specifically, the lua manual 5.1 states Quote math.atan2 (y, x) Returns the arc tangent of y/x (in radians), but uses the signs of both parameters to find the quadrant of the result. (It also handles correctly the case of x being zero.) So based on this I expected the math.atan2 to give the correct answer without the need to determine if it was greater than or less than 360 degrees. Given yours and @Grimes responses, I did what I should have done to start with. I wrote a lua, non-dcs, script to see what it did and if it would give me the correct answer. And it doesn't give me the answers that I thought it would. You still need to determine if it is greater than or less than 360. From this I deduce I can read the manual and time to go post on Stackoverflow and see what I misread. Thanks for the help. Edited September 18, 2024 by AKA_Clutter ---------------- AKA_Clutter Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080 FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.
Recommended Posts