Jump to content
  • Advertisement
Sign in to follow this  
-Aurora-

2D game library for simulating thousand small moving objects (with collision detection)

This topic is 2353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
I'm searching for a high-performance game library (graphic library with collision detection). I would like to program a game, where you can place a bacteria with a left click of the mouse. If such a bacteria "meets" another bacteria, they gave birth to a new bacteria. So, logically, there will be a huge number of bacterias in a short amount of time (I plan to set a limit around 1000 - 2000 bacterias).
I tried to program it with C++, using the Clanlib library, but it wasn't really fast enough for this. The framerate actually dropped already with around 80 moving bacterias. Maybe because I chose the wrong way to do this- I created an array of sprites and each of these sprites checked for a collision with another sprite.. which isn't really a fast way for collision detection, but I couldn't think of a better way.

So my question is: What library and which programing language would be the best choice for this game? I also looking for something not too complicated to understand, since I'm still a beginner in programing.

For those which might be wondering what I want with those bacterias: In the game you could place the next higher lifeform as soon as there are enough bacterias around. This next lifeform will feed on bacterias and otherwise will work excactly like the bacterias (regarding reproduction and movements).
Both bacterias and higher lifeforms can reach a certain age: After reaching this age, they will die and disappear. The goal of the game is to get the perfect balance of bacterias and higher lifeforms. In a next stage of the game, there might be also some levels, in which a certain goal need to be reached- to make the game more interesting.

Share this post


Link to post
Share on other sites
Advertisement
Sounds to me like your bottleneck is collision detection (though I don't know how Clanlib does graphics). Might be best to try a physics engine like Box2D for collision detection. Testing each object against every other object adds up very, very quickly. sum(1:n-1) quickly. So at 80 objects you are testing 3160 collisions every frame. A physics engine (like Box2D) should be able to split your space into regions so that cells only check against other cells in the same region. Assuming for a moment that your 80 cells are evenly split into 4 regions, that already reduces your collision checks per frame to 760.

Share this post


Link to post
Share on other sites
Thanks, sounds like a better idea indeed, but as far as I know, Box2D is only a physic library? I'm not quite sure yet how to connect a graphic library with a physic library... are there any good tutorials around? I already had a look at the provided examples of Box2D - the "Testbed". But I'm a little bit confused by it, because it contains a lot of things which I don't need. I would prefer to have a look at a simple collision detection example to understand the libraries better.
This algorithm probably will work perfectly for 80 bacterias- will it work also for around 2000?

Share this post


Link to post
Share on other sites
If you're mainly interested in collision detection, use the quad tree structure, http://en.wikipedia.org/wiki/Quad_tree. I've used it in a similar scenario (C++, simulating 1000-2000 individual warriors fighting each other) on the iOS and it works reasonably well, I estimate that collision detection takes maybe a third of the CPU.

Share this post


Link to post
Share on other sites
So, as far as I understand the quadtree, you divide the gamefield into smaller sectors and this allows you to make a only collision detection with the elements which are located in one sector. Right?
I'm not sure if I'm yet good enough to implement this, though I can try it. Although it looks like Box2D can do exactly that, as per Zael's post?


Edit:
I think I managed it smile.png
I used, like suggested by Zael, Box2D for collision detection and SFML for rendering the graphics. I'm still getting a stack overflow with more than 1700 objects, but everthing below that works perfectly fine. Thanks again for the help!

Edit 2:
The stack overflow is caused due to a too big array. I switched over to a pointer, which solved the problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!