actionscript-3flashcollision-detectionbitmapdata

detecting color for a golf game to know that the ball is on the green, etc


Honestly my question may have been answered but I don't have a college degree and only understand basic math and some algebra. I do have a lot of experience coding in as3 and animate and have read alot of answers and questions so far but I'm still lost.

I know how to do a hitTest on the object and picture I made for the green in my golf game. It appears that bitmap data is what I need to use but I'm new to it and never used it at all.

What Im thinking is when the golf ball enters the hitTestObject bounding box and is true to get the bitmap data and only look for color and dont worry about the transparent pixels. Then when the ball stops it should be on the green???

I have no idea so far on how to do this or is this the best thing to do?

I attached a picture of the green and some of the course so you can look and try to understand whats going on. Sand traps will be an issue but for now I want to focus on the green.

golf green with bounding box


Solution

  • What you're looking for is BitmapData.hitTest.

    First, you need to have a BitmapData of the green grass. You only need to do this once so don't include this part inside your loop when you check for collision.

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Point;
    
    private var bm:Bitmap;
    private var bitmapData:BitmapData;
    
    //after everything has instantiated:
    bitmapData = new BitmapData(backhole1.green1.width, backhole1.green1.height);
    bitmapData.draw(backhole1.greenMap);
    bm = new Bitmap(bitmapData);
    

    When ready, check for collision:

    if (bm.bitmapData.hitTest(new Point(backhole1.greenMap.x, backhole1.greenMap.y), 255, new Point(backhole1.golfball.x, backhole1.golfball.y), 255)) {
      //collision detected, do stuffs
    }
    

    The value 255 is to check for opacity of the bitmap. Anything that is not 100% opague will be treated as transparent in this case.