DEV Community

Hillary Chibuko
Hillary Chibuko

Posted on

Collision detection with javascript

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay