DEV Community

loading...

Collision detection with javascript

Hillary Chibuko
A frontend web developer Based in Africa,Nigeria
・1 min read

Hello world.....

Todays topic is focused on collision detection, i am a big fan of making 2d games with javscript and i could remember vividly in the past i had to quit some of mu games because of no knowledge on how to handle collisions ....
But luckily i am here to unveil a method that could work.....

I used this for one of my piano tiles game ,basically what it does is render some random tiles and when they are hit they give off a sound...
Seeming interesting huh...

Well for the fun part i handled collisions by simply calling " tiles.getBoundingClientRect().bottom " and i kept monitoring its position which is relative to the window....

So i ran an if statement that checks when the boxBottom cordinates is >= window.innerHeight

So then i would cancel the interval....
Thats it collision detected

For vertical scrolls

Leave a comment if you would like to view the game or source code on github,and you could also suggest me better ways to handle collisions

Discussion (0)