【发布时间】:2015-10-21 20:39:08
【问题描述】:
长话短说,我正在制作一个 LWJGL 引擎,并且正在绘制一个基本的 QUAD。当我绘制这个 QUAD 并使用键盘侦听器时,它可以完美地向上/向下/向左/向右移动。
但是,当我平移和旋转时,它也可以围绕其中心点旋转,如果将它们一起使用,那么就会出现问题。旋转开始偏离轴心,每次移动时,都会绕着其他地方的一个奇怪点旋转。
我怎样才能使我可以移动 QUAD(与旋转无关)以及旋转它?
编辑 1
我发现这个 QUAD 的一个主要问题是,当我旋转它时,我的整个屏幕(包括文本)都会旋转...
我目前的结果:
运动前
移动后(希望你注意到它在一个圆圈中移动,而不是在我向左移动时只是向左移动。)
代码:
(显示设置)
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.setVSyncEnabled(vsync);
Display.create();
open = true;
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
// Sets (0, 0) to the top left corner.
GL11.glOrtho(0, this.width, this.height, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
} catch (LWJGLException e) {
e.printStackTrace();
}
(键盘监听器)
public void userLogic() {
if (keyboard.isKeyDown(Keyboard.KEY_A)) {
rect.setLocation(rect.x - 1, rect.y, rect.width, rect.height);
rect.setRotation(-1);
} else if (keyboard.isKeyDown(Keyboard.KEY_D)) {
rect.setLocation(rect.x + 1, rect.y, rect.width, rect.height);
rect.setRotation(1);
} if (keyboard.isKeyDown(Keyboard.KEY_W)) {
rect.setLocation(rect.x, rect.y - 1, rect.width, rect.height);
} else if (keyboard.isKeyDown(Keyboard.KEY_S)) {
rect.setLocation(rect.x, rect.y + 1, rect.width, rect.height);
}
}
(运动/旋转逻辑)
public void setRotation(float degrees) {
// TODO: Fix whatever the hell the problem is here.
GL11.glTranslatef(x + (width / 2), y/* + (height / 2)*/, 0);
GL11.glRotatef(degrees, 0f, 0f, 1f);
GL11.glTranslatef(-(x + (width / 2)), -(y/* + (height / 2)*/), 0);
}
public void setLocation(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
(绘制 QUAD,(与渲染过程的其余部分分离))
public void render() {
switch(type) {
case IMAGE:
// Not ready yet.
break;
case TRIANGLE:
// Not ready yet.
break;
case RECTANGLE:
GL11.glColor3f(colour.red, colour.green, colour.blue);
// X, Y, WIDTH, HEIGHT are the QUADS coords, not the displays or anything else's.
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x, y);
GL11.glVertex2f(x + width, y);
GL11.glVertex2f(x + width, y + height);
GL11.glVertex2f(x, y + height);
GL11.glEnd();
break;
}
}
我们将不胜感激任何帮助,如果您要投反对票,请给出一个理由,以便我可以改进这个问题,或者更好的是,改为评论。
【问题讨论】:
标签: java rotation logic 2d lwjgl