Collision detection issue

Started by
1 comment, last by Kurama_gamer 15 years, 6 months ago
Hi guys, I am back, after quite a while, since I was away from my programming projects and the forum due school and other stuff, and I am needing help again =D About my 'space-invaders' clone (which I am using C and SDL), I am having trouble with the collision detection between the spaceship bullet and the aliens. I did the tutorial from layfoo and it helped me a lot, but I am not getting how to change it to work for each alien. First of all, I am creating my aliens using arrays, like:

for(int nY = 0; nY < 3; nY++)
{
     for(int nX = 0; nX <8; nX++)
     {
	enemiesAlive[nX][nY] = true;
	apply_surface(nX*108 + alienBox.x, nY*92 + alienBox.y, alien, screen, &clipsAlien[ frame ]);
     }
}
Then, I was trying to create a collision box for each one of the cells of this array, but it is not working, and the way the basic tutorial teaches, it creates a big box involving all the aliens as one. Another thing, I want to know how can I animate an explosion in the place I have hit and after this delete everything and leave an empty space there (how can I delete a sprite in SDL). So, how is the best way for me to do it? Thanks in advance.
Advertisement
Id probably have a seperate enemy class, with a pointer to an SDL surface, a rectangle bounding box, and anything else you need. Then just loop through all your enemies and draw them.

For collision, that is pretty simple, just have a bullet class, and every time the player shoots just create an instance of a bullet, with a certain velocity, and update it every frame. Also every frame, check if it is colliding with any of the enemys, and if so, destroy the enemy (maybe have a Destroy() function in the enemy class), and play an explosion animation.
cclyde, thanks for your reply, and let me explain how my game is working, and what is my issue right now.

First of all, I already have a SDL surface to the 'aliens' layer, and I have a class for the 'gamePlay - state machine'. On this class I have 5 subdivisions with the following tasks:

-'Game::Game()' -> loads my images and sounds on its respective surfaces and                    set some values to stuff like 'alien.x' (alien's initial                   position)-'Game::~Game()' -> free surfaces-'Game::handle_events()' -> handle user input-'Game::logic()' -> set the logic of everything, for example 'if(playerFire)                                                           { bullet.y -= 20; }'-'Game::render()' -> render everything, where I have the rendering of the aliens                    (function cited in the beginning of the topic)


And I also have a bool function to check the collision between the bullet and the aliens, created following the lazyfoo tutorial. In it I have the following algorithm that I implemented trying to make it create individual collision box for each array cell:

for( nY = 0; nY < 3; nY++)	{		for( nX = 0; nX < 8; nX++)		{                        //the 'B' from leftB and B.x is substituted to the alien                        //values in the game class			leftB = B.x * (nX+1);			rightB = B.x * (nX+1) + ALIEN_WIDTH;			topB = B.y * (nY+1);			bottomB = B.y * (nY+1) + ALIEN_HEIGHT;			if(playerFire)			{                                //the 'A' from leftA is substituted to the                                //bullet values in the game class				if(leftA <= rightB && rightA >= leftB && topA <= bottomB)				{					return true;				}			}		}	}


The problem is that it isnt working totally right since it doenst collide anymore with the bottom aliens, but with the middle ones,
and when I set the following code on the 'checkCollision' loop, the numbers for nX and nY it shows doesnt seem to follow an order:
if(playerFire)	{		bullet.y -= 20;		checkCollision(bullet, alienBox);		if(checkCollision(bullet, alienBox))		{			playerFire = false;			bullet.y = 675;			bullet.x = shipx + BULLET_OFFSET;			printf("Collision Detected! \n");			printf("Alien number %d %d \n", nX, nY);		}	}


P.S.: if anyone needs more code or explanation please ask
and sorry for the long reply, but it is hard for me to explain everything on just a few words

This topic is closed to new replies.

Advertisement