Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Removing elements from vector

Name: Anonymous 2011-12-05 9:25

Hi there /prog/

I have a vector that looks like:
<code>
std::vector<Bullet> bullets;
</code>

My bullet struct contains the bullet ID, pixel data, and position.
I set the ID to bullets.size(); on creation.

Whenever a bullet is outside of the camera view, I call: (obviously inside of a loop)
<code>
bullets.erase(bullets.begin() + bullets[bi].ID);
</code>

This works pretty well, unless I spam bullets on the screen.  After a few bullets leave the screen, they all disappear.

What would be the best way to handle cleaning up my bullets?

Name: Anonymous 2011-12-05 9:26

Well, shit.  I meant to use [ code ] tags instead of <code>
I certainly feel silly.

Name: Anonymous 2011-12-05 9:38

well
vector are good for
     Accessing individual elements by their position index (constant time).
    Iterating over the elements in any order (linear time).
    Add and remove elements from its end (constant amortized time).
So if the bullets are not removed from the end it might just be slow

Name: Anonymous 2011-12-05 9:46

consider limiting the number of bullets and storing them in a
statically allocated structure to minimize memory overhead

post some moar code on pastebin so we can laugh at/criticise you
maybe some of it will be constructive

Name: Anonymous 2011-12-05 9:51

>>3
What would be a better method for handling my bullet clean up?  Initially I assumed you meant something like this:

bullets.erase(bullets.end() - bullets[bi].ID);


but obviously that's essentially the same thing.

I was thinking about using an std::map<int, Bullet> object, and keep adding one to the ID each time instead of setting the ID to bullets.size();

Would that be an efficient method?

Name: Anonymous 2011-12-05 9:58

bullets.erase(bullets.begin() + bullets[bi].ID);
doesn't this imply that the bullet.ID == bi
are you sure you can guarantee that when spamming
bullets.begin()+ bi might be better
if this loop looks like
for(vector<Bullet>::iterator bi;bi!=bullets.end();++bi)
{
if(something)
bullets.erase(bi);
}

Name: Anonymous 2011-12-05 10:01

>>5
it implies that bullets.push_back and bullets.pop_back are really fast lol
you are probably interested in linear iteration time and fast removal so map might be good if you have unique ids, lists if if don't have ids
>>6
should be vecotr<Bullet>::iterotor bi=bullets.begin()

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2011-12-05 10:04

OOP bullets?  SO when you fire a bullet you create an object, with all the properties of the bullet, like speed/weight, which bullet factory and gun was used on it, maximum firing range,etc. You may need an extra layer of OOP handling to safely initiliaze,check and fire each bullet produced by BulletFactory,after awhile you query BULLETSQL databases(optimized for bullets, no bloat), scaling bullet production into extra servers and resynergizing the BulletCloud using latest metatemplates from stackoverflow to produce bullets at ludicrous speed.

Name: Anonymous 2011-12-05 10:07

Read http://gamesfromwithin.com/data-oriented-design and make your own optimized bullet container.

Name: Anonymous 2011-12-05 10:12

>>9
>>8
No silver bullets, bo exceptions

Name: Anonymous 2011-12-05 10:14

>>8
10/10
Would rage again.

Name: Anonymous 2011-12-05 10:14

>>10
Use an optimized reference container, it would have transformed your post's references into ``>>8,9''.

Name: Anonymous 2011-12-05 11:32

>>8

Or you could just


class Bullet {
unsigned short mass
unsigned short volume
Bullet();
~Bullet()
};


And not be a little bitch about it. OOP isn't about how many features you can use and abuse. Use what you need, be more abstract when making libraries, don't be a bitch.

Name: Anonymous 2011-12-05 13:04

>>13
Bullet();
~Bullet()


Kill yourself you little shitstain.

Name: Anonymous 2011-12-05 13:34

Applying OOP to bullets kinda brings OOP back to its 40 year old roots in simulations.

Name: Anonymous 2011-12-05 14:39

ITT:OOP 101
you can't into OOP if you never made your own silver bullet

Name: Anonymous 2011-12-05 15:22

>>1
The problem I see with using the ID value that you are, is that if I add 2 bullets to the list (IDs 0, and 1 respectively), and then the first bullet goes off screen / gets erased, then my list now has 1 single bullet with ID 2.

Are you updating each bullet's ID to its new position after you erase any others?

Name: Anonymous 2011-12-05 16:29

It seems you are trying to do things in unnecessarily complicated way.

Why do you need to store some array position into the bullet? If you are iterating over the vector, you will know anyway the index of the current bullet, and as >>17 pointed out, the indices will be invalid after you delete some element from the middle.

Anyway, I'd suggest using std::remove for removing your bullets. You can use it like this:

std::vector<Bullet>::iterator last = std::remove(bullets.begin(), bullets.end(), bullet_not_visible);
bullets.erase(last, bullets.end());

This is much more efficient than using bullets.erase for single bullets if you need to remove many bullets.

Name: Anonymous 2011-12-05 16:29

what happens if you use magic numbers in your bullet handling code?

http://en.wikipedia.org/wiki/Magic_Bullet_%28appliance%29

Don't change these.
Name: Email:
Entire Thread Thread List