gpt4 book ai didi

c++ - 错误地检测矩形之间的碰撞

转载 作者:行者123 更新时间:2023-11-28 07:58:50 24 4
gpt4 key购买 nike

我正在使用并扩展一个函数来检测两个 2d 旋转矩形是否发生碰撞。函数来自here .

我的问题:该函数错误地检测到以下矩形之间的碰撞。

Rect1 = centreX=0, centreY=0, width=8, height=4 angle=0
Rect1 = centreX=16, centreY=0, width=8, height=4 angle=0

我错了还是这些矩形不应该碰撞?我应该如何编辑函数以便它检测旋转矩形之间的像素完美碰撞?我觉得我需要编辑最后两行,但我不确定。

int RotRectsCollision(_RotRect * rr1, _RotRect * rr2)
{
_Vector2D A, B, // vertices of the rotated rr2
C, // center of rr2
BL, TR; // vertices of rr2 (bottom-left, top-right)

float ang = rr1->ang - rr2->ang, // orientation of rotated rr1
cosa = cos(ang), // precalculated trigonometic -
sina = sin(ang); // - values for repeated use

float t, x, a; // temporary variables for various uses
float dx; // deltaX for linear equations
float ext1, ext2; // min/max vertical values

// move rr2 to make rr1 cannonic
C = rr2->C;
SubVectors2D(&C, &rr1->C);

// rotate rr2 clockwise by rr2->ang to make rr2 axis-aligned
RotateVector2DClockwise(&C, rr2->ang);

// calculate vertices of (moved and axis-aligned := 'ma') rr2
BL = TR = C;
SubVectors2D(&BL, &rr2->S);
AddVectors2D(&TR, &rr2->S);

// calculate vertices of (rotated := 'r') rr1
A.x = -rr1->S.y*sina; B.x = A.x; t = rr1->S.x*cosa; A.x += t; B.x -= t;
A.y = rr1->S.y*cosa; B.y = A.y; t = rr1->S.x*sina; A.y += t; B.y -= t;

t = sina*cosa;

// verify that A is vertical min/max, B is horizontal min/max
if (t < 0)
{
t = A.x; A.x = B.x; B.x = t;
t = A.y; A.y = B.y; B.y = t;
}

// verify that B is horizontal minimum (leftest-vertex)
if (sina < 0) { B.x = -B.x; B.y = -B.y; }

// if rr2(ma) isn't in the horizontal range of
// colliding with rr1(r), collision is impossible
if (B.x > TR.x || B.x > -BL.x) return 0;

// if rr1(r) is axis-aligned, vertical min/max are easy to get
if (t == 0) {ext1 = A.y; ext2 = -ext1; }
// else, find vertical min/max in the range [BL.x, TR.x]
else
{
x = BL.x-A.x; a = TR.x-A.x;
ext1 = A.y;
// if the first vertical min/max isn't in (BL.x, TR.x), then
// find the vertical min/max on BL.x or on TR.x
if (a*x > 0)
{
dx = A.x;
if (x < 0) { dx -= B.x; ext1 -= B.y; x = a; }
else { dx += B.x; ext1 += B.y; }
ext1 *= x; ext1 /= dx; ext1 += A.y;
}

x = BL.x+A.x; a = TR.x+A.x;
ext2 = -A.y;
// if the second vertical min/max isn't in (BL.x, TR.x), then
// find the local vertical min/max on BL.x or on TR.x
if (a*x > 0)
{
dx = -A.x;
if (x < 0) { dx -= B.x; ext2 -= B.y; x = a; }
else { dx += B.x; ext2 += B.y; }
ext2 *= x; ext2 /= dx; ext2 -= A.y;
}
}

// check whether rr2(ma) is in the vertical range of colliding with rr1(r)
// (for the horizontal range of rr2)
return !((ext1 < BL.y && ext2 < BL.y) ||
(ext1 > TR.y && ext2 > TR.y));
}

最佳答案

我认为这些矩形不应该发生碰撞。

第一个以 (0,0) 为中心,宽度为 8 => 它在 X 轴上的范围为 -4 到 4。

第二个以 (16,0) 为中心,宽度再次为 8 => 它在 X 轴上从 16 - 4 = 12 跨越到 16 + 4 = 20。

因此,第一个和第二个在 X 轴上不重叠。由于两者没有旋转,因此它们不会发生碰撞。

我没有检查过代码,但给出示例矩形,它们似乎没有发生碰撞。

关于c++ - 错误地检测矩形之间的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12032862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com