闵可夫斯基总和矩形交点计算 [英] Minkowski sum for rectangle intersection calculation

查看:231
本文介绍了闵可夫斯基总和矩形交点计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找到最佳的方式确定2矩形相交,并一直在寻找到使用夫斯基总和。

我会很感激,如果有人可以解释如何确定的时间和地点(即,它的边缘)2矩形碰撞使用夫斯基总和。

我已经读了很多关于这一点,但我不知道如何正确地实现这一点。

感谢

在code是:

 浮法W = 0.5 *(A.width()+ B.width());
浮H = 0.5 *(A.height()+ B.height());
浮DX = A.centerX() -  B.centerX();
浮DY = A.centerY() -  B.centerY();

如果(绝对(DX)其中; = W&安培;&安培; ABS(镝)其中= H)
{
/* 碰撞! * /
浮WY = W * DY;
浮HX = H * DX;

如果(WY> HX)
    如果(WY> -hx)
        / *在顶部碰撞* /
    其他
        /* 在左边 */
其他
    如果(WY> -hx)
        /* 在右边 */
    其他
        /* 在底部 */
}
 

解决方案

如果您的矩形是轴对齐,再有就是简单的方法:

矩形A和B的不要相交,如果

 (A.Left> B.Right)或
(A.Right< B.Left)或
(A.Top< B.Bottom)或
(A.Bottom> B.Top)
 

否则就会发生冲突。

如果您的矩形是不是轴对齐,那么你可以使用有效分离轴算法

编辑:使用Minkowski和动态碰撞:

让我们感动的矩形A和A的站在矩形B.让我们的速度矢量(VX,VY)。我们想找到第一次碰撞的时刻,确定哪些边相互接触。

起初,收缩的矩形与同一中心点,扩大B.Width通过A.Width对称和B.Height由A.Height(使闵可夫斯基之和)。现在我们必须找到行(线)与矩形的交集。

有一些方法,例如,我们可以使用良巴斯基线裁剪算法。请注意,我们没有检查所有交叉点,因为我们只需要在第一次碰撞 - 如果VX> = 0,检查交叉口左侧边缘而忘记了右边缘等等...

I've been looking into the best way to determine where 2 rectangles intersect and have been looking into using the Minkowski sum.

I would be grateful if someone could explain how to determine when and where (ie, which edge) 2 rectangle collide using the Minkowski sum.

I've read a lot regarding this, but I'm not sure how to properly implement this.

Thanks

The code is:

float w = 0.5 * (A.width() + B.width());
float h = 0.5 * (A.height() + B.height());
float dx = A.centerX() - B.centerX();
float dy = A.centerY() - B.centerY();

if (abs(dx) <= w && abs(dy) <= h)
{
/* collision! */
float wy = w * dy;
float hx = h * dx;

if (wy > hx)
    if (wy > -hx)
        /* collision at the top */
    else
        /* on the left */
else
    if (wy > -hx)
        /* on the right */
    else
        /* at the bottom */
}

解决方案

If your rectangles are axis-aligned, then there is simple method:

rectangles A and B don't intersect, if

(A.Left > B.Right) or 
(A.Right < B.Left) or 
(A.Top < B.Bottom) or 
(A.Bottom > B.Top)

otherwise collision occurs.

If your rectangles are not axis-aligned, then you can use effective separating axes algorithm

Edit: Dynamic collision using Minkowski sum:

Let's we have moving rectangle A and standing rectangle B. Let's velocity vector of A is (vx, vy). We want to find a moment of first collision and determine what edges touch each other.

At first, shrink A rectangle to the point with the same center, expand B.Width by A.Width symmetrically and B.Height by A.Height (make Minkowski sum). Now we have to find intersection of the line (ray) with rectangle.

There are some methods, for example, we can use Liang-Barsky line clipping algorithm. Note the we have not to check all intersections, because we need only the first collision - if vx >=0, check intersection with left edge and forget about right edge and so on...

这篇关于闵可夫斯基总和矩形交点计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆