我的边界框检测已关闭。当盒子“关闭”但不接触时,它会检测到碰撞。这是我的代码的相关部分。
if(bounding_box_collision(playerOne.x, playerOne.y, playerOne.x + 15, playerOne.y + 15, foodx, foody, foodx + 10, foody + 10))
{
cout << "Collision" << endl;
playerOne.score += 1;
foodx = rand() % 786;
int foody = rand() % 586;
}
这是“bounding_box_collison”,我有自己的功能,但它不起作用,所以我从 allegro wiki 复制粘贴了一个http://wiki.allegro.cc/index.php?title=Bounding_Box。我仍然有同样的问题。
int bounding_box_collision(int b1_x, int b1_y, int b1_w, int b1_h, int b2_x, int b2_y, int b2_w, int b2_h)
{
if ((b1_x > b2_x + b2_w - 1) || // is b1 on the right side of b2?
(b1_y > b2_y + b2_h - 1) || // is b1 under b2?
(b2_x > b1_x + b1_w - 1) || // is b2 on the right side of b1?
(b2_y > b1_y + b1_h - 1)) // is b2 under b1?
{
// no collision
return false;
}
// collision
return true;
}