Display 3D data from radar on 2D HUD

221 Views Asked by At

I'm writing a script for game Stormworks in in-game Lua based microcontroller thingy. It can get bunch of stuff as an input, do whatever is in the script, and output it to whatever. Currently, I have a Radar component that provides Azimuth, Elevation and Distance to objects in front of me, mentioned microcontroller and a HUD that sits in front of the player.

The idea is to take the data from the radar and display a point on the HUD exactly where the object that the radar is tracking. I've already experimented with displaying position of object from "top-down view" (aka drawing the world map on a screen and displaying a point on the location of the object, works pretty well and got myself familiar with trigonometry and a bit of matrices).

The problem is, how do I translate the angles and distance from Radar to a 2D screen in front of me that's using XYZ coordinate system.

I've tried:

  • rotating the computed local XYZ coordinates of the object (local = radar is the origin point and the coordinates are distance from the radar) around the Y axis, that doesn't work
  • using Homogenous Coordinates, which kind of didn't helped at all, just made it a bit simpler to understand. There was a formula however, that I've been told should work, but it doesn't. See my post about this on math.stackexhange. It also contains mathematical notations of what I'm trying to achieve, what I've tried and a tiny bit more information about the math

Here is the code I have written that is kind-of close but not really:


for i = 1, max_targets do
        if targets[i] then
            -- calculate radar's values in radians
            azi_rad = target_azi[i] * pi2
            ele_rad = target_ele[i] * pi2
            
            -- local "unrotated" values
            target_x_unrot = math.sin(azi_rad)
            target_y_unrot = math.cos(ele_rad)
            
            -- rotate the radar values around the roll and pitch
            target_x = ((target_x_unrot * math.cos(roll)) + (target_y_unrot * math.sin(roll))) * target_dst[i]
            target_y = (-(target_x_unrot * math.sin(pitch)) + (target_y_unrot * math.cos(pitch))) * target_dst[i]
    
            -- idk why those numbers are like this, but this is the least f.cky solution
            target_px = ((w/2) - target_x/2)
            target_py = (target_y/2)
 
            -- draw the point on the screen where the enemy should be
            screen.setColor(255, 0, 0)
            screen.drawCircle(target_px, target_py, 2)  -- target point
        
            target_count = target_count + 1 -- update target cnt
        end
    end

Many variations of the rotation were tried, I've tried rotating all coordinates around every axis (specifically Y), I've tried computing the intersection using (x/z, y/z, 1) but that seems to produce similar results to the matrix rotation.. Logically it makes sense to me to rotate the local coordinates around the Y axis, but apparently that is not the case.

I'm expecting to get a pointon the HUD that corresponds to "physical" in-game location of the object that the radar is tracking, no matter the approach angle and/or distance (obviously, if it's in the detection distance of the radar).

0

There are 0 best solutions below