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

时间:2015-06-20 20:00:06

标签: 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) {
        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());
请帮助我。

2 个答案:

答案 0 :(得分:0)

默认情况下,您的rotation变量已初始化为0.尝试将其初始化为90,并且您的播放器应该开始面向北方:

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

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

答案 1 :(得分:0)

我强烈建议您使用libgdx' Scene2d来处理您的游戏逻辑。具体而言,Image Actor已经具有您期望从图像实体获得的大部分功能。例如,创建和旋转播放器(图像)将如下所示:

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