0
votes

i have a 3D scene with three equally spaced boxes, i want the camera to be able to rotate around the camera target and move in any direction, typically like the camera in a 3D modeling application, however my setup works only when my camera target is positioned at the origin, whenever i move the camera at any direction my camera position and camera target translates to the new position correctly, however my camera still rotates around the origin of the scene

the image below shows my camera target and rotation at the origin of coordinate system enter image description here

the image below shows my camera target shifted left however the camera rotates with respect to the origin of the coordinate system enter image description here

the following is my code for camera rotation and movement on x/y plane:

if (event.type == SDL_MOUSEMOTION && leftMouseButtonPressed == true)
{

    New.x = event.button.x;
    New.y = event.button.y;

    delta = New - prev;


    if (delta.x != 0) {

        angleX = delta.x / 2.0f;

        rotMat = glm::mat4(1);
        rotMat =   glm::rotate(rotMat, glm::radians(angleX), up);
        cameraUp = glm::normalize(glm::vec4(up, 1) * rotMat);
        cameraRight = glm::normalize(glm::cross(cameraUp, cameraDirection));
        cameraPos = rotMat * glm::vec4(cameraPos, 1) ;
        cameraDirection = glm::normalize(cameraPos - cameraTarget);

        camera = glm::lookAt(cameraPos, cameraTarget, up);


    }





if (event.type == SDL_MOUSEMOTION && rightMouseButtonPressed == true)
{

    New.x = event.button.x;
    New.y = event.button.y;

    delta = New - prev;


    if (delta.x != 0) {

        delta.x /= 10.0f;

        translateMat = glm::mat4(1);
        translateMat = glm::translate(translateMat, cameraRight * delta.x);
        cameraPos = translateMat * glm::vec4(cameraPos, 1);
        cameraTarget = translateMat * glm::vec4(cameraTarget, 1) ;


        camera = glm::lookAt(cameraPos, cameraTarget , cameraUp);


    }
1
I'm not sure I understand your problem statement; if the yellow dot is supposedly the camera target, why do you want it to face the origin?meowgoesthedog
@meowgoesthedog my camera is shifted towards the negative z axis ( iam using vulkan coordinate system), the middle box model is stationed at the origin as well as my camera target, so technically my camera looks at the origin, my problem is if i move the camera position and camera target at any direction later when i want to rotate around the new camera target my camera still rotates with respect to the initial origin of the scene, you can see the rotation path circle of the camera in both pictures aboveBulBul
Instead of implicitly accumulating the rotation matrix by transforming cameraPos, you should keep track of the total rotation angle itself. It's more straightforward to construct the transformation from scratch than to accumulate it.meowgoesthedog
@meowgoesthedog i also tried that, and calculated the rotation for all three coordinates explicitly, in both cases the rotation does not seem to be aware about the translation that is applied to the camera position and camera target, can you write a pseudocode or code example as an answer so that i can apply the possible fixed?BulBul

1 Answers

1
votes

Accumulate the horizontal and vertical angles (yaw and pitch); every time these change:

  • Create and cache the rotation matrix cameraRot using the yawPitchRoll function.
  • Calculate and cache the camera's front and up directions using this matrix:

    cameraDirection = cameraRot * glm::vec3(-1.0f, 0.0f, 0.0f);
    cameraUp = cameraRot * glm::vec3(0.0f, 1.0f, 0.0f);
    

    Save for floating point precision issues, these will already be normalized.

  • The camera's position can then be calculated from the target using:

    cameraPos = cameraTarget - cameraDistance * cameraDirection;
    

    Where cameraDistance is the distance from the target to the camera.