半山枫叶的专栏

专注ios开发,让我们一起进步吧!

点对点碰撞检测
-(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;
}

阅读更多
文章标签: returning
个人分类: ObjectC2.0学习日志
想对作者说点什么? 我来说一句

碰撞检测 有关C++

2011年08月04日 2.19MB 下载

没有更多推荐了,返回首页

不良信息举报

点对点碰撞检测

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭