相反方向时奇怪的相机定位

问题描述 投票:0回答:0

在我的程序中,第一人称相机在朝相机前进的方向移动时有一个奇怪的错误。当方向设置为相反方向时,此错误会导致相机随机偏移。

这是一个视觉演示:https://youtu.be/pJEOYKZ6Qko

这是代码

namespace CharacterSystem {

    // Internal Variables
    static float terminalVelocity = 8.0f;

    void update(CharacterController& controller, Camera& camera, Rigidbody& rb)
    {
        float speed = controller.controllerBaseSpeed;
        if (controller.isRunning)
        {
            speed = controller.controllerRunSpeed;
        }

        rb.acceleration = camera.forward * controller.movementAxis.x;
        rb.acceleration += camera.right * controller.movementAxis.z;
        // The y-axis just changes the player's global y position
        // instead of using their local up axis
        rb.acceleration.y += controller.movementAxis.y;

        // Only normalize the acceleration if it is greater than 0. This
        // way we avoid division by 0 errors
        float accelerationMagnitudeSquared = glm::length2(rb.acceleration);
        if (accelerationMagnitudeSquared > 0)
        {
            float denominator = glm::inversesqrt(accelerationMagnitudeSquared);
            rb.acceleration *= denominator * speed;
        }

        rb.velocity += rb.acceleration * Application::deltaTime;
        camera.position += rb.velocity * Application::deltaTime;

        // If acceleration is 0, apply friction to decelerate the object
        if (glm::all(glm::epsilonEqual(rb.acceleration, glm::vec3(0.0f), 0.001f)))
        {
            rb.velocity *= rb.friction * Application::deltaTime;
        }

        if (glm::length2(rb.velocity) >= terminalVelocity)
        {
            rb.velocity = glm::normalize(rb.velocity) * terminalVelocity;
        }

        // Snap the velocity to 0 when it gets close enough
        if (glm::all(glm::epsilonEqual(rb.velocity, glm::vec3(0.0f), 0.001f)))
        {
            rb.velocity = glm::vec3(0.0f);
        }

        // Mouse movements don't need to be smoothed over time.
        // They alread represent a change in position
        controller.viewAxis *= controller.movementSensitivity * 1000;

        if (controller.viewAxis.x != 0.0f)
        {
            // Yaw globally
            glm::quat yaw = glm::quat(glm::vec3(0.0f, glm::radians(controller.viewAxis.x) * Application::deltaTime, 0.0f));
            camera.orientation = camera.orientation * yaw;
        }

        if (controller.viewAxis.y != 0.0f)
        {
            // Pitch locally (this effects order of operation with quaternion multiplication)
            glm::quat pitch = glm::quat(glm::vec3(glm::radians(-controller.viewAxis.y) * Application::deltaTime, 0.0f, 0.0f));
            camera.orientation = pitch * camera.orientation;
        }
    }
}
namespace PlayerController {
    void update(CharacterController& controller) {
        controller.movementAxis.x = Input::isKeyDown(GLFW_KEY_W)
            ? -1.0f
            : Input::isKeyDown(GLFW_KEY_S)
            ? 1.0f
            : 0.0f;
        controller.movementAxis.z = Input::isKeyDown(GLFW_KEY_D)
            ? 1.0f
            : Input::isKeyDown(GLFW_KEY_A)
            ? -1.0f
            : 0.0f;
        controller.movementAxis.y = Input::isKeyDown(GLFW_KEY_LEFT_CONTROL)
            ? -1.0f
            : Input::isKeyDown(GLFW_KEY_SPACE)
            ? 1.0f
            : 0.0f;

        controller.viewAxis.x = Input::deltaMouseX;
        controller.viewAxis.y = Input::deltaMouseY;
    }
}

我不确定我能在这里做什么。我相信这可能是由于相机方向从 1 变为 -1。

c++ glm
© www.soinside.com 2019 - 2024. All rights reserved.