OpenGL GLM How to rotate cube when camera rotates?

  • Sander172
    Likes 0

    Problem Description

    I want a first person character. I’m trying to rotate my character when I rotate the camera. The problem is that my character rotates super fast and it doesn't have the sensitivity of the camera.

    This is my code:

    glm::mat4 projection = glm::perspective(camera.GetZoom( ), ( float )SCREEN_WIDTH/( float )SCREEN_HEIGHT, 0.1f, 1000.0f );

    shader.Use( );

    glm::mat4 view = camera.GetViewMatrix();
    glUniformMatrix4fv( glGetUniformLocation( shader.Program, "projection" ), 1, GL_FALSE, glm::value_ptr( projection ) );
    glUniformMatrix4fv( glGetUniformLocation( shader.Program, "view" ), 1, GL_FALSE, glm::value_ptr( view ) );

    glm::mat4 model;
    model = glm::translate( model, modelPosition); // Translate it down a bit so it's at the center of the scene
    model = glm::scale( model, glm::vec3( 0.2f, 0.2f, 0.2f ) ); // It's a bit too big for our scene, so scale it down
    model = glm::rotate(model, camera.GetYaw(), glm::vec3(0,1,0));
    glUniformMatrix4fv( glGetUniformLocation( shader.Program, "model" ), 1, GL_FALSE, glm::value_ptr( model ) );
    ourModel.Draw( shader ); 
     
     

  • Sander172
    Likes 0

    I finally fixed it!

    Replaced:

    model = glm::rotate(model, camera.GetYaw(), glm::vec3(0,1,0));

    With:

    model = glm::rotate(model, glm::radians(camera.GetYaw() - 90.0f), glm::vec3(0, -1.0f, 0));

Login to reply