1

私はプログラミングにかなり慣れていないので、戦車とその上にあるさまざまな銃を独立して制御(回転)できる小さなゲームを作ろうとしています。(私はスリックを使用しています)

戦車の回転中、銃は取り付けられているため、戦車の画像の中心を中心に回転する必要があります。

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);
}

これまでのところうまくいきます。銃が取り付けられ、タンクと一緒に回転します。しかし、戦車なしで銃を回転させたい (そして戦車は既に回転している) 場合、回転の中心を銃に戻すと、銃のイメージは元の位置に引き戻され、戦車の周りの回転から位置が失われます.. .

編集:解決策は、別のアプローチを使用することでした。tankImage 回転の sin/cos に依存する gunImage を描画します。

4

1 に答える 1

1

解決策は、別のアプローチを使用することでした。tankImage 回転の sin/cos に依存する gunImage を描画します。

//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 ));
于 2013-03-11T09:28:33.207 に答える