与Java中的其他对象发生冲突

时间:2016-01-02 07:01:35

标签: java game-physics

好的,我有这个程序可以创建一个

  

NanOrg

作为移动球的物体,从墙壁和其他NanOrgs反弹

`公共类NanOrg扩展了Ellipse2D.Float {

public int x_speed, y_speed;
private int d;
private int width = MainWindow.WIDTH;
private int height = MainWindow.HEIGHT; 
private ArrayList<NanOrg> nanorgs;
Rectangle2D r = new Rectangle2D.Float(super.x, super.y, d, d);

public NanOrg(int diameter, ArrayList<NanOrg> nanorgs){
    super((int) (Math.random() * (MainWindow.WIDTH - 20) + 1), (int) (Math.random() * (MainWindow.HEIGHT - 20) + 1), diameter, diameter);
    this.d = diameter;

    this.x_speed = (int) (Math.random() * 5 + 1);
    this.y_speed = (int) (Math.random() * 5 + 1);
    this.nanorgs = nanorgs;

}
public void move(){
    for(NanOrg nanorg : nanorgs){
        if(nanorg != this && nanorg.intersects(r)){
            int tempx = x_speed;
            int tempy = y_speed;
            x_speed = nanorg.x_speed;
            y_speed = nanorg.y_speed;
            nanorg.x_speed = tempx;
            nanorg.y_speed = tempy;
            break;
        }
    }
    if(super.x < 0){
        super.x = 0;
        x_speed = Math.abs(x_speed);
    }
    else if(super.x > width - d){
        super.x = width - d;
        x_speed = -Math.abs(x_speed);
    }
    if(super.y < 0){
        super.y = 0;
        y_speed = Math.abs(y_speed);
    }
    else if(super.y > height - d){
        super.y = height - d;
        y_speed = -Math.abs(y_speed);
    }
    super.x += x_speed;
    super.y += y_speed;
}

}`和

  

代表不动的油滴的物体。

public class Oil extends Ellipse2D.Float {

private int width = MainWindow.WIDTH;
private int height = MainWindow.HEIGHT; 
private int d;
private ArrayList<Oil> oils;
Rectangle2D rect = new Rectangle2D.Float(super.x, super.y, d, d);

public Oil(int diameter){
    super((int) (Math.random() * (MainWindow.WIDTH - 20) + 1), (int) (Math.random() * (MainWindow.HEIGHT - 20) + 1), diameter, diameter);
    this.d = diameter;
}

}

NanOrgs在随机位置产生并以随机方向和速度移动。 NanOrg能够互相反弹,但我需要帮助找出如何处理Oil和NanOrg之间的碰撞,特别是因此NanOrg可以&#34;吃&#34;通过去除它的油。我有另外两个类:

  

MainWindow和PaintSurface

MainWindow类将窗口创建为JApplet。

`public class MainWindow扩展了JApplet {

public static final int WIDTH = 500;
public static final int HEIGHT = 500;
private PaintSurface canvas;

public void init(){
    this.setSize(WIDTH, HEIGHT);
    this.setName("NanOrg Program");
    canvas = new PaintSurface();
    this.add(canvas, BorderLayout.CENTER);
    Timer t = new Timer(20, e -> {canvas.repaint();});
    t.start();
        }

} The PaintSurface Class "draws" the Oil and NanOrgs公共类PaintSurface扩展了JComponent {

public ArrayList<NanOrg> nanorgs = new ArrayList<NanOrg>();
public ArrayList<Oil> oils = new ArrayList<Oil>();

public PaintSurface(){
    for(int i = 0; i < 5; i++)
        nanorgs.add(new NanOrg(10, nanorgs));

    for(int o = 0; o < 30; o++)
        oils.add(new Oil(10));
}

public void paint(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.MAGENTA);


    for(NanOrg nanorg : nanorgs){
        nanorg.move();
        g2.fill(nanorg);

    }
    for(Oil oil : oils){
        g2.setColor(Color.BLACK);
        g2.fill(oil);
    }

}

}`

那是我的程序,所以回顾一下:我已经创建了它,所以NanOrgs互相反弹和墙壁,但我需要帮助我需要为NanOrg编码以及#34; eat& #34;油(例如将其移除)以及放置油的代码的位置。我试过了 public void eatOil(){ for(Oil oil : oils){ if(oil.intersects(r)){ oils.remove(oil); } } } 在NanOrg类中,然后在我的PaintSurface类中调用该方法,但是当我运行它时,我得到了NullPointerException。然后我尝试在Oil和PaintSurface类中执行相同的代码,但是我无法使用这两个类中的任何一个&#34; r&#34;作为NanOrg中定义的变量。所以,如果你能帮我弄清楚该怎么做那会很棒,或者告诉我如果我做错了怎么办。如果您有任何问题需要尝试并澄清ypur知识,请询问并且我会尽我所能回答。

1 个答案:

答案 0 :(得分:0)

你必须在NanOrgs中创建一个方法来检测给定的NanOrg和给定的油是否相互交叉。您需要在Oil类中维护一个Oil实例列表,就像NanOrg类中的NanOrgs列表一样。

然后在每次调用NanOrg的mo​​ve()时,你必须检查NanOrg是否与Oil列表中的任何油滴相交。如果你发现它们相交,你就必须从油中的清单中除去油。

这就是全部。