Before I start, I would like to put forth that I would count Math amoungst my worst subjects - and Geometry amoungst my worst fields in Math. This partially has to do with the fact I took Geometry back when I was in eight-grade, and that, like every thirteen year-old, I was an idiot who did not care about learning. As such, there might be mistakes in the method I am about to put forth; all I know is that it works, and that to my mind, it seems correct. Feel free to mention any mistakes you find.
Consider the following scene; you are the entity marked "P," there is another entity marked "E," and there is the origin named "O:"
[IMG]http://i133.photobucke*****m/albums/q65/attilathedud/WorldToScreen%20Pictures/img1.jpg[/IMG]
Strip away the buildings and all the fancy graphical effects, and this boils down to:
[IMG]http://i133.photobucke*****m/albums/q65/attilathedud/WorldToScreen%20Pictures/img2.jpg[/IMG]
Now knowing this, how are we to translate the entity E's position into a 2D coordinate? Well the first measure is to understand what we actually mean. There is no way to directly translate a 3D coordinate into a 2D coordinate, just as there is no way to directly translate a cube into a square; however, notice the word "directly." Let us continue with the example of a cube - there obviously has to be a way to translate a cube into a square, or at least into a 2D space, otherwise something like:
[IMG]http://i133.photobucke*****m/albums/q65/attilathedud/WorldToScreen%20Pictures/img3.png[/IMG]
would simply not be possible. So how do we go about this? Whenever we choose to draw a 3D object on a 2D plane (such as paper), we must first pick a view-port, which is exactly what it sounds like - a static view of the scene with a fixed source. To turn a cube into a square, we simply pick the view-port to be directly facing one of the sides of the cube, as so:
[IMG]http://i133.photobucke*****m/albums/q65/attilathedud/WorldToScreen%20Pictures/img4.jpg[/IMG]
To model the earlier model (the common drawing that is used to represent a cube), we simply place the view-port so that it looks directly at one of the angles:
[IMG]http://i133.photobucke*****m/albums/q65/attilathedud/WorldToScreen%20Pictures/img5.jpg[/IMG]
This same idea applies to any F.P.S.'s - in fact, it is central to the notion of them. Most modern F.P.S.'s conceive the illusion that as you move, you move your character around the map; this is simply not true. When "moving," all one is doing is adjusting their view-port in relation to the world. To track entity location then, most games make use of a series of 3D vectors that give the location of an entity relative to the origin of the map.
Knowing this, let us now go back to original representation of our scene and make it more accurate:
[IMG]http://i133.photobucke*****m/albums/q65/attilathedud/WorldToScreen%20Pictures/img6.jpg[/IMG]
If we eliminate the Y-axis in our problem (we will assume the map is constantly flat), we can take a top down view of the above image, and greatly simplify our task:
[IMG]http://i133.photobucke*****m/albums/q65/attilathedud/WorldToScreen%20Pictures/img7.jpg[/IMG]
Given that these two entities could be at any location on the map (even though we assume they are always in Quadrant I), it makes sense to stop trying to focus the scene around the map's origin, and instead makes sense to allow our entity P to be the origin - we can do this by subtracting our origin vector and the enemy's origin vector. The result will be the absolute distance of the entity E from the entity P.
Assume vAbsDistance is a three dimensional vector. Let vAbsDistance be equal to: