c++ - Translating a camera along a quaternion using glm -
i trying translate camera's position along orientation defined in glm::quat.
void camera::translatecameraalongz(float distance) { glm::vec3 direction = glm::normalize(rotation * glm::vec3(0.0f, 0.0f, 1.0f)); position += direction * distance; }
this works fine when rotation identity quaternion. x , z translations not work when rotation else. when camera rotated 45 degrees left , call translatecameraalongz(-0.1f)
translated backwards , left when should going backwards , right. translations not @ perfect 90 degree increments messed up. doing wrong here, , simplest way fix it? in case might relevant, here function generates view matrix , function rotates camera:
glm::mat4 camera::getview() { view = glm::tomat4(rotation) * glm::translate(glm::mat4(), position); return view; } void camera::rotatecameradeg(float x, float y, float z) { rotation = glm::normalize(glm::angleaxis(x,glm::vec3(1.0f, 0.0f, 0.0f)) * rotation); rotation = glm::normalize(glm::angleaxis(y,glm::vec3(0.0f, 1.0f, 0.0f)) * rotation); rotation = glm::normalize(glm::angleaxis(z,glm::vec3(0.0f, 0.0f, 1.0f)) * rotation); std::cout << glm::eulerangles(rotation).x << " " << glm::eulerangles(rotation).y << " " << glm::eulerangles(rotation).z << "\n"; }
i'm guessing "rotation" inverse of camera's rotation (thinking of camera object in world). is, "rotation" takes object world space camera space. if want move camera's world space position forwards, need take local forward vector (0,0,1) or (0,0,-1)?, multiply inverse of "rotation" (to move vector camera space world space), scale , add position.
Comments
Post a Comment