如何正确旋转 LibGDX 中的图像?

How do i correctly rotate an image in 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) {
        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());

请大家帮帮我。

您的 rotation 变量默认初始化为 0。尝试将其初始化为 90,您的玩家应该开始面向北方:

public Player(float x, float y,int width,int height) {
    ...
    rotation = 90.0f;
}

您的其余代码似乎可以处理它。

我强烈建议使用 libgdx 的 Scene2d for handling your game logic. Specifically the Image Actor 已经具有您希望从图像实体获得的大部分功能。例如,创建和旋转播放器(图像)将像这样完成:

Image player = new Image(playerTR);
player.setRotation(rotation_in_degrees);