[英]Java Collision Detection
我正在嘗試使用intersects
檢測兩個形狀之間的碰撞,但是檢測不起作用。
打印出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);
}
}
intersects
方法期望正確設置x,y,width和height屬性,因為它們用於檢測一個對象是否與另一個對象碰撞。
因此,在您的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
代替setxPos
和setyPos
。
完整的代碼將如下所示:
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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.