I have made a game that runs poorly. I suspect that the way that I programmed the collision detection in the game loop could be the problem.
My game has two player-controlled tanks which fire missiles; there are also power-ups, "gifts", that tanks can collect from the field. At every iteration of my game loop, I detect collisions like this:
- Loop through all the missiles on the screen, and update their location (move them).
- Loop through all of tank 1's missiles, and for every one check if collides with tank 2.
- Loop through all of tank 2's missiles, and for every one check if collide with tank 1.
- Loop through all the missiles on the screen, and check if collide with screen boundaries.
- Check if the two tanks collide with each other.
- Loop through all of the four "gifts" on the screen, and check if they touch a tank.
This method is presumably inefficient. How can I implement collision detection for all of these objects in a better fashion?