## Checking for Overlap

This cart demonstrates one way to check for overlap between two boxes. There are many ways, but this is a simple way to do it in PICO-8. (Unlike other carts in this collection, the source code code for this cart is not intended to be used, but it just to display what you see when you run it.)

Notes: This method of checking for overlap actually checks to see if the two boxes are not overlapping. The example above is simplified into using `x1,y1` and `x2,y2` of each box. But in practice, you would usually just have an `x,y` and a `w,h` (width and height) to work with. In that case, `x1,y1` would the same as `x,y` and `x2,y2` would be `x+w,y+h`.

### Turning This Into Code

If you want to turn this into a function for checking overlap, you just need to return `true` if all the conditions in the tutorial are false, but return `false` if any of the conditions are true.

Below is a verbose example function that assumes you have two boxes (`a` and `b`) that each have an `x,y` and `w,h`.

The second function is the same as the first, but it's condensed into one statement. It's not as easy to read, but it does the same thing.

```function overlap(a,b)
local test1 = a.x > (b.x + b.w)
local test2 = a.y > (b.y + b.h)
local test3 = (a.x + a.w) < b.x
local test4 = (a.y + a.h) < b.y
return not (test1 or test2 or test3 or test4)
end
```
```--condensed version
function overlap(a,b)
return not (a.x>b.x+b.w
or a.y>b.y+b.h
or a.x+a.w<b.x
or a.y+a.h<b.y)
end```
 Status Released Category Other Platforms HTML5 Rating Rated 5.0 out of 5 stars(9 total ratings) Author MBoffin (Dylan Bennett) Genre Simulation, Educational Made with PICO-8 Tags PICO-8, sourcecode

overlap.p8.png 7 kB

## Install instructions

Save this file to your PICO-8 carts folder. Then load it by typing `load overlap` in the command line mode of PICO-8.
{"ft_lang_label":"__label__en","ft_lang_prob":0.91123956,"math_prob":0.75077283,"size":2318,"snap":"2023-40-2023-50","text_gpt3_token_len":646,"char_repetition_ratio":0.1028522,"word_repetition_ratio":0.0,"special_character_ratio":0.26100087,"punctuation_ratio":0.13893966,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9690537,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-21T19:58:26Z\",\"WARC-Record-ID\":\"<urn:uuid:e92dc3fc-e9fb-4ab1-aa8b-c76c85082aa1>\",\"Content-Length\":\"34848\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b7f383cb-c099-4803-abc5-9418c2962959>\",\"WARC-Concurrent-To\":\"<urn:uuid:d2f37f9d-f51b-4656-82f1-82375d72daf6>\",\"WARC-IP-Address\":\"173.255.250.29\",\"WARC-Target-URI\":\"https://mboffin.itch.io/pico8-overlap\",\"WARC-Payload-Digest\":\"sha1:R5VYNB5I2HKA6J5RYWOS5Z7DXVRB5ECZ\",\"WARC-Block-Digest\":\"sha1:3G64GCVO36HWMKP326MNPAW3N3PZBLXS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506029.42_warc_CC-MAIN-20230921174008-20230921204008-00275.warc.gz\"}"}