我正在尝试使我正在绘制的立方体看起来每隔几秒钟就会“跳跃”一次。这是我的代码:
for (int i=0; i<25; i++)
{
if(j<rows)
{
//Timer used in vibration calculation when drawing cubes
float time = (std::clock() - timer);
//Calculate the amount of simulated vibration based on amount of distortion (intensity)
float offset = sin(1.0f * 3.14159265359f * time) * shake;
//Draw cubes right and left of centre point
drawCube((x+xShift), y, (-300 +depth), 1.0f, cubeColour, offset);
drawCube((x-xShift), y, (-300 +depth), 1.0f, cubeColour, offset);
xShift -= gap;
}
}
而drawCube代码是:
void drawCube(float x, float y, float z, float opacity, float col[], float offset)
{
//Draw cube taking into account any offset caused by simulated vibration
glTranslatef((-x+offset), (-y+offset), -z);
glColor4f(col[0], col[1], col[2], opacity);
glutWireCube(20);
glTranslatef((x-offset), (y-offset), z);
}
我假设我需要使用一个定时器,每 N 秒提高一次 y 值,所以立方体看起来会跳跃,但我不确定如何做到这一点?