-->

在二维圆之间均匀网格碰撞检测(Uniform grid collision detection be

2019-10-19 06:48发布

我工作在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.
            }
        }
    }

但是,这是我在一个完全丧失哪里。 我什至不知道我提供的源代码是正确的。 请让我知道如何使基于网格碰撞的工作。 我读过基本上每个教程中,我能得到我的手,但没有多大效果。 谢谢。

文章来源: Uniform grid collision detection between circles in 2d