Cocos2d: Testing for collisions, says that the two sprites always intersect

558 views Asked by At

Hello I am making a cocos2d side scroller. I am trying to test for collisions between two sprites. I checked and the rects I am making for the sprites are what they are supposed to be, but it says that the two rects intersect all of the time whether or not they actually do. Here is the code:

-(void)checkForRedEnemyCollisions{
    CGRect playerRect = CGRectMake(player.position.x -     (player.playerSprite.contentSize.width/2),
                                   player.position.y -     (player.playerSprite.contentSize.height/2),
                                   player.playerSprite.contentSize.width,
                                   player.playerSprite.contentSize.height);

    CGRect redEnemyRect = CGRectMake(redEnemy.position.x -     (redEnemy.bulletSprite.contentSize.width / 2) ,
                                     redEnemy.position.y -     (redEnemy.bulletSprite.contentSize.height /2 ),
                                     redEnemy.bulletSprite.contentSize.width,
                                     redEnemy.bulletSprite.contentSize.height);
    if (CGRectIntersectsRect(playerRect, redEnemyRect)) {
        CCLOG(@"collision");
    }
}

Here is more code:

-(id)init{
if((self = [super init])){
    CGSize size = [[CCDirector sharedDirector]winSize];
    screenWidth = size.width;
    screenHeight = size.height;

    gravity = 2;

    playerSprite = [CCSprite spriteWithFile:@"thefinalcharacter.png"];
    playerSprite.scale = 1.5;
    playerSprite.position = ccp(screenWidth/3.4, screenHeight/2);
    [self addChild:playerSprite z:-3];

    [self schedule: @selector(flight:)interval:1.0f/7.0f];

    }
return self;
}

-(void)flight:(ccTime)delta{
flightCounter ++;

if (flightCounter % 2){
    [playerSprite setTexture:[[CCSprite spriteWithFile:@"thefinalcharacter.png"]texture]];
}else{
    [playerSprite setTexture:[[CCSprite    spriteWithFile:@"thefinalcharacter2.png"]texture]];
}
[self schedule:@selector(updatePosition:)interval:1.0f/30.0f];
}

-(void)updatePosition:(ccTime)delta{
if(playerSprite.position.y < 35){
    gravity = 0;
}else if(playerSprite.position.y > screenHeight - 150) {
        playerSprite.position = ccp(playerSprite.position.x, playerSprite.position.y -    100);
}else{
    gravity = 2;
}

playerSprite.position = ccp(playerSprite.position.x, playerSprite.position.y - gravity);
}
1

There are 1 answers

5
Yucel_K On

make sure that you are creating the bounding rectangles in world coordinates.

try to convert the points to world coordinates like the following;

CGPoint playerWorldPosition = [player ConvertToWorldSpace:player.position];
CGRect playerRect = CGRectMake(playerWorldPosition.x -  (player.playerSprite.contentSize.width/2),
                               playerWorldPosition.y -  (player.playerSprite.contentSize.height/2),
                               player.playerSprite.contentSize.width,
                               player.playerSprite.contentSize.height);

do the same for redEnemy. this will make sure you are comparing them in world space but not in another space down the hierarchy of layers.

if you using anchor point instead of the default (0.0,0.0) on your player or redEnemies. use the ConvertToWorldSpaceAR function instead of the ConvertToWorldSpace.