[英]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.