Currently the way I check for overlap is as follows:
Each game cycle, before entering the star render loop, I first calculate the number of visible asteroids on the screen. This is stored in an array.
Then the star render loop begins. The position of the star on the screen is calculated. If the star is "behind" the asteroids on the Z axis (ie, starZ > 0), and if the CameraZ is less than the star's Z (ie the star is in front of the camera), then the star's rendered coordinates are checked against the positions of all asteroids in the array we just created.
This involves running a new for loop to check the star against each visible asteroid in the array we created earlier.
So if there are 400 stars and 2 asteroids onscreen, 800 iterations will be carried out.
If there are 400 stars and 40 asteroids onscreen, 16000 iterations will be carried out.
It's easy to see why this causes massive framerate drops... carrying out 16000 iterations 60 times per second is a bit insane really.
Because this part of the engine is so computationally expensive, I do some cheap checks first to try to "rule out" the possibility that the star could be overlapping with each asteroid.
First, I check if (StarX - AsteroidX) > AsteroidRadius. If true, then there is no way they could possibly overlapping, and subsequent checks are unnecessary. The vast majority of checks return true at this stage, rendering additional more expensive checks unnecessary. However, some asteroids which are horizontally aligned will return true, in which case we proceed to the second "cheap" check.
The second check is exactly the same, except for the Y axis: if (StarY - AsteroidY) > AsteroidRadius then the star cannot possibly be overlapping with this asteroid. Having checked both of these, we can say that we've checked the "bounding box" of the asteroid.
If we have a bounding box overlap, then there's nothing for it but to do the expensive check where we see if the star _actually_ overlaps with this asteroid. If it does, the star will NOT be drawn.
So that's how it works at the moment. Any ideas for total optimisation ?