Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In my simple game I use boundingBox for pick coins and other stuff, but I need use irregular area detection of Sprite(with out Alpha)... There is alternative to boundingBox? Here is code:

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView:[touch view]];
CGPoint point = [[CCDirector sharedDirector] convertToGL:location];
curentPosition = point;
arrToDel = [[NSMutableArray alloc] initWithCapacity:0];

CCSprite *coin = nil;

for (Coins *coins in self.bugs) {
    if (CGRectContainsPoint([coins boundingBox], curentPosition)) {
        coin = coins; // found sprite
    }
}

if (coin != nil) {
    NSMutableArray *checkList = [NSMutableArray arrayWithCapacity:0];

    for (Coins *coins in self.bugs) {
        if (CGRectIntersectsRect([coin boundingBoxInPixels], [coins boundingBox]) && coins != coin) {
            [checkList addObject:coins];
        }
    }

    int max = coin.zOrder;

    for (Coins *b in checkList) {
        if (b.zOrder > max)
            max = b.zOrder;
    }

    if (max == coin.zOrder) {

        [self removeChild:coin cleanup:YES];

        [arrToDel addObject:coin];

        for (Coins *coins in  arrToDel) {

            [self.bugs removeObject:coin];

        }
    }
}

}

share|improve this question

3 Answers 3

up vote 1 down vote accepted

For this you may use CGPath. You can find answer on your question here

share|improve this answer
1  
-1. A plain link is not an answer. –  bummzack Jul 28 '11 at 7:42
    
I fixed my answer on this question –  Darth Vader Jul 28 '11 at 8:07
    
Slightly better now.. –  bummzack Jul 28 '11 at 8:13

If your sprites can be decomposed into a series of bounding boxes or other easy-to-detect primitive shapes, you could use that collection of bounding boxes instead. That probably won't scale, however, -- a more common approach is to use collision masks. A mask is essentially a B&W version of the sprite where white means "not part of the sprite" and black means "part of the sprite."

If your source sprites have alpha or color key data, you can either use directly as the mask or use it produce the mask images programmatically. Otherwise you may need to author the mask images in a paint program.

Anyhow, once you have the mask you first do a simple bounding-box check to trivially reject collisions that aren't even close. Then you test against the mask image -- in this case it looks like you'd transform currentPosition into the local space of the mask image (subtract the current position from the origin of the bounding box) and then see if the pixel in the mask at the transformed index was black or white, and that will let you know if the user touched part of the sprite or not.

Note that you may also find that you'll need to enlarge the hit test area from a single pixel in order to make this feel good, especially on a real phone when you're using real fingers as the input instead of a mouse on the simulator.

share|improve this answer
    
I was under the impression that it's usually white 1 for "part of the sprite" and black 0 for "not part of the sprite". –  bummzack Jul 27 '11 at 15:03
    
Most masks I saw in the days when this was a vastly more prevalent technique (and even today) look like the one here. It really doesn't matter though, so I wouldn't be surprised to see such a mask. –  Josh Petrie Jul 27 '11 at 15:08
    
I see. I guess I had alpha-channels in mind.. in the end it doesn't make much of a difference. –  bummzack Jul 27 '11 at 15:26

If the sprite doesn't change, you could give it a convex bounding polygon. Computing collisions is more complicated than with rectangles, but not much more.

Even if it does change, computing an appropriate polygon at runtime is not terribly hard.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.