简体   繁体   English

FPS相机上不需要的胶卷

[英]Unwanted Roll on FPS Camera

So just recently I've been learning to use quaternions, I got an fps camera going but I get some "unwanted" roll when rotating from the local up and right vectors, however I'm pretty sure this is normal, but how do I prevent it from doing that? 因此,最近我一直在学习使用四元数,可以使用fps相机,但是从局部向上向右矢量旋转时会出现一些“不必要的”滚动,但是我敢肯定这是正常的,但是我该怎么做呢?阻止它这样做? I tried a few methods from here and here , both did not work and gave weird results when rotating the camera. 我从这里这里尝试了几种方法,两种方法都无效,并且在旋转相机时给出了奇怪的结果。 Any help would be appreciated! 任何帮助,将不胜感激!

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());
}

I found the answer, when I used this solution, I didn't know I needed to use world vectors instead of local vectors for the axis angles as I found from this solution. 我找到了答案,当我使用解决方案时,我不知道我需要使用世界矢量来代替我从解决方案中找到的局部矢量。

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM