我一直在寻找最好的方法来确定2矩形相交的位置,并且一直在考虑使用Minkowski和。Minkowski sum for rectangle intersection calculation
如果有人能解释如何使用Minkowski和来确定何时和在哪里(即哪条边)两个矩形碰撞,我将不胜感激。
我已阅读了很多关于此,但我不知道如何正确实现这一点。
由于
的代码是:
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 */
}
Hi @MBo - I'm使用轴对齐的简单边界矩形/正方形。没有旋转(或者至少没有需要CD的旋转)。你的方法存在的问题是,它不能告诉我哪条边发生碰撞,并且如果两条边相交,哪条边首先相交,我需要这些信息来对碰撞进行反应/重新定位移动的精灵。你有什么想法,我会这样做,因为我一直在寻找2天坚实,我迄今为止尝试过的一切都失败了。帮助将不胜感激。谢谢 – Zippy 2013-04-25 12:45:07
请您详细说明一下@Mbo,我不太明白你的意思....谢谢! – Zippy 2013-04-25 23:16:32
@Zippy图片添加 – MBo 2013-04-26 03:57:07