Java 如何在LibGDX中正确旋转图像?
我正在尝试制作一个类似小行星的游戏,我希望玩家面对并向上移动(北),但当游戏开始时,玩家面对的是右侧(东)。这是我的密码:Java 如何在LibGDX中正确旋转图像?,java,libgdx,Java,Libgdx,我正在尝试制作一个类似小行星的游戏,我希望玩家面对并向上移动(北),但当游戏开始时,玩家面对的是右侧(东)。这是我的密码: public Player(float x, float y,int width,int height) { this.width = width; this.height = height; this.x = x; this.y = y; } public void update(float delta) { if(up) {
public Player(float x, float y,int width,int height) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
}
public void update(float delta) {
if(up) {
x += Math.cos(Math.toRadians(rotation)) * Constants.PLAYER_SPEED * delta;
y += Math.sin(Math.toRadians(rotation)) * Constants.PLAYER_SPEED * delta;
}else if(right){
rotation -= Constants.PLAYER_ROTATION * delta;
}else if(left){
rotation += Constants.PLAYER_ROTATION * delta;
}
wraparound();
}
然后我从纹理区域绘制播放器,如下所示:
batch.draw(playerTR, player.getX(),player.getY(),
player.getWidth()/2.0f,player.getHeight()/2.0f,
player.getWidth(),player.getHeight(),1,1,player.getRotation());
Image player = new Image(playerTR);
player.setRotation(rotation_in_degrees);
请帮助我。您的
旋转
变量默认初始化为0。尝试将其初始化为90,您的玩家应开始朝北:
public Player(float x, float y,int width,int height) {
...
rotation = 90.0f;
}
剩下的代码似乎可以处理它。我强烈建议使用libgdx来处理游戏逻辑。特别是,已具有图像实体的大部分功能。例如,创建和旋转播放器(图像)的操作如下:
batch.draw(playerTR, player.getX(),player.getY(),
player.getWidth()/2.0f,player.getHeight()/2.0f,
player.getWidth(),player.getHeight(),1,1,player.getRotation());
Image player = new Image(playerTR);
player.setRotation(rotation_in_degrees);