Archived

This topic is now archived and is closed to further replies.

Bullets

This topic is 5505 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

Hello, I''ve been thinking about how to keep track of lots of bullets in a shooting game (2D or 3D), would it be best to create an array of bullets and define a maximum (a value greater then the number of bullets that could ever be reached) and just sorting through the array each game loop, taking out the ones that collided after you test them all for collisions first of course. Or is there some other, better way to keep track of them? slip

Share this post


Link to post
Share on other sites
IF you want to have "higher numbers", use a "list of arrays".

Put maybe 1000 bullets into every array. You can dynamically add arrays if needed, as well as clean up old arrays.

I consider this "compromise" better than a static list that MAY be overfilled.

Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

Share this post


Link to post
Share on other sites
I assume the language is C++

std::vector or std::list are probably the best solutions, but if you''re a beginner programmer and want to do something with what you know, just go with a large array. Even as much as 5000 bullets in an array probably won''t take more than, say, 100 kB of RAM.

The benefit of std::vector and std::list is that they can grow, so there''s no need to specify a maximum. And lists have constant time (=very fast) insertion and deletion from the middle of list. But you can fake constant time deletion with vectors too, if you swap the last and the removable element first and then delete the last element. Vectors have the benefit of a simpler structure and random access.

Share this post


Link to post
Share on other sites
I agree that a simple linked list is the way to go. Every time you create a bullet, just add it to the list, O(1). Every time a bullet dies, just remove it O(1). And you''ve got not limit to size other than the physical RAM limit. Cant get much better than that.

-Pseudo

Share this post


Link to post
Share on other sites
Bäh. Lets see.

Linkes list:
(a) memory overhead (might be small)
(b) destroying memory coherency. Means: every object might be on a spearate page. BAD.

Possible if you use your own allocator. Otherwise - not too good.


Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

Share this post


Link to post
Share on other sites
thanks guys
I''ve written a small program that uses dynamic arrays already, I just wanted to know if there was a better way to do it
=)
slip

Share this post


Link to post
Share on other sites