加快轮换速度

时间:2015-01-31 11:04:56

标签: java opengl lwjgl

当我按下'A'时,方块正在加速自己的旋转,但是当我释放'A'时它比以前旋转禁止但看起来比按'A'时慢。

package backend;
import java.nio.ByteBuffer;

import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;

public class runGame {

    private long window;
    private GLFWKeyCallback keyCallback;
    private GLFWErrorCallback errorCallback;

    public static void main(String[] args) {
        new runGame().run();
    }
    private void run()  {
        try {
            init();
            loop();
            glfwDestroyWindow(window);
            keyCallback.release();
        } finally {
            glfwTerminate();
        }
    }
    private static final int width = 640;
    private static final int height = 480;
    private void init() {
        glfwSetErrorCallback(errorCallback);
        if(glfwInit() != GL11.GL_TRUE) {
            throw new IllegalStateException("Unable to initialize GLFW");
        }
        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
        //glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

        window = glfwCreateWindow(width, height, "mazeRunner Game", 0, 0);
        if (window == NULL)
            throw new RuntimeException("Failed to create the GLFW window");
        glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
            @Override
            public void invoke(long window, int key, int scancode, int action, int mods) {
                if( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) {
                    glfwSetWindowShouldClose(window, GL_TRUE);
                }
                if( key == GLFW_KEY_A && action == GLFW_PRESS ) {
                    speedUp = true;
                }
                if( key == GLFW_KEY_A && action == GLFW_RELEASE ) {
                    speedUp = false;
                }
            }
        });
        ByteBuffer glfwvidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(window, 
                (GLFWvidmode.width(glfwvidmode)-width)/2,
                (GLFWvidmode.height(glfwvidmode)-height)/2);
        glfwMakeContextCurrent(window);
        glfwSwapInterval(1);
        glfwShowWindow(window);



        GLContext.createFromCurrent();
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        float aspect_ratio = ((float)height) / width;
        glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50);
        glMatrixMode(GL_MODELVIEW);
    }
    private final double fps = 60.0f;
    private double time;
    private void loop() {
        time = glfwGetTime();
        while(glfwWindowShouldClose(window) == GL_FALSE) {
            if(glfwGetTime()-time>=1/fps) { 
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                time = glfwGetTime(); draw(); 
                glfwSwapBuffers(window);
                glfwPollEvents();
            }
        }
    }

    private void draw() {
        glLoadIdentity();
        glTranslatef(0.0f, 0.0f, -3.0f);
        if(speedUp==true) speed = speed+0.9f;
        drawSquare(1.0f, 0.0f, 0.0f);
    }

    boolean speedUp;
    private float speed = 20.0f;
    private void drawSquare(float red,float green,float blue) {
        glRotatef(speed*(float)time, 0.0f, 0.0f, 1.0f);
        glBegin(GL_QUADS);      // Draw The Cube Using quads
        {
            glColor3f(red, green, blue);
            glVertex2f(0, 0);
            glColor3f(red * .8f, green * .8f, blue * .8f);
            glVertex2f(0, 1);
            glColor3f(red * .5f, green * .5f, blue * .5f);
            glVertex2f(1, 1);
            glColor3f(red * .8f, green * .8f, blue * .8f);
            glVertex2f(1, 0);
        }
        glEnd();            // End Drawing The Cube
    }

}

1 个答案:

答案 0 :(得分:0)

速度不同的问题与您处理时间的方式有关。变量'time'将包含自计算旋转角度以来程序启动以来的总时间。

glRotatef(speed*(float)time, 0.0f, 0.0f, 1.0f);

这意味着速度差异不仅应用于当前帧,还应用于自程序启动以来的所有先前帧。这就是按“a”键时旋转比预期更快的原因。

很容易验证这一点。如果在程序启动时直接按'a',您会发现速度差异很小。现在等一下左右再按'a'。现在,由于累计时间较长,速度差异要大得多。

要解决此问题,您可能应该更改旋转计算,以便使用帧时间而不是总程序时间。即将当前旋转角度存储在一个变量中,根据速度和帧时间增加每个帧。