如何以一致的速度直线移动和对象

时间:2014-05-04 00:12:25

标签: java

我试图直线移动一个圆圈。但我的守则没有给出预期的结果。我使用mouseMotionListener来不断获取目标点,x_pos和y_pos是为我的圈子协调的。我正在使用三角函数sin cos和atan,以直线移动对象 - 我从堆栈溢出上发布的问题看到here的逻辑 - 但它没有给我预期的结果,我做错了什么plz帮助:

 import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;

public class Snake extends JPanel {
    static Snake snake = new Snake();
    static Co_Ordinates co = new Co_Ordinates();
    static int x_pos = 10, y_pos = 500;
    static int slope;
    int delta_x,delta_y;
    double direction;
    double x_inc,y_inc;
    public Snake() {
    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);
        Graphics2D g2d=(Graphics2D)g;
        g2d.setColor(Color.green);
        g2d.fillArc(x_pos, y_pos, 20, 20, 0, 360);
        try {
            Thread.sleep(10);
        } catch (Exception e) {
        }
        /*if(x_pos==co.x_Cur){
            if(co.y_Cur>=y_pos)
                y_pos++;
            else if(co.y_Cur<=y_pos)
                y_pos--;

        }
        else if(y_pos==co.y_Cur){
            if(co.x_Cur>=x_pos)
                x_pos++;
            else if(co.x_Cur<=x_pos)
                x_pos--;

        }
        */
        //slope=((co.y_Cur - y_pos) / (co.x_Cur - x_pos));
        //y_pos = slope*x_pos+y_pos;
        //x_pos++;
        //System.out.println("...");
        if(x_pos!=co.x_Cur&&y_pos!=co.y_Cur){
             int delta_x = co.x_Cur - x_pos;
             int delta_y = co.y_Cur - y_pos;
             direction = Math.atan(delta_y / delta_x); // Math.atan2(deltaY, deltaX) does the same thing but checks for deltaX being zero to prevent divide-by-zero exceptions
             double speed = 5.0;
             x_inc = (speed * Math.cos(direction));
             y_inc = (speed * Math.sin(direction));
             x_pos+=x_inc;
             y_pos+=y_inc;
        }

        //x_pos = co.x_Cur;
        repaint(10);// parameters
    }

    public void move() {

    }

    public static void main(String args[]) {
        JFrame jf = new JFrame();
        jf.add(co);
        jf.addMouseMotionListener(co);
        jf.addMouseListener(co);
        jf.add(snake);
        jf.setSize(600, 600);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }
}
class Co_Ordinates extends JPanel implements MouseMotionListener, MouseListener {


    static int slope;
    static Snake sn = new Snake();
    static int x_Cur=sn.x_pos-20;
    static int y_Cur=sn.y_pos-40;
    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        x_Cur = e.getX() - 20;
        y_Cur = e.getY() - 40;

    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}// Class Listener ends here

1 个答案:

答案 0 :(得分:0)

在您使用Math.atan的代码中,应更改为Math.atan2atan仅提供介于-90deg和90deg之间的角度值。 atan2给出的角度值介于-180deg和180deg之间。

我修改了以下代码部分。

  • 首先,将atan更改为atan2
  • 其次,如果圆圈符合鼠标指针(目标?),则速度变为零
  • 第三种是次要的,但除非性能至关重要,否则不需要if条件。

请试试这个。

// if(x_pos!=co.x_Cur&&y_pos!=co.y_Cur){
    delta_x = co.x_Cur - x_pos;
    delta_y = co.y_Cur - y_pos;
    direction = Math.atan2(delta_y, delta_x); 
    double speed = Math.sqrt(delta_x*delta_x + delta_y*delta_y);
    speed = Math.min(speed, 5.0);
    x_inc = (speed * Math.cos(direction));
    y_inc = (speed * Math.sin(direction));
    x_pos+=x_inc;
    y_pos+=y_inc;
//}