This may be hard to understand, but I'll do my best.

enter image description here

You will see that the first image is a normal Mario, and the other is the result I am looking for. I'm trying to find a way to extract rectangles (biggest possible to smallest) from a texture2D, without overlapping rectangles. If the pixel is completely transparent, leave it out of the loop. I know it would be really easy to implement a rectangle for every pixel, but millions of rectangles isn't going to help performance wise. The goal is to get rectangles somewhat like this, decreasing the total rectangles greatly.

Any ideas or links? Anything would be great.

  • 2
    \$\begingroup\$ Why? (I smell a classic X Y Problem) \$\endgroup\$ – Seth Battin Mar 8 '13 at 4:56
  • \$\begingroup\$ Lol, I'm partly wanting to try this as a different way of collision detection, but I can also see more uses for it as well. I know and use other ways of collision detection, but I would just like to see what I can use it for. Though, I'm not looking for ways to detect collision, I am looking to see if this is do-able, and what I can use it for. Why not? \$\endgroup\$ – RustyGearGames Mar 8 '13 at 5:41
  • \$\begingroup\$ Why not just put a box around the sprite completely? I mean, a sprite like that looks like unless your game really demands it a box around the edge of the sprite will probably cut it... that's how Super Mario was implemented, fyi. \$\endgroup\$ – Vaughan Hilts Mar 8 '13 at 6:17
  • 1
    \$\begingroup\$ Throw the colors into a 2D array. If element is transparent go to next else store where element is as a subX, subY and move to next element until you hit transparency. throw subX, subY, difference of current X/Y from subX/Y as width/height (height is 1 if no difference) into a list of rectangles. Use Rectangle.Union() to join rectangles. Probably in a similar fashion to how you got them in the first place. Can't justify actually trying to implement it, but best of luck to you. \$\endgroup\$ – Dialock Mar 8 '13 at 7:41
  • 1
    \$\begingroup\$ There are multiple duplicate questions of this with different terminology. See for example stackoverflow.com/questions/5919298/… and the linked questions. \$\endgroup\$ – msell Mar 12 '13 at 20:44

Honestly, this is a solution in search of a problem. I can't see any benefits towards doing such a thing, and it doesn't seem like you can think of any either. Best just to take existing solutions and run with those. (and chances are "a new way of doing collision detection" will suck, when pretty damn smart people like the chipmunk physics and box2d devs are regularly improving their algorithms).

You're creating more rectangles when that's the wrong way to go about it.

What you should be doing is creating one polygon from a trace/outline of the sprite. There are loads of algorithms out there which can do it, including marching squares/cubes. Chipmunk Physics (Pro) even has the ability to construct physics objects from traces, as do several other physicy things that I am aware of.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.