2017-03-05 251 views
0

我正在嘗試使用intersects檢測兩個形狀之間的碰撞,但檢測不起作用。Java碰撞檢測

打印出Circle對象顯示x和y位置設置爲0.我懷疑可能是位置getter和setter方法工作不正常,但是打印出此信息會顯示非零值。

爲什麼檢測不起作用?

謝謝。

編輯:下面的代碼現在正在工作。請參閱評論以瞭解問題/解決方案的細節。

主類

import java.awt.Graphics; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Main extends JPanel { 

    public static List<Circle> circleList = new ArrayList<>(); 
    private static Random random = new Random();  

    public Main() { 
     for (int i = 0; i < 2; i++) { 
      circleList.add(new Circle(random.nextInt(500), random.nextInt(500))); 
     }  
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (Circle CircleShape : circleList) { 
      CircleShape.collision(); 
      CircleShape.drawCircle(g); 
     } 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setSize(500, 500); 
     frame.setResizable(true); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.add(new Main()); 
    } 

} 

Circle類

import java.awt.Graphics; 

public class Circle extends Shape { 

    public Circle(int x, int y) { 
     super(x, y); 
     super.setSize(200, 200); 
    } 

    public void drawCircle(Graphics g) { 
     g.setColor(colour); 
     g.fillOval(x, y, 200, 200); 
    } 

    public void collision() { 
     for (Circle CircleShape : Main.circleList) { 
      System.out.println(CircleShape); 
      System.out.println(CircleShape.getxPos()); 

      if (this.intersects(CircleShape)) { 
       System.out.println("collision detected"); 
      } else { 
       System.out.println("no collision detected"); 
      } 

     } 
    } 

} 

形狀類

import java.awt.Color; 
import java.awt.Rectangle; 

public class Shape extends Rectangle { 

    //int x, y; 
    Color colour; 

    public Shape(int x, int y) { 
     //this.setxPos(x); 
     //this.setyPos(y); 
     this.setPos(x, y); 
    } 
/* 
    public int getxPos() { 
     return this.x; 
    } 

    public void setxPos(int x) { 
     this.x = x; 
    } 

    public int getyPos() { 
     return this.y; 
    } 

    public void setyPos(int y) { 
     this.y = y; 
    } 
*/ 

    public void setPos(int x, int y) { 
     super.setLocation(x, y); 
    } 

    public Point getPos() { 
     return new Point(x, y); 
    } 

} 
+0

你應該在'setxPos'和'setyPos'方法中調用'super.setLocation(x,y)',否則座標將被忽略。在子類中重新定義變量不會覆蓋超類中的變量。 – BackSlash

+0

感謝您的回覆。我使用'super.setLocation(x,y)'創建了一個新的getter和setter方法,但是,碰撞檢測仍然不起作用。 – user1334130

+0

你能否用新代碼更新問題? – BackSlash

回答

1

intersects方法指望的x,y,寬度和高度屬性被正確地設置,因爲它們用於檢測物體是否與另一個物體發生碰撞。

所以,在你的setter方法中,你需要調用super.setLocation(newX, newY),你也應該提供一個有效的寬度和高度。

所以,它應該是:

public void setxPos(int x) { 
    this.x = x; 
    super.setLocation(x, y); 
} 

public void setyPos(int y) { 
    this.y = y; 
    super.setLocation(x, y); 
} 

public Circle(int x, int y) { 
    super(x, y); 
    super.setSize(200, 200); 
} 

或者,你可以使用已經提供的方法從Rectangle類:

public Circle(int x, int y, int width, int height) { 
    super(x, y, width, height); 
} 

,還可以使用setLocation而不是setxPossetyPos

完整的代碼會變成這樣:

Shape

import java.awt.Color; 
import java.awt.Rectangle; 

public class Shape extends Rectangle { 

    Color colour; 

    public Shape(int x, int y, int width, int height) { 
     // provided by the Rectangle class. Needed for proper collision detection 
     super(x, y, width, height); 
    } 
} 

Circle類:

import java.awt.Graphics; 

public class Circle extends Shape { 

    public Circle(int x, int y, int width, int height) { 
     super(x, y, width, height); 
    } 

    public void drawCircle(Graphics g) { 
     g.setColor(super.colour); 
     // instead of writing values here, we get them from width and height fields 
     g.fillOval(x, y, (int) getWidth(), (int) getHeight()); 
    } 

    public void collision() { 
     for (Circle CircleShape : Main.circleList) { 
      System.out.println(CircleShape); 
      System.out.println(CircleShape.getLocation().x); 

      if (this.intersects(CircleShape)) { 
       System.out.println("collision detected"); 
      } else { 
       System.out.println("no collision detected"); 
      } 

     } 
    } 
} 

Main類:

import java.awt.Graphics; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Main extends JPanel { 

    public static List<Circle> circleList = new ArrayList<>(); 
    private static Random random = new Random(); 

    public Main() { 
     for (int i = 0; i < 2; i++) { 
      // width and height are specified here instead of inside the Circle.drawCircle method. 
      circleList.add(new Circle(random.nextInt(500), random.nextInt(500), 200, 200)); 
     } 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (Circle CircleShape : circleList) { 
      CircleShape.collision(); 
      CircleShape.drawCircle(g); 
     } 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setSize(500, 500); 
     frame.setResizable(true); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.add(new Main()); 
    } 

}