trouble with a 2d asteroid game

I am currently working on a 2d asteroid game with SDL and I have run into a problem I can't fix. The player controls a ship and shoots lasers. I have a class for the asteroids which move at random speeds and angles, and I have a class for the ship which contains the functions for the lasers and the ship.

I want 6 asteroids on the screen at any given time, and at the moment, when I try to check the collision between the lasers and the asteroids, the lasers will only hit one of the asteroid class objects at a time. There are 3 lasers that can be on the screen at any given time too. When I remove all the instances of the asteroids except for 1, the program appears to run perfectly, when I add more asteroids, only 1 asteroid object is able to collide with the lasers.

here is how I am trying to have the collision work at the moment:

1
2
3
4
5
6
7
8
9
10
for(int i = 0; i<=2; i++)
		{
			if(slave1.fired[i] == true)
			{
				for(int j = 0; j<=5; j++)
				{
					slave1.hit[i]= collision_check(slave1.bbox[i], a[j].box);
				}
			}
		}


collision check returns a bool value to slave1.hit which would reset the laser and draw an explosion.

any suggestions?
Why don't you check for collisions first, the process the collisions, rather than completely processing a collision each time?

You are clearly doing something in your collision processing that's chaninging further checks.

Also, it's conventional for loops to use < rather than <=

For example:
1
2
    int nLasers = 3;
    for (int i  = 0; i < nLasers; ++i)

Thanks, I'll remember to use < from now on. The code I provided earlier is supposed to be the collision checker between 3 lasers and 6 asteroids. If the checker returns true, it causes another function to draw the explosion etc... I've gone through line by line with my debugger and it checks to see if each laser hits each asteroid, yet the program still seems ignore the other asteroids. It seems like the checking code is somehow being skipped over, and that it moves on to other functions before running the test for the other asteroids. I would put a call to the collision checker right after each laser is fired, but I don't know how to use certain instances of class objects declared in main() as arguments to a class function.



I think I may have to go back to learning from books to get a better understanding of how the language works before I can continue.
If anyone is interested, you can download a zipped executable of my asteroid game at

http://www.goldenmolegames.com/asteroid.htm

It is pretty much done. For the time being I am going to go back and start learning better c++ fundamentals before I start working on games again though.
Topic archived. No new replies allowed.