I'm trying to write a code (in-game scripts) that takes data from radar (distance, azimuth, elevation) and displays points on a HUD in front of the player. Yesterday, I've managed to visualize radar data on a top-down display (i.e. projecting target locations on a 2D map). Today, I'm trying to draw a point on a HUD that is in front of the player, so it will overlay the actual object in-game (how HUDs kinda usually works).
To my disposal are:
- data from radar (distance $d$, azimuth $\alpha$, elevation $\epsilon$) of the target in radians
- $(X,Y)$ coordinates of my vehicle
- roll $\phi$, pitch $\theta$, yaw $\psi$ (in radians) of my vehicle
So far, I am super close to having it working. But the closer I am to the target, the higher (Y axis on HUD) the point goes, and I can't figure out why. This is what I'm using currently (and sorry for half-mathematical half-programmer syntax, my math background is barely high school):
- get original "local" unrotated radar data $l\alpha = sin(\alpha)$ for azimuth and $l_\epsilon = cos(\epsilon)$ for elevation
- rotate both azimuth and elevation along Y axis (using matrix rotation, however the in-game engine does not support matrix operations so I had to dissect it) around roll and pitch to get my "new" $(X,Y)$ coordinates $$ target_x = ((l_\alpha * cos \phi) + (l_\epsilon * sin \phi)) * d\\ target_y = (-(l_\alpha * sin \theta) + (l_\epsilon * cos \theta)) * d $$
- draw the points on the screen, for $X = (screenWidth / 2) - (target_x / 2)$ and for $Y = target_y / 2$
As my understanding of all of this goes, I basically need to get the radar's elevation to become my display's Y axis, and my radar's azimuth angle to become X axis on the screen. And we do that by taking those measurements and rotating it "around" my vehicle's roll and pitch values (since they are my "new" $X,Y$ axes).
I'm not 100% certain if I'm even doing this correctly, for example I'm multiplying both axes with $d$ but that shouldn't be the case (maybe if I want to scale the points based on distance but I don't need that). Because in this version, the closer I get to the target, the greater the error is. If anyone would be interested, here is the relevant part of the code in Lua: pastebin
Am I missing something super basic (granted, I've learned about matrices literally yesterday), or some concept is totally unbeknown to me.. I've read that something like vector plane intersection might be a solution for this, but as I've mentioned, math is extremely not my thing and I had no idea what I was looking at while checking out that. Any help will be appreciated, thanks :)