繁体   English   中英

如何在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处理您的游戏逻辑。 具体来说, Image Actor已经具有您期望从图像实体获得的大多数功能。 例如,创建和旋转播放器(图像)将像这样完成:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM