Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411419 Posts in 69363 Topics- by 58416 Members - Latest Member: timothy feriandy

April 17, 2024, 10:37:55 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Generating a Polygon Path given a 2D array
Pages: [1]
Print
Author Topic: Generating a Polygon Path given a 2D array  (Read 4513 times)
clockwrk_routine
Guest
« on: February 06, 2014, 06:12:57 PM »

I started reworking how my tile editor builds it collision in unity, using the polygon collider 2d component.  The polygon collider 2d component takes a sequential paths of points and generates a shape based on those points.  My problem is generating those paths when your collision data for your tiles is stored in a 2d array so it looks something like this:

0 X X 0
X X 0 0
X X 0 0
0 X X 0

I considered doing a bunch of "if block is in X dirrection: move to block" checks, until I got a complete path around the shape, adding checked blocks to a list of already visited, but I'm wondering if there is a more efficient method out there.

edit:
I found what I was looking for:
http://www.imageprocessingplace.com/downloads_V3/root_downloads/tutorials/contour_tracing_Abeer_George_Ghuneim/theo.html

edit:
nvm same solution but I now know what I should be looking for, contour tracing algorithms.
« Last Edit: February 06, 2014, 06:48:32 PM by keo » Logged
surt
Level 7
**


Meat by-product.


View Profile
« Reply #1 on: February 06, 2014, 11:26:28 PM »

You might want to combine that with connected-component labelling to extract the individual islands.
Logged

Real life would be so much better with permadeath.
PJ Gallery - OGA Gallery - CC0 Scraps
Columbo
Level 0
***


View Profile
« Reply #2 on: February 07, 2014, 09:35:32 AM »

The problem looks quite similar to marching squares (http://en.wikipedia.org/wiki/Marching_squares), the challenge would be repurposing the concept to be able to trace a line around.

Maybe you could attack it in two passes:

Pass 1: Run the marching squares algorithm to generate all the intersection points and categorize all of the cells in your array.
Pass 2: Iterate through the cells, as soon as you hit one with some content, trace the contour generating your line point until you come full circle back to where you started, as you go mark the cells as processed so that you can skip them as you continue iterating.

As a bonus, by using a marching square based approach, you could choose to have more complexity so that instead of always having 45-degree or 90 degree aligned slopes, you could have other slopes too. Although that might not really fit in with your tile editor.
Logged

clockwrk_routine
Guest
« Reply #3 on: February 07, 2014, 01:09:17 PM »

^ Yea that's exactly what I'm trying to implement now : )
thanks surt and columbo
Logged
gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #4 on: February 07, 2014, 08:54:21 PM »




seems similar to this problem (unity3D)
Logged

clockwrk_routine
Guest
« Reply #5 on: February 09, 2014, 10:49:30 PM »

Looks like I didn't need to do any categorizing/connected component labeling, I ran marching squares in one pass marking all the visited cells as I went along, gathering every island path into an array.  I used polygoncollider2d.setpath for each path, and unity seemed to merge the paths togethers.  Also kept whether there were any holes in the polygon intact which is nice.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic