Difference between revisions of "BoundingBox.lua"

(article created)
 
m (Actually follow guidelines and have the properities be at the end of the article.)
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
This script is used for Bounding Box Collision Detection. It will see if two rectangular objects over-lap.  It returns true if there is a collision, and false otherwise.
+
This script is used for axis-aligned bounding box collision detection. It will check whether two rectangular objects overlap.  It returns true if they do, and false otherwise.
  
This form of collision detection is not suited for games that have lots of circles and non-rectangular shapes, since the invisible space will be considered part of the collision rectangle. However, this should not be a problem for games such as Space Invaders clones.
+
This form of collision detection is not suited for games that have lots of circles, non-rectangular shapes, or rotated rectangles, since space not filled by those shapes may still be part of the collision rectangle, or alternatively there may be parts that lie outside of it.
 +
 
 +
However, this should work fine for games such as Space Invaders clones.
  
 
<source lang="lua">
 
<source lang="lua">
-- Collision detection function.
+
-- Collision detection function;
-- Checks if box1 and box2 overlap.
+
-- Returns true if two boxes overlap, false if they don't;
-- w and h mean width and height.
+
-- x1,y1 are the top-left coords of the first box, while w1,h1 are its width and height;
function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
+
-- x2,y2,w2 & h2 are the same, but for the second box.
    if box1x > box2x + box2w - 1 or -- Is box1 on the right side of box2?
+
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
      box1y > box2y + box2h - 1 or -- Is box1 under box2?
+
  return x1 < x2+w2 and
      box2x > box1x + box1w - 1 or -- Is box2 on the right side of box1?
+
        x2 < x1+w1 and
      box2y > box1y + box1h - 1    -- Is b2 under b1?
+
        y1 < y2+h2 and
    then
+
        y2 < y1+h1
        return false                -- No collision. Yay!
 
    else
 
        return true                -- Yes collision. Ouch!
 
    end
 
 
end
 
end
 
</source>
 
</source>
 +
 +
[[Category:Snippets]]
 +
{{#set:LOVE Version=any}}
 +
{{#set:Description=Collision checking function based on axis-aligned bounding boxes.}}

Revision as of 17:14, 11 November 2016

This script is used for axis-aligned bounding box collision detection. It will check whether two rectangular objects overlap. It returns true if they do, and false otherwise.

This form of collision detection is not suited for games that have lots of circles, non-rectangular shapes, or rotated rectangles, since space not filled by those shapes may still be part of the collision rectangle, or alternatively there may be parts that lie outside of it.

However, this should work fine for games such as Space Invaders clones.

-- Collision detection function;
-- Returns true if two boxes overlap, false if they don't;
-- x1,y1 are the top-left coords of the first box, while w1,h1 are its width and height;
-- x2,y2,w2 & h2 are the same, but for the second box.
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end