简体   繁体   English

Java冲突检测

[英]Java Collision Detection

I am attempting to detect collisions between two shapes using intersects , however, the detection is not working. 我正在尝试使用intersects检测两个形状之间的碰撞,但是检测不起作用。

Printing out the Circle objects reveal the x and y positions are set as 0. I suspected that maybe the position getter and setter methods were not working correctly, however, printing out this information reveals non-zero values. 打印出Circle对象会显示x和y位置设置为0。我怀疑位置获取和设置方法可能无法正常工作,但是,打印此信息会显示非零值。

Why is the detection not working? 为什么检测不起作用?

Thank you. 谢谢。

Edit: The below code is now working. 编辑:下面的代码现在正在工作。 See comments for detail of the issue / solution. 有关问题/解决方案的详细信息,请参见评论。

Main Class 主班

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 Class 圆班

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

         }  
    }

}

Shape Class 形状等级

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

}

The intersects method expect x, y, width and height properties to be correctly set, because they are used to detect if an object collides with another. intersects方法期望正确设置x,y,width和height属性,因为它们用于检测一个对象是否与另一个对象碰撞。

So, in your setter methods you need to invoke super.setLocation(newX, newY) , and you should provide a valid width and height too. 因此,在您的setter方法中,您需要调用super.setLocation(newX, newY) ,并且还应该提供有效的宽度和高度。

So, it should be: 因此,应为:

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

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

and

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

Or, you could just use the already provided methods from class Rectangle : 或者,您可以只使用Rectangle类中已经提供的方法:

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

And also use setLocation instead of setxPos and setyPos . 并且还使用setLocation代替setxPossetyPos

The complete code would become like this: 完整的代码将如下所示:

Shape class 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 class: 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 class: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM