# Can't figure out why my mouse picking code doesn't work

I am trying to create code to check for intersections between bounding spheres and the mouse. I get a mouse ray but it doesnt seem to point in the direction you think it should Attached is a screenshot and code. If anyone can help I would appreciate it as this is driving me crazy.

    auto state = m_mouse->GetState();

float mouseX = state.x;
float mouseY = state.y;
float m_screenWidth = m_deviceResources->GetScreenViewport().Width;
float m_screenHeight = m_deviceResources->GetScreenViewport().Height;

// Normalized device coordinates
float x = (2.0f * mouseX) / m_screenWidth - 1.0f;
float y = 1.0f - (2.0f * mouseY) / m_screenHeight;

DirectX::XMMATRIX inverseviewproj = DirectX::XMMatrixInverse(nullptr, viewMatrix * projectionMatrix);

DirectX::SimpleMath::Vector3 origin = DirectX::SimpleMath::Vector3(x, y, 0);
DirectX::SimpleMath::Vector3 farPoint = DirectX::SimpleMath::Vector3(x, y, 1);

DirectX::SimpleMath::Vector3 rayorigin = DirectX::XMVector3TransformCoord(origin, inverseviewproj);
DirectX::SimpleMath::Vector3 rayend = DirectX::XMVector3TransformCoord(farPoint, inverseviewproj);
DirectX::SimpleMath::Vector3 raydirection = (rayend - rayorigin);
raydirection.Normalize();

rayComponentHandle->position = rayorigin;
rayComponentHandle->direction = raydirection;

• Can you attach a mesh to your ray so you can see how it's moving around? – Jay Jan 16 at 12:29
• Yeah there is a cube attached to the origin which follows the mouse its just the direction that seems off. – Hayden Morris Jan 17 at 8:00
• I would use that to do a bit of troubleshooting. Eg, put the mouse at (0,0) and see where is the ray? Try at centre screen. Try moving on the y-axis and the x-axis. See how your cube responds – Jay Jan 17 at 8:26
• A tip: (I don't know if this will work) In your origin/farpoint, use the near-plane and far-plane instead of 0 and 1 – Jay Jan 17 at 8:28