2017-03-05 6 views
0

intersectsを使用して2つの図形間の衝突を検出しようとしていますが、検出が機能していません。Java Collision Detection

Circleオブジェクトを印刷すると、xとyの位置が0に設定されていることがわかります。位置のゲッターとセッターのメソッドが正しく動作していない可能性がありますが、この情報を印刷するとゼロ以外の値が示されます。

なぜ検出が機能しないのですか?

ありがとうございます。

編集:以下のコードは現在動作しています。問題/ソリューションの詳細については、コメントを参照してください。なぜなら彼ら

メインクラス

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()); 
    } 

} 

サークルクラス

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、幅及び高さのプロパティが正しく設定されることを期待しますオブジェクトが別のオブジェクトと衝突するかどうかを検出するために使用されます。

したがって、セッターメソッドでは、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を使用setxPosおよびの代わりに。

完全なコードは次のようになる:

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()); 
    } 

}