Thread: Vector of arrays

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    30

    Vector of arrays

    Hi,

    I have a problem with a game I'm making. I'll try to make this as succinct as possible...

    All the sprites are stored in a vector, and the collision detection is done by rolling through the vector and checking for collisions.

    As well as individual sprites, I have put into the vector a few arrays of sprites too (because some of the sprites are of the same type, like "enemies").

    The collision detection has recently become unpredicatable, only detecting collisions with about 50-60% accuracy.

    Although I have a few suspicions about what could be causing it, my main concern is that a vector of sprites cannot hold both sprites and arrays of sprites - and that is the root of the collision detection problem.

    Cannot anyone tell me whether that is right or wrong?


    Any help is greatly appreciated. Really.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A vector cannot hold arrays by themselves. Maybe you are putting pointers into the vector and somehow remembering which pointers refer to single objects and which refer to an array? An example of your vector declaration and how you add an array to it might make things clearer.

    A better course of action might be to use a vector of vectors. The inner vector can have zero, one or many sprites in it. That way you can use it in the same way regardless of whether it has one or more.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    Well, right now I am just adding arrays to the vector as if they were a pointer to one sprite. The pointers to the sprite arrays are mixed in with the pointers to single sprites.

    So, I should change it to a vector of vectors eh? Sounds like a plan.

    But if I am looping through the vector of vectors and comparing them to see if there have been any collisions, will it automatically know to go through each "internal" vector member in turn?

    When it is rolling through will it be ordered like (please excuse the nonsense syntax):

    1. DogVector(1) -------> (a vector containing one sprite)
    2. CatVector(1)
    3. SheepVector(1) --------> (a vector containing many sprites)
    4. SheepVector(2)
    5. SheepVector(3)
    6. BiscuitVector(1)

    Hopefully that makes some sense, and you know what I am on about.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It won't automatically know to go through each internal vector, but all you have to do is code it that way. Basically, your code should not care whether there is one sprite or many sprites. It should just use a loop (with iterators or indexes) that goes through the entire internal vector looking for collisions. A properly coded loop will work for a single entry or multiple entries (or zero entries).

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    Thanks for the help thus far. If I could encroach a little further on your generosity...

    Here is one of the loops that runs through my original vector of sprites and checks for collisions...

    Code:
    BOOL GameEngine::CheckSpriteCollision(Sprite* pTestSprite) {
    
      // See if the sprite has collided with any other sprites
      vector<Sprite*>::iterator siSprite;
      for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)
      {
        // Make sure not to check for collision with itself
        if (pTestSprite == (*siSprite))
          continue;
    
        // Test the collision
        if (pTestSprite->TestCollision(*siSprite))    //if TestCollision is true...
          // Collision detected
          return SpriteCollision((*siSprite), pTestSprite);
    
      }
    
      // No collision
      return FALSE;
    }
    So, is this an appropriate re-write of the function in order to make it run through a vector of sprite vectors instead and still check every individual sprite for any collisions...?

    Code:
    BOOL GameEngine::CheckSpriteCollision(vector<Sprite*> pTestVector) {
      
    vector <vector <*Sprite>> :: iterator siSpriteVector;
    for (siSpriteVector = m_vVectorSprites.begin(); siSpriteVector != m_vVectorSprites.end();
          siVectorSprites++) {
            
            for (i = 0; i < TestVector.size(); i++) {
              for (j = 0; j < siVectorSprite.size(); j++) {
                
                if (TestVector[i] == siVectorSprite[j]) {
                  continue;
                }
                
                if (TestVector[i]->TestCollision(siVectorSprite[j])) {
                  return SpriteCollision(siVectorSprite[j], TestVector[i]);
                } 
              }
            }
            
            return false;        
    }
    Sadly, I can't successfully compile it without making a lot more changes to my game code. So if anyone could cast a really scrutinizing eye over it and see if they spot any obvious problems, that would be truely fantastic.

    Cheers.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The most obvious problem is that there is a weird syntax rule with nested templates that requires a space between the closing > brackets. It should be
    Code:
    vector <vector <Sprite*> >

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM