围绕不同的点旋转图像。围绕不同点旋转图像

问题描述 投票:1回答:1

我是一个相当新的编程,我试图做一个小游戏,你可以控制(旋转)一个坦克和不同的枪在坦克顶部独立。(我使用的是Slick)

在坦克旋转过程中,炮应该围绕坦克图像的中心旋转,因为它们是连接在一起的。

public void drawTankandGuns(){
  tankImage.draw(position.x, position.y);
  gunImage.draw(position.x+canonOffsetX, position.y+canonOffsetY);
}

public void rotateDuringMovement(){
  gunImage.setCenterOfRotation(tankImage.getWidth/2-gunOffsetX,
    tankImage.getHeight/2-gunOffsetY);

  gunImage.rotate(angle);
  tankImage.rotate(angle);
}

但如果我想在没有坦克的情况下旋转枪支(坦克已经旋转了),并将旋转中心设置为枪支,那么枪支图像就会被拉回到原来的位置,失去了围绕坦克旋转的位置......

编辑:解决方案是使用不同的方法。绘制枪的图像依赖于坦克图像旋转的sincos。

java rotation center point slick2d
1个回答
1
投票

解决的办法是使用不同的方法.绘制炮图取决于坦克图旋转的sincos。

//calculate the gun position on top of the tank
gunPosX = tankPosX + gunPosOffsetX;
gunPosY = tankPosY + gunPosOffsetY;

//calculate the tank rotation center
tankRotationsCenterX = tankPosX + tankImage.getCenterOfRotationX();
tankRotationsCenterY = tankPosY + tankImage.getCenterOfRotationY();

//calculate distance between gun position and tank rotation center
dx = tankRotationsCenterX - gunPosX ;
dy = tankRotationsCenterY - gunPosY ;
dis = Math.sqrt(dx * dx + dy * dy);

//calculate the offset based on the rotation of the tank
//rotation offset for the gun placement
gunRotaOff = 20;

gunX_offset = dis*Math.cos(Math.toRadians(tankImage.getRotation()+gunRotaOff));
gunY_offset = dis*Math.sin(Math.toRadians(tankImage.getRotation()+gunRotaOff));

gunXhalf = gun.getImage().getWidth() / 2;
gunYhalf = gun.getImage().getHeight() / 2;

//draws the gun dependend on the ship position and the ship rotation
//don't forget to subtract half the width/height for exact positioning
gun.drawIngame(tankRotationsCenterX - gun_x_offset)-gunXhalf , (tankRotationsCenterY - gun_y_offset) - gunYhalf ));
© www.soinside.com 2019 - 2024. All rights reserved.