轴对齐边界框碰撞:哪些边发生碰撞
我正在尝试制作一款类似打砖块的游戏,现在我面临着碰撞问题。像往常一样,砖块是矩形(在我的例子中是正方形),球是圆形,所以这都是关于轴对齐的边界框。
到目前为止,我已经有了简单的碰撞检测算法,但我需要知道球击中每块砖块的哪一侧。例如:
到目前为止,我有一个算法来检查每一面是否都被击中:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要只是将其设置为 1,而是将其设置为穿透深度:
然后,当您完成后,您可以选择最小的一侧作为您的主侧。
顺便说一句,我认为你不需要第三个条件。想象这样一个情况:
在这里,你没有发生左碰撞或右碰撞。
(您可能还需要检查其余条件的正确性。)
Don't just set it to one, set it to the penetration depth:
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:
Here, you have no left or right collision.
(You may also want to review the remaining conditionals for correctness.)