How to Code: Rectangular Collision Detection with JavaScript

preview_player
Показать описание

Rectangular collision detection is a must-know when it comes to video game development. It's a way to tell when two objects touch, so you can call some sort of reactionary event in return, such as gaining a power-up, or ending the game. Here I'll show you how rectangular collision detection works from a high-level perspective, then walk you through how to implement the effect using HTML canvas and JavaScript.

0:00 - How it works
2:07 - Detection on the x-axis
6:40 - Detection on the y-axis (with x-axis)
Рекомендации по теме
Комментарии
Автор

Every one of these tutorials is absolute gold, dude

muhzungoo
Автор

Best channel on JS Game Development by far. The variety of your game demos is mind blowing. Keep it up, pleaaaase :) We profit all so much from you

losrobbosful
Автор

i've been wrecking my head to find the formulas for the collisions and you avoided me some more headaches, thanks you very much <3

meosky
Автор

I appreciate you adding in the part about using variables or OOP to make the code cleaner / more dynamic. Great tutorial as always.

This reminds me of the c++ code I had to write in highschool back in 2003 ish when we made pong and had to detect if the ball hit the paddle.

tim.e.l
Автор

Like how you started with the conditions then the code, was easy to understand

fourty
Автор

thank you this has helped me to create collisions in my java game

MaxChip
Автор

Just a comment to support the channel. Keep up the good work. Very interesting content.

gektorix
Автор

first time someone has explained the "x+width <= other.x &&..." to make sense, thanks a lot man.

santiagofantoni
Автор

I don't even code, but it's so much fun to see where games go wrong through these tutorials

suyci
Автор

This really helped me out so much because i couldn't figure out how to code it, thank you!

novanoid
Автор

Thanks for this! I've been trying to detect collisions in the game im making for ages. I'm going to try this soon.

Coreplay_
Автор

something you can do to simplify the code if you want to work efficiently, is to follow the following steps.

Let the width of the rectangles be w, and their x coordinates be x1 and x2.

we know as proven in this video that when the rectangles collide, the following statements are true:
1. x1 + w >= x2
2. x1 <= x2 + w

let's look at 1.:
1. x1 + w >= x2
1. w >= x2 - x1

let's look at 2.:
2. x1 <= x2 + w
2. 0 <= x2 + w - x1
2. -w <= x2 - x1

if we'll combine the simplified equations, we'll get:
-w <= x2 - x1 <= w


by the definition of inequivalents in absolute values, we'll get:
|x2 - x1| <= w

now to turn it to code, we can simply change the absolute value symbol:
abs(x2 - x1) <= w

Let the height of the rectangles be h
we'll do the same for y2 and y1. and we'll get the following "if" statement:
if( abs(x2-x1) <= w &&
abs(y2-y1) <= h
)

this cuts the computing time of hitboxes in half, saving precious computing time when dealing with lots of hitboxes.

tomergngn
Автор

Doing God's work my man. Thank you so much for such a great and illustrative tutorial.

andrecoccoconde
Автор

Thank you for this video! Really helped with my assignment!

dweebycat
Автор

Very usefull video about html canvas rectangle collision detection. Thank you!

parnasmi
Автор

Great video! Would love to see one for arbitrary polygons.

andreagi
Автор

Hi, could you please tell me which skin/style you used for VS code, like the colors and font. Thanks!

mirzakadic
Автор

Great video! Thanks for the clear instructions.

Rico-cpxp
Автор

you are the only one that explains schit well and not like a 40 year physisit

bhasim
Автор

But in case that we need to detecte which side wetouching what is the best methode? to know which velocity to reset to 0
bc if i can move my character with AWSD, i need to know which side of my rec is touching to know which key to restrect from moving

MkhitarKojoyan
join shbcf.ru