I make a FPS game with OpenGL (C++). I want to align a weapon like a gun to the camera, so that she moves and rotates with the camera.
I have accomplished that the weapon moves with the camera by removing the translation part from the view-matrix so that the weapon is always at the same position.
So I have removed the translation part (I work with OpenGL Mathematics (GLM)):
view = glm::mat4(glm::mat3(view));
The problem is that the weapon does not rotate with the camera. That means when I start the program the gun is aligned correctly but if I turn around the weapon don't turns with me so I can't see her any more.
Can anybody help to accomplish that the weapon is fixed to the camera?
You've to draw the "weapon" in view space rather than world space. This means the "weapon" must not be transformed by the view matrix.
Init the view matrix for "weapon" by the Identity matrix:
view = glm::mat4(1.0f);
Note, an object of the scene seems to move in the world (in relation to the view), because the object is transformed by the view matrix. The view matrix transforms from world space to view space. The view matrix is the inverse matrix of that matrix, which defines the camera position and orientation.
If an object has to be drawn in view space, this mean the object has to keep its position in relation to the camera (view), then the transformation by the view matrix has to be skipped. The position of the object is not a position in the world, but it is a position relative to the camera.