Hi Guys,
I am writhing a game where I have all of the game assets in an std::vector.
But, I am wondering if there is a faster way to do what I am doing.
Essentially, when ever I deal with a particular game asset I get the data from it similar to below
std::vector<ObjectInstance>::iterator it;
for(it=vObject.begin();it<vObject.end();it++)
{
if(szName==it->szName.c_str())
return it->x;
}
So, everything is handled this way, collisions, rendering, positioning, etc...
I am finding (in particular collisions) that this seems to be fairly expensive on the CPU as I am calling the above code (or similar to) possibly several hundred (maybe thousand) times per loop. Whilst it works perfectly in its current form, I am worried that when I get a lot of game assets in the scene (currently only four) that this will be too slow.
For example, I have a function that sorts out collisions and if the object is intersecting with another object the function calls a while loop to place the object back where it should have been before the collision.
So, I did a check. If the object is 256 pixels deep into the other object it takes 0.5 seconds for the loop to be exited.
Normally you wouldn't let an object collide that far into another object. But, the recovery time is quite long.
Some testing points the finger at the Vector routine (as it loops over and over).
Is there a faster way to achieve what I am after (while making the code as flexible as it is)?
Thanks in advance