Results 1 to 6 of 6
  1. #1
    You do not feel comfortable sailer or at home
    MPGH Member
    sn1p3ro12's Avatar
    Join Date
    Feb 2009
    Gender
    male
    Posts
    1,104
    Reputation
    10
    Thanks
    198

    ScreenToWorld / deproject function (Casting a ray into the screen)

    Been working quite some time on a method to go from a screen location to a location in the world. So I can for example find what my crosshair is pointing at. When I get the time I might implement matrix functions so you no longer need directX. Below is the code I currently use.

    What this does

    *It takes a pixel on the screen and converts it into a directional vector in world space.
    *This directional vector can be used to create a ray
    *The ray can in turn be used to find a location in world space
    *A good use of this would be to cast a ray from your crosshair


    You will need

    *DirectX headers and lib linked
    *The proper HUD class released by me HERE

    Code:
    D3DXVECTOR3 FVectorToD3DVector(FVector* in)
    {
        return D3DXVECTOR3(in->X, in->Y, in->Z);
    }
    
    FVector ScreenToWorld(float screenX, float screenY, float screenWidth, float screenHeight,  FMatrix* EngineViewProjection)
    {           
                D3DXMATRIX ViewProjection;
                memcpy(&ViewProjection, EngineViewProjection, sizeof(D3DXMATRIX));  //Get the D3D view projection  
            
                D3DXMATRIX View;
                FVector Forward, Up, Right;
                GetAxes(PlayerController->PlayerCamera->Rotation,&Forward, &Right,&Up); // Get where the camera axes are pointing                        
                D3DXMatrixLookAtLH(&View, &D3DXVECTOR3(0,0,0) , &FVectorToD3DVector(&Forward),  &FVectorToD3DVector(&Up)); // Build a D3D view matrix from found up and forward vector
    
                D3DXMATRIX inverseView;
                D3DXMatrixInverse(&inverseView, NULL, &View); // inverse view matrix
                
                D3DXMATRIX projecti********;
                projecti******** = inverseView * ViewProjection; // calculate the projection matrix NOTE: this only really needs to be done once.             
                    
                //Start Ray Calculation
                float w  = screenWidth ;
                float h  = screenHeight;
                
                FVector v;
                v.X =  ( ( ( 2.0f * screenX ) / w  ) - 1 ) / projecti********._11;
                v.Y = -( ( ( 2.0f * screenY ) / h ) - 1 ) / projecti********._22;
                v.Z =  1.0f;
                
                FVector rayDir;
                rayDir.X  = v.X*inverseView._11 + v.Y*inverseView._21 + v.Z*inverseView._31;
                rayDir.Y  = v.X*inverseView._12 + v.Y*inverseView._22 + v.Z*inverseView._32;
                rayDir.Z  = v.X*inverseView._13 + v.Y*inverseView._23 + v.Z*inverseView._33;
                //End Ray Calculation
                
                return rayDir;
    }

    How to use

    Here is how I use it.

    Code:
            float distance = 10000.0f // 100m
                FVector AimDir = ScreenToWorld( pCanvas->ClipX / 2.0f, pCanvas->ClipY / 2.0f, pCanvas->ClipX, pCanvas->ClipY, &PlayerController->myHUD->m_OldViewProjecti********); // you need to fix the HUD class in the playercontroller or this won't work.
                FVector AimingPoint = PlayerController->PlayerCamera->Location + AimDir *  distance; // create target
    Extra info

    I am calculating the projection matrix all the time here, you really only need to calculate this once for effeciency, this means you can save on matrix inversions in the future as well
    The ******** into the codes means Projection Matrix without the space

    Thanks

    Credits:
    Dogmatt
    Fatboy88
    EddyK
    Last edited by Margherita; 08-27-2011 at 08:59 AM.
     


    DONT FORGET TO THANK ME

  2. #2
    ofir001's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Posts
    1
    Reputation
    10
    Thanks
    0
    I new what I was using it runs the software that anything please help me
    need Dev c/C++?

  3. #3
    Margherita's Avatar
    Join Date
    Jan 2011
    Gender
    female
    Posts
    11,299
    Reputation
    783
    Thanks
    1,287
    My Mood
    Bashful
    Nice job, the function of this is pretty interesting
    PM Me | VM Me | Rules

    MARGHERITA

  4. #4
    Eduardkhil's Avatar
    Join Date
    Jul 2011
    Gender
    male
    Posts
    13
    Reputation
    10
    Thanks
    2
    I don't really mind you re-releasing this here. I mean, if I didn't want the code to be used I would've kept it private. You could've been a little more clear in your post about the fact that you didn't write this though, and put me as the author a little clearer, instead of as a footnote. The same counts for the w2s code that dogmatt wrote. The post reads as if you wrote this.

    View O1GKUU.png on ScreenSnapr

  5. #5
    Margherita's Avatar
    Join Date
    Jan 2011
    Gender
    female
    Posts
    11,299
    Reputation
    783
    Thanks
    1,287
    My Mood
    Bashful
    Fixed credits, it was pretty clear, but be more clear next time sn1p
    PM Me | VM Me | Rules

    MARGHERITA

  6. #6
    Threadstarter
    You do not feel comfortable sailer or at home
    MPGH Member
    sn1p3ro12's Avatar
    Join Date
    Feb 2009
    Gender
    male
    Posts
    1,104
    Reputation
    10
    Thanks
    198
    Quote Originally Posted by SODMG View Post
    Fixed credits, it was pretty clear, but be more clear next time sn1p
    Ok bro next time i becarefull thank you !!
     


    DONT FORGET TO THANK ME

Similar Threads

  1. How to make your program minimize into the task bar?
    By deathninjak0 in forum Visual Basic Programming
    Replies: 8
    Last Post: 11-29-2009, 05:01 AM
  2. does any1 know how to get into the port glitch?
    By shiffty120 in forum CrossFire Hacks & Cheats
    Replies: 8
    Last Post: 07-11-2009, 06:18 AM
  3. Got into the beta
    By Synns in forum Battlefield Heroes Hacks
    Replies: 24
    Last Post: 04-06-2009, 07:30 AM
  4. do i need to extract both links into the folder???
    By Architrex13 in forum Suggestions, Requests & General Help
    Replies: 0
    Last Post: 12-26-2008, 05:13 PM
  5. How to blend sig into the background of forum?
    By Zhellbound in forum Art & Graphic Design
    Replies: 4
    Last Post: 09-14-2008, 04:33 PM