轴对齐边界框碰撞:哪些边发生碰撞

发布于 2024-10-15 20:56:23 字数 887 浏览 4 评论 0原文

我正在尝试制作一款类似打砖块的游戏,现在我面临着碰撞问题。像往常一样,砖块是矩形(在我的例子中是正方形),球是圆形,所以这都是关于轴对齐的边界框。

到目前为止,我已经有了简单的碰撞检测算法,但我需要知道球击中每块砖块的哪一侧。例如: Collision Case

到目前为止,我有一个算法来检查每一面是否都被击中:

up = left = right = down = 0;

if(mouse.left < brick.left && brick.left < mouse.right && mouse.right < brick.right){   
    left = 1;
}

if(brick.left < mouse.left && mouse.left < brick.right && brick.right < mouse.right){
    right = 1;
}

if(mouse.top < brick.top && brick.top < mouse.bottom && mouse.bottom < brick.bottom){
    up = 1;
}

if(brick .top < mouse.top && mouse.top < brick.bottom && brick.bottom < mouse.bottom){
    down = 1;
}

但是在靠近角落的碰撞中,就像图像中的第三个一样,两个标志(例如,向左和向下)设置为 1,所以我不知道如何决定。

在这些情况下通常会做什么?

I'm trying to make an arkanoid-like game, and now I'm facing a problem with the collisions. As usual, the bricks are rectangles (in my case, squares) and the ball is a circle, so it's all about axis aligned bounding boxes.

So far, I've got the trivial collision detection algorithm, but I need to know what side of each brick the ball hits. For example:
Collision cases

So far I have an algorithm that checks whether each side has been hit:

up = left = right = down = 0;

if(mouse.left < brick.left && brick.left < mouse.right && mouse.right < brick.right){   
    left = 1;
}

if(brick.left < mouse.left && mouse.left < brick.right && brick.right < mouse.right){
    right = 1;
}

if(mouse.top < brick.top && brick.top < mouse.bottom && mouse.bottom < brick.bottom){
    up = 1;
}

if(brick .top < mouse.top && mouse.top < brick.bottom && brick.bottom < mouse.bottom){
    down = 1;
}

But in the collisions close to the corners, like the third in the image, two flags (for instance, left and down) are set to 1, so I don't know how to decide.

What is usually done in these cases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2024-10-22 20:56:23

不要只是将其设置为 1,而是将其设置为穿透深度:

// why is it named mouse?
if(mouse.left < brick.left && brick.left < mouse.right &&
    mouse.right < brick.right)
{
    left = mouse.right - brick.left;
}

// and so on...

然后,当您完成后,您可以选择最小的一侧作为您的主侧。


顺便说一句,我认为你不需要第三个条件。想象这样一个情况:

   +-+
   |B|
+--| |--+
|M | |  |
|  | |  |
+--| |--+
   +-+

在这里,你没有发生左碰撞或右碰撞。

(您可能还需要检查其余条件的正确性。)

Don't just set it to one, set it to the penetration depth:

// why is it named mouse?
if(mouse.left < brick.left && brick.left < mouse.right &&
    mouse.right < brick.right)
{
    left = mouse.right - brick.left;
}

// and so on...

Then when you're done, you can pick whichever is minimum to be your main side.


By the way, I don't think you want that third conditional. Imagine a case like this:

   +-+
   |B|
+--| |--+
|M | |  |
|  | |  |
+--| |--+
   +-+

Here, you have no left or right collision.

(You may also want to review the remaining conditionals for correctness.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文