2d内圆之间的均匀网格碰撞检测

时间:2014-02-01 08:28:20

标签: java grid javafx collision-detection uniformgrid

我正在开发一款2d街机游戏,我有5种不同大小的圆圈:船只,导弹和3种类型的怪物。

这就是它的样子:

enter image description here

目前我正在使用蛮力碰撞检测,我会检查每个导弹与每个怪物,而不考虑碰撞概率。可悲的是,这使得这个过程非常缓慢。

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

    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.
            }
        }
    }

但这就是我完全失去的地方。我甚至不确定我提供的源代码是否正确。请让我知道如何使基于网格的碰撞工作。我基本上都阅读了每本可以获得的教程但没有太大影响。 感谢。

0 个答案:

没有答案
相关问题