How to Code: Rectangular Collision Detection with JavaScript

Поделиться
HTML-код
  • Опубликовано: 13 июн 2024
  • Learn web development with 117+ additional tutorials only at chriscourses.com 🚀
    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)

Комментарии • 57

  • @ChrisCourses
    @ChrisCourses  2 года назад +44

    #bringBackDislikes

  • @muhzungoo5963
    @muhzungoo5963 2 года назад +15

    Every one of these tutorials is absolute gold, dude

    • @ChrisCourses
      @ChrisCourses  2 года назад +2

      Thanks man, we'll see if I can obtain the elusive, consistent release schedule for some more 😂

  • @tim.e.l
    @tim.e.l 2 года назад +14

    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.

    • @ChrisCourses
      @ChrisCourses  2 года назад +2

      No problem-getting the code as clean as possible is almost just as important as learning the concept itself. Wanted to keep things as succinct as possible and not dive down any OOP rabbit hole.

  • @losrobbosful
    @losrobbosful Год назад +2

    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

  • @dweebycat9929
    @dweebycat9929 2 года назад +2

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

  • @Coreplay_
    @Coreplay_ 2 месяца назад

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

  • @Rico-cp4xp
    @Rico-cp4xp 2 года назад +1

    Great video! Thanks for the clear instructions.

  • @gektorix
    @gektorix 2 года назад +2

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

  • @MaxChip101
    @MaxChip101 11 месяцев назад +1

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

  • @santiagofantoni2643
    @santiagofantoni2643 2 года назад

    first time someone has explained the "x+width

  • @parnasmi
    @parnasmi 2 года назад +1

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

  • @suyci
    @suyci 2 года назад +5

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

    • @ChrisCourses
      @ChrisCourses  2 года назад +2

      Thank you sir - have a couple more game concept-like explainers / tutorials coming out. Gonna try and do one on spritesheets next week.

  • @andrecoccoconde3668
    @andrecoccoconde3668 Год назад +1

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

  • @bennydaniel93
    @bennydaniel93 2 года назад

    Amazing man!

  • @tomergngn
    @tomergngn Год назад +5

    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

    • @michael22000
      @michael22000 Год назад

      Only works if the object have the same sizes.

    • @tomergngn
      @tomergngn Год назад

      @@michael22000 true, but in this case they are the same size

  • @gopinathkrm58
    @gopinathkrm58 2 года назад

    amazing tutorial

  • @andreagi8746
    @andreagi8746 2 года назад +3

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

    • @ChrisCourses
      @ChrisCourses  2 года назад

      Thanks Andre! Collision detection for arbitrary polygons actually use, drum roll please, rectangles! You can create detection for complex shapes, but its typically CPU intensive and hard to actually implement. Therefore developers use rectangular hit-boxes as a way to get more precise detection. Check this article out and scroll down to the hitbox section to see what I mean-lots of good info in here: spicyyoghurt.com/tutorials/html5-javascript-game-development/collision-detection-physics

    • @ChrisCourses
      @ChrisCourses  2 года назад +1

      @@bartdemeyere9766 Will check it out today 🙌

  • @buz_zed
    @buz_zed 2 года назад

    Stg I've missed your vids man
    Haven't been coding in a while though

  • @adarshmaurya7553
    @adarshmaurya7553 Год назад +1

    Amazing video

  • @jerisbocou8802
    @jerisbocou8802 2 года назад

    Awesome Wondefull and Genius and it's not enough

  • @user-hj8rh2vi6x
    @user-hj8rh2vi6x 4 месяца назад

    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

  • @rajamohamed134
    @rajamohamed134 5 месяцев назад

    @chrisCourses can you please make a video for collision for complex shape such as triangle?

  • @mirzakadic9174
    @mirzakadic9174 Год назад

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

  • @user-ss3lr1ku4d
    @user-ss3lr1ku4d 9 месяцев назад

    Can you do a collision detection for a circle combined with a rectangle (assume a circle has label on the right for example)

  • @bhasim4299
    @bhasim4299 2 года назад

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

  • @sherifsalah5563
    @sherifsalah5563 2 года назад

    Can you please do collision avoidance? I don’t wanna rectangles overlap each others on mouse movement.

  • @ramlaaal
    @ramlaaal 5 месяцев назад

    condition for collision is too lengthy we could use this instead.
    Math.abs(mouse.x - (canvas.width/2 - 50))

  • @kylemorely1812
    @kylemorely1812 Год назад

    how could you detect the collision of more than two rectangles

  • @x_way4539
    @x_way4539 2 года назад

    Well 👍🏽

  • @anmolbakshi7983
    @anmolbakshi7983 2 года назад

    suuuperb

  • @Fouzia60
    @Fouzia60 Год назад +1

    Plz give a link of source code

  • @D4rkJvck
    @D4rkJvck 4 месяца назад

    Why not using object.right, .left, .top, .bottom?

  • @muhammadraabit3158
    @muhammadraabit3158 6 месяцев назад

    Canvas image Interaction Events . how to Change Product image color with color pallet

  • @behnamparsaeifard3883
    @behnamparsaeifard3883 Год назад

    What if two rectangles are rotated, let’s say with two different angles a1 and a2?

    • @onepieceworld121
      @onepieceworld121 Год назад

      It works with the same code . Just need some updates

  • @elliot_yoyo
    @elliot_yoyo 2 года назад

    nice

  • @archicoders9620
    @archicoders9620 2 года назад

    Polygon Collision Detection please

  • @willmil1199
    @willmil1199 Год назад

    If you need help in your code, you'll find what you need at: 2:00

  • @bhasim4299
    @bhasim4299 2 года назад

    why do you only have 90 k subs????

  • @funnylifestyle8174
    @funnylifestyle8174 2 года назад +1

    Lit video...

  • @jdd1325
    @jdd1325 2 года назад

    Love the tutorial. Top notch production value and great tips.
    P.S. You are so freakin cute 😍😉😁. Whoever is in your life is a lucky woman/man/person

    • @ChrisCourses
      @ChrisCourses  2 года назад +1

      Haha thank you, appreciate the compliment! I'd say I'm pretty lucky to have my girlfriend 😉

  • @gameplaysongs360
    @gameplaysongs360 2 года назад

    How to learn javascript game developement

  • @kaissoune
    @kaissoune 2 года назад +1

    Wow, as pretty as an amazing tutorial. praise be to God

    • @ChrisCourses
      @ChrisCourses  2 года назад +1

      Thank you Kaissoune, always appreciate the support 🙏

  • @mynameisreza1
    @mynameisreza1 Год назад

    I was having so much trouble understanding this until I realized that the coordinates start in the corner ugh

  • @Pawlo370
    @Pawlo370 Год назад

    And now think about making game with thousands collisions