PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
HitTest again... ough..
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: HitTest again... ough..

  1. #1
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268

    Question [RESOLVED] HitTest again... ough..

    Hi all. I need an advice from pros. Here's the situation.
    In my game, I've created a background which is built dynamically using xml list. I can type a single line to my xml file to add some object, it works absolutely great, but now I added some water which is drawn into the same Bitmap as the grass terrains and other stuff are so the game objects now have no way to define whether they're over a grass or a water.
    That's how it looks:


    The helicopter knows that it's touching a background Bitmap but how can I make it define what color it touches?

    I know how to hittest transparent pixels, I tried to use the same method for this blue color, but it didn't work out as I wanted.
    Last edited by caseyryan; 07-19-2010 at 01:46 PM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    How is the water placed there? You should test against the logical structure of the game world, not its visual representation.

  3. #3
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    The water is a bitmap object which is drawn into the background's BitmapData using copyPixels() method.

    I've just tried to use getPixel() and traced the result like so:
    Actionscript Code:
    var pixel:* = MissionConstructor.map.containerBMP.bitmapData.getPixel(this.x, this.y);
    trace(pixel.toString(16));

    It traces colors like this when the object's over some grass or anything else but water:
    1e8ca7
    1c8ba9
    1c85a9
    1982aa
    107dab
    1172a6
    106ca3
    And this sort of colors when it's over water surface:

    e6aa4
    d75af
    b6ca8
    b72b2
    969a9
    b79b7
    97bb7
    d82be
    a71ae
    b71b0
    a7ab7
    e90cc
    97cb7
    96faa
    I definitely see the difference, and I guess I have to use this method but the problem now is that I can't figure out a way for the object to tell them apart..

    I mean I need to compare this colors in some way... but how?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    So you've got a separate bitmapdata with just the water in it? Can you test to see whether that bitmap has non-empty pixels at the corresponding location?

    Ideally, you would have some gameworld representation that tells your other code where to draw grass and water, etc. Checking against that representation would make more sense than trying to compare pixel values.

    You could deconstruct the pixel value returned and see whether the RGB channels fall into certain ranges. For instance, it seems that there is a noticable gap between the red values of the water and non-water. I would have expected larger gaps in the green and blue channels, but that may be misleading. It might suffice to test whether the pixel is "mostly blue", by comparing the blue channel to the red and green and seeing whether it is the highest value.

  5. #5
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    So you've got a separate bitmapdata with just the water in it?
    Yes, but it's only used to copy pixels from it, it's never added to the display list itself

    Ideally, you would have some gameworld representation that tells your other code where to draw grass and water, etc. Checking against that representation would make more sense than trying to compare pixel values.
    I thought about it, but this way would slow the game down. And I want it to be as optimized as possible.

    You could deconstruct the pixel value returned and see whether the RGB channels fall into certain ranges. For instance, it seems that there is a noticable gap between the red values of the water and non-water. I would have expected larger gaps in the green and blue channels, but that may be misleading. It might suffice to test whether the pixel is "mostly blue", by comparing the blue channel to the red and green and seeing whether it is the highest value.
    That's exactly what I need, but I have no idea on how to do it. I'd really appreciate it if you could show me some example

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    BitmapData objects don't need to be on the display (in fact, they can't. Bitmaps can.) to be in memory and available for computation. Similarly, your gameworld representation would not be a DisplayObject that gets rendered, but just a logical object that describes the world. The Model in Model-View-Controller, if you like.

    To deconstruct a pixel value into RGB, just use bitshifting and bitwise AND operations.
    Code:
    var pixel:uint = 0x3355aa;
    var r:uint = (pixel >> 16)  & 0xff; //0x33;
    var g:uint = (pixel >> 8)  & 0xff;  //0x55;
    var b:uint = pixel & 0xff; //0xaa;
    Edit: Note that if you will be doing lots of pixel-level tests, it is vastly faster to use getVector to get a Vector of pixel values and then compare against that, than it is to call getPixel or getPixel32 multiple times.
    Last edited by 5TonsOfFlax; 07-19-2010 at 01:34 PM.

  7. #7
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    BitmapData objects don't need to be on the display (in fact, they can't. Bitmaps can.) to be in memory and available for computation.
    Yeah, I've messed it up a little. I got a bitmap with water and the pixels are copied from it like this:
    Actionscript Code:
    ks_bitmapData.copyPixels(dirtAndStuff.bitmapData, new Rectangle(0, 360, 370, 340), new Point(dirtNstuffPath[i].@x, dirtNstuffPath[i].@y), null, null, true);
    well, no matter

    Similarly, your gameworld representation would not be a DisplayObject that gets rendered, but just a logical object that describes the world. The Model in Model-View-Controller, if you like.
    I think it's way too complicated for me at the moment.

    To deconstruct a pixel value into RGB, just use bitshifting and bitwise AND operations.

    Code:
    var pixel:uint = 0x3355aa;
    var r:uint = (pixel >> 16)  & 0xff; //0x33;
    var g:uint = (pixel >> 8)  & 0xff;  //0x55;
    var b:uint = pixel & 0xff; //0xaa;
    Thanks I'll try this

  8. #8
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    This works! All objects now can define water
    Actionscript Code:
    var pixel:uint = MissionConstructor.map.containerBMP.bitmapData.getPixel(this.x, this.y);
               
                var r:uint = (pixel >> 16)  & 0xff;
                var g:uint = (pixel >> 8)  & 0xff;  
                var b:uint = pixel & 0xff;
               
                if (b > 130)
                {
                    trace("I'm in water");
                }

    5TonsOfFlax, thanks again This helped me a lot

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Don't even bother calculating red and green if you're not using them. Every little optimization helps, right?

  10. #10
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    Yeah, you're right. I gotta comment those two 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