移动物体使用增量时间卡顿以统一速度
Moving object stutters using delta time to unify speed
我尝试过不同的机器,VSync 开机和关机。
我已经提供了我的主要方法和显示方法。在主要外观中,我使用 GLFW 的 GetTime 方法计算增量。
如果我显式设置 deltaTime = 0.016 以锁定目标速度,则三角形移动平稳。
int main(int argc, char** argv)
{
/*
INIT AND OTHER STUFF SNIPPED OUT
*/
double currentFrame = glfwGetTime();
double lastFrame = currentFrame;
double deltaTime;
double a=0;
double speed = 0.6;
//Main loop
while(true)
{
a++;
currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
/** I know that delta time is around 0.016 at my framerate **/
//deltaTime = 0.016;
x = sin( a * deltaTime * speed ) * 0.8f;
y = cos( a * deltaTime * speed ) * 0.8f;
display();
if(glfwGetKey(GLFW_KEY_ESC) || !glfwGetWindowParam(GLFW_OPENED))
break;
}
glfwTerminate();
return 0;
}
void display()
{
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(playerProgram);
glUniform3f(playerLocationUniform,x,y,z);
glBindBuffer(GL_ARRAY_BUFFER, playerVertexBufferObject);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glUseProgram(0);
glfwSwapBuffers();
}
您使用deltaTime
就好像它是全局帧速率一样,并根据帧数(a
)乘以该速率计算正弦和余弦。 这意味着帧之间deltaTime
的微小波动将随着a
变大而引起更大的位置变化。
在另一种情况下,您设置了一个常量deltaTime
,当帧在错误的时间渲染时,您仍然会遇到小故障。
您实际需要做的是:
#define TAU (M_PI * 2.0)
currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
a += deltaTime * speed;
// Optional, keep the cycle bounded to reduce precision errors
// if you plan to leave this running for a long time...
if( a > TAU ) a -= TAU;
x = sin( a ) * 0.8f;
y = cos( a ) * 0.8f;
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 从持续时间构造std::chrono::system_clock::time_point
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 使用简单类型列表实现的指数编译时间.为什么
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 在已经使用Git的情况下减少编译时间
- 为什么在读取文件大小时文件IO速度会发生变化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 从文本文件中读取时钟时间和事件时间并进行处理
- 具有未知值时的时间复杂性
- MF SinkWriter mp4文件的播放持续时间是添加音频样本时的一半,图像的播放速度也是添加音频样本的两倍
- 移动物体使用增量时间卡顿以统一速度
- iOS上的OpenCV:二进制大小、加载时间、速度等
- Cocos2D BezierBy 随着时间的推移而增加速度
- 运行时间比较 - 类似的代码运行速度慢 4 倍
- 速度基于时间而不是FPS
- C++ 代码随着时间的推移运行速度变慢
- 如何提高超过时间限制的速度