关闭

点对点碰撞检测

843人阅读 评论(0) 收藏 举报
-(BOOL) pixelMaskIntersectsRegularNode:(CCNode*)other
{
   CGRect intersectRect = [self intersectRectInPixels:self otherNode:other];
   
   // check if any of the flags in the pixelMask are set in intersection
   NSUInteger maxX = intersectRect.origin.x + intersectRect.size.width;
   NSUInteger maxY = intersectRect.origin.y + intersectRect.size.height;
   for (NSUInteger y = intersectRect.origin.y; y < maxY; y++)
   {
      for (NSUInteger x = intersectRect.origin.x; x < maxX; x++)
      {
         NSUInteger index = y * pixelMaskWidth + x;
         return pixelMask[index];
      }
   }
   
   return NO;
}


-(CGRect) intersectRectInPixels:(CCNode*)node otherNode:(CCNode*)other
{
   CGRect myBBox = [node boundingBox];
   CGRect otherBBox = [other boundingBox];
   CGRect intersectRect = CGRectIntersection(myBBox, otherBBox);
   
   // transform the rect to the sprite's space and convert points to pixels
   intersectRect.origin = [node convertToNodeSpace:intersectRect.origin];
   return CC_RECT_POINTS_TO_PIXELS(intersectRect);
}

-(BOOL) pixelMaskIntersectsPixelMaskSprite:(KKPixelMaskSprite*)other
{
   CGRect intersectSelf = [self intersectRectInPixels:self otherNode:other];
   CGRect intersectOther = [self intersectRectInPixels:other otherNode:self];
   NSUInteger originOffsetX = intersectOther.origin.x - intersectSelf.origin.x;
   NSUInteger originOffsetY = intersectOther.origin.y - intersectSelf.origin.y;
   NSUInteger otherPixelMaskWidth = other.pixelMaskWidth;
   BOOL* otherPixelMask = other.pixelMask;
   // check if any of the flags in the pixelMask are set in intersection area
   NSUInteger maxX = intersectSelf.origin.x + intersectSelf.size.width;
   NSUInteger maxY = intersectSelf.origin.y + intersectSelf.size.height;
   for (NSUInteger y = intersectSelf.origin.y; y < maxY; y++)
   {
      for (NSUInteger x = intersectSelf.origin.x; x < maxX; x++)
      {
         NSUInteger index = y * pixelMaskWidth + x;
         
         if (pixelMask[index])
         {
            // check if there's a pixel set at the same location
            // in the pixelMask of the other sprite
            NSUInteger otherX = x + originOffsetX;
            NSUInteger otherY = y + originOffsetY;
            NSUInteger otherIndex = otherY * otherPixelMaskWidth + otherX;
            
            if (otherPixelMask[otherIndex])
            {
               return YES;
            }
         }
      }
   }
   return NO;
}


-(BOOL) pixelMaskIntersectsNode:(CCNode*)other
{
   if (rotation_ != 0.0f || other.rotation != 0.0f ||
        self.scale != 1.0f || other.scale != 1.0f)
   {
      CCLOG(@"either or both nodes are rotated and/or scaled, returning NO!");
      return NO;
   }
   
   if ([self intersectsNode:other])
   {
      if ([other isKindOfClass:PixelMaskSpriteClass])
      {
         KKPixelMaskSprite* maskSprite = (KKPixelMaskSprite*)other;
         return [self pixelMaskIntersectsPixelMaskSprite:maskSprite];
      }
      else
      {
         return [self pixelMaskIntersectsRegularNode:other];
      }
   }
   
   return NO;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:326338次
    • 积分:3836
    • 等级:
    • 排名:第8310名
    • 原创:41篇
    • 转载:116篇
    • 译文:1篇
    • 评论:22条
    最新评论
    ios专栏
    ios专栏