Help WorldToScreen

Posts 1–4 of 4 · Page 1 of 1
Help WorldToScreen
I'm trying to create a WorldToScreen, this is my function:

Code:
function WorldToScreen(pDev: IDirect3DDevice9; vWorld: TD3DXVECTOR3; pos: TD3DXVECTOR3): bool;
var
projection, view, world: TD3DXMATRIX;
g_ViewPort: TD3DViewport9;
begin
        pDev.GetViewport(g_ViewPort);
        pDev.GetTransform(D3DTS_VIEW, view);
        pDev.GetTransform(D3DTS_PROJECTION, projection);
        pDev.GetTransform(D3DTS_WORLD, world);
        pDev.GetViewport(g_ViewPort);

         g_ViewPort.X := 0;
         g_ViewPort.Y := 0;
         g_ViewPort.MinZ := 0;
         g_ViewPort.MaxZ := 1;

        D3DXVec3Project(Pos, vWorld, g_ViewPort, projection, view, world);

     	if (Pos.z < 1.0) and (Pos.x > 0.0) and (Pos.y > 0.0) and (Pos.x < g_ViewPort.Width) and (pos.y < g_ViewPort.Height)
  then begin
		vWorld.x := pos.x;
		vWorld.y := pos.y;
		vWorld.z := pos.z;
    result := true;
  end else begin
    result := false;
  end;
end;
But it doesn't convert properly to a 2D position.

The interesting fact is that FOV works perfectly, so, the function only returns TRUE if the player is in my field of view.

Is the problem at the matrixes or at the 3d player position?

If the problem is at matrixes, can someone help me to get ViewMatrix, ProjectMatrix and WorldMatrix of CrossFire?

Thanks
Why do you set the ViewPort dimensions to zero?

Code:
bool worldToScreen(IDirect3DDevice9 &device, D3DXVECTOR3 worldPos,
    D3DXVECTOR2 &screenPos)
{
    D3DXMATRIX view, projection, world;
    D3DVIEWPORT9 viewPort;
    if ((device.GetTransform(D3DTS_VIEW, &view            ) != D3D_OK) ||
        (device.GetTransform(D3DTS_PROJECTION, &projection) != D3D_OK) ||
        (device.GetTransform(D3DTS_WORLD, &world          ) != D3D_OK) ||
        (device.GetViewport(&viewPort)                      != D3D_OK))
    {
        return false;
    }
    D3DXVECTOR3 screenPos3D;
    D3DXVec3Project(&screenPos3D, &worldPos, &viewPort, &projection, &view, &world);
    if (screenPos3D.z >= 1)
    {
        return false;
    }
    screenPos.x = screenPos3D.x;
    screenPos.y = screenPos3D.y;
    return true;
}
Edit: As you are using Delphi, you have to make 'pos' an output parameter:
Code:
function WorldToScreen(pDev: IDirect3DDevice9; vWorld: TD3DXVECTOR3; var pos: TD3DXVECTOR3): Boolean;
Without the 'var' modifier you will work on a local copy.
ohh bad codding looks like this...

you can do all that stuff in 5-6 lines..

Code:
bool WorldToScreen(LPDIRECT3DDEVICE9 pDev, CPlayer* pPlayer, D3DXVECTOR3* Pos)
{
D3DXVECTOR3 vWorld(pPlayer->Object->x ,pPlayer->Object->y, pPlayer->Object->z);
D3DVIEWPORT9 viewPort = {0};
D3DXMATRIX projection, view, world;
pDev->GetTransform(D3DTS_VIEW, &view);
pDev->GetTransform(D3DTS_PROJECTION, &projection);
pDev->GetTransform(D3DTS_WORLD, &world);
pDev->GetViewport(&viewPort);

D3DXVec3Project(Pos, &vWorld, &viewPort, &projection, &view, &world);
if(Pos->z < 1)
{
return true;
}
return false;
}
i got this from google becouse i'm too lazy to get from my base, anyway, works and is more clean.
Quote Originally Posted by XarutoUsoCrack View Post
ohh bad codding looks like this...
you can do all that stuff in 5-6 lines..
WTF is wrong with you guy? Your code sucks big time. Sure you can do this in even one line, but small code is not always good code. Most times it's even the other way around.

Just some facts why my code is much cleaner than yours:
  1. You pass the device as pointer. There is no need to modify the device reference from inside the function, so just use a reference instead of a pointer!
  2. You do not check any error code. Thats VERY bad coding style. You should ALWAYS check error codes, even if you need some more lines!
  3. You use D3DXVector3 as your output parameter. Thats very confusing because everyone expect 2D coordinates as the result.
  4. Your function only works for very specific player objects.


Anyways the real problem in noobinhos implementation was the missing var modifier.
Posts 1–4 of 4 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Talk with us