Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would have thought that if the object is on-screen that this function should return screen coordinates. When used in conjunction with the directX draw text function, it works fine. Textual overlays track the 3D objects perfectly, however consider the following code:

D3DXVec3Project(&xyCoords, &HeadPosition(), &viewport, &matProjection, &matView, &matFinal);//for thought bubble

    (0.0f / SCREEN_WIDTH) * 2.0f - 1.0f,
    (0.0f / SCREEN_HEIGHT) * 2.0f - 1.0f);

    (xyCoords.x / SCREEN_WIDTH) * 2.0f - 1.0f,
    (xyCoords.y / SCREEN_HEIGHT) * 2.0f - 1.0f);

lineRenderer.Draw2DLine(origin, headPos, D3DXCOLOR(1, 0, 0, 1), D3DXCOLOR(1, 1, 0, 1));

origin has a value of -1, -1 which is fine (texture coords for the screen go from -1 to 1 in each axis) but headPos has the value of -1.#IND000 in both the x and y. I think this may be because D3DXVec3Project is returning MASSIVE numbers, although I could be wrong.

Who knows what's going on under the hood of DrawText_A in the following code, but it clearly compensates for whatevers not working with the above code.

D3DXVec3Project(&xyCoords, &HeadPosition(), &viewport, &matProjection, &matView, &matFinal);

    rct.left   = xyCoords.x;
    rct.right  = xyCoords.x;    = xyCoords.y;
    rct.bottom = xyCoords.y;

    m_font->DrawTextA(NULL, stateStream.str().c_str(), -1, &rct, DT_LEFT|DT_NOCLIP ,   D3DXCOLOR(1,0,0,1));

This all works perfectly. Where am I going wrong with the first bit of code?

EDIT - the code below gives me the following values (also listed below)

    (0.0f / 1280.0f) * 2.0f - 1.0f,
    (0.0f / 720.0f) * 2.0f - 1.0f);

    (xyCoords.x / 1280.0f) * 2.0f - 1.0f,
    (xyCoords.y / 720.0f) * 2.0f - 1.0f);

for origin I get (-1, 1.2) and for headPos I get (0, 1.401e-043#DEN). FYI HeadPosition() will return a vector3 that has a 0 z component and x and y values from about 2 to roughly 33.

Now how on earth can me with a calculator be better than the computer? what am I missing?

0.0f / 720.0f) * 2.0f - 1.0f should clearly = -1, not 1.2! what is going on?

0 / anything = 0, 0 * 2 = 0, 0 - 1 = -1!!!

share|improve this question
-1.#IND000 is negative infinity which is usually the result of a divide by zero. It's possible that DrawTextA is simply clamping the input coordinates to the screen rectangle, so you still get something valid if you pass a bogus coordinate. – Nathan Reed Jan 11 '12 at 1:52
but screen height and screen width are global constant integers. they are never zero. – SirYakalot Jan 11 '12 at 8:42
Check the value of xyCoords. It could be coming out infinite due to a divide by zero inside D3DXVec3Project (i.e. the parameters to that call are wrong in some way). – Nathan Reed Jan 11 '12 at 8:52
but then why would the second block of code I posted work every time? same variable, same function call. – SirYakalot Jan 11 '12 at 9:24
How do you print the values? Are they from the debugger or from printf/std::cout? – sam hocevar Jan 11 '12 at 10:44
up vote 4 down vote accepted

-1.#IND means division by 0, make sure that SCREEN_WIDTH and SCREEN_HEIGHT have a non-zero value.

share|improve this answer
they do. In fact this is the strange thing, if I work out the result on a calculator I get a proper number, but the code returns strange stuff. – SirYakalot Jan 11 '12 at 8:41
try printing out xyCoords, I think if the value is infinity it also creates a division by 0 error. – Robert Rouhani Jan 12 '12 at 2:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.