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;
}