0
\$\begingroup\$

I'm trying to create a camera object for my 2D game engine, but I just can't seem to get the view matrix down right.

Here's the code I'm currently using:

glm::mat4 Camera::GetViewMatrix()
{
    glm::mat4 view;
    glm::vec3 camPos = glm::vec3(this->pos, CAM_Z);
    GLfloat xup, yup;

    xup = sin(this->rot);
    yup = cos(this->rot);
    view = glm::lookAt(camPos, camPos + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(xup, yup, 0.0f));

    return view;
}

The result I get from this is as follows Before rotation: Picture 1

After rotation: Picture 2

As you can see, the camera is rotating around its top left corner. The top left corner is the origin point (0,0,0) and the camera's position is (0,0). Shouldn't the lookAt function position the camera at (0,0,1) and point it at (0,0,0)? That's not what seems to be happening.

I'm just trying to get the camera to roll about it's set position. Any help would be greatly appreciated! Thanks in advance!

\$\endgroup\$
2
\$\begingroup\$

I found a solution :D

I just had to scrap the use of glm::lookAt and create the view matrix myself (quite easy since it's a 2D camera).

Here's the code that's working for me:

glm::mat4 Camera::GetViewMatrix()
{
    glm::mat4 view;
    glm::vec2 midOffset = glm::vec2(this->width * 0.5, this->height * 0.5);

    view = glm::translate(view, glm::vec3(midOffset, 0.0));
    view = glm::rotate(view, -this->rot, glm::vec3(0.0, 0.0, 1.0));
    view = glm::translate(view, glm::vec3(-midOffset, 0.0));
    view = glm::translate(view, glm::vec3(-this->pos, CAM_Z));

    return view;
}
\$\endgroup\$

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.