Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am making a game where you fly a ship around some randomly generated planets. Since I am using a for loop to draw over 5000 planets, using the rectangle class or an oval-type class for this is not an option, since creating many objects will severely affect performance. Bitmasking each planet will likely result in performance issues too, so the only candidate is color based collision detection, because I don't need to apply some sort of object to everything I want to check for collisions. Is any way to check the perimeter around the ship for a certain color?

share|improve this question
2  
How do you fit 5000 planets on screen? Is it a 2D or 3D game? In 2D game you would just draw planets on a bitmap, and check what color has a pixel on X,Y coordinates. –  Markus von Broady Oct 12 '12 at 20:21
    
not onscreen all at once.They scroll past the player. it's a 2d game –  user1486826 Oct 12 '12 at 20:33
    
So you don't need to check each planet in a loop. Just check those on the screen. Or even better, use a QuadTree. –  Gustavo Maciel Oct 12 '12 at 20:43
    
I will need some additional data like size of your universe (galaxy? the space you are traveling in) in comparison to size of planets and how you spread them (planetary systems with huge void space between them or just planets evenly distributed) to give you a best fitting advice. –  Markus von Broady Oct 12 '12 at 20:50
    
The universe is infinite, The planets are randomly generated from a seed. As for checking each onscreen planet in a loop, that could be a possible solution, but more difficult and there still could be some performance issues with that. I will look into a QuadTree, but what I am looking for remains color based collision detection. –  user1486826 Oct 12 '12 at 21:15
show 3 more comments

1 Answer

up vote 3 down vote accepted

Color based checking is really slow. It is used rarely these days, and only for some specific problems like when using deformable terrain.

Probably a better approach is to minimize number of comparisons. I suggest you organize your planets in a quadtree first. Trees will quickly limit number of comparisons, because if you know that a planet fits in one quadrant, you immediately know it cannot intersect any planets in other 3 quadrants thus reducing your search space by 75% with a single comparison.

After this, you should do rough collision testing using rectangles because these are quick tests, much faster then circle or oval. Only if the hit is probable, then you do a thorough check, maybe even color or bitmask if you have to. But then the number of comparisons is really low because you have eliminated great many planets by this time.

This problem is basic scene management, and you will be well advised to use a 3rd party library rather then inventing your own.

share|improve this answer
    
I still have no code of a color-based collision/detection system. –  user1486826 Oct 12 '12 at 23:14
    
The code is very specific. But the idea is to calculate common coordinate system for both planets/sprites. Then you loop over all pixels of one image. If it is transparent then skip. If it is a color that satisfies a colision criteria, then you check the same pixel in the other image. If the color is not important, meaning that any color can hit any other color, then a bitmask is easier, meaning you use one bit for each pixel, and then you can check 32 bits at a time by doing a logical AND. You cannot do only edge as this will miss one object being inside another. –  MikiJ Oct 12 '12 at 23:59
    
Color-based collision just works as a grid, where width and height of the cell is 1x1 pixel. Smaller cell means slower but also more precise detection. Keep in mind, though, it's precision is still far from being perfect. If two objects have both parallel vectors to either columns or rows of pixel grid, surface of their collision will always be perpendicular to the vectors. What I mean here, "color collision" is just an unfortunate naming. Similarly one could say he stabilized a teeter by a "book stabilizer" while really it's a "ballast stabilizer" as words don't stabilize. –  Markus von Broady Oct 13 '12 at 9:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.