Sign in to follow this  

help: 2d HUD Overlay for distant 3d objects (to show waypoints far off),

Recommended Posts

Yablargo    122
Hello. I am writing a 3d space game as part of as class project and we used the pop framework by rudy rucker ( was it com?)). The graphics/3d world uses cVector3's, cMatrix'es and each object has an attitude matrix which gets multiplied when that object is drawn. the player has an attitude matrix as well. I used regular openGL texture mapped quads for the HUD, but I am rather stumped on a certain problem. the game has waypoints that are represented by wireframe spheres, but when one is far away I want to draw a square or something to show the player where he needs to go... Im having trouble translating the 3d coordinates onto an onscreen 2d coord. I can find distance to the object, Distance and direction to it. as well as the normal, binormal and tangent directions of the player and the object. Im just having trouble figuring out how to use all of this to get an accurate 2d position of where an object is on the screen. Can anyone help point me in the right direction? Thanks :) I know its not the best worded question but if someone replies for specifics i can do my best to help supply more specific info.

Share this post

Link to post
Share on other sites
DirectXFreak    166
Are you looking to translate a 3D vector on the screen? That simply requires some 3D math, here's the code:

POINT pTransform3Dto2D(const CVector &vPoint)
// variables
float fClip_x, fClip_y;
float fXp, fYp, fWp;
DWORD dwWidth, dwHeight;

dwWidth = m_VP[m_nStage].dwWidth;
dwHeight = m_VP[m_nStage].dwHeight;

fClip_x = (float)(dwWidth >> 1);
fClip_y = (float)(dwHeight >> 1);

fXp = (m_mViewProj._11*vPoint.x) + (m_mViewProj._21*vPoint.y)
+ (m_mViewProj._31*vPoint.z) + m_mViewProj._41;
fYp = (m_mViewProj._12*vPoint.x) + (m_mViewProj._22*vPoint.y)
+ (m_mViewProj._32*vPoint.z) + m_mViewProj._42;
fWp = (m_mViewProj._14*vPoint.x) + (m_mViewProj._24*vPoint.y)
+ (m_mViewProj._34*vPoint.z) + m_mViewProj._44;

float fWpInv = 1.0f / fWp;

// transform from [-1,1] to actual viewport dimentions
pt.x = (LONG)( (1.0f + (fXp * fWpInv)) * fClip_x );
pt.y = (LONG)( (1.0f + (fYp * fWpInv)) * fClip_y );

return pt;

I hope you can get something out of this, I can't explain all the math there, just try and work it out if you're confused.
I hope this helps.

Share this post

Link to post
Share on other sites
Nitage    1107
If you require something like text above an object in the distance, the easiest way to do this is without actually leaving your 3D projection.

If you use text written on to a billboarded quad (that is a quad that is always rotated toward the camera) then not only will the text always be correctly oriented, but the text will automatically scale with distance (thanks to the 3D perspective calculations).

So Google for billboarding + OpengGL.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this