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.

I'm developing a top down game in cocos2d and I have collision code that checks collisions between enemies and the player so that they don't go through each other. The code seems to work but the problem is that when one of the enemies colides with an enemy or the player then they just freeze and don't continue their movement. Any help would be appreciated.

Collision detection between enemies:

-(void)checkCollisionWithPosition:(CGPoint)position OldPos:(CGPoint)oldPos tileMap:(CCTMXTiledMap *)map andLayer:(CCTMXLayer *)metaLayer andEnemies:(NSArray *)enemies {
CGPoint tileCoord = [self tileCoordForPosition:position withMap:map];
int tileGrid = [metaLayer tileGIDAt:tileCoord];
if (tileGrid) {
    NSDictionary *properties = [map propertiesForGID:tileGrid];
    if (properties) {
        NSString *collision = [properties valueForKey:@"Colidable"];
        if (collision && [collision compare:@"True"] == NSOrderedSame) {
            self.position = oldPos;
            [self changeStateWithState:enemIdle];
            //[self setViewpointCenter:player.position];
            return;
        }
    }
}

for (BasicEnemy *enemy in enemies) {
    if (enemy != self && CGRectIntersectsRect([enemy boundingBox], [self boundingBox]) && CGPointEqualToPoint(_facingDir, [enemy getDirectionInRelationToPlayer:enemy.position])) {
        self.position = oldPos;

        return;
    }
}

self.position = position;
}
share|improve this question
    
How do you handle collisions with walls in this game? I may have an idea for you –  Savlon Feb 21 '13 at 6:39
    
It is in the code up there. I have meta tiles on the map that are placed on walls. I use these to see if the sprite is hitting the wall and set the sprite back to its old position –  Slayter Feb 21 '13 at 14:17
    
Ok cool, personally I don't use that kind of method anymore as I believe it is very glitchy. Another method would be to check the tile the player wants to move to and if it is solid then don't let him move that way... You could use this method with the enemies as well, save the enemies tile coordinates into an array and make those tiles "solid" so that the player cannot move into the occupied tile... There are million ways to skin a cat and this is just one of them :) –  Savlon Feb 21 '13 at 19:50
    
well so far the solid tiles seem ok. It's more of when sprites collide with each other that they get stuck –  Slayter Feb 21 '13 at 20:20
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.