Results 1 to 8 of 8

Thread: 2D Mask Collision

  1. #1

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    2D Mask Collision

    Hi guys, I'm trying to find a way to do pixel perfect sprite collisions using VB.Net 2005 and just so you all know I can't use DirectX for this app.

    I've heard sprite masks mentioned for collisions and I was wondering how I could do this and if it would give me pixel perfect collisions?

    The sprites are all random, they are all different sizes and I can't use rectangles or circles for bounding boxes as it won't give me pixel perfect collisions that way

    Thanks in advance, knxrb
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  2. #2
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: 2D Mask Collision

    Well, how I used to do it was a two-step (or three step) process:

    Get the bounds of your sprites in a rectangle. Perform a hit-test between the bounds rectangle. If they don't intersect then not additional testing necessary.

    Optionally, break down each sprite into smaller bound rectangles (this is precomputed at sprite generation). Check an intersect between these smaller rectangles and the intersect region. If there are no hits then go about our business.

    Get the intersections and perform pixel/boolean hit testing on the sprite masks in that region (again the sprite masks are precomputed).

    Performing the intermediate test can speed things up, especially if you have 'full hit' flags for each sub-rectangle: this means if there's an intersect with this rectangle then it's a hit with no pixel level check required.

    This can all be done with simple arrays, if-thens and loops. If you are mathematically inclined then you can use vector and array transformations...

    ...however, this is with old, VB and API calls to C DLLs. There may be some fancy smancy .NET stuff which could help (barring your DirectX limitation of course).
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  3. #3

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: 2D Mask Collision

    So you mean:
    1. Check for a collision with the bounding box rectangle.
    2. If there is a collision then split image and check each bit for a collision.

    How do I check if there is a collision between a bit of a picture and another sprite's bit of a picture?

    knxrb
    Attached Images Attached Images  
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  4. #4
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: 2D Mask Collision

    Quote Originally Posted by knxrb View Post
    How do I check if there is a collision between a bit of a picture and another sprite's bit of a picture?
    Loop through the rectangle areas which overlap on the masks.

    Since the masks are basically 2D arrays of boolean values, if both mask bits are true then you have a collision.

    It's a brute force method, but if you are smart with your sprite design it can be very quick (i.e. the planning phase determines how well your code performs).

    Based on your drawing (a circle for your sprite) you wouldn't split the circle into quadrants: you'd probably have a single square which covers the center (any intersect on this rectangle is a collision); 4 rectangles which cover the remaining portions of the circle. The 'corners' do not have any rectangles in them, as any 'overlap' here would be a miss, regardless.

    So, for a circle, you'd store 6 rectangles: the full bounds rectangle (black), the center rectangle (E) and the 4 border rectangles (a,b,c,d).

    Name:  tester.png
Views: 190
Size:  2.5 KB
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: 2D Mask Collision

    Granted, a routine like this would work, but it could potentially be pretty slow. There's no mechanism outside of DirectX that does fast sprite intersection. XNA has a really good and simple implementation for determining sprite intersection, but that too uses DirectX to do it.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  6. #6

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: 2D Mask Collision

    Ok cool, but how would I know if the sprites collide because even your rectangles have massive gaps around the sprite.
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: 2D Mask Collision

    Quote Originally Posted by knxrb View Post
    Ok cool, but how would I know if the sprites collide because even your rectangles have massive gaps around the sprite.
    Do you mean the corners? Of course: there's no point in checking these areas because the sprite doesn't cover that area (considering our circular sprite). So even if you have a rectangular sprite, for example, that is 'in' that area, there is no collision.

    The left/right/top/bottom rectangles will require you to 'scan' each pixel in the overlapping region. That is, find the intersecting region and loop through the pixels in that region. If a pixel in one sprite is set and the pixel in the other sprite is set then you have a collision.

    Note that it does require math, but not particularly complex.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  8. #8

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: 2D Mask Collision

    Oh I get it, so I see if a black pixel from one mask and a black pixel from the other mask are in the same spot, got you
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width