Print

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

 


Alias: MOGRE_Projecting_3D_position_to_2D


Contributors to this page: jacmoe111451 points  , OgreWikiBot and Beauty5965 points  .
Page last modified on Monday 01 of August, 2011 00:24:04 GMT by jacmoe111451 points .


The content on this page is licensed under the terms of the Creative Commons Attribution-ShareAlike License.
As an exception, any source code contributed within the content is released into the Public Domain.