So at work I have been writing an HTML5/JavaScript drag and drop game. Totally new stuff for me. I am using the CreateJS library, check it out!
So I needed to detect if two items had a collision. These items were boxes and after some searching I found this YouTube video that helped me with the math.
I wanted to share the function I came up with for this.
Setup Boxes. I made objects, but you can just use regular variables for the numbers.
1 | var boxA = { |
The function itself. Modify to fit your needs. In my first version I passed in 8 numbers.
1 | function isCollision(boxA, boxB) { |
Usage:
1 | if (isCollision(boxA, boxB)) { |
Enjoy!