samedi 28 novembre 2015

Issues wih BitmapData.hitTest with rotated MovieClips

I've been having a lot of trouble with collision testing in as3. I sort of figured out how to handle the BitmapData.hitTest() function along with the matrix stuff to handle rotated movieclips, but for some objects it still doesn't work.

I'm trying to make a game where you shoot targets. I have a class called Portal, which has an attribute called partnerPortal, which is another portal. When the ammo hits the receptor of the portal, a little rectangular MovieClip at the back of the opening, the ammo's x and y simply get reassigned to the partnerPortal's x and y (with some additions to make it look right). When it hits anywhere else, it calls ammo's selfDestruct function.

Link to an image of the portal and receptor:

Here's the code for collision detection:

    public function adjustMatrix(transformer:HitObject):void {
        var hitRect = transformer.getBounds(this);
        var hitMatrix = transformer.transform.matrix;
        var xOffset:Number = transformer.x - hitRect.x
        var yOffset:Number = transformer.y - hitRect.y
        hitMatrix.tx = 0;
        hitMatrix.ty = 0;
        hitMatrix.translate(xOffset, yOffset);
        var hitMap = new BitmapData(hitRect.width, hitRect.height, true, 0);
        hitMap.draw(transformer, hitMatrix);

    public function checkStrike(ammo:Ammo):void {
        for(var i = 0; i <= hitObjects.length - 1; i++) {
            var hitObject = hitObjects[i];              
            if (hitObject.hitTestObject(ammo)) {
                trace("box " + hitObject); // Sees if the ammo has hit the bounding box
                var ammoPoint:Point = new Point(ammo.hitRect.x, ammo.hitRect.y);
                var hitPoint:Point = new Point(hitObject.hitRect.x, hitObject.y);
                if (hitObject.hitMap.hitTest(hitPoint, 1, ammo.hitMap, ammoPoint, 1)) {
                    hitObject.hit(ammo); // calls the portal's (or whatever else the ammo hits) hit function

And for the portal's hit function:

    public function hit(ammo:Ammo):void {
        trace(ammo.y,  this.y, ammo.x, this.x);
        if (receptor.hitTestObject(ammo)){
            adjustPosition(ammo); // Aligns the ammo so it looks like it is emerging from the portal

The default portal is facing upwards, and the rotation value for each instance is stored in a JSON file. For the up and right instances, they work fine. For the left and down instances it starts to break. When a portal is facing left, the ammo hits the bounding box when it hits the portal, but it moves right over it and doesn't interact. When facing up, the portal calls the selfDestruct funtion when the ammo reaches the opening, not the receptor.

Aucun commentaire:

Enregistrer un commentaire