点对点碰撞检测

- ( 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 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值