I’ve been working a game with OpenGL, and want a gun model that I have to move with the camera’s position and rotation (like a gun in a FPS game). I have this so far, which keeps the gun in view as the player moves but doesn’t move along with rotation:
ModelMatrix4 = glm::translate(ModelMatrix4, glm::vec3(getPosition().x+0.25, getPosition().y-0.65, getPosition().z-1)); ModelMatrix4 = glm::scale(ModelMatrix4, glm::vec3(0.1f, 0.15f, 0.15f)); ModelMatrix4 = glm::rotate(ModelMatrix4, 1.5708f, glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 MVP4 = ProjectionMatrix * ViewMatrix * ModelMatrix4;
Leaving out the ‘* ViewMatrix’ on the last line keeps the gun in view as I rotate the camera, but the gun model doesn’t appear at the right position and as the player moves, it moves away from them.
Essentially, with the multiplication of the ViewMatrix in the MVP equation, the movement is as expected, and without it, the rotation is as expected. So what exactly can I do to have the best of both? Thanks in advance.