当我顺时针旋转鼠标时,屏幕上的对象逆时针旋转,反之亦然。这不是一个巨大的错误,但它似乎是错误的。
我将 Eigen 用于四元数和向量,所以问题应该属于我发布的代码。
相机:
#include <Eigen/Dense>
#include <cstring>
#include <math.h>
typedef Eigen::Vector3f vec3;
typedef Eigen::Quaternionf Quaternion;
namespace vec
{
//
// Constants
//
const vec3 i(1, 0, 0);
const vec3 j(0, 1, 0);
const vec3 k(0, 0, 1);
const vec3 zero(0, 0, 0);
const vec3 ones(1, 1, 1);
}
class Camera
{
void init();
void moveX(float dist);
void moveY(float dist);
void moveZ(float dist);
void rotateX(float radians); //yaw
void rotateY(float radians); //pitch
void rotateZ(float radians); //roll
void moveLeft(float dist);
void moveForward(float dist);
void moveUp(float dist);
void rotateLeft(float radians); //yaw
void rotateUp(float radians); //pitch
void rollLeft(float radians); //roll
void setPosition(const vec3 &pos);
void setDirection(const vec3 &dir);
void lookAt(const vec3 &pos); //changes direction
// Camera Vectors
vec3 left() const;
vec3 right() const;
vec3 up() const;
vec3 down() const;
vec3 forward() const;
vec3 backward() const;
const vec3 &pos() const;
protected:
Quaternion mRotation;
vec3 mPos;
void rotateL(float radians, const vec3 &axis);
void rotateR(float radians, const vec3 &axis);
};
执行:
void Camera::init()
{
mPos.setZero();
mRotation.setIdentity();
}
inline void Camera::rotateL(float radians, const vec3 &axis)
{
Quaternion q(Eigen::AngleAxis<float>(radians, axis));
mRotation = (q * mRotation).normalized();
}
inline void Camera::rotateR(float radians, const vec3 &axis)
{
Quaternion q(Eigen::AngleAxis<float>(radians, axis));
mRotation = (mRotation * q).normalized();
}
void Camera::moveX(float dist)
{
mPos.x() += dist;
}
void Camera::moveY(float dist)
{
mPos.y() += dist;
}
void Camera::moveZ(float dist)
{
mPos.z() += dist;
}
void Camera::rotateX(float radians)
{
rotateL(radians, vec::i);
}
void Camera::rotateY(float radians)
{
rotateL(radians, vec::j);
}
void Camera::rotateZ(float radians)
{
rotateL(radians, vec::k);
}
void Camera::moveLeft(float dist)
{
mPos += dist * left();
}
void Camera::moveUp(float dist)
{
mPos += dist * up();
}
void Camera::moveForward(float dist)
{
mPos += dist * forward();
}
void Camera::rotateLeft(float radians)
{
rotateL(radians, up());
}
void Camera::rotateUp(float radians)
{
rotateL(radians, left());
}
void Camera::rollLeft(float radians)
{
rotateL(radians, forward());
}
void Camera::setPosition(const vec3 &pos)
{
mPos = pos;
}
void Camera::setDirection(const vec3 &dir)
{
mRotation.setFromTwoVectors(vec::k, dir);
mRotation.normalize();
}
void Camera::lookAt(const vec3 &pos)
{
setDirection(mPos - pos);
}
// Camera Vectors
vec3 Camera::left() const
{
return mRotation._transformVector(vec::i);
}
vec3 Camera::right() const
{
return -left();
}
vec3 Camera::up() const
{
return mRotation._transformVector(vec::j);
}
vec3 Camera::down() const
{
return -up();
}
vec3 Camera::forward() const
{
return mRotation._transformVector(vec::k);
}
vec3 Camera::backward() const
{
return -forward();
}
const vec3 &Camera::pos() const
{
return mPos;
}
更新功能:
void App::onMouseMotion(int x, int y, int dx, int dy)
{
cam.rotateUp(dy * ROTATE_SCALE);
cam.rotateLeft(-dx * ROTATE_SCALE);
}
void App::update()
{
cam.moveLeft(((int)Keyboard::isKeyDown('a')) * MOVE_SCALE * mDt);
cam.moveLeft(((int)Keyboard::isKeyDown('d')) * -MOVE_SCALE * mDt);
cam.moveForward(((int)Keyboard::isKeyDown('w')) * MOVE_SCALE * mDt);
cam.moveForward(((int)Keyboard::isKeyDown('s')) * -MOVE_SCALE * mDt);
cam.rollLeft(((int)Keyboard::isKeyDown('q')) * -ROLL_SCALE * mDt);
cam.rollLeft(((int)Keyboard::isKeyDown('e')) * ROLL_SCALE * mDt);
}
我读过一些问题是由于在不正确的一侧组合了四元数......例如 Q R 而不是 R Q。按照这里的建议切换 X 轴绝对没有帮助。
对于我的实施,我也很感激任何建议。我已经考虑切换到矩阵进行旋转,但归一化和组合它们更昂贵。