This is probably the crudest collision engine that I ever wrote, but it should work. I would advise anybody experienced with coding to check over my work.

Code:
//have SHIPWIDTH,  SHIPHEIGHT, ENEMYWIDTH, and ENEMYHEIGHT defined somewhere
int index;
for(index = 0;index < NUM_ENEMIES;index++){
    //check if ship is able to be checked for collsion along the y axis
    if(ship.y + SHIPHEIGHT/2 < enemy[index].y - ENEMYHEIGHT/2 &&
       ship.y - SHIPHEIGHT/2 > enemy[index].y + ENEMYHEIGHT/2){
        if(ship.x + SHIPWIDTH/2 > enemy[index].x - ENEMYWIDTH/2){
            ship.x = enemy[index].x - ENEMYWIDTH/2 - SHIPWIDTH/2;
        }
        else if(ship.x - SHIPWIDTH/2 < enemy[index].x + ENEMYWIDTH/2){
            ship.x = enemy[index].x + ENEMYWIDTH/2 + SHIPWIDTH/2;
        }
    }
    //check if ship is able to be checked for collision along the x axis
    if(ship.x + SHIPWIDTH/2 > enemy[index].x - ENEMYWIDTH/2 &&
       ship.x - SHIPWIDTH/2 < enemy[index].x + ENEMYWIDTH/2){
        if(ship.y - SHIPHEIGHT/2 < enemy[index].y + ENEMYWIDTH/2){
            ship.y = enemy[index].y + ENEMYHEIGHT/2 + SHIPHEIGHT/2;
        }
        else if(ship.y + SHIPHEIGHT/2 > enemy[index].y - ENEMYHEIGHT/2){
            ship.y = enemy[index].y - ENEMYHEIGHT/2 - SHIPHEIGHT/2;
        }
    }
}



What this basically does is stop your ship from going through your enemy. Well, that is what I think it does. Play around with the code.

Last edited by Sepiantum; 11/27/09 17:58.