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