繁体   English   中英

流畅地加快旋转速度

[英]Speeding up rotation fluently

当我按下“ 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
    }

}

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

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

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

验证这一点很容易。 如果在程序启动时直接按“ a”,则发现速度差异很小。 现在等待一分钟左右,然后再次按“ a”。 现在,由于累计时间更长,所以速度差也更高。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM