That's right, I'm back for more . So I started working on the collision detection for my space invaders clone and it's, errr.....sort of working. I don't have the code on me now (at work currently) but this is roughly what it looks like out of memory...
Code: Select all
player::checkcollision();
{
for(int counter = 0; counter < 10; counter++)
{
if(pBullets.bDead == 1) //only check for active bullets
{
for(counterY = 0; counterY < 5; counterY++)
{
for(counterX = 0; counterX < 8; counterX++)
{
if(pbullets.bRect.y > (blocksRects[counterY][counterX].y + BLOCK_HEIGHT)
&& pbullets.bRect.y < (blocksRects[counterY][counterX].y - BULLET_HEIGHT)
&& pbullets.bRect.x > (blocksRects[counterY][counterX].x + BLOCK_WIDTH)
&& pbullets.bRect.x < (blocksRects[counterY][counterX].x + BULLET_WIDTH)
&& blocksAlive[counterY][counterX] == 1
&& pBullets.bDead == 1)
{
pBullets.bDead = 0; //set the bullet to dead
blocksAlive[counterY][counterX] = 0; // set the block to dead
break; //break the loop
}
}
}
}
}
}
1. Bullet travels through the first 4 blocks in the column (5 blocks in a column)
2. Collision detection takes effect at the 5th block. Seems to be the right location, but of course it just ignored the first 4 blocks on its way there.
3. 5th block disappears. Bullet disappears.
4. 2nd shot fired, collision takes effect at the exact same location (5th block).
5. 2nd block in the column disappears? (wtf?)
I will post the real code on here when I get home. Any ideas on what I've shown so far? BTW I am calling this detection loop every frame.