0
votes

I've spent all the day on this, it's time to ask for your help :)

I'm trying to do collision detection of two display objects, both have centered registration point.

On my stage I have fixed elements that when added to stage are pushed in an Array called "zoneUsed". All the displayObject in my project have the registration point in the center.

My goal is to click on the stage, and check if in the clicking coords I could create a circle. My plan was to create a Sprite for the new object, cycle on the zoneUsed array, and check if the new sprite have enough space to live.

Here my code so far:

 private function checkSpaceForNewMarker (markerToCheck:Sprite):Boolean {
        var isPossible:Boolean = true;
        var bmdataToCheck:BitmapData = new BitmapData (markerToCheck.width, markerToCheck.height, true, 0);
        var m:Matrix = new Matrix ();
        m.tx = markerToCheck.width/2;
        m.ty = markerToCheck.height/2;
        bmdataToCheck.draw (markerToCheck, m);

        for (var i:int = 0; i<zoneUsed.length; i++) {
            trace ("*** CHECKING ****");
            var bmddataOnTheTable:BitmapData = new BitmapData (zoneUsed[i].width, zoneUsed[i].height, true, 0);

            var tableMatrix:Matrix = new Matrix ();
            tableMatrix.tx = zoneUsed[i].width/2;
            tableMatrix.ty = zoneUsed[i].height/2;
            bmddataOnTheTable.draw(zoneUsed[i], tableMatrix);


            if (bmdataToCheck.hitTest(new Point(markerToCheck.x, markerToCheck.y), 255, bmddataOnTheTable, new Point (zoneUsed[i].x, zoneUsed[i].y), 255)) {
                trace ("COLLISION");
                                    isPossible = false;
            } else {
                trace ("NO COLLISION");
                                    isPossible = true;
            }
        }


        return isPossible;
    }

....But right now the results are weird. Depending on the zones, my traces work or not. What am I doing wrong?

2

2 Answers

1
votes

The problem is , you are drawing 1/4 (quarter) part of every object. BitmapData is not like a Shape, Sprite, MovieClip, and it crops all the pixels, when the drawing bounds is out of the bounds of (0,0,bitmapdata.width, bitmapdata.height) rectangle. Just remove this lines:

 m.tx = markerToCheck.width/2;
 m.ty = markerToCheck.height/2;

and also

 tableMatrix.tx = zoneUsed[i].width/2;
 tableMatrix.ty = zoneUsed[i].height/2;

You don't need this translations.

Also your code may be cause for memory leak. You are creating bitmapdata, but do not dispose it. The garbage collector will not release the memory you have allocated.You must release memory explicitly. Call bitmapdata.dispose() every time you have no need of that bitmapdata.

0
votes

I'm not sure that the origin of the bitmap has anything to do with the test itself. The very nature of the test would seem to imply that the hittest is based on the RGBA value of the two supplied bitmaps. Anyway rather than picking apart your own implementation I'll just refer you to a tutorial by Mike Chambers (adobe platform evangelist). http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/

Also for more flash tutorials check out www.gotoandlearn.com.