0

Graphcis2D と AffineTransform を使用して固定位置で画像を回転させると問題が発生します。

アイデアは、体の回転に従って画像を回転させることです。

画像の回転角度が体の回転角度と一致するため、回転は正しく行われています。ただし、回転が行われるため、ボディが描画されるはずの位置に画像が描画されません。絵を描くメソッドのコードは次のとおりです。

public void paintPicture(Graphics g, Body body) {

    Graphics2D g2 = (Graphics2D) g;

    Vector2f[] vertices = ((Box) body.getShape()).getPoints(body.getPosition(), body.getRotation());

    Vector2f topLeftCorner = vertices[0];

    AffineTransform oldTransform = g2.getTransform();

    AffineTransform at = new AffineTransform();

    at.rotate(body.getRotation());

    g2.setTransform(at);

    g2.drawImage(this.img, (int) topLeftCorner.x, (int) topLeftCorner.y, null);

    g2.setTransform(oldTransform);
}

座標 (topLeftCorner.x、topLeftCorner.y) に従って描画するのではなく、画像の動きを引き起こす可能性のあるアイデアはありますか?

4

1 に答える 1

0

最初にオブジェクトを移動して、アンカー ポイント (回転させたいポイント) が原点にくるようにし、回転を実行してから元に戻す必要があります。したがって、ポイント (50, 75) を中心に回転させたい場合は、次のようにします。

at.translate (-50, -75);
at.rotate (body.getRotation());
at.translate (50, 75);

あなたの AffineTransform クラスが変換を蓄積できると仮定しています。そうでない場合は、3 つの異なる変換が必要になります。

于 2012-01-03T18:05:32.657 に答える