0

我有一个 3D 立方体,我试图沿 Y 轴平滑旋转。截至目前,当我单击鼠标按钮时,立方体会立即旋转到新值(如俄罗斯方块),但希望显示它逐渐旋转到特定的新旋转值。这是我正在使用的代码:

// Within Shp_Cube class
public float cubeRotY = 1.0f; // rotate on Y-axis
public static float cubeAngle = .01f; // angle of rotation
public static float cubeSpeed = 1.0f; // speed of rotation
...

// Within drawCube() method (called in JOGL's display(GLAutoDrawable...) )
gl.glRotatef(cubeAngle, 0, cubeRotY, 0); // rotation of cube
...

// Within MouseInput class
@Override
public void mouseClicked(MouseEvent m) 
{
    switch(m.getButton())
    {
    case 1:
        System.out.println("Left Mouse Button Clicked");

        if(Shp_Cube.cubeAngle < Shp_Cube.cubeAngle + 90f)
        {
            Shp_Cube.cubeAngle += Shp_Cube.cubeSpeed;
        }

        break;
    case ...

我一直试图达到的预期效果是当用户单击鼠标左键时,立方体将开始旋转并继续旋转,直到它的角度达到某个值。

4

1 回答 1

0

您正在更新方法中的多维数据集,mouseClicked()但它应该在display()方法中更新:

public void display(GLAutoDrawable drawable) {
    updateCube();
    drawCube(...);
}

private void updateCube() {
    if(Shp_Cube.cubeAngle < Shp_Cube.cubeAngle + 90f) {
        Shp_Cube.cubeAngle += Shp_Cube.cubeSpeed;
    } else {
        Shp_Cube.cubeSpeed = 0; // stop rotattion after angle reched
    }
}

public void mouseClicked(MouseEvent m) {
    switch(m.getButton()) {
    case 1:
        Shp_Cube.cubeSpeed = INITIAL_SPEED; // this should only start rotation by setting positive speed
        break;
    }
}

要以恒定帧速率进行平滑渲染,请将以下内容附加到 main() 方法:

Animator animator = new FPSAnimator(canvas, 60);
animator.add(canvas);
animator.start();

有关更多信息,请参阅本教程末尾的代码。

于 2013-02-04T08:10:05.363 回答