1

私が描いている立方体を数秒ごとに「ジャンプ」するように見せようとしています。これが私のコードです:

            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 値を上げるタイマーを使用する必要があると想定していますが、これを行う方法がわかりませんか?

4

1 に答える 1

1

必要なのは、呼び出しで y 座標を調整することdrawCubeです。これは、ベース値 + ジャンプの高さである必要があります。

ジャンプの高さを一度に計算する簡単な方法の 1 つtは、次のとおりです。N 秒後にこの変数をリセットする必要があります。したがって、変数は 0 から まで実行されNます。

この変数は、ジャンプの高さの計算のベースになります。jumpDurationジャンプの持続時間とジャンプの最大高さを定義するとjump_height、ジャンプ オフセットは次の 2 つの関数で計算できます。

jump_offset = 0 // if t > jump_duration
jump_offset = -4 * jump_height / jump_duration^2 * t^2 + 4 * jump_height / jump_duration * t // else
于 2013-04-22T16:03:22.457 に答える