OpenGL如何向上移动相机然后回到网格上

Posted

技术标签:

【中文标题】OpenGL如何向上移动相机然后回到网格上【英文标题】:OpenGL how to move the camera upward and then come back on the Grid 【发布时间】:2020-08-09 22:33:38 【问题描述】:

我想实现一个传统的 FPS 控件。通过按下空格键,相机向上移动,然后回到网格上,模拟跳跃动作。现在,我只能将相机向上移动一段距离,但我不知道如何让相机下降。

这是我的相机类:

Camera::Camera(glm::vec3 cameraPosition, glm::vec3 cameraFront, glm::vec3 cameraUp)
   position = glm::vec3(cameraPosition);
   front = glm::vec3(cameraFront);
   up = glm::vec3(cameraUp);

   // Set to predefined defaults
   yawAngle = -90.0f;
   pitchAngle = 0.0f;
   fieldOfViewAngle = 45.0f;


Camera::~Camera() 

void Camera::panCamera(float yaw)
yawAngle += yaw;

updateCamera();    

void Camera::tiltCamera(float pitch)
    pitchAngle += pitch;
    // Ensure pitch is inbounds for both + and - values to prevent irregular behavior
    pitchAngle >  89.0f ? pitchAngle =  89.0f : NULL;
    pitchAngle < -89.0f ? pitchAngle = -89.0f : NULL;

    updateCamera();

void Camera::zoomCamera(float zoom)
    if (fieldOfViewAngle >= MIN_ZOOM && fieldOfViewAngle <= MAX_ZOOM)
        fieldOfViewAngle -= zoom * 0.1;

    // Limit zoom values to prevent irregular behavior
    fieldOfViewAngle <= MIN_ZOOM ? fieldOfViewAngle = MIN_ZOOM : NULL;
    fieldOfViewAngle >= MAX_ZOOM ? fieldOfViewAngle = MAX_ZOOM : NULL;

glm::mat4 Camera::calculateViewMatrix()
    return glm::lookAt(position, position + front, up);

void Camera::updateCamera()
    front.x = cos(glm::radians(yawAngle)) * cos(glm::radians(pitchAngle));
    front.y = sin(glm::radians(pitchAngle));
    front.z = sin(glm::radians(yawAngle)) * cos(glm::radians(pitchAngle));
    front = glm::normalize(front);

void Camera::moveForward(float speed)
    position += speed * front;

void Camera::moveBackward(float speed)
    position -= speed * front;

void Camera::moveLeft(float speed)
    position -= glm::normalize(glm::cross(front, up)) * speed;

void Camera::moveRight(float speed)
    position += glm::normalize(glm::cross(front, up)) * speed;

void Camera::moveUpward(float speed)
    position.y += speed;

这就是我在 main.cpp 中实现它的方式:

if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)

    camera.moveUpward(cameraSpeed);

有人可以帮我吗?

【问题讨论】:

也许您需要在用户不按空格键时向下移动相机,但前提是它位于网格上方? 【参考方案1】:

您可以通过对 y 位置施加加速度来模拟跳跃。您需要为结构添加 3 个新属性:加速度 (vec3)、速度 (vec3) 和 onGround(布尔值)。

void Camera::jump() 
    // Apply a upwards acceleration of 10 units. You can experiment with 
    // this value or make it physically correct and calculate the 
    // best value, but this requires the rest of your program to
    // be in well-defined units.
    this->onGround = false;
    this->acceleration.y = 10.0f;



void Camera::update() 
    // Your other update code

    // The acceleration should decrease with gravity. 
    // Eventually it'll become negative.
    this->acceleration.y += GRAVITY;  // Define 'GRAVITY' to a negative constant.

    // Only update if we're not on the ground.
    if (!this->onGround) 
        // Add the acceleration to the velocity and the velocity to 
        // the position. 
        this->velocity.y += this->acceleration.y
        this->position.y += this->velocity.y;
        if (this->position.y <= 0) 
            // When you're on the ground, reset the variables.
            this->onGround = true;
            this->acceleration.y = 0;
            this->velocity.y = 0;
            this->position.y = 0;
        
    

在您的事件处理中,您将调用 jump 方法。

if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)

    camera.jump();

但是,此代码可能不应该在相机上,而是在某种物理对象上。但它会起作用。

【讨论】:

以上是关于OpenGL如何向上移动相机然后回到网格上的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL正交相机缩放无法正常工作

OpenGL,俯仰强制偏航为0

OpenGL:移动相机时如何使光线静止?

OpenGL模拟波斯王子相机

如何让物体跟随相机?

OpenGL相机的相对旋转