# D3DXVec3Project returns odd values

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

D3DXVECTOR2 origin = D3DXVECTOR2(
(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)

D3DXVECTOR2 origin = D3DXVECTOR2(
(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!!!

• -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. Jan 11 '12 at 1:52
• but screen height and screen width are global constant integers. they are never zero. 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). 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. Jan 11 '12 at 9:24
• How do you print the values? Are they from the debugger or from printf/std::cout? Jan 11 '12 at 10:44

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