Colton McGraw
Colton McGraw

Reputation: 13

How do I rotate glm camera

I have been working for a couple of weeks to get my glm camera to rotate but to no avail. I have try quite a few different methods. The first one is best camera i've had. If someone could explain to me where i'm going wrong, and how to fix it, THANK YOU!

1 - Problem - If i look, over my shoulder, aka 90 degrees to the left and try to look up it won't.

void addRotation(const glm::vec3 rot) {

    _for = glm::rotate(_for, rot.x, glm::vec3(0,1,0));

    _for = glm::rotate(_for, rot.y, glm::vec3(1,0,0));
}

_for is a vec3 in the direction i'm looking.

glm::mat4 getViewProj() { return proj * lookAt(_pos, _pos + _for, _up); }

proj is defined as

proj = glm::perspective(fov, aspect, 0.01f, 100.0f);
  1. This code didn't really work but i'll include it anyway.

    void addRotation(const glm::vec3 rot) {
    
        glm::mat4 rx = glm::rotate(glm::mat4(1.0f), rot.x, glm::vec3(0,1,0));
    
        glm::mat4 ry = glm::rotate(glm::mat4(1.0f), rot.y, glm::vec3(1,0,0));
    
        _for = rx * ry; //_for was a mat4, everything else was the same
    }
    

If have tried += on the first method, but all I is math errors because the decimal point goes basicly to infinity (though if i add a constant number that end in 0 it would fix that).

If anyone can help it would be MUCH appreciated. Thank you in advance!

Upvotes: 0

Views: 1149

Answers (1)

Colton McGraw
Colton McGraw

Reputation: 13

Ok, I have found a solution after 4 more hours of looking,

Issue with GLM Camera X,Y Rotation introducing Z Rotation

This is a link to another glm camera issue question. Their problem was that there camera was doing random barroll rolls, while mine didn't pitch when looking to my left. But after copied down their code the solution I found to it was everywhere but when you ASSIGN the new up vector in there code, replace it with glm::vec3(0.0f, 1.0f, 0.0f). I did this with a variable and it has work fine for me no bugs yet.

I'm not fully sure I understand why this answer worked myself, but from what I'm guessing is happening when assigning a variable using a variable that's been adjusted already it's going to slowly create math errors. But having a concrete reference point and just using the adjusted up variable in the glm::lookAt calculation works fine. But if anyone can explain it better, knows a better solution, or knows for sure why it works please do tell me.

P.S. I am not deleting this because anyone else, who was in the situation I was in, will hopefully find this comment useful.

Upvotes: 1

Related Questions