2

我已经在java中实现了分离轴定理。碰撞检测本身效果很好。但是在解决碰撞时我被卡住了。

我的翻译方法如下:

public float getOverlap(final Projection other) {
    float start = m_min > other.m_min ? m_min : other.m_min;
    float end = m_max < other.m_max ? m_max : other.m_max;
    float translation = end - start;
    return translation;
}

假设图片中两个矩形的投影看起来像这样。

R1.min = 2
R1.max = 8
R2.min = 5
R2.max = 11

分离轴平移

当我检查 R1 与 R2 时,翻译将为 3 当我检查 R2 与 R1 时,翻译也将为 3

现在我将翻译添加到标准化轴

Normalized axis = Vector(1,0)
Translation Vector = Vector(1,0)*3 = Vector (3,0)

现在 R1 和 R2 都向右移动了 3 个点,但它们应该向不同的方向移动。R1 应该移动 Vector(-3,0),R2 应该移动 Vector(3,0)。

我如何计算正确的方向?

4

2 回答 2

0

我的解决方案:

我从中心向量 R2 中减去中心向量 R1,将点积构建到测试轴,如果点积小于 0,则反转平移

Vector centerR1(R1.x,R1.y);
Vector centerR2(R2.x,R2.y);

Vector R1toR2 = centerR2 - centerR1;

if(R1toR2.dot(axis)<0){
  translation = -translation
}

“当向量(R1toR2)指向负方向时,反转平移”

于 2012-05-01T18:08:27.450 回答
0

只是要在这里发布另一个答案,为我解决它。

点积的解决方案对我不起作用,尽管您似乎检查了点是否< 0在您的答案中,而在其他任何地方我都看到检查是>= 0,所以我不确定这会改变什么。

我决定在我的getOverlap函数中返回正或负重叠,这将指示解析的方向。这将取决于第一个对象的最小值是否小于第二个对象的最小值。

// Check for overlap of two 1 dimensional lines
function getLineOverlap(min1, max1, min2, max2) {
    let min = Math.max(min1, min2);
    let max = Math.min(max1, max2);

    // if negative, no overlap
    let result = Math.max(max - min, 0);

    // add positive/negative sign depending on direction of overlap
    return result * ((min1 < min2) ? 1 : -1);
};

如果这仍然不起作用,您可能需要将得到的分辨率向量乘以-1.

我从这个 stackoverflow 答案中得到了这个解决方案。

于 2020-06-19T12:30:47.700 回答