Target-Tracking HUD: How To?

Started by
2 comments, last by zedz 14 years, 11 months ago
Hello, I'm currently writing a sort of sci-fi flight sim, and I'm encountering some problems. I'm used to creating games where the camera is viewing directly down onto the scene, which is rendered with a glOrtho() projection. Drawing the HUD was simply a matter of disabling depth testing and rendering last the alpha-blended quads that make up the HUD. This new game is in full 3D. Drawing a stationary HUD is easy enough; this just requires a switch from a gluPerspective() to a glOrtho() view. However, some items on the HUD must be able to track and move with certain fighters. Initially what I did was position the HUD marker/tracker at the camera position, move it towards the fighter, and then orient the marker to face the camera. This would all be done with a perspective projection. While this works, that marker is subject to the perspective distortions. It could be a neat effect, but not what I'm looking for. A solution could be to lower my FOV, which is currently at 60, but the game seems to look better with this FOV than, say, 40 or 50. I could also convert the perspective coords of the fighter being tracked into orthogonal coords, and then render the marker at those new coords with an ortho projection, but I'm unsure how to do that. Could someone please shed some light on this problem? Thanks a lot.
Advertisement
I believe the function your looking for is gluProject() (and gluUnProject() todo the opposite)! Go check it out on google. Basically it takes 3D world cords, and transforms then into screen cords so you can simply draw you sprite in 2D at the position it tells you (it returns -1 for offscreen from memory).

One thing though - make your you pass in the correct proj/view matrix! I had alot of trouble early on trying to call this halfway through update when the viewport was still set to glOrtho and it all went to hell. Just make sure you get your view/projection matrix after the 3D camera is setup (if you use gluLookAt() then storing the matrix from directly after that call is probably perfect).
Hmmm...I see what you mean. I've written the following...

Vector2 windowCoords = realToWindow(targetPos);   //use gluProjectsetupOrtho();                                     Vector3 hudCoords = windowToReal(windowCoords);   //gluUnProjectrenderMarkerAt(hudCoords);

...and it works great! The only problem is that when you face the direction opposite from the fighter, the marker still shows on the other side. But that's just a matter of hiding it when the camera can't see the fighter. Thanks very much!
just do
if ( dot( forward_dir, normal(fighter-camera_pos) ) < 0.0 )
dont draw

This topic is closed to new replies.

Advertisement