繁体   English   中英

增加按键上模型的加速度?

[英]Increasing acceleration of model on keypress?

我正在尝试在开放空间中旋转单个多维数据集。 首先,它从静止开始,然后在按键时,应该在特定按键上的特定访问中提高旋转速度。

我的初始代码如下所示:

model = glm::rotate(model, glm::radians(rotationAngle.x * glfwGetTime()), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians(rotationAngle.y * glfwGetTime())), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, glm::radians(rotationAngle.z * glfwGetTime())), glm::vec3(0.0f, 0.0f, 1.0f)

在一般的按键上,比如说A,代码如下所示:

if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS){
    rotationAngle.x += 0.01;
}

这会增加旋转速度,但是由于glfwGetTime()不断增加,所以当我按住键时,旋转会非常快,然后当按下键时,它将恢复为正常旋转速度。

我可能做错了什么?

您可能要使用增量时间(自上一帧以来的时间变化)而不是时间。 我不确定GLFW是否具有特定功能,但是您可以这样做:

time = glfwGetTime();
delta = time - lastTime;

// do stuff with delta ...

lastTime = time;

从描述中,我认为代码是这样的

while(condition)
{
    model = glm::rotate(model, glm::radians(rotationAngle.x * glfwGetTime()), glm::vec3(1.0f, 0.0f, 0.0f));
    model = glm::rotate(model, glm::radians(rotationAngle.y * glfwGetTime())), glm::vec3(0.0f, 1.0f, 0.0f));
    model = glm::rotate(model, glm::radians(rotationAngle.z * glfwGetTime())), glm::vec3(0.0f, 0.0f, 1.0f));

    if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
    {
        rotationAngle.x += 0.01;
    }
}

按住键时,rotationAngle每次增加0.01,并且每次循环迭代时,rotate函数在每个循环中旋转的量更大。 为了防止这种情况发生,if语句仅应在从“未按下”变为“按下”时激活。 我们可以用一个标志来做到这一点。

while(condition)
{
    static bool keyDown = false;

    model = glm::rotate(model, glm::radians(rotationAngle.x * glfwGetTime()), glm::vec3(1.0f, 0.0f, 0.0f));
    model = glm::rotate(model, glm::radians(rotationAngle.y * glfwGetTime())), glm::vec3(0.0f, 1.0f, 0.0f));
    model = glm::rotate(model, glm::radians(rotationAngle.z * glfwGetTime())), glm::vec3(0.0f, 0.0f, 1.0f));

    if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
    {
        // We don't combine !keyDown in the outer if statement because
        // we don't want the else statement to be activated whenever either 
        // statement is false.

        // If key was not previously pressed, then activate. Otherwise, 
        // key is already down and we ignore this.
        if(!keyDown)
        {
            rotationAngle.x += 0.01;
            keyDown = true;
        }
    }
    // Once you let go of the key, this part activates and resets the flag 
    // as well as the rotation angle.
    else
    {
        rotationAngle.x = 0.0;
        keydown = false;
    }
}

它看起来或多或少是这样的。 我不知道glfwGetKey的详细信息,因此您可能需要更多条件来检查密钥状态。

暂无
暂无

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

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