FPS相机上不需要的滚动

时间:2017-04-04 21:58:57

标签: c++ visual-studio-2017 quaternions glm-math

所以就在最近我一直在学习使用四元数,我有一台fps相机,但我得到了一些"不想要的"从本地向上向右向量旋转时滚动,但是我很确定这是正常的,但是如何阻止它这样做呢?我尝试了herehere中的一些方法,两种方法都不起作用,并且在旋转相机时给出了奇怪的结果。任何帮助将不胜感激!

Download Executable

void Transform::Update()
{
    transform = position * glm::toMat4(quatRot) * scale;
}

void Transform::ApplyRotation(glm::vec3 rot, bool isDegr = true)
{
    if (isDegr)
        quatRot = glm::quat(GetRads(rot)) * quatRot;
    else
        quatRot = glm::quat(rot) * quatRot;
}

glm::vec3 Transform::Forward() const
{
    return glm::normalize(glm::vec3(transform[2][0], transform[2][1], transform[2][2]));
}

glm::vec3 Transform::Right() const
{
    return glm::normalize(glm::vec3(transform[0][0], transform[0][1], transform[0][2]));
}

glm::vec3 Transform::Up() const
{
    return glm::normalize(glm::vec3(transform[1][0], transform[1][1], transform[1][2]));
}

void FPCamera::Update()
{
    Transform *trans = GetOwner()->GetTransform();

    Vec2_i difference = GetWindow()->GetDifference();

    glm::vec3 tmpDiff(-(static_cast<float>(difference[1]) * 0.5f), -(static_cast<float>(difference[0]) * 0.5f), 0.0f);

    trans->ApplyRotation(trans->Right() * tmpDiff[0]);
    trans->ApplyRotation(trans->Up() * tmpDiff[1]);

    view = glm::inverse(trans->GetMatrix());
}

1 个答案:

答案 0 :(得分:0)

我找到了答案,当我使用this解决方案时,我不知道我需要使用世界向量而不是像this解决方案中找到的轴角度的局部向量。

void Transform::ApplyRotation(glm::quat yaw, glm::quat pitch)
{
    quatRot = pitch * quatRot * yaw;
}

void FPCamera::Update()
{
    Transform *trans = GetOwner()->GetTransform();

    Vec2_i difference = GetWindow()->GetDifference();

    glm::vec3 tmpDiff(-(static_cast<float>(difference[1]) * 0.5f), -(static_cast<float>(difference[0]) * 0.5f), 0.0f);

    trans->ApplyRotation(glm::quat(GetRads(glm::vec3(tmpDiff[0], 0.0f, 0.0f))), glm::quat(GetRads(glm::vec3(0.0f, tmpDiff[1], 0.0f))));

    view = glm::inverse(trans->GetMatrix());
}