I have a view matrix via glm. I want to make a ray casting system that receive an X and Y coordinate from the screen and generates a ray in the world. What math is needed?

My view matrix is built with this function:

glm::mat4 Camera::cameraViewMatrix()
    glm::mat4 matrix;

    matrix = glm::rotate(matrix, glm::radians(pitch),     {1, 0, 0});
    matrix = glm::rotate(matrix, glm::radians(yaw),       {0, 1, 0});
    matrix = glm::rotate(matrix, glm::radians(roll),      {0, 0, 1});

    matrix = glm::translate(matrix, glm::vec3(position.x, position.y + height, position.z)*-1.f);

    int width, height;
    SDL_GetWindowSize(display.GetWindow(), &width, &height);

    return glm::perspective(glm::radians(fov), (float)width/(float)height, 0.001f, 1000.f) * matrix;

I'd like to use this to select objects under the cursor.

  • \$\begingroup\$ Related, but also unanswered: gamedev.stackexchange.com/questions/48831/…. I'm surprised not to find a good dupe target for this question, since it's a very common problem, but most of the relevant questions are overly specific to certain scenarios or contexts. \$\endgroup\$ – Josh Sep 11 '17 at 15:30
  • \$\begingroup\$ Thanks for the edit, I looked and remembered my mistakes. But I think I found a mistake "an X" that would be "a". Aboit this rau casting, I think the best would be glm::unproject, normalize the vector and see if it worked. If it worked I would leave a answer with it. Thanks again for heling me unserstanding my grammar mistakes. \$\endgroup\$ – Laceanu George Sep 11 '17 at 20:02

Your Answer

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

Browse other questions tagged or ask your own question.