A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] occupied pixels?

  1. #1
    Member
    Join Date
    Jun 2009
    Posts
    35

    resolved [RESOLVED] occupied pixels?

    I'm building a kind of dynamic level generator, at the moment I have it draw a grid of alternating coloured squares/plots (for visual debug), currently set at 1 pixel which I then scale up so they are easier to see. I make a list of all these open 'plots' in an array(maybe a dictionary would be better?)...

    Next I draw a random selection of roads on top of my grid in another sprite. Ranging in width from 2 to 8 plots. First drawing all the horizontal, and then the vertical ones. Now I'm wondering what would be the best way of finding all the plots/pixels that these roads occupy?

    I thought perhaps I could draw the roads into bitmap data, and then find the x,y values of all the black pixels? something like:

    PHP Code:
    var bData:BitmapData = new BitmapData(roads.widthroads.heightfalse);
                
    bData.draw(roads)
                
                for (var 
    px:int 0px bData.widthpx++)
                {
                    for (var 
    py:int 0py bData.heightpy++)
                    {
                        if (
    bData.getPixel(pxpy) == 0x000000)
                        {
                        
    //Do Something
                        
    }
                    }
                } 
    Which I assume works? as trying to trace the result crashes it.

    Or would it be better to perform some maths when each road is built and do this operation for each one at its creation? What would be the best way to accomplish this do you think? thanks.
    Last edited by davivid; 11-26-2009 at 05:32 PM.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    You should be keeping a map of your level in some form - I'd start with a multidim array of [x][y][z] - with z being the actual cel on your grid containing a stack of terrains in there (eg. 0 would be the ground, 1 would be road, etc).
    Please use [php] or [code] tags, and mark your threads resolved 8)

  3. #3
    Member
    Join Date
    Jun 2009
    Posts
    35
    Thanks - storing the data in amultidim array with its tile type sounds like a good idea.

    I've got my original idea working and then redrawing the roads again from the new data to check it works, with a speeded up structure I think?

    PHP Code:
    var px:int = -1;
                
                    while (
    px mapsize )
                    {
                        
    px++
                        var 
    py:int = -1;
                        
                        while (
    py <  mapsize)
                        {
                            
    py++
                        
                            if (
    bData.getPixel(pxpy) == 0x000000)
                        {
                            
    //push data.
                        
    }
                        }
                    } 
    Since this adds the data and tile type in one go, avoiding duplicated data from overlapping roads, would it be better to use this method or adding the data as each road is made, and then checking for duplicates?

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Definitely better to avoid duplication in the first place - although if you're tracking everything in an array you could avoid the getPixel call which would be a pretty big optimization for you.
    Please use [php] or [code] tags, and mark your threads resolved 8)

  5. #5
    Member
    Join Date
    Jun 2009
    Posts
    35
    Yes that makes sense. Adding data to the arrays as each object is created sounds like the correct way of doing things, however say I draw a vertical road - which will essentially be a rectangle say x:100, y:0, w:8, h:mapsize - I can easily note which pixels have been used.

    Next say I add a horizontal road - x:0, y:100, w:mapsize, h:4 - these roads will cross, how best avoid the duplicated data? comparing the data after its drawn, or using a loop checking and drawing pixel by pixel?

    Eventually after I have my simple roads drawn, I will be running loops that randomly scatter objects on unused areas of the maps, up until a certain percentage of free space has been reached- So I need a good fast method of tracking the already used pixels.

  6. #6
    Member
    Join Date
    Jun 2009
    Posts
    35
    well I seem to be getting there now. I decided to use a simple array, using the index number as the pixel location and just storing a digit, set as 0. As the roads are drawn they set the corresponding pixel array locations to 1, marking them as used.

    next I check the unused locations, and find the boundaries of the area where the pixel is located, and add this area to a new array, marking those pixels used in the original array. This repeats until I have all the new usable locations, in which I can later add objects. Its quite slow(takes around 50s), so that's something that will need to be sorted out!

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

HTML5 Development Center