在简单测试中失败的碰撞检测“;游戏”;
本文关键字:游戏 碰撞检测 简单 测试 失败 | 更新日期: 2023-09-27 18:27:19
我已经重新做了几次,但我无法让它工作。。。当按钮1位于按钮2的最下部和/或最右侧时,它会检测到碰撞,但如果它位于按钮的最上部和/或左侧,则不会检测到碰撞。。。很高兴知道问题出在哪里,因为我在调试方面很差劲。。。
if (
(
(button1.Top >= button2.Top && button1.Top <= (button2.Top + button2.Height))
|| (button1.Bottom >= button2.Bottom && button1.Bottom <= (button2.Bottom + button2.Height))
)
&&
(
(button1.Left >= button2.Left && button1.Left <= (button2.Left + button2.Width))
|| (button1.Right >= button2.Right && button1.Right <= (button2.Right + button2.Width))
)
)
我做了这个,效果很好。它基本上只是检查左上角是否在另一个按钮的位置。棘手的部分只是在第二次比较中添加宽度和高度,这基本上抵消了按钮1的尺寸,因此如果按钮2具有,则位置将大于按钮2的位置
if ((button1.Location.X > button2.Location.X && button1.Location.Y > button2.Location.Y)
||(button1.Location.X + button1.Size.Width > button2.Location.X
&& button1.Location.Y + button1.Size.Height > button2.Location.Y))
MessageBox.Show("In side other button");
然而,如果你想用一种更简单的方式来做,你可以做这个
if(button1.Bounds.IntersectsWith(button2.Bounds))
MessageBox.Show("Within button");
这将进行您尝试进行的比较。
看起来问题就在这里:
(button1.Bottom >= button2.Bottom && button1.Bottom <= (button2.Bottom + button2.Height))
这大概是想看看button1
的Bottom是否在button2
内部,所以它应该与button2.Top
和button2.Top + button2.Height
进行比较。
button1.Right
可能也存在类似的问题。