I made a triangle and a rectangle for my game and am sending them both to pixelPerfectCheck and the results are far from perfect. Are there any known ways to make this work at its best? I would post my code, but it is a simple boolean if statement. When pixelPerfectCheck returns true, it shows me. And it isn't even close to perfect.

When I run the normal flixel collisions function, I get the expected results.

EDIT: Here is more detail:

here is a screenshot of the collision test failing. I am falling my ship onto the platform. The reason it is stopped is because the game stops when the collision test returns true.

Here is the code that is being run on each update check (game is being run at 60 fps). Note that "player" is the ship and "landingPad" is the white rectangle. Also keep in mind that the code outside of the collision check is sound. Using the normal collision check has given me perfect results outside of the fact that there is empty space between the two objects:

if(FlxCollision.pixelPerfectCheck(player, landingPad)){

player.hitLandingPad();

}

Here is the code of the pixelPerfectCheck that is being called:

public static function pixelPerfectCheck(contact:FlxSprite, target:FlxSprite, alphaTolerance:int = 255, camera:FlxCamera = null):Boolean

{

var pointA:Point = new Point;

var pointB:Point = new Point;

if (camera)

{

pointA.x = contact.x - int(camera.scroll.x * contact.scrollFactor.x) - contact.offset.x;

pointA.y = contact.y - int(camera.scroll.y * contact.scrollFactor.y) - contact.offset.y;

pointB.x = target.x - int(camera.scroll.x * target.scrollFactor.x) - target.offset.x;

pointB.y = target.y - int(camera.scroll.y * target.scrollFactor.y) - target.offset.y;

}

else

{

pointA.x = contact.x - int(FlxG.camera.scroll.x * contact.scrollFactor.x) - contact.offset.x;

pointA.y = contact.y - int(FlxG.camera.scroll.y * contact.scrollFactor.y) - contact.offset.y;

pointB.x = target.x - int(FlxG.camera.scroll.x * target.scrollFactor.x) - target.offset.x;

pointB.y = target.y - int(FlxG.camera.scroll.y * target.scrollFactor.y) - target.offset.y;

}

var boundsA:Rectangle = new Rectangle(pointA.x, pointA.y, contact.framePixels.width, contact.framePixels.height);

var boundsB:Rectangle = new Rectangle(pointB.x, pointB.y, target.framePixels.width, target.framePixels.height);

var intersect:Rectangle = boundsA.intersection(boundsB);

if (intersect.isEmpty() || intersect.width == 0 || intersect.height == 0)

{

return false;

}

// Normalise the values or it'll break the BitmapData creation below

intersect.x = Math.floor(intersect.x);

intersect.y = Math.floor(intersect.y);

intersect.width = Math.ceil(intersect.width);

intersect.height = Math.ceil(intersect.height);

if (intersect.isEmpty())

{

return false;

}

// Thanks to Chris Underwood for helping with the translate logic :)

var matrixA:Matrix = new Matrix;

matrixA.translate(-(intersect.x - boundsA.x), -(intersect.y - boundsA.y));

var matrixB:Matrix = new Matrix;

matrixB.translate(-(intersect.x - boundsB.x), -(intersect.y - boundsB.y));

var testA:BitmapData = contact.framePixels;

var testB:BitmapData = target.framePixels;

var overlapArea:BitmapData = new BitmapData(intersect.width, intersect.height, false);

overlapArea.draw(testA, matrixA, new ColorTransform(1, 1, 1, 1, 255, -255, -255, alphaTolerance), BlendMode.NORMAL);

overlapArea.draw(testB, matrixB, new ColorTransform(1, 1, 1, 1, 255, 255, 255, alphaTolerance), BlendMode.DIFFERENCE);

// Developers: If you'd like to see how this works, display it in your game somewhere. Or you can comment it out to save a tiny bit of performance

debug = overlapArea;

var overlap:Rectangle = overlapArea.getColorBoundsRect(0xffffffff, 0xff00ffff);

overlap.offset(intersect.x, intersect.y);

if (overlap.isEmpty())

{

return false;

}

else

{

return true;

}

}