自定义碰撞盒

Custom collisionbox

我正在使用 Eclipse 创建我自己的游戏引擎。我用方形瓷砖进行所有渲染,但我现在有一些精灵不需要精灵的边缘作为碰撞的地方。是否可以在像素级别制作自定义碰撞框?

在下面的代码中你可以看到我使用玩家在 x 和 y 方向上的速度来测试他是否与 "solid" 方块发生碰撞,但我希望它有一个自定义的碰撞盒而不是边缘。

public boolean hasCollided(int xa, int ya) {
    int xMin = 0;
    int xMax = 8;
    int yMin = 0;
    int yMax = 5;

    for (int x = xMin; x < xMax; x++) {
        if (isSolidTile(xa, ya, x, yMin) || isSolidObject(xa, ya, x, yMin)) {
            return true;
        }
    }
    for (int x = xMin; x < xMax; x++) {
        if (isSolidTile(xa, ya, x, yMax) || isSolidObject(xa, ya, x, yMax)) {
            return true;
        }
    }
    for (int y = yMin; y < yMax; y++) {
        if (isSolidTile(xa, ya, xMin, y) || isSolidObject(xa, ya, xMin, y)) {
            return true;
        }
    }
    for (int y = yMin; y < yMax; y++) {
        if (isSolidTile(xa, ya, xMax, y) || isSolidObject(xa, ya, xMax, y)) {
            return true;
        }
    }
    return false;
}

这里是 isSolidObject,因为瓷砖部分正在工作

protected boolean isSolidObject(int xa, int ya, int x, int y) {
    if(level == null) {
        return false;
    }

    GameObject newObject = level.getGameObject((this.x + x + xa) >> 4, (this.y + y + ya) >> 4);

    if(newObject.isSolid()) {
        return true;
    }
    return false;
}

提前致谢!

也许您可以在 https://www.lwjgl.org/ Lightweight Java Game Library (open source) or http://slick.ninjacave.com/(开源游戏库)

中找到它

在这个线程中是使用 Slick2D 的碰撞检测 Slick2D Rectangle Collision Detection

(对于 C++,有一个完整的开源游戏引擎 http://www.cocos2d-x.org/

对于HTML5游戏有phaser.iohttp://phaser.io/(这真的很不错...)