godzilla
godzilla

Reputation: 3125

cocos2d pixel collision detection

i am currently writing a game with a number of rectangles (say 30), if a user clicks on one of the four sides a certain action will need to be taken. Trouble is each card will have the same side which will perform this action. For example with rectangle 1 the side which will perform this action will be the left, but rectangle 3 the side will be the top side. My guess is a pixel collision detection is needed here. What do you guys think? Also is there a good example on pixel collision using cocos2s out there?

Upvotes: 1

Views: 451

Answers (3)

godzilla
godzilla

Reputation: 3125

Thanks for that Dhaval, here is the code i wrote a future reference for anyone who wishes to do similar, it can be further fine tuned, if i figure out a more accurate way of doing this i will update this thread accordingly

-(void)touchWhichSide:(CCSprite *)sprite1 collidingSprite:(CCSprite *)sprite2
{
    BOOL retValue = NO;

    CGPoint spriteCentre1 = sprite1.position;
    CGPoint spriteCentre2 = sprite2.position;

    NSLog(@"X %f",spriteCentre2.x - spriteCentre1.x);
    NSLog(@"Y %f",spriteCentre1.y - spriteCentre2.y);
    float xAxisDiff = spriteCentre1.x - spriteCentre2.x;
    float yAxisDiff = spriteCentre1.y - spriteCentre2.y;

    NSLog(@"x axis %f",xAxisDiff);
    NSLog(@"y axis %f",yAxisDiff);

    if((spriteCentre1.x < spriteCentre2.x) && (xAxisDiff < yAxisDiff))  
    {
        NSLog(@"right touch");
    }
    else if((spriteCentre2.x < spriteCentre1.x) && (xAxisDiff > yAxisDiff)) 
    {
        NSLog(@"left touch");
    }
    else if((spriteCentre2.y < spriteCentre1.y) && (yAxisDiff > xAxisDiff))
    {
        NSLog(@"Bottom touch");
    }
    else if((spriteCentre1.y < spriteCentre2.y) && (yAxisDiff < xAxisDiff))
    {
        NSLog(@"Top touch");
    }

}

Upvotes: 0

godzilla
godzilla

Reputation: 3125

yes good point, the solution was a little messy but it worked, this is what i did: I subclassed CCSprite and created a new attribute called state saving the current position if rotated relative to that rotation. Depending on what side was touched i would use that information to determine which side of the rectangle was touched. –

if((spriteCentre1.x < spriteCentre2.x) && (xAxisDiff < yAxisDiff))  
    {
    [HelloWorldLayer whichSideTouched:sprite1 sideTouched:kStateRightPlus touchingSprite:sprite2];
}

+(int)whichSideTouched:(SpriteCard *)sprite sideTouched:(int)touched touchingSprite:(SpriteCard *)sprite2 {

switch (sprite.state) { case kStateUpPlus: case kStateUpMinus: if(touched == kStateDownPlus) { NSLog(@ "touched up "); [sprite setTop:sprite2]; retValue = kStateUpPlus; } else if(touched == kStateRightPlus) { NSLog(@ "touch left"); [sprite setRight:sprite2]; retValue = kStateLeftPlus;
} }

Upvotes: 0

iDhaval
iDhaval

Reputation: 7844

You can simply use following code

if (CGRectIntersectsRect(Rect1, Rect2)) 
{
     //Your code ...                
}   

USING above CGRectIntersectsRect you can detect the collision pixels within a rectangular area.

Upvotes: 2

Related Questions