Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I am writing a 3d space game as part of as class project and we used the pop framework by rudy rucker (rudyrucker.org(or 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
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
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!