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.

Ray Error

    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 viewMatrix = DirectX::XMMatrixTranspose(DirectX::XMLoadFloat4x4(&cameraComponentHandle->view));
    DirectX::XMMATRIX projectionMatrix = DirectX::XMMatrixTranspose(DirectX::XMLoadFloat4x4(&cameraComponentHandle->projection));
    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);

    rayComponentHandle->position = rayorigin;
    rayComponentHandle->direction = raydirection;
  • 1
    \$\begingroup\$ Can you attach a mesh to your ray so you can see how it's moving around? \$\endgroup\$ – Jay Jan 16 '19 at 12:29
  • \$\begingroup\$ Yeah there is a cube attached to the origin which follows the mouse its just the direction that seems off. \$\endgroup\$ – Hayden Morris Jan 17 '19 at 8:00
  • \$\begingroup\$ 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 \$\endgroup\$ – Jay Jan 17 '19 at 8:26
  • \$\begingroup\$ 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 \$\endgroup\$ – Jay Jan 17 '19 at 8:28

As it turns out the code is correct and my problem was with the rendering of the ray itself. I was calculating a world matrix when the coordinates were already in worldspace.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.