我正在使用 OpenGL。对于我的图块,我正在使用显示列表,而我只是为我的播放器使用即时更多(现在)。当我移动玩家时,我想让他在窗口的中心居中,但允许他在 y 轴上跳跃而不是让相机跟随他。但问题是,我不知道如何让玩家在视口中居中!这是播放器更新方法:
public void update() {
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
World.scrollx -= Constants.scrollSpeed;
setCurrentSprite(Sprite.PLAYER_RIGHT);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
World.scrollx += Constants.scrollSpeed;
setCurrentSprite(Sprite.PLAYER_LEFT);
}
move((Constants.WIDTH) / 2 + -World.scrollx, getY());
}
World.scrollx 和 World.scrolly 是我增加/减少以移动图块的变量。move() 只是一个设置玩家位置的方法,没有别的。我在当前坐标处渲染玩家,如下所示:
public void render() {
glBegin(GL_QUADS);
Shape.renderSprite(getX(), getY(), getCurrentSprite());
glEnd();
}
Shape.renderSprite 是这样的:
public static void renderSprite(float x, float y, Sprite sprite){
glTexCoord2f(sprite.x, sprite.y + Spritesheet.tiles.uniformSize());
glVertex2f(x, y);
glTexCoord2f(sprite.x + Spritesheet.tiles.uniformSize() , sprite.y + Spritesheet.tiles.uniformSize());
glVertex2f(x + Constants.PLAYER_WIDTH, y);
glTexCoord2f(sprite.x + Spritesheet.tiles.uniformSize(), sprite.y);
glVertex2f(x + Constants.PLAYER_WIDTH, y + Constants.PLAYER_HEIGHT);
glTexCoord2f(sprite.x, sprite.y);
glVertex2f(x, y + Constants.PLAYER_HEIGHT);
}
很简单,我只是在当前玩家的位置渲染四边形。这就是我实际渲染所有内容的方式:
public void render(float scrollx, float scrolly) {
Spritesheet.tiles.bind();
glPushMatrix();
glTranslatef(scrollx, scrolly, 0);
glCallList(tileID);
glPopMatrix();
player.render();
}
这是我很困惑的部分。我根据 scrollx 和 scrolly 变量翻译图块,然后将玩家渲染到当前位置。但是玩家的移动速度比瓷砖滚动快,他可以逃出屏幕的一侧!如何使用移动的瓷砖使播放器居中?
谢谢你的帮助!