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); D3DXVECTOR2 headPos = D3DXVECTOR2( (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); D3DXVECTOR2 headPos = D3DXVECTOR2( (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!!!
DrawTextAis simply clamping the input coordinates to the screen rectangle, so you still get something valid if you pass a bogus coordinate. \$\endgroup\$
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\$