dimanche 27 septembre 2015

AS3 Bitmap Collision detection for rotated movie clips

I've constructed a Bitmap Collision system for rotated movieclips (using rotateAroundExternalPoint, not included here), yet it's sometimes off after rotation and detects collision many pixels away at certain angles even when matrices were used. I think it might be connected with bounding boxes ( getBounds(this) ), but I'm not sure how to offset that if that's the problem.

Here is a picture of two simple shapes showing collision while still not touching (ignore the black space): Simple Shapes show collision

function bitmapCollision(one:MovieClip,two:MovieClip){

if(one.hitTestObject(two)){     //hitboxes overlap = close enough to test
    var point1:Point;
    var point2:Point;
    var bmp1:BitmapData;
    var bmp2:BitmapData;
    var box1:Rectangle;
    var box2:Rectangle;
    var angle1:Number;
    var angle2:Number;
    var one_newX:Number;
    var one_newY:Number;
    var two_newX:Number;
    var two_newY:Number;
    var m1:Matrix = new Matrix();
    var m2:Matrix = new Matrix(); 
    box1 = one.getBounds(this);
    box2 = two.getBounds(this);
    one_newX = one.x - box1.x;
    one_newY = one.y - box1.y;

    m1 = one.transform.matrix;
    m2 = two.transform.matrix;
    m1.tx = 0;
    m1.ty = 0;
    m2.tx = 0;
    m2.ty = 0;

    m1.translate(one_newX, one_newY);
    m2.translate(two_newX, two_newY);

    bmp1 = new BitmapData(box1.width, box1.height, true, 0x00000000);
    bmp2 = new BitmapData(box2.width, box2.height, true, 0x00000000);

    bmp1.draw(one, m1);
    bmp2.draw(two, m2);

    point1 = new Point(box1.x, box1.y);
    point2 = new Point(box2.x, box2.y);

    if( bmp1.hitTest(point1, 50, bmp2, point2, 50) ){
        trace("Collision of "+one.name+" and "+two.name)
        collisionDetected = true;

        //Tint change for both objects
        colorTint = one.transform.colorTransform;
        colorTint.redOffset = 227;
        colorTint.blueOffset = 0;
        colorTint.greenOffset = 153;
        one.transform.colorTransform = colorTint;
        colorTint = two.transform.colorTransform;
        colorTint.redOffset = 227;
        colorTint.blueOffset = 0;
        colorTint.greenOffset = 153;
        two.transform.colorTransform = colorTint;
    }
}
}

Any ideas would be greatly appreciated :)

Aucun commentaire:

Enregistrer un commentaire