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;
    rct.top    = 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!!!

  • \$\begingroup\$ -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. \$\endgroup\$ Jan 11, 2012 at 1:52
  • \$\begingroup\$ but screen height and screen width are global constant integers. they are never zero. \$\endgroup\$ Jan 11, 2012 at 8:42
  • \$\begingroup\$ 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). \$\endgroup\$ Jan 11, 2012 at 8:52
  • \$\begingroup\$ but then why would the second block of code I posted work every time? same variable, same function call. \$\endgroup\$ Jan 11, 2012 at 9:24
  • \$\begingroup\$ How do you print the values? Are they from the debugger or from printf/std::cout? \$\endgroup\$ Jan 11, 2012 at 10:44

1 Answer 1


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

  • \$\begingroup\$ 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. \$\endgroup\$ Jan 11, 2012 at 8:41
  • \$\begingroup\$ try printing out xyCoords, I think if the value is infinity it also creates a division by 0 error. \$\endgroup\$ Jan 12, 2012 at 2:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .