我的Slick2d图像没有按预期旋转

时间:2013-12-24 10:27:26

标签: java rotation slick2d image-rotation

我在基于Slick2d的Java游戏中有一个简单的图像,我试图将它旋转90度。但是,当使用“Image.rotate(角度)”方法时,图像最终会移动以及旋转。我已经尝试将旋转中心强制设置为图像的精确中心,但结果是相同的。这是前后拍摄。这个想法是坦克将旋转90度并保持在同一个方格。强制设定的旋转中心由旋转后的点显示:

在: before

在: after

代码:

    public void turn(String direction){
    if(direction.equals("right")){
        if(movableEntityImage.getRotation() != 90){
            System.out.println("Pre rotate x = " + x + " . Pre rotate y = " + y + ". width = " + width + ". height = " + height);
            System.out.println("Pre rotate rotation = " + movableEntityImage.getRotation());
            float rotX = (x + (width/2));
            float rotY = (y + (height/2));
            movableEntityImage.setCenterOfRotation(rotX, rotY);
            System.out.println("center of rotation X = " + movableEntityImage.getCenterOfRotationX() + ".center of rotation y = "+ movableEntityImage.getCenterOfRotationY());
            movableEntityImage.rotate(90);


            System.out.println("Post rotate x = " + x + " . Post rotate y = " + y);
            System.out.println("Post rotate rotation = " + movableEntityImage.getRotation());



        }

    }

最后是控制台输出:

The tank has been created with an x of: 50.0 and a y of: 50.0
Pre rotate x = 50.0  Pre rotate y = 50.0  width = 25.0  height = 25.0
Pre rotate rotation = 0.0
Center of rotation X = 62.5  Center of rotation y = 62.5
Post rotate rotation = 90.0

我错过了什么吗?或者我以错误的方式解决了这个问题。

谢谢, 约什

1 个答案:

答案 0 :(得分:0)

解决。 问题在于旋转中心。看来,当您为图像设置旋转中心时,轴0,0实际上位于图像的左上角,而不是游戏容器的左上角。 所以在我的例子中,我将旋转中心x坐标设置为图像的X坐标加上宽度超过2,实际上我只需将其设置为宽度超过2。

相关问题