使用增量时间的对象动画

animation of an object using delta time

我正在尝试制作球运动的动画。我想在用户按下 space 栏时启动动画。

现在的动画很奇怪

//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;

    }
...
}

当您不希望动画变为 运行 时,只 "stopping time" 怎么样?类似于:

float current_frame = glfwGetTime();

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

last_frame = current_frame;