Search code examples
c++openglglm-math

how can I rotate an object around a point other than the origin point with glm::rotate?


I am trying to make my world rotate around my camera no matter where my camera is. I am not doing any crazy math yet, I am leaving middle school this year and I don't know what quaternions are. My problem is that every time I use glm::rotate function for anything it only allows me to rotate around an axis at the origin point and I can't find a way to fix this. if there is any somewhat simple answer to this problem I am having please let me know how I can rotate my world around any given point. thanks

glm::mat4 look(1.0f);
float Rrotation;
Rrotation = 20.0f;
glm::vec3 the_axis_not_orientation(0.0f, 1.0f, 0.0f);

look = glm::rotate(look, Rrotation, the_axis_not_orientation);

Solution

  • What you actually do is to rotate the model:

    model_view = look * rotate
    

    If you want to rotate the view, then you have to swap the order of the matrices. Note, the matrix multiplication is not Commutative:

    model_view = rotate * look
    

    For your code that menas:

    glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), Rrotation, the_axis_not_orientation)
    look = rotate * look;