0

For my collision detection I need to check of the ball rect intersects any of the wall rects. Right now I have it working but its checking to see if the ball's position is at one of the tile's GID, using this.

-(void) checkHits:(CGPoint)position {
    CGPoint tileCoord = [self tileCoordForPosition:position];
    int tileGid = [levelLayer tileGIDAt:tileCoord];
    //NSLog(@"%g",tileRect.origin);
    if (tileGid) {
        NSDictionary *properties = [level propertiesForGID:tileGid];
        if (properties) {
            NSString *collision = [properties valueForKey:@"break"];
            if (collision && [collision compare:@"True"] == NSOrderedSame) {
                //for blocks
                //[[SimpleAudioEngine sharedEngine] playEffect:@"hit.caf"];
                [levelLayer removeTileAt:tileCoord];
                velocity.y *= -1;
            }
            if (collision && [collision compare:@"False"] == NSOrderedSame) {
                //for edges 
                //[[SimpleAudioEngine sharedEngine] playEffect:@"hit.caf"];
                velocity.y *= -1;
            }      
        }    
    }
}

I need to know how to change this to checking to see if the balls rect/boundingbox intersects with any of the tiles rects/boundingboxex(and how to get the tiles rect/boundingbox in the first place) that have the property break. P.S. I'm using the Tiled map editor.

stenger96
  • 224
  • 3
  • 17

1 Answers1

0

Figured this out a little while ago:

    CCSprite *tile;
    for (int i = 0; i < level.contentSize.width/level.tileSize.width; i ++)
        for (int j = 0; j < level.contentSize.height/level.tileSize.height; j ++){
            tile = [levelLayer tileAt:ccp(i,j)];
            if (CGRectIntersectsRect(ball.boundingBox, tile.boundingBox)) {
                //Do whatever...
            }
        }
    }

I recommend having a collide layer to make map making easier.

stenger96
  • 224
  • 3
  • 17