Python Pygame Tutorial - Pixel Perfect Collision Using Mask

Поделиться
HTML-код
  • Опубликовано: 15 ноя 2024

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

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

    Thank you for this video I had trouble understanding what masks are by just reading through the docs!

  • @Pakoaradillas
    @Pakoaradillas 7 лет назад +6

    This one worked magic for me! I was using distance and rect collisions only! thanks!

  • @timw594
    @timw594 4 года назад

    Outstanding stuff... Long time business programmer but wanted to dabble a bit in gaming and this content is really well done. Thanks for taking the time.

  • @yvonlegall8560
    @yvonlegall8560 7 лет назад +3

    Hi, I just discovered your channel, very good videos there :)
    What you should do for performance when using mask is:
    -First check for a rectangle collision
    -If there is one check masks
    -else do nothing

    • @codePylet
      @codePylet  7 лет назад

      +Yvon LE GALL I was under the belief that the pygame.mask.overlap function did that for you. I'll run some timings on it tomorrow and see. If it does do it automatically then they'll be a tiny lag between the two mask rectangle's overlapping and not overlapping because when they do overlap the function will have to do a bitwise collision check on it. If it doesnt check rectangular collision first then the timings will be the same either way.

    • @codePylet
      @codePylet  7 лет назад +1

      Yes you are absolutely right my friend! There is a significant performance increase when rect detection is done first. Thank you for bringing that to my attention :)

    • @yvonlegall8560
      @yvonlegall8560 7 лет назад

      Hey, happy to hear I was right (I had a little doubt after your comment). Can you please tell me which module you use for test? I remember using one but I can't find his name :/

    • @codePylet
      @codePylet  7 лет назад

      Yeah sure it's
      from timeit import default_timer as timer
      start = timer()
      [code]
      end = timer()
      duration = end - start
      :)

    • @yvonlegall8560
      @yvonlegall8560 7 лет назад

      Ok, I'll use that, thank you and keep doing those worked and good qualities videos :)

  • @FiveMinuteGaming
    @FiveMinuteGaming 7 лет назад +10

    Can I combine this with the sprites sheet class you did?

    • @codePylet
      @codePylet  7 лет назад +7

      +Sausage Fingers Hi Sausage, good question. You can bit instead of creating a list rectangles you need to create a list of images with it's corresponding mask. For that I'd recommend defining a class called sprite which contains the image and the mask. Hope that helps 👍

  • @duck-billedplatypus126
    @duck-billedplatypus126 7 лет назад +4

    Firstly great video :) secondly which quicker the rectangle collision detection or this one? thanks

    • @codePylet
      @codePylet  7 лет назад

      +Duck-Billed Platypus erm, not sure! I'll test and let you know :D

    • @codePylet
      @codePylet  7 лет назад +5

      Ok Duck-Billed,
      I've run two tests, each at 100,000,000 cycles. The first test was this:
      result = bx1 > ox2 or bx2 < ox1 or by1 > oy2 or by2 < oy1
      bxy1/2 are the blob rectangle coords and oxy1/2 are the obstacles. In 100,000,000 cycles this method took 17.6408686894s
      The second test was:
      result = obstacle_mask.overlap(blob_mask, offset) # offset has been calculated before entering the cycles
      In 100,000,000 cycles this method took 40.4696911013s.
      In the test no other calculations took place within the 100,000,000 cycle loop.
      So to answer your question, the rectangle based collision detection is the faster than the mask method by 129.4% :O
      Thanks for the question, I enjoyed that!

    • @duck-billedplatypus126
      @duck-billedplatypus126 7 лет назад

      Cool thanks. I guess both have there advantages :)

    • @Shocker99
      @Shocker99 6 лет назад +1

      Quite clearly, one requires 4 calculations and the other requires x pixels * y pixels of calculations (worst case). Computationally, one is significantly lighter than the other.

    • @josephstringer1987
      @josephstringer1987 3 года назад

      Wouldn't it be faster to use rectangle collision to detect when two objects are near each other and only when that is true run the mask overlap detection? probably would speed it up a bit.

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

    How can I get the distance between the sprite and the mask's closest collision point?

  • @wengel_eth
    @wengel_eth 7 лет назад +2

    How do I implement this into your sprite class? I don't quite get how to implement this anywhere else.
    EDIT: You said somewhere about instead of creating a list of rectangles, to create a list of images. How would I go about doing that?

    • @vaibhavkrkm
      @vaibhavkrkm 4 года назад

      For Sprite class, I think spritecollide method is good..

  • @nmgreg11
    @nmgreg11 6 лет назад +1

    Excellent videos. Thank you for your time and effort. It helped!!!!

  • @hibob1321
    @hibob1321 4 года назад +1

    thanks for the vid. is there a way to make a mask for a pygame.draw though? I have been searching for a bit but couldn't find any way to do it

    • @bradirv
      @bradirv 4 года назад

      i'm looking for that too

  • @jmmatas08
    @jmmatas08 3 года назад

    Thank you, very well expained !!!

  • @apamu1563
    @apamu1563 4 года назад

    Obrigadão, my thanks for your very much

  • @RaviKiran-bk2hw
    @RaviKiran-bk2hw 7 лет назад

    thanks for the nice explanation. I also saw the sprite animation video which you made and was helpful. I've been looking for collision with animated sprites and this doesn't work for them as the sprite keeps changing ( as we store the sprites in a list and keep changing them). Can you help me with this?

    • @codePylet
      @codePylet  7 лет назад

      +Ravi Kiran an option is to split the spritesheet up after loading into individual sprites and create a mask from each and store in a list so spritesheet ID also indexes the list of masks.

    • @RaviKiran-bk2hw
      @RaviKiran-bk2hw 7 лет назад

      Thank you very much. I just tried and it works fine. AMAZINGLY fast reply though!!!!

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

    Wow, nice Video :)

  • @dmvy.official
    @dmvy.official 5 лет назад

    Thanks a lot! Very helpful!

  • @Dtomper
    @Dtomper 3 года назад

    Thank you

  • @LakituStar
    @LakituStar 4 года назад

    Why do I get this error "argument 1 must be pygame.Surface, not pygame.Rect
    "?

    • @LakituStar
      @LakituStar 4 года назад

      I set image to rect by accident

    • @LakituStar
      @LakituStar 4 года назад

      WHY IS PROGRAMMING SO DIFFICULT

    • @LakituStar
      @LakituStar 4 года назад

      hey I got it to work! that's never happened to me before!

    • @LakituStar
      @LakituStar 4 года назад

      hm, the hitbox is still too large! darn

  • @zeldafan3399
    @zeldafan3399 5 лет назад

    what does the convert_alpha in the obstacle variable do?

    • @davidollo346
      @davidollo346 3 года назад

      convert the image with a format that pygame like more

  • @medhavimonish41
    @medhavimonish41 5 лет назад

    Thanks a lot , Sir

  • @kavishkartha5450
    @kavishkartha5450 4 года назад

    what does the .convert_alpha() do?

    • @codePylet
      @codePylet  4 года назад

      It basically converts an image to the current display surfaces pixel format and preserves the alpha channel (transparency). Blitting converted surfaces is faster than blitting non-converted surfaces.

  • @user-lf7uc3tq1w
    @user-lf7uc3tq1w 7 лет назад

    why your script in my computer not work
    TypeError: integer argument expected, got float
    please help me

    • @user-lf7uc3tq1w
      @user-lf7uc3tq1w 7 лет назад

      is when i enter the part of result = obstacle_mask.overlap(blob_mask, offset.

    • @codePylet
      @codePylet  7 лет назад

      +Legends JD it'll be in the circle draw function. Try putting (int(x), int(y)) instead

    • @user-lf7uc3tq1w
      @user-lf7uc3tq1w 7 лет назад

      code.Pylet where i put that on result or offset plz

  • @anandsuralkar2947
    @anandsuralkar2947 5 лет назад

    Hi i m making collision simulator i want to do it without using vectors..but just plain maths and geometry

  • @brendanchamberlain4666
    @brendanchamberlain4666 7 лет назад

    I got
    AttributeError: 'pygame.mask.Mask' object has no attribute 'get_rect'

    • @codePylet
      @codePylet  7 лет назад

      That's because pygame's mask object doesn't have the get_rect() method. To get the dimensions of the image you need to call get_rect for a surface object like this:
      img = pygame.image.load("image.png").convert_alpha()
      img_rect = img.get_rect()
      img_width = img_rect.width
      img_height = img_rect.height
      img_width, img_height = img_rect.size
      hope that helps :)

  • @user-lf7uc3tq1w
    @user-lf7uc3tq1w 7 лет назад

    the 3.6 we have to download the 2.7?

    • @codePylet
      @codePylet  7 лет назад

      +Legends JD the script is written for 2.7, I will write it again and upload to Google Drive. :)

    • @codePylet
      @codePylet  7 лет назад

      Hi JD, I written the code so it'll now work for Python3. Download pixelperfect3.py from here drive.google.com/open?id=0BwDJQBs1OukNY2lmWnlsaWI0WTQ

    • @user-lf7uc3tq1w
      @user-lf7uc3tq1w 7 лет назад

      code.Pylet thank you

  • @shinydewott
    @shinydewott 4 года назад

    Can I create a mask of a surface where it only masks one colour in the surface? And can I change the colour using the mask? I know this video is 3 years old but I hope someone answers

    • @codePylet
      @codePylet  4 года назад

      I think you'll be able to do that in verion 2 of Pygame but not currently. To do that now you should use PixelArray

  • @user-lf7uc3tq1w
    @user-lf7uc3tq1w 7 лет назад

    code.Pylet where i put that

    • @codePylet
      @codePylet  7 лет назад

      +Legends JD are you using Python 3 or 2.7?

  • @user-lf7uc3tq1w
    @user-lf7uc3tq1w 7 лет назад +1

    code.Pylet, one more question we can do a pixel perfect collision without the pos of mouse. So the move of player with keyboard

  • @ArachnidAbby
    @ArachnidAbby 6 лет назад

    no work

  • @shubhamshirdhone6557
    @shubhamshirdhone6557 4 года назад

    Amoebae in pygame 🤣🤣😂😂