简体   繁体   English

Java 2D旋转和全局坐标?

[英]Java 2D Rotation and Global Coordinates?

I am making a simple game where there's an object (a square with a rectangular barrel) that looks towards the mouse. 我正在做一个简单的游戏,其中有一个朝着鼠标的对象(一个带有矩形桶的正方形)。 The rotation part works pretty well, but I want the object to move when a button is pressed along the y axis, but directly upwards, not relative to its rotation. 旋转部分效果很好,但是我希望对象在沿y轴按下按钮时移动,但直接向上移动,而不是相对于其旋转。 Now, when a button is pressed, the object moves forward in the direction it is facing. 现在,当按下按钮时,对象将朝其面对的方向向前移动。 How is it possible to move it directly up the y axis, regardless of what direction the object is in? 无论对象处于哪个方向,如何将其直接沿y轴向上移动? Here's the code: 这是代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class RotationTestMain extends JFrame implements MouseMotionListener, KeyListener{

    public static final long serialVersionUID = 666L;

    public static final int SIZE = 50;

    double xComponent;
    double yComponent;

    Image dbImage;
    Graphics dbg;

    int x;
    int y;
    int centerX;
    int centerY;

    JPanel area;

    public RotationTestMain(){
        area = new JPanel();
        area.addMouseMotionListener(this);
        addKeyListener(this);

        x = 487;
        y = 359;

        centerX = x + SIZE/2;
        centerY = y + SIZE/2;

        add(area);

        setSize(1024,768);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void paint(Graphics g){
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        Graphics2D g2d = (Graphics2D) dbg;
        draw(g2d);
        g.drawImage(dbImage, 0, 0, this);
    }

    public void draw(Graphics2D g2d){
        g2d.setColor(Color.BLACK);
        g2d.rotate(calculateRotation(), centerX, centerY);
        g2d.fillRect(x, y, SIZE, SIZE);
        g2d.fillRect(centerX-5,y-20,10,25);
        repaint();
    }

    public double calculateRotation(){
        double rot;
        if(yComponent >= 0){
            rot = 3 * Math.PI - Math.atan(xComponent/yComponent);
        }else {
            rot = 2 * Math.PI - Math.atan(xComponent/yComponent);
        }
        return rot;
    }

    public static void main(String[] args) {
        new RotationTestMain();
    }

    @Override
    public void mouseDragged(MouseEvent m) {
        centerX = x + SIZE/2;
        centerY = y + SIZE/2;
        xComponent = m.getX() - centerX;
        yComponent = m.getY() - centerY;
    }

    @Override
    public void mouseMoved(MouseEvent m) {
        centerX = x + SIZE/2;
        centerY = y + SIZE/2;
        xComponent = m.getX() - centerX;
        yComponent = m.getY() - centerY;
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        //TODO FIX THE MOVING PROBLEM
        this.y -= 3;
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
    }
}

Thanks so much for the help!! 非常感谢你的帮助!!

I think the problem is in this line g2d.rotate(calculateRotation(), centerX, centerY); 我认为问题出在这行g2d.rotate(calculateRotation(),centerX,centerY);

you are not rotating the object but the whole g2d. 您不是旋转对象,而是整个g2d。 Therefore the x,y axis are also rotated, as they are part of the g2d. 因此,x,y轴也旋转,因为它们是g2d的一部分。

So when you do this.y -= 3; 因此,当您执行此操作时。y-= 3; You are indeed moving along the y axis, but the y axis has been rotated. 您确实沿y轴移动,但是y轴已旋转。

I recommend to find a way to rotate the object instead of the whole g2d. 我建议找到一种旋转对象而不是整个g2d的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM