0

Java/Slick 2D を使用してグラフィックを操作し、マウスを使用して画像を回転させています。ただし、奇妙なことが起こります。画像が必ずしもマウスの方を向いているとは限りません。通常の線から 45 度の角度ではそうですが、遠くに行くほどずれが大きくなります。以下の画像を参照してください (白い円はマウス、テキストは角度です): 80度の画像 45 度の画像

使用した回転コードは次のとおりです。

int mX = Mouse.getX();
        int mY = HEIGHT - Mouse.getY();
        int pX = sprite.x;
        int pY = sprite.y;
        int tempY, tempX;
        double mAng, pAng = sprite.angle;
        double angRotate=0;

        if(mX!=pX){
            mAng = Math.toDegrees(Math.atan2(mY - pY, mX - pX));
            if(mAng==0 && mX<=pX)
                mAng=180;
        }
        else{
            if(mY>pY)
                mAng=90;
            else
                mAng=270;
        }

        sprite.angle = mAng;
        sprite.image.setRotation((float) mAng);     

何が起こっているのですか?画像座標が左上から来ているという事実と関係があると思いますが、それに対抗する方法がわかりません。参考: 画面 640x460、画像 128x128、ウィンドウの中央に配置。

編集: 残念ながら、実際には何も機能しませんでした。これは、いくつかの詳細情報を含む画像です。

35 度の矢印

EDIT2:答えが見つかりました!変更する必要がありました: int px/py = sprite.x/y に

        int pX = sprite.x+sprite.image.getWidth()/2;
    int pY = sprite.y+sprite.image.getHeight()/2;
4

2 に答える 2

2

左からマウスの値を取得し、その距離を回転に設定しているように見えます...ここに役立つものがあります:

http://www.instructables.com/id/Using-Java-to-Rotate-an-Object-to-Face-the-Mouse/?ALLSTEPS

于 2012-09-26T21:09:08.077 に答える
0

This is some example code I wrote of a similar question which might help.

Now it doesn't use slick, it uses Swing and Graphics2D but it might help you gain some ideas.

public class TestRotatePane extends JPanel {

    private BufferedImage img;
    private Point mousePoint;

    public TestRotatePane() {

        try {
            img = ImageIO.read(getClass().getResource("/MT02.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseMoved(MouseEvent e) {

                mousePoint = e.getPoint();

                repaint();

            }

        });

    }

    @Override
    public Dimension getPreferredSize() {

        return new Dimension(img.getWidth(), img.getHeight());

    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g.create();

        double rotation = 0f;

        int width = getWidth() - 1;
        int height = getHeight() - 1;

        if (mousePoint != null) {

            int x = width / 2;
            int y = height / 2;

            int deltaX = mousePoint.x - x;
            int deltaY = mousePoint.y - y;

            rotation = -Math.atan2(deltaX, deltaY);

            rotation = Math.toDegrees(rotation) + 180;

        }

        int x  = (width - img.getWidth()) / 2;
        int y  = (height - img.getHeight()) / 2;

        g2d.rotate(Math.toRadians(rotation), width / 2, height / 2);
        g2d.drawImage(img, x, y, this);

        x = width / 2;
        y = height / 2;
        g2d.setStroke(new BasicStroke(3));
        g2d.setColor(Color.RED);
        g2d.drawLine(x, y, x, y - height / 4);
        g2d.dispose();

    }

}

enter image description here

You will, obviously, need to supply your own image ;)

于 2012-09-26T22:51:02.957 に答える