문제

After a long battle with Graphics2D, I have finally been able to draw, rotate and move a small triangle in an applet. Now what I want to do is find the angle the triangle is pointing in, then move it in that direction. Is there a method or algorithm to make it do this? I have tried this:

public double calcAngleMoveX(double angle)
    {
        return (double)(Math.cos(angle*Math.PI/180));
    }
public double calcAngleMoveY(double angle)
    {
        return (double)(Math.sin(angle*Math.PI/180));
    }


ship.incY(ship.calcAngleMoveY(ship.getFaceAngle()-90));
ship.incX(ship.calcAngleMoveX(ship.getFaceAngle()-90));

But it does really strange things. Can anyone solve this for me?

EDIT: This is my code in paint():

        g2d = (Graphics2D)g;
        AffineTransform identity = new AffineTransform();
        g2d.drawString(Double.toString(ship.getX()),100,100);
        g2d.drawString(Double.toString(ship.getY()),100,120);
        int width = getSize().width;
        int height = getSize().height;
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0,0,width,height);
        g2d.translate(ship.getX(),ship.getY());
        g2d.rotate(Math.toRadians(ship.getFaceAngle()));
        g2d.setColor(Color.RED);
        g2d.fill(shape);

And this is my keyPressed method:

int ke = e.getKeyCode();
        switch(ke)
        {
        case KeyEvent.VK_LEFT:
            ship.setFaceAngle(ship.getFaceAngle()-5);
            break;
        case KeyEvent.VK_RIGHT:
            ship.setFaceAngle(ship.getFaceAngle()+5);
            break;
        case KeyEvent.VK_UP:
            ship.incX(ship.calcAngleMoveX(ship.getFaceAngle())*ship.velocity);
            break;
        case KeyEvent.VK_DOWN:
            ship.incY(ship.calcAngleMoveY(ship.getFaceAngle())*ship.velocity);
            break;
        }
        repaint();

These are the calculation methods:

public double calcAngleMoveX(double angle)
    {
        return (double)(Math.sin(angle * Math.PI / 180.0));
    }
    public double calcAngleMoveY(double angle)
    {
        return (double)(-Math.cos(angle * Math.PI / 180.0));
    }
도움이 되었습니까?

해결책

The ship movement depends on the coordinate system used in your graphics.

Since you've said 90 degrees is "right", I'm going to assume that you're using clockwise bearings, with 0 degrees as "up".

Rather than manipulating the angle to convert from the cartesian convention of 0 degrees being right and then rotating clockwise, it's easier to simply flip around which trigonometric function you call for each axis.

Graphics2D, like most other graphics systems, uses increasing X coordinates as you move right, so:

delta_x = Math.sin(angle * Math.PI / 180.0)

You should be able to trivially see that this gives delta_x = 1.0 for 90 degrees, and delta_x = -1.0 for 270 degrees.

In Graphics2D Y coordinates increase as you move downwards (i.e. [0, 0] is at top-left), so use:

delta_y = -Math.cos(angle * Math.PI / 180.0)

Giving delta_y = -1.0 for 0 degrees, and delta_y = 1.0 for 180 degrees.

If you were using a system with [0, 0] at bottom-left you would just remove the unary minus (-) from the delta_y formula.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top