Advertisement
happybirthday

Pixel Perfect Collision

Sep 2nd, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. bool GameLayer::areSpritesColliding(cocos2d::CCSprite *spr1, cocos2d::CCSprite *spr2, bool pp) {
  2.     bool isColliding = false;
  3.     CCRect intersection;
  4.     CCRect r1 = spr1->boundingBox();
  5.     CCRect r2 = spr2->boundingBox();
  6.    
  7.    
  8.    
  9.     // Look for simple bounding box collision
  10.     if (r1.intersectsRect(r2)) {
  11.     // If we're not checking for pixel perfect collisions, return true
  12.         if (!pp) {
  13.             return true;
  14.         }
  15.         CCLog("Bounding Box Collision");
  16.    
  17.         float tempX;
  18.         float tempY;
  19.         float tempWidth;
  20.         float tempHeight;
  21.        
  22.         if (r1.getMaxX() > r2.getMinX()) {
  23.             tempX = r2.getMinX();
  24.             tempWidth = r1.getMaxX() - r2.getMinX();
  25.         } else {
  26.             tempX = r1.getMinX();
  27.             tempWidth = r2.getMaxX() - r1.getMinX();
  28.         }
  29.        
  30.         if (r1.getMinY() < r2.getMaxY()) {
  31.             tempY = r1.getMinY();
  32.             tempHeight = r2.getMaxY() - r1.getMinY();
  33.         } else {
  34.             tempY = r2.getMinY();
  35.             tempHeight = r1.getMaxY() - r2.getMinY();
  36.         }
  37.        
  38.         intersection = CCRectMake(tempX * CC_CONTENT_SCALE_FACTOR(), tempY  * CC_CONTENT_SCALE_FACTOR(), tempWidth * CC_CONTENT_SCALE_FACTOR(), tempHeight * CC_CONTENT_SCALE_FACTOR());        
  39.        
  40.         unsigned int x = intersection.origin.x;
  41.         unsigned int y = intersection.origin.y;
  42.         unsigned int w = intersection.size.width;
  43.         unsigned int h = intersection.size.height;
  44.        
  45.         unsigned int numPixels = w * h;
  46.         // Draw into the RenderTexture
  47.         _rt->beginWithClear( 0, 0, 0, 0);
  48.        
  49.         // Render both sprites: first one in RED and second one in GREEN
  50.         glColorMask(1, 0, 0, 1);
  51.         spr1->visit();
  52.         glColorMask(0, 1, 0, 1);
  53.         spr2->visit();
  54.         glColorMask(1, 1, 1, 1);
  55.        
  56.         // Get color values of intersection area
  57.         ccColor4B *buffer = (ccColor4B *)malloc( sizeof(ccColor4B) * numPixels );
  58.         glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  59.        
  60.         _rt->end();
  61.        
  62.         // Read buffer
  63.         unsigned int step = 1;
  64.         for(unsigned int i=0; i<numPixels; i+=step) {
  65.             ccColor4B color = buffer[i];
  66.             //CCLog("Pixel color: %d, %d, %d", color.r, color.g, color.b);
  67.             if (color.a > 0 && color.g > 0) {
  68.                 isColliding = true;
  69.                 CCLog("Colliding");
  70.                 break;
  71.             }
  72.         }
  73.        
  74.         // Free buffer memory
  75.         free(buffer);
  76.     }
  77.    
  78.    
  79.     return isColliding;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement