Storing Vectors

Hi Everyone,

I'm re-doing a project that I originally constructed in Flash with AS 2.0, this time I'm using C++ and including Box2D.

I just wanted some advice on how any of you would approach this:
The game allows you to move objects around, and later be able to see how you moved them in an almost 'replay' like action.

Basically I'm trying to figure out the best way to store position information for each object. In the flash version I was storing the xy positions of the objects in an array with a new element for each point in time. After a while the read/write from the different arrays really slowed down the game and I figure there must be a better way of doing this.

Since the objects are now all box2D bodies, I can store the position of the body as a vector. I can use an array, but I don't know beforehand how many elements I will need and it's still pretty read/write heavy.

Any ideas?

Thanks.
You should use a std::vector which is like an array but resizeable.
It automatically grows/shrinks if you add/remove stuff from it, and it should keep the same speed all the time.
Ah yes, vectors seem to be the way to go, although I am concerned about the reallocation of vectors that seems to mean that whenever I extend the container, all of the stored vectors will change where they are in the container.

Am I understanding this wrong? Because that seems like a pretty annoying feature that would mean your indexing would always become inaccurate whenever you added anything to the vector.
Objects in a vector will retain their relative position when it's extended. If you have ABC and add D at the end, you'll always end up with ABCD.

If you want to access an object based on an index that never changes, you may want to use a map instead.
Thanks Guys,

I seem to have got the system I wanted working with vectors quite nicely.

Thanks for the help.
Okay, so now I am trying to use std::vector to allow me to dynamically store bodies as they are generated at run time by the user and I don't know how many there will be.

I've read about this but I can't get it to work.

If I don't know how many there will be, how can I name them when they are generated. Surely they need names so I can put them into the vector?

Thanks.
You need to look into dynamic memory management. You don't need to name every object, just create a new object whenever you need to.

Original thread: https://box2d.org/forum/viewtopic.php?t=3543