This library contains a function that will project any UI point down onto the world. It requires knowing the player's camera target, distance, yaw, pitch, roll, fov and their resolution. Additionally it assumes the terrain is flat. Arbitrary terrain could be done with tracelines.
It's original purpose as a mouse tracker has been made obsolete but one person has requested it. The inverse function, mapping world to UI coordinates has more uses and I'll see if I can get that up soon [EDIT: Done].
Added World to UI Projection. See picture below
Added an intialization function that must be run every time camera position or orientation changes
The example map uses the mouse coordinates to pick the UI point to project onto the world to place the baneling under the cursor. It is not using the Mouse World Pos. Also the baneling's position is updated regardless if the mouse is moved, so moving the camera with arrow keys will also move the baneling to the correct position. Camera orientation can be altered with holding right click, distance with W/S, field of view with Q/E, height offset with mouse side buttons and roll with A/D.
After placing the unit, a dialog is moved to the location of the baneling. This function is not using the MouseUI X/Y but the World -> UI projection from the calculated position of the unit. The same is done for a tank unit on the map.
Great work. The code looks pretty clean, too. I don't think that further optimization is needed (I don't know if Galaxy has some sort of auto-optimization, but I guess not), because it is already nearly as fast as using "Mouse World Pos". Maybe it can be changed, so that the cos() and sin() values are only recomputed, when the angle of the camera axis actually changes, otherwise there are only few multiplications to save.
Projecting world to UI coordinates sounds more interesting indeed. I played with the thought of using this once for an UI that pops up "on top" of a unit, but that idea had some disadvantages. I would be interesting what people would come up with.
Out of interest, if you already have the Mouse World Coordinate (from mouse tracking), what is the minimum code required to project this world coordinate to the UI?
I have a dummy unit moving with the mouse, but can't think of a simple way to translate that to the UI.
The mouse already does this; you can get both it's world and UI coordinates. I only use the baneling example to show how accurate the functions are. This library is useful if you don't/can't use the mouse for that purpose.
For instance, in the example map I have an arrow that points at the siege tank no matter where it is. Since I'm not following the siege tank with the mouse, there is no easy way to know it's position on the screen and that's where the library comes in.
Additionally you can know where the mouse is pointing on the world even if you don't move it, such as if the camera moves but the mouse doesn't.
You'd want to use this library any time you want UI elements that interact with elements in the world, such as by having custom unit bars or have a command card appear on top of a unit.
Here is how you get the UI coordinates from the mouse moved event. To move the dialog to the exact cursor's position, you need to know the player's resolution. Simply using the Mouse UI coordinates will not work because dialogs are scaled down in-game depending on the player's resolution and aspect ratio.