Game Development Reference
In-Depth Information
5.5W ORLD S PACE M ARKERS
Often in games we want to display 2D information relative to objects in 3D space. We may
want to display icons that represents targets, information about the game object such as its
health, its distance to the player, whether it is friendly or not, or any other information we
wish to present to the player.
For each entity in our world (or in the active section of the world if it is partitioned) we will
compute the distance to the player.
The direction from the player to the entity is calculated by subtracting the entity's position
from the players'. Now we can use this vector to compute the dot product against the play-
er's own forward vector.
Recall the property of the dot product , if the dot product is equal to 0, the vectors are per-
pendicular, if it's less than zero, the angle between the vectors is greater than π/2 (90 de-
grees) this information allows us to ignore or handle differently the case in which entities
are not in front of the player. In fact we need to check if the dot product falls outside of the
camera's horizontal field of view to make sure that as long as the entity is within the view
frustum we consider it.
vector3 dirToEntity = (entity->Position() - playerPosition);
float distance = dirToEntity.Length();
dirToEntity.Normalize();
float horizontalFOV = camera.HorizontalFieldOfView() * 0.5f;
if (dot > horizontalFOV)
{
screenPosition = math::vector2(device->GetViewport().Project(entity->Position(), camera.Projection(), camera.View(),
math::matrix::Identity));
}
Oncewehavedeterminedthattheentityisindeedwithinthecamera'sviewfrustumwepro-
ject his world space coordinates into screen space. We do not need the distance to the entity
for anything else than displaying this information to the user, if it's not required to display
it we can avoid computing it. We can optimize this situation by providing a vector function
Search WWH ::




Custom Search