AABB碰撞检测不一致

时间:2016-03-10 22:25:51

标签: javascript 2d collision-detection

昨天我在gamedev上问过这个问题,但没有人回复。

我在第一场比赛中制作了2d无尽的跳投,并且我在玩家和平台之间的单向碰撞检测中遇到了一些问题。

大多数时候都会正确检测到碰撞,但偶尔玩家会莫名其妙地穿过平台。

我怀疑这是通过平台重影,但我不明白为什么,我太靠近它,无法确定问题的根源。

我的代码:

for (i = 0, len = this._platforms.length; i < len; i++) {
        var p = this._platforms[i];

        // Get difference between center of player and center of platform
        p.proximity = Math.abs(p.x / 2 - player.x / 2) + Math.abs(p.y / 2 - player.y / 2);

        if (p.closest()) {
            if (p.collision()) {
                player.onplatform = true;
                player.y = p.y - player_s_right.height;
            } else {
                player.onplatform = false;
            }
        }
 ... 
}

它循环通过平台对象,仅当当前平台是最接近播放器的平台时才运行碰撞测试。

以下是最近的()和collision()检查:

// Return whether platform is the closest one to the player
Platform.prototype.closest = function() {

    var minprox = Math.min.apply(Math, platforms._platforms.map(function (obj) {
    return obj.proximity;
    }));

    return this.proximity === minprox;
}

// Collision detection
Platform.prototype.collision = function() {

// offset of 15 pixels to account for player's bandana
    var px = player.direction > 0 ? player.x + 15 : player.x,
        px2 = player.direction > 0 ? player.x + player_s_right.width : player.x + player_s_right.width - 15,
        py = player.y + player_s_left.height,
        py2 = player.y + player_s_right.height + player.yvelocity,

        platx2 = this.x + platform_s.width,
        platy2 = this.y + platform_s.height;

    if (((px > this.x && px < platx2) || (px2 > this.x && px2 < platx2)) && (py2 >= this.y && py2 <= platy2) && py <= this.y) {
        return true;
    }
};

如果有人能对此有所了解,我真的很感激。

0 个答案:

没有答案