Thanks for such a quick reply. I am tryin to do basic RTS game so i will obviously need to select units (so create a ray to check if it selects some object under cursor) but i will also need to draw HP bar for example over unit's head which requires the 3d unit location to be converted to screen, to draw the 2D bar just above.
So to achieve first i created a white box with edges on (-60, -13, -60), (60, -10, 60) and this piece of code detects perfectly if the ray hit (the code is called every frame when i move cusor so i see if the "ray hit" text disappears or shows up). "s" is mouse xy:
D3DVIEWPORT9 vp;
d3ddev->GetViewport(&vp);
D3DXMATRIX view;
D3DXMATRIX proj;
d3ddev->GetTransform(D3DTS_VIEW, &view);
d3ddev->GetTransform(D3DTS_PROJECTION, &proj);
float px = (((2.0f * s.x) / vp.Width) - 1.0f) / proj(0, 0);
float py = (((-2.0f * s.y) / vp.Height) + 1.0f) / proj(1, 1);
Ray ray;
ray._origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
ray._direction = D3DXVECTOR3(px, py, 1.0f);
D3DXMATRIX viewInverse;
D3DXMatrixInverse(&viewInverse, 0, &(view));
D3DXVec3TransformCoord(&ray._origin, &ray._origin, &viewInverse);
D3DXVec3TransformNormal(&ray._direction, &ray._direction, &viewInverse);
D3DXVec3Normalize(&ray._direction, &ray._direction);
if (D3DXBoxBoundProbe(&D3DXVECTOR3(-60, -13, -60), &D3DXVECTOR3(60, -10, 60), &ray._origin, &ray._direction))
{
RECT rect = { 20, 400, 220, 450 };
font.printf(rect, D3DCOLOR_XRGB(123, 123, 123), "RAY HIT");
}
I assume it is enough to loop through all objects and check if D3DXBoxBoundProbe returns true with their collision box and this way i will select unit (is it the optimal way tho?).
I understand its all about multiplying matrices, but I have to admit that i dont fully understand few d3d things. If we are in 3D world, why are matrices 4x4 and not 3x3? Lookin at functions "D3DXVec3TransformNormal" and "D3DXVec3TransformCoord" i see they differ only by "w" value 0/1. What is the magical "w" (msdn doesnt help )?
Greetz
Edit: Yeah the matrix multiplication order was messed in 3d to screen :) I read also on the xyzw, i get everything now :) Thanks a lot