Results 1 to 4 of 4
  1. #1
    noobinho123's Avatar
    Join Date
    May 2014
    Gender
    male
    Posts
    1
    Reputation
    10
    Thanks
    0

    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

  2. #2
    Zacherl's Avatar
    Join Date
    May 2009
    Gender
    male
    Posts
    150
    Reputation
    10
    Thanks
    42
    My Mood
    Aggressive
    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.
    Last edited by Zacherl; 05-14-2014 at 11:57 PM.

  3. #3
    XarutoUsoCrack's Avatar
    Join Date
    Apr 2011
    Gender
    male
    Location
    CFAL Honra & Glória Server
    Posts
    1,087
    Reputation
    51
    Thanks
    2,543
    My Mood
    Relaxed
    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.

  4. #4
    Zacherl's Avatar
    Join Date
    May 2009
    Gender
    male
    Posts
    150
    Reputation
    10
    Thanks
    42
    My Mood
    Aggressive

    Thumbs down

    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.

Similar Threads

  1. [Help] DayZ Standalone - WorldToScreen help
    By Anddos in forum General Game Hacking
    Replies: 0
    Last Post: 05-08-2014, 12:25 PM
  2. [Help Request] Combat arms Vid help
    By djw111 in forum Combat Arms Help
    Replies: 4
    Last Post: 12-24-2011, 05:06 PM
  3. [Help Request] AFK Bot [help]
    By fet in forum Combat Arms Help
    Replies: 7
    Last Post: 04-28-2011, 03:17 AM
  4. [Help Request] Ajuda / Help
    By - Battery' in forum Combat Arms BR Coding Help
    Replies: 3
    Last Post: 04-22-2011, 07:15 PM
  5. [Help Request] Help my!
    By Windowns7 in forum Combat Arms BR Coding Help
    Replies: 2
    Last Post: 04-18-2011, 01:41 PM