0

Someone on a forum I go to said I shouldn't use Rectangle.intersects for my collision detection, and I should use this algorithm instead:

boolean rectangleIntersects(float rect1x, float rect1y, float rect1w, 
                            float rect1h, float rect2x, float rect2y, 
                            float rect2w, float rect2h)
{
    return (rect1x + rect1w >= rect2x &&
            rect1y + rect1h >= rect2y &&
            rect1x <= rect2x + rect2w &&
            rect1y <= rect2y + rect2h);
}

But isn't the Rectangle.intersects algorithm different, and better than this?

jball
  • 24,791
  • 9
  • 70
  • 92
William
  • 8,630
  • 23
  • 77
  • 110
  • You may find [this question](http://stackoverflow.com/questions/115426/algorithm-to-detect-intersection-of-two-rectangles) helpful in developing a better intersection algorithm. – jball Nov 24 '10 at 19:14
  • Did they give a reason for the suggestion? – jzd Nov 24 '10 at 19:15
  • 1
    You had duplicated parameter names in your method signature, I've edited with my assumption as to what you intended. – jball Nov 24 '10 at 19:19

1 Answers1

4

Rectangle.intersects is basically the same except that your algorithm uses float instead of double and includes equality conditions for the bounds.

Mark Elliot
  • 75,278
  • 22
  • 140
  • 160