This is a port of this Ogre page. It was created for Mogre 1.7.
One way to have HUD Elements like targeting indicators or playernames positioned on 3D objects is projecting the 3D position to 2D and positioning the HUD Elements there. The following code can be used to do that.
Table of contents
Projecting position
// using MOGRE; // returns a vector with x,y with clamped screencoords in [-1;1] public Vector2 ProjectPos (Camera cam, Vector3 pos) { Vector2 ret = new Vector2(); Vector3 eyeSpacePos = cam.GetViewMatrix(true) * pos; if (eyeSpacePos.z < 0) { Vector3 screenSpacePos = cam.ProjectionMatrix * eyeSpacePos; ret.x = screenSpacePos.x; ret.y = screenSpacePos.y; } else { ret.x = (-eyeSpacePos.x > 0) ? -1 : 1; ret.y = (-eyeSpacePos.y > 0) ? -1 : 1; } return ret; }
Notes
- to check if the object is visible, check if the projected x,y are in -1;1 if not, it's behind the camera