0

My bounding box detection is off. It detects collision when the boxes are "close" but not touching. Here is the relevant part off my code.

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;
}

And here is "bounding_box_collison" I had my own function but it wasn't working so I copy pasted one off the allegro wiki here http://wiki.allegro.cc/index.php?title=Bounding_Box. I still have the same problem.

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;
 }
user701329
  • 127
  • 2
  • 7

1 Answers1

0

The function calls for width and height, but you are passing right and bottom.

e.g., Instead of playerOne.x + 15, you should pass 15.

Matthew
  • 47,584
  • 11
  • 86
  • 98