ThreeJS中的定向盒相交

尼古拉斯

我想知道是否2包围盒使用threeJS相交。

有一个box3.intersect方法,但它仅适用于未定向的边界框。

是否已经内置了某些东西,或者我必须自己实现它?

我想知道在下面的示例中每个蓝色框是否与粉红色框相交。我只想知道他们是否接触,而不是相交处的样子。

在此处输入图片说明

谢谢

布伦丹·安纳布尔(Brendan Annable)

如果您打算自己动手,我建议阅读Johnny Huynh的《定向边界框分离轴定理》

这是一种碰撞检测方法,给出了我为正在开发的游戏制作的两个定向边界框。它不在ThreeJS中,但转换起来应该很简单。

注意:它将glMatrix用于矩阵数学。重要说明:我相信分辨率矢量中有一个错误,但我没有找到。不过,交叉路口检测似乎很不错。

/**
 * Checks if two given bounding boxes intersect with one another.
 *
 * Results an object with the following keys:
 * @property {boolean} intersects - True if the bounding boxes intersect
 * @property    {vec3} resolution - A vector specifying the shortest distance
 * and magnitude to move the boxes such that they are no longer intersecting
 *
 * Uses the Separating Axis Theorem
 * See http://en.wikipedia.org/wiki/Hyperplane_separation_theorem)
 * Looks for separating planes between the bounding boxes.
 *
 * @param {PhysJS.util.math.BoundingBox} box1 The first bounding box
 * @param {PhysJS.util.math.BoundingBox} box2 The second bounding box
 * @returns {Object} Containers two properties, 'intersects' and 'resolution'
 */
intersects: function (box1, box2) {
    // assumes the position of each box to be an orthonormal basis
    var pos1 = box1.getPosition(); // mat44
    var pos2 = box2.getPosition(); // mat44
    var center1 = vec4.transformMat4(vec4.create(), box1.getCenter(), pos1);
    var center2 = vec4.transformMat4(vec4.create(), box2.getCenter(), pos2);
    var centerDifference = vec4.subtract(vec4.create(), center2, center1);

    var results = {
        intersects: true,
        resolution: null
    };

    // broad phase
    var maxDiameter1 = vec4.length(vec4.subtract(vec4.create(), box1.getMax(), box1.getMin()));
    var maxDiameter2 = vec4.length(vec4.subtract(vec4.create(), box2.getMax(), box2.getMin()));
    if (vec4.length(centerDifference) > maxDiameter1 + maxDiameter2) {
        results.intersects = false;
        return results;
    }

    // narrow phase

    // get the axis vectors of the first box
    var ax1 = mat4.col(pos1, 0);
    var ay1 = mat4.col(pos1, 1);
    var az1 = mat4.col(pos1, 2);
    // get the axis vectors of the second box
    var ax2 = mat4.col(pos2, 0);
    var ay2 = mat4.col(pos2, 1);
    var az2 = mat4.col(pos2, 2);

    // keep them in a list
    var axes = [ax1, ay1, az1, ax2, ay2, az2];

    // get the orientated radii vectors of the first box
    var radii1 = box1.getRadii();
    var radX1 = vec4.scale(vec4.create(), ax1, radii1[0]);
    var radY1 = vec4.scale(vec4.create(), ay1, radii1[1]);
    var radZ1 = vec4.scale(vec4.create(), az1, radii1[2]);

    // get the orientated radii vectors of the second box
    var radii2 = box2.getRadii();
    var radX2 = vec4.scale(vec4.create(), ax2, radii2[0]);
    var radY2 = vec4.scale(vec4.create(), ay2, radii2[1]);
    var radZ2 = vec4.scale(vec4.create(), az2, radii2[2]);

    var smallestDifference = Infinity;
    // there are 15 axes to check, so loop through all of them until a separation plane is found
    var zeros = vec4.create();
    for (var i = 0; i < 15; i++) {
        var axis;

        // the first 6 axes are just the axes of each bounding box
        if (i < 6) {
            axis = axes[i];
        }
        // the last 9 axes are the cross product of all combinations of the first 6 axes
        else {
            var offset = i - 6;
            var j = Math.floor(offset / 3);
            var k = offset % 3;
            axis = vec4.cross(vec4.create(), axes[j], axes[k + 3]);
            if (vec4.close(axis, zeros)) {
                // axes must be collinear, ignore
                continue;
            }
        }

        // get the projections of the first half box onto the axis
        var projAx1 = Math.abs(vec4.dot(radX1, axis));
        var projAy1 = Math.abs(vec4.dot(radY1, axis));
        var projAz1 = Math.abs(vec4.dot(radZ1, axis));

        // get the projections of the second half box onto the axis
        var projAx2 = Math.abs(vec4.dot(radX2, axis));
        var projAy2 = Math.abs(vec4.dot(radY2, axis));
        var projAz2 = Math.abs(vec4.dot(radZ2, axis));

        // sum the projections
        var projectionBoxesSum = projAx1 + projAy1 + projAz1 + projAx2 + projAy2 + projAz2;

        // get the projection of the center difference onto the axis
        var projectionDifference = Math.abs(vec4.dot(centerDifference, axis));

        if (projectionDifference >= projectionBoxesSum) {
            // If the projection of the center difference onto the axis is greater
            // than the sum of the box projections, then we found a separating plane!
            // The bounding boxes therefore must not intersect
            results.intersects = false;
            break;
        }
        else {
            // keep track of the difference, the smallest gives the minimum distance
            // and direction to move the boxes such that they no longer intersect
            var difference = projectionBoxesSum - projectionDifference;
            if (difference < smallestDifference) {
                results.resolution = vec4.scale(vec4.create(), axis, difference);
                smallestDifference = difference;
            }
        }
    }

    if (results.intersects) {
        // make sure the resolution vector is in the correct direction
        var dot = vec4.dot(results.resolution, centerDifference);
        var sign = dot ? dot < 0 ? -1 : 1 : 0;
        vec4.scale(results.resolution, results.resolution, -sign);
    }

    return results;
}

来源

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章