Picking In DirectX11 Using Bounding Box

Started by
1 comment, last by Jason Z 7 years, 10 months ago

I want to implement an on board GUI where i first select a area and then i can write something in it but i'm stuck in first part where i have to select Billboard Text,

here is my Code for Picking:


    POINT ptCursor;
    	GetCursorPos(&ptCursor);
    	ScreenToClient(AngelCore::AngelSubSystemResources::WindowProperties::GetHWND(), &ptCursor);
    
    	UINT numOfVP = 1;
    	D3D11_VIEWPORT vp;
    	AngelCore::AngelSubSystemResources::GraphicDeviceResources::GetDeviceContext3D()->RSGetViewports(&numOfVP, &vp);
    
    
    	DirectX::XMFLOAT3 v;
    	v.x = (((2.0f * ptCursor.x) / vp.Width) - 1) / proj4X4._11;
    	v.y = -(((2.0f * ptCursor.y) / vp.Height) - 1) / proj4X4._22;
    	v.z = -1.0f;
    
    	DirectX::XMMATRIX translation = DirectX::XMMatrixTranslation(
    		      DirectX::XMVectorGetX(
    			AngelCore::AngelSubSystemResources::BaseCameraProperties::BCamera.GetCameraPosition())
    			, DirectX::XMVectorGetY(
    				AngelCore::AngelSubSystemResources::BaseCameraProperties::BCamera.GetCameraPosition())
    			, DirectX::XMVectorGetZ(
    					AngelCore::AngelSubSystemResources::BaseCameraProperties::BCamera.GetCameraPosition()));
    
    	DirectX::XMMATRIX worldView = View;
    	DirectX::XMMATRIX inv = DirectX::XMMatrixInverse(nullptr, worldView);
    	DirectX::XMFLOAT4X4 invWorldView4X4;
    	DirectX::XMStoreFloat4x4(&invWorldView4X4, inv);
    
    	DirectX::XMFLOAT3 vPickRayDir;
    	DirectX::XMFLOAT3 vPickRayOrig;
    
    	vPickRayDir.x = v.x * invWorldView4X4._11 + v.y * invWorldView4X4._21 + v.z * invWorldView4X4._31;
    	vPickRayDir.y = v.x * invWorldView4X4._12 + v.y * invWorldView4X4._22 + v.z * invWorldView4X4._32;
    	vPickRayDir.z = v.x * invWorldView4X4._13 + v.y * invWorldView4X4._23 + v.z * invWorldView4X4._33;
    	vPickRayOrig.x = invWorldView4X4._41;
    	vPickRayOrig.y = invWorldView4X4._42;
    	vPickRayOrig.z = invWorldView4X4._43;
    
    	DirectX::XMFLOAT3 rayObjOrigin, rayObjDirection;
    
    	DirectX::XMVECTOR rayOrig =  DirectX::XMVector3TransformCoord(DirectX::XMVectorSet(
    		vPickRayOrig.x, vPickRayOrig.y, vPickRayOrig.z, 1.0f), invWorld);
    
    	DirectX::XMVECTOR rayDir = DirectX::XMVector3TransformNormal(DirectX::XMVectorSet(
    		vPickRayDir.x, vPickRayDir.y, vPickRayDir.z, 1.0f), invWorld);
    
    	rayDir = DirectX::XMVector3Normalize(rayDir);

    float dist = 0;
    	if (b.Intersects(rayOrig,
    		rayDir, dist))
    	{
    		return true;
    	}

Here is the details:

Matrix translation do nothing.

"b" is the bounding box from the object that i want to check for ray intersection.

I also pass inverse world matrix to this function.

My code won't work correctly,it's like bounding box is moving with camera,every time i move camera it shows intersection in different position!

Advertisement

// Get Picking Ray from Camera

        Public Function GetPickingRay(sp As Vector2) As Ray
            Dim v1 As Vector3
            Dim v2 As Vector3           
            Dim vp as Matrix = GetViewProjMatrixFromCamera()
            Dim Point As Vector3 = New Vector3(sp.X, sp.Y, 0)
            v1 = Vector3.Unproject(Point, Viewport.X, Viewport.Y, Viewport.Width, Viewport.Height, Viewport.MinZ, Viewport.MaxZ, vp)
            Point.Z = 1
            v2 = Vector3.Unproject(Point, Viewport.X, Viewport.Y, Viewport.Width, Viewport.Height, Viewport.MinZ, Viewport.MaxZ, vp)

            Dim Loc As Vector3 = v1
            Dim Dir As Vector3 = Vector3.Normalize(v2 - v1)

            Return New Ray(Loc, Dir)
        End Function

...


// Get Ray at Mouse Position
    Private Function GetTransformedRay(MousePos As Vector2, worldMat As Matrix, Editor As TEditor) As Ray
        Dim cRay As Ray = Editor.MyView.Camera.GetPickingRay(MousePos)
        Dim InvMat As Matrix = Matrix.Invert(worldMat)
        Return New Ray(Vector3.TransformCoordinate(cRay.Position, InvMat), Vector3.TransformNormal(cRay.Direction, InvMat))
    End Function




Here some Code, how I am doing ray picking an object. Using the GetTransformRay function you will get the Ray in ObjectSpace for the Object you want to test.

To the OP: Do you understand what the code is supposed to be doing? If you are trying to create a ray and intersect it with a bounding box, are you sure that both the ray and the box are in the same coordinate space (i.e. view space, world space, projection space, clip space, etc...)?

Try to map out what you are actually trying to accomplish, and break the overall task into smaller pieces that you can verify step by step. If you try to do the whole task at once, then it is very difficult to debug. On the other hand, with smaller tasks, you can more easily verify that each of them is doing what you expect. I find that this often forces me to understand the algorithm I am implementing far better as well.

This topic is closed to new replies.

Advertisement