Bearfoot Posted May 8, 2016 Posted May 8, 2016 Given two units, I would like to get the bearing of unit from another. I have got the following code: function calcBearing(dx, dz) -- dx = to_x - from_x -- where x corresponds to first horizontal coordinate -- dz = to_z - from_z -- where z corresponds to second horizontal coordiante local bearing = nil if dx ~= 0 then -- local angle = (math.atan(dz, dx) * 180/math.pi) local angle = math.atan(dz, dx) bearing = angle elseif dz > 0 then bearing = 0 elseif dz < 0 then bearing = 180 else bearing = 0 end if bearing < 0 then bearing = bearing + 360 end bearing = bearing % 360 return bearing end unit1 = Unit.getByName("Pilot #001") unit2 = Unit.getByName("Pilot #002") pos1 = unit1.getPosition().p pos2 = unit2.getPosition().p dx = unit2.x - unit1.x dz = unit2.z - unit1.z bearing = calcBearing(dx, dz) trigger.action.outText("Bearing of unit 2 from unit 1: " .. bearing, 1) And it works in the sense that it produces a result between 0 and 360, but the numbers do not make sense. I think either the origin of the coordinate system or the fact that I am not taking into account heading is the problem? What am I doing wrong? Is there a better way to go around this?
FSFIan Posted May 8, 2016 Posted May 8, 2016 The trigonometric functions in Lua, like in most other programming languages, use radians instead of degrees to express angles. A full circle is 2*pi. function deg2rad(deg) return deg/180*math.pi end function rad2deg(rad) return rad/math.pi*180 end DCS-BIOS | How to export CMSP, RWR, etc. through MonitorSetup.lua
Bearfoot Posted May 8, 2016 Author Posted May 8, 2016 Ian;2771959']The trigonometric functions in Lua' date=' like in most other programming languages, use radians instead of degrees to express angles. A full circle is 2*pi. function deg2rad(deg) return deg/180*math.pi end function rad2deg(rad) return rad/math.pi*180 end Ah yes. In the production version, I actually do this conversion (you can see it commented out above). I was just experimenting to see if the radians/degrees things was the issue. But even with the conversion, it does not always makes sense. E.g. given a 4-ship right-heavy wedge heading (roughly) north, 1 2 3 4 The bearing from 3 to 4 as reported by the above is 88.85, i.e. ~90 after accounting for error. Heading roughly east is ~270. Heading roughly south is also ~270. Heading roughly west is also ~90. So, first of all, the bearing from 3 to 4 should be invariant (IMHO) with respect to heading. Secondly, I cannot see how any of those bearings make sense from what I see on the map of the relative positions of 3 and 4. I would expect ~135, regardless of heading.
Bearfoot Posted May 9, 2016 Author Posted May 9, 2016 Update. Fixed how the angles are normalized, and now I think I am getting correct absolute bearings (with 0 deg = North): function calcBearing(dx, dz) -- dx = to_x - from_x -- where x corresponds to first horizontal coordinate -- dz = to_z - from_z -- where z corresponds to second horizontal coordiante local bearing = nil if dx ~= 0 then local angle = math.atan2(dz, dx) if angle < 0 then angle = angle + 2 * math.pi end bearing = math.deg(angle) elseif dz > 0 then bearing = 0 elseif dz < 0 then bearing = 180 else bearing = 0 end return bearing end Of course, what I want is relative bearings ...
Bearfoot Posted May 9, 2016 Author Posted May 9, 2016 Ok, figured it out: local unit1_velocity = unit1_unit:getVelocity() local unit1_heading = calcFormationUnitBearing(unit1_velocity.x, unit1_velocity.z) local relative_bearing = (360 - unit1_heading + absolute_bearing) % 360 where 'absolute_bearing' is given by the 'calcBearing()' function above
Recommended Posts