Java 二维圆间均匀网格碰撞检测

Java 二维圆间均匀网格碰撞检测,java,grid,javafx,collision-detection,uniformgrid,Java,Grid,Javafx,Collision Detection,Uniformgrid,我正在做一个2d街机游戏,我有5种不同大小的圆圈:飞船、导弹和3种怪物 这就是它看起来的样子: 目前,我正在使用蛮力碰撞检测,在不考虑碰撞概率的情况下检查每一枚导弹和每一个怪物。不幸的是,这使得这个过程非常缓慢 这是我的网格类,但它不完整。我非常感谢你的帮助 public class Grid { int rows; int cols; double squareSize; private ArrayList<Circle>[][] gri

我正在做一个2d街机游戏,我有5种不同大小的圆圈:飞船、导弹和3种怪物

这就是它看起来的样子:

目前,我正在使用蛮力碰撞检测,在不考虑碰撞概率的情况下检查每一枚导弹和每一个怪物。不幸的是,这使得这个过程非常缓慢

这是我的网格类,但它不完整。我非常感谢你的帮助

    public class Grid {

    int rows;
    int cols;
    double squareSize;
    private ArrayList<Circle>[][] grid;

    public Grid(int sceneWidth, int sceneHeight, int squareSize) {
        this.squareSize = squareSize;
// Calculate how many rows and cols for the grid.
        rows = (sceneHeight + squareSize) / squareSize;
        cols = (sceneWidth + squareSize) / squareSize;
// Create grid
        this.grid = (ArrayList[][]) new ArrayList[cols][rows]; //Generic array creation error workaround
    }

The addObject method inside the Grid class.
    public void addObject(Circle entity) {
// Adds entity to every cell that it's overlapping with.
        double topLeftX = Math.max(0, entity.getLayoutX() / squareSize);
        double topLeftY = Math.max(0, entity.getLayoutY() / squareSize);
        double bottomRightX = Math.min(cols - 1, entity.getLayoutX() + entity.getRadius() - 1) / squareSize;
        double bottomRightY = Math.min(rows - 1, entity.getLayoutY() + entity.getRadius() - 1) / squareSize;

        for (double x = topLeftX; x < bottomRightX; x++) {
            for (double y = topLeftY; y < bottomRightY; y++) {
                grid[(int) x][(int) y].add(entity); //Cast types to int to prevent loosy conversion type error.
            }
        }
    }
公共类网格{
int行;
int cols;
双平方;
私有ArrayList[][]网格;
公共网格(int-sceneWidth、int-sceneHeight、int-squareSize){
这个.squareSize=squareSize;
//计算网格的行数和列数。
行=(场景高度+平方大小)/平方大小;
cols=(场景宽度+平方尺寸)/平方尺寸;
//创建网格
this.grid=(ArrayList[][])新建ArrayList[cols][rows];//通用数组创建错误解决方法
}
Grid类中的addObject方法。
public void addObject(圆实体){
//将实体添加到与其重叠的每个单元格中。
double-topLeftX=Math.max(0,entity.getLayoutX()/squareSize);
double-topLeftY=Math.max(0,entity.getLayoutY()/squareSize);
double-bottomRightX=Math.min(cols-1,entity.getLayoutX()+entity.getRadius()-1)/squareSize;
double bottomRightY=Math.min(行-1,entity.getLayoutY()+entity.getRadius()-1)/squareSize;
对于(双x=topLeftX;x
但这正是我完全不知所措的地方。我甚至不确定我提供的源代码是否正确。请让我知道如何使基于网格的碰撞正常工作。我基本上已经阅读了所有我能接触到的教程,但没有多少效果。
谢谢。

看起来你已经差不多做到了,只要你的圆圈离开你的正方形,你就可以再次从列表中删除它们。