示例#1
0
 bool PixelPerfectTest(const sf::Sprite& Object1, const sf::Sprite& Object2, sf::Uint8 AlphaLimit) {
     sf::FloatRect Intersection;
     if (Object1.getGlobalBounds().intersects(Object2.getGlobalBounds(), Intersection)) {
         sf::IntRect O1SubRect = Object1.getTextureRect();
         sf::IntRect O2SubRect = Object2.getTextureRect();
         
         sf::Uint8* mask1 = Bitmasks.GetMask(Object1.getTexture());
         sf::Uint8* mask2 = Bitmasks.GetMask(Object2.getTexture());
         
         // Loop through our pixels
         for (int i = Intersection.left; i < Intersection.left+Intersection.width; i++) {
             for (int j = Intersection.top; j < Intersection.top+Intersection.height; j++) {
                 
                 sf::Vector2f o1v = Object1.getInverseTransform().transformPoint(i, j);
                 sf::Vector2f o2v = Object2.getInverseTransform().transformPoint(i, j);
                 
                 // Make sure pixels fall within the sprite's subrect
                 if (o1v.x > 0 && o1v.y > 0 && o2v.x > 0 && o2v.y > 0 &&
                     o1v.x < O1SubRect.width && o1v.y < O1SubRect.height &&
                     o2v.x < O2SubRect.width && o2v.y < O2SubRect.height) {
                     
                     if (Bitmasks.GetPixel(mask1, Object1.getTexture(), (int)(o1v.x)+O1SubRect.left, (int)(o1v.y)+O1SubRect.top) > AlphaLimit &&
                         Bitmasks.GetPixel(mask2, Object2.getTexture(), (int)(o2v.x)+O2SubRect.left, (int)(o2v.y)+O2SubRect.top) > AlphaLimit)
                         return true;
                     
                 }
             }
         }
     }
     return false;
 }
示例#2
0
bool PixelPerfectTest( const sf::Sprite& object1, const sf::Sprite& object2, sf::Uint8 AlphaLimit, const sf::Image & object1CollisionMask,  const sf::Image & object2CollisionMask )
{
    sf::FloatRect intersection;
    if ( object1.getGlobalBounds().intersects( object2.getGlobalBounds(), intersection ) )
    {
        //We've got an intersection we need to process the pixels
        //in that Rect.

        //Bail out now if AlphaLimit = 0
        if ( AlphaLimit == 0 ) return true;

        //There are a few hacks here, sometimes the TransformToLocal returns negative points
        //Or Points outside the image.  We need to check for these as they print to the error console
        //which is slow, and then return black which registers as a hit.

        sf::Vector2f o1v;
        sf::Vector2f o2v;
        //Loop through our pixels
        for ( int i = intersection.left; i < intersection.left+intersection.width; i++ ) //4ian : Rect now uses width/height.
        {
            for ( int j = intersection.top; j < intersection.top+intersection.height; j++ ) //4ian : Rect now uses width/height.
            {

                o1v = object1.getInverseTransform().transformPoint( i, j ); //Creating objects each loop :(
                o2v = object2.getInverseTransform().transformPoint( i, j );

                //Hack to make sure pixels fall within the Sprite's Image
                if ( o1v.x > 0 && o1v.y > 0 && o2v.x > 0 && o2v.y > 0 &&
                        o1v.x < object1CollisionMask.getSize().x && o1v.y < object1CollisionMask.getSize().y &&
                        o2v.x < object2CollisionMask.getSize().x && o2v.y < object2CollisionMask.getSize().y )
                {
                    //If both sprites have opaque pixels at the same point we've got a hit
                    if (( object1CollisionMask.getPixel( static_cast<int>( o1v.x ), static_cast<int>( o1v.y ) ).a > AlphaLimit ) &&
                        ( object2CollisionMask.getPixel( static_cast<int>( o2v.x ), static_cast<int>( o2v.y ) ).a > AlphaLimit ) )
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    return false;
}