我正在尝试在开放空间中旋转单个多维数据集。首先,它从静止开始,然后在按键时,应该在特定按键上的特定访问中提高旋转速度。
我的初始代码如下:
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()
不断增加,因此当我按住键时,旋转会非常快,然后在按下键时,它将恢复到正常旋转速度。
我可能做错了什么?
答案 0 :(得分:3)
您可能要使用增量时间(自上一帧以来的时间变化)而不是时间。我不确定GLFW是否具有特定功能,但是您可以这样做:
time = glfwGetTime();
delta = time - lastTime;
// do stuff with delta ...
lastTime = time;
答案 1 :(得分:0)
从描述中,我看到代码是这样的
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;
}
}
按住键时,旋转角度每次增加0.01,并且每次循环迭代时,旋转功能都会在每个循环中旋转更大的角度。为了防止这种情况发生,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的详细信息,因此您可能需要更多条件来检查密钥状态。