【问题标题】:animation of an object using delta time使用增量时间的对象动画
【发布时间】:2019-09-12 07:43:56
【问题描述】:

我正在尝试为球的运动设置动画。我想在用户按下空格键时开始动画。

现在的动画很奇怪。

//global
float delta_time = 0.0f;
float last_frame = 0.0f;

// render loop
    while (!glfwWindowShouldClose(window))
    {
        float current_frame = glfwGetTime();
        delta_time = current_frame - last_frame;
        last_frame = current_frame;

        if (isAnimate)
        {
            delta_time += 1.0f;
        }

        processInput(window);

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // render
        render_scene();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

void render_scene(Shader inShader,
                  float radius)
{
    ...

    float x_pos = 0;
    float y_pos = 0;
    float z_pos = 0;

    x_pos +=  vx * delta_time;
    y_pos +=  vy * delta_time - (g / 2.0f) * delta_time * delta_time;
    z_pos +=  0.0f;

    model = glm::translate(model, glm::vec3(x_pos, y_pos, z_pos));

    draw_s();

}

void processInput(GLFWwindow *window)
{
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);

    if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
    {
        if (isAnimate) isAnimate = 0;
        else
        {
            isAnimate = 1;
        }

    }

    if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
    {
        isAnimate = 0;
        delta_time = 0.0f;

    }
...
}

【问题讨论】:

  • 它看起来更像是你一直在制作动画,但是当isAnimate 为真时,帧时间会增加一秒。如果您以每秒 60 帧的速度运行,那么您的动画时间每秒会提前一分钟以上。 (当然假设您以每秒为单位测量速度和加速度。)
  • @molbdnilo: 不是故意的。我只是想让动画在我按下空格键时开始。我试图从if loop 中删除delta_time +=1.0f;,而是将delta_time 定义放在isAnimate 循环中,但仍然没有好的动画。

标签: c++ glfw opengl-3


【解决方案1】:

当您不希望动画运行时,仅“停止时间”如何?比如:

float current_frame = glfwGetTime();

if (isAnimate)
    delta_time = current_frame - last_frame;
else
    delta_time = 0;

last_frame = current_frame;

【讨论】:

  • @George 即使将 delta_time 设置为 0?
  • @jackw11111:不,当屏幕加载对象不移动。当我按空格键时,它会移动
  • 对此还有什么想法吗?谢谢!
  • @George 你能解释一下它现在是如何工作的,以及你希望它如何工作吗?
  • 我上传了一个视频here。我想当用户按下空格键开始动画。现在,你可以看到它的移动很奇怪
猜你喜欢
  • 2020-06-02
  • 2014-08-11
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多