gpt4 book ai didi

c++ - 使用增量时间的对象动画

转载 作者:搜寻专家 更新时间:2023-10-31 01:24:41 24 4
gpt4 key购买 nike

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

现在,动画很奇怪。

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

}
...
}

最佳答案

当您不想让动画运行时,“停止时间”怎么样?像这样的东西:

float current_frame = glfwGetTime();

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

last_frame = current_frame;

关于c++ - 使用增量时间的对象动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57901979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com