0

I am making a camera in openGl and I am having troubles with first person camera. So I had a few versions of camera transformation and all of them had their own problems. So at first, I was doing transformations in this order: I would first translate the object in the positive direction when trying to move away from it and I would translate it in the negative direction when trying to move towards it. After this translation, I would perform rotations arround X and Y axis. Now, when I try to use this camera, I found out that when I have objects in my scene, lets say a few cubes, and when I rotate, everything is fine, but when after this rotation I try translation, all of the objects converge to me or better to say, towards the "player". So after I gave this some thought I realized that because I am doing translations first, in the next frame when I try to translate the player in the direction in which the camera is looking at that moment, what happens is, objects get translated first and then rotated so I get, as a result of this, movement of the objects towards or away from the player. Code for this is here (and dont mind camUp and camRight vectors, these are just y and x axis vectors and are not transformed at all):

m_ViewMatrix = inverse(glm::rotate(glm::mat4(1.0f), m_Rotation, camUp))* inverse(glm::rotate(glm::mat4(1.0f), m_TiltRotation, camRight)) * glm::translate(glm::mat4(1.0f), m_Position);

But option to rotate and then translate is not good because then I get editor sort of camera which is actually fine but that is not what I want. So I thought about it some more and tried to make small transformations and then reset the parameters, acumulating all the transformations in this way:

m_ViewMatrix = inverse(glm::rotate(glm::mat4(1.0f), m_Rotation, camUp)) * glm::translate(glm::mat4(1.0f), m_Position)* inverse(glm::rotate(glm::mat4(1.0f), m_TiltRotation, camRight))*m_ViewMatrix;
        m_Position = { 0.0f, 0.0f, 0.0f };
        m_Rotation = 0.0f;
        m_TiltRotation = 0.0f;

But now I have a problem with rotations arround z axis which I don't want. This problem was not there before. So now I have no idea what to do, I read some answers here but couldn't apply them I don't know why. So if anyone could help me in the context of the code I just copied here, that would be great.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • see [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) especially the example links at the bottom – Spektre Apr 16 '22 at 13:43
  • This has nothing to do with what I am asking man. – Žarko Tomičić Apr 17 '22 at 05:48
  • You use Euler angles and translation vector to construct 4x4 matrix that is latter used for rendering. That brings some problems like gimbal lock, turn singularities etc. if you skip this process and use coumulative 4x4 matrix directly you will drop all of these problems. If you look at the examples you would see that your matrix should be: `MVP = Model*Inverse(Camera)*Projection` (while using native OpenGL notation and order) and also `Camera = avatar_mesh_matrix*camera_location_and_orientation` where the last matrix can use your Euler angles to limit head tilt to physiological limits – Spektre Apr 17 '22 at 08:24
  • What do you mean by cumulative 4x4 matrix directly? – Žarko Tomičić Apr 17 '22 at 10:01
  • You reset and reconstruct your matrix in every frame ... coumulative matrix is persistent ... not being reseted to identity ... it accumulates all the transformations iteratively ... you move forward you translate it localy on key press ... on mouse or key turn you rotate it ... and so on ... You just normalize it time to time to get rid of accumulated rounding errors ... – Spektre Apr 17 '22 at 14:55
  • I am not reseting matrix, it is cumulative. I just reset parameters used to calculatr previous matrix – Žarko Tomičić Apr 17 '22 at 21:19

0 Answers0