Difference between revisions of "BoundingBox.lua"
m (linkification) |
m |
||
(6 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | This script is used for | + | This script is used for axis-aligned bounding box (AABB) 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 | + | 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; |
− | -- | + | -- 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; |
− | function CheckCollision( | + | -- 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 | end | ||
</source> | </source> | ||
+ | |||
+ | == Other languages == | ||
+ | {{i18n|BoundingBox.lua}} | ||
[[Category:Snippets]] | [[Category:Snippets]] | ||
+ | {{#set:LOVE Version=any}} | ||
+ | {{#set:Description=Collision checking function based on axis-aligned bounding boxes.}} |
Latest revision as of 07:57, 22 June 2023
This script is used for axis-aligned bounding box (AABB) 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
Other languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info