gpt4 book ai didi

C++ 矩形到矩形的碰撞

转载 作者:行者123 更新时间:2023-11-28 00:45:56 25 4
gpt4 key购买 nike

我在这里寻找代码中的错误真的很糟糕。

我的碰撞检测在这里不起作用,即使是我在 Google 中搜索的算法也是如此。

void PollEvents()
{

for (int i = 0;i < NUMBER_OF_BLOCKS; ++i)
{

Rectangle& a = blocks[i];

if (mouse.state == GLFW_PRESS)
{
//look for any block to grab
if (mouse.leftClick && !blocks[selectedBlock].Grab() &&
a.Hover(mouse.pos.x, mouse.pos.y))
{
//prevent grabbing another block
if (i != selectedBlock) {
selectedBlock = i;
}

a.Grab() = true;

if (a.IsTypeHorizontal()) {
diff = mouse.pos.x - a.Left();
} else {
diff = mouse.pos.y - a.Top();
}
}

if (a.Grab())
{

for (int j = 0;j < NUMBER_OF_BLOCKS; ++j)
{
//skip for any self-checking
if (i == j) continue;

Rectangle& b = blocks[j];

//check for rectangle collision
if (!a.Collide(b) || b.Collide(a)) {
//j++;
//how does this block will move.

if (a.IsTypeVertical()) {
a.SetY(mouse.pos.y - diff);
} else {
a.SetX(mouse.pos.x - diff);
}

} else {

switch (a.sideHit)
{
case UP:
//a.SetY(b.Bottom());
printf("UP\n");
break;
case DOWN:
//a.SetY(b.Top() + a.GetHeight());
printf("DOWN\n");
break;
case LEFT:
//a.SetX(b.Right());
printf("LEFT\n");
break;
case RIGHT:
//a.SetX(b.Left() - a.GetWidth());
printf("RIGHT\n");
break;
}
}

//check for bound collision
a.BoundCheck(1.f);
}

}

} else {
a.Grab() = false;
}
}
}

碰撞检测:

bool Rectangle::Collide(const Rectangle& r) {

if (IsTypeHorizontal()) {
if (r.Hover(Left(), Top()) && r.Hover(Right(), Top())) {
sideHit = UP;
return true;
} else if (r.Hover(Right(), Bottom()) && r.Hover(Left(), Bottom())) {
sideHit = DOWN;
return true;
}
// } else if (r.Hover(Left(), Top())) {
// sideHit = UP;
// return true;
// } else if (r.Hover(Right(), Top())) {
// sideHit = UP;
// return true;
// } else if (r.Hover(Right(), Bottom())) {
// sideHit = DOWN;
// return true;
// } else if (r.Hover(Left(), Bottom())) {
// sideHit = DOWN;
// return true;
// }
} else {
if (r.Hover(Left(), Top()) && r.Hover(Left(), Bottom())) {
sideHit = LEFT;
return true;
} else if (r.Hover(Right(), Top()) && r.Hover(Right(), Bottom())) {
sideHit = RIGHT;
return true;
}
// } else if (r.Hover(Left(), Top())) {
// sideHit = LEFT;
// return true;
// } else if (r.Hover(Left(), Bottom())) {
// sideHit = LEFT;
// return true;
// } else if (r.Hover(Right(), Top())) {
// sideHit = RIGHT;
// return true;
// } else if (r.Hover(Right(), Bottom())) {
// sideHit = RIGHT;
// return true;
// }
}

return false;

}

悬停的代码:

inline float Hover(float X, float Y) const {
return X >= Left() &&
X <= Right() &&
Y >= Bottom() &&
Y <= Top();
}

我正在尝试自己制作 unblockme .

请帮助我进行碰撞检测。自从我陷入这个问题已经 3 天了。

更新

我发现了为什么所有 rect-rect 碰撞检测在我的程序中都不起作用的问题。

错误:

if (!a.Collide(b)) {

//Move()

} else {

//Resolve collision
}

这个应该是

if (!Rectangle::Collide(a, b)) {

//Move()

} else {

//Resolve collision

}

使 Collide() 成为 Rectanglestatic 成员,因为正如您在我的 Collide() 实现中看到的那样,它根据自己的成员做出决定,因此 a.Hover(b.x, b.y) 没有任何意义。

希望这对像我这样的新手有所帮助。

最佳答案

要进行矩形/矩形碰撞检测,如果一个矩形(平行于 x 和 y 轴的边)的四个点中的任何一个在另一个矩形内,我们就会发生碰撞。

比检查四个点中的每一个更简单的方法是检查一个 X 边是否在另一个矩形的两个 X 边之间,以及一个 Y 边是否在另一个矩形的两个 Y 边之间 - 如果两者都为真,我们发生碰撞(因为两条边必须在另一个矩形内的某个点相遇)。所以我们只是在两个方向上检查:

bool isclamped(float mid, float A, float B)
{
if (A > B)
{
return mid >= B && mid <= A;
}
return mid >= A && mid <= B;
}

bool checkcollisiononeway(rect rectA, rect rectB)
{
if (isclamped(rectA.left, rectB.left, rectB.right)
|| isclamped(rectA.right, rectB.left, rectB.right))
&& (isclamped(rectA.bottom, rectB.bottom, rectB.top)
|| isclamped(rectA.top, rectB.bottom, rectB.top))
{
return true;
}
return false;
}

bool checkcollisionbothways(rect rectA, rect rectB)
{
return checkcollisiononeway(rectA, rectB) || checkcollisiononeway(rectB, rectA);
}

要在检测到碰撞后确定碰撞角度,请使用 atan2(rectA.y - rectB.y, rectA.x - rectB.x) 找到它们两个中心之间的角度(角度以弧度而不是度数返回)

关于C++ 矩形到矩形的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16228969/

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