vector out of bounds...where?

This is a discussion on vector out of bounds...where? within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, I've been coding a collision detection function for about 12 hours now using DarkGDK, DirectX, and C++. My ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    30

    Question vector out of bounds...where?

    Hey everyone,

    I've been coding a collision detection function for about 12 hours now using DarkGDK, DirectX, and C++.

    My 2D game detects the function call. I know this because the frame rate drops like crazy and I can fix that.

    My issue is in this function here:
    Code:
    bool pixelCollision(int spriteOne, int spriteTwo)
    {
    	int minX = 0;
    	int maxX = 0;
    	int minY = 0;
    	int maxY = 0;
    	vector<int> spriteOneVect;
    	vector<int> spriteTwoVect;
    
    
    	/*---------------------------------------------------------------------------*/
    	//                         Sprite Masking                                    //
    	//                                                                           //
    	/*---------------------------------------------------------------------------*/
           //fill vectors with '0' for non-opaque and '1' for opaque colors from bitmap
    	for(int y = 0; y < dbSpriteHeight(spriteOne); y++)
    	{
    		for(int x = 0; x < dbSpriteWidth(spriteOne); x++)
    		{
    			if(readPixel(dbGetImagePointer(spriteOne),x,y) == D3DCOLOR_XRGB(255,0,255))
    			{
    				spriteOneVect.push_back(0);
    			}
    			else
    			{
    				spriteOneVect.push_back(1);
    			}
    		}
    	}
    	for(int y = 0; y < dbSpriteHeight(spriteTwo); y++)
    	{
    		for(int x = 0; x < dbSpriteWidth(spriteTwo); x++)
    		{
    			if(readPixel(dbGetImagePointer(spriteTwo),x,y) == D3DCOLOR_XRGB(255,0,255))
    			{
    				spriteTwoVect.push_back(0);
    			}
    			else
    			{
    				spriteTwoVect.push_back(1);
    			}
    		}
    	}
    	
        /*---------------------------------------------------------------------------*/
    	//                         Collision Overlap                                 //
    	//                                                                           //
    	/*---------------------------------------------------------------------------*/
    	//find min and max coodinates of collision
    	if(dbSpriteX(spriteOne) <= dbSpriteX(spriteTwo))
    	{
    		minX = dbSpriteX(spriteTwo) - dbSpriteX(spriteOne);
    		maxX = dbSpriteX(spriteOne) + dbSpriteWidth(spriteOne) - dbSpriteX(spriteTwo);
    	}
    	else
    	{
    		minX = dbSpriteX(spriteOne) - dbSpriteX(spriteTwo);
    		maxX = dbSpriteX(spriteTwo) + dbSpriteWidth(spriteTwo) - dbSpriteX(spriteOne);
    	}
    	if(dbSpriteY(spriteOne) <= dbSpriteY(spriteTwo))
    	{
    		minY = dbSpriteY(spriteTwo) - dbSpriteY(spriteOne);
    		maxY = dbSpriteY(spriteOne) + dbSpriteHeight(spriteTwo) - dbSpriteY(spriteTwo);
    		
    	}
    	else
    	{
    		minY = dbSpriteY(spriteOne) - dbSpriteY(spriteTwo);
    		maxY = dbSpriteY(spriteTwo) + dbSpriteHeight(spriteOne) - dbSpriteY(spriteOne);
    	}
    	for(int i = minY; i < maxY; i++)
    	{
    		for(int j = minX; j < maxX; j++)
    		{
    			if(spriteOneVect.size() <= (i*j) && spriteTwoVect.size() <= (i*j))
    			{
    				if(spriteOneVect[i*j] == spriteTwoVect[i*j] && spriteOneVect[i*j] == 1)
    					return true;
    			}
    		}
    	}
    	return false;
    }
    The highlighted code in red never returns true. It is my vector bounds I know, but I don't know where. As you can see, I have set minX,maxX, minY, and maxY values (I have a hunch its in here).

    In case you are wondering, this detects collision between 2 bitmap images, it allows for the opaque colors to be used for detection instead of the non-opaque. This is a solution for pixel perfect collision detection using darkGDK functions.

    If you can help me find my bound errors, i'd be very grateful.

    Please ask questions if you have any, thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if you've flattened a two-dimensional vector into a one-dimensional array, then to index you need to something like [i*number_of_columns + j]. Otherwise I have no idea why you would be looking at that product there.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,990
    I think you mixed it up. It probably should be:
    Code:
    if((i*j) < spriteOneVect.size() && (i*j) < spriteTwoVect.size())
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    @tabstop
    yep, i'm using the vector as a sort of 2D array (I don't know how to allocate memory for the 2D so I used a vector instead). Yeah, my product is probably wrong as well...VERY TIRED X[


    @laserlight
    Well, it detects some sort of collision now

    Unfortunatly, its still off. Collision is detected but in wrong parts of bitmap...the lower half of bitmap is not being detected.

    pic of game so far

    little guy is link :b
    Last edited by dhardin; 04-02-2010 at 12:06 PM. Reason: link broken?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    How about using at at function for debugging out-of-bounds problems?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Quote Originally Posted by Elysia View Post
    How about using at at function for debugging out-of-bounds problems?
    what function?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,990
    Quote Originally Posted by dhardin
    what function?
    std::vector's at() member function, e.g., instead of spriteOneVect[i*j] use spriteOneVect.at(i*j). std::out_of_range will be thrown if an out of bounds condition is detected.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    O.K. i'm confused by my own code. So i'm gonna think out loud to you guys.

    The minX, maxX, minY, and maxY values are supposed to be the coordinates within the whole game where the collision is happening:

    http://i40.tinypic.com/j9x6yu.png

    the 'O' marks are my min and max values. This way it doesn't have to search the whole vector for the collision, just those coordinates.

    This then returns true or false depending on what the coordinates hold (an opaque or non-opaque pixel).


    Thanks for the help so far, i'm just losing myself

    I had this almost working last night somehow...but it only worked when i walked to the right
    Last edited by dhardin; 04-02-2010 at 12:32 PM.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Alright, well I'm no longer getting the vector out of bounds error. I found my new issue to be my lack of knowledge in directX. My initial problem was that the pink in the above image was transparent, so when I sent it to the backbuffer, my vector filled up with 1's (no non-opaque colors).

    I have rewritten some of my code to make it easier to read/follow:

    Code:
    bool pixelCollision(int spriteOne, int spriteTwo)
    {
    	vector<int> spriteOneVect;
    	vector<int> spriteTwoVect;
    	
    
    
    	/*---------------------------------------------------------------------------*/
    	//                         Sprite Masking                                    //
    	//                                                                           //
    	/*---------------------------------------------------------------------------*/
    	for(int y = 0; y < dbSpriteHeight(spriteOne); y++)
    	{
    		for(int x = 0; x < dbSpriteWidth(spriteOne); x++)
    		{
    			if(readPixel(dbGetImagePointer(spriteOne),x,y) == D3DCOLOR_XRGB(255,0,255))
    			{
    				spriteOneVect.push_back(0);
    			}
    			else
    			{
    				spriteOneVect.push_back(1);
    			}
    		}
    	}
    	
    	for(int y = 0; y < dbSpriteHeight(spriteTwo); y++)
    	{
    		for(int x = 0; x < dbSpriteWidth(spriteTwo); x++)
    		{
    			if(readPixel(dbGetImagePointer(spriteTwo),x,y) == D3DCOLOR_XRGB(255,0,255))
    			{
    				spriteTwoVect.push_back(0);
    			}
    			else
    			{
    				spriteTwoVect.push_back(1);
    			}
    		}
    	}
    	
        /*---------------------------------------------------------------------------*/
    	//                         Collision Overlap                                 //
    	//                                                                           //
    	/*---------------------------------------------------------------------------*/
    	//find min and max coodinates of collision
    	int minX;
    	int maxX;
    	int minY;
    	int maxY;
    	if (dbSpriteX(spriteOne) <= dbSpriteX(spriteTwo))
    	{
    		minX = dbSpriteX(spriteTwo);
    	}
    	else
    	{
    		minX = dbSpriteX(spriteOne);
    	}
    	if(dbSpriteX(spriteOne) + dbSpriteWidth(spriteOne) <= dbSpriteX(spriteTwo) + dbSpriteWidth(spriteTwo))
    	{
    		maxX = dbSpriteX(spriteOne) + dbSpriteWidth(spriteOne);
    	}
    	else
    	{
    		maxX = dbSpriteX(spriteTwo) + dbSpriteWidth(spriteTwo);
    	}
    	if(dbSpriteY(spriteOne) <= dbSpriteY(spriteTwo))
    	{
    		minY = dbSpriteY(spriteTwo);
    	}
    	else
    	{
    		minY = dbSpriteY(spriteOne);
    	}
    	if(dbSpriteY(spriteOne) + dbSpriteHeight(spriteOne) <= dbSpriteY(spriteTwo) + dbSpriteHeight(spriteTwo))
    	{
    		maxY = dbSpriteY(spriteOne) + dbSpriteHeight(spriteOne);
    	}
    	else
    	{
    		maxY = dbSpriteY(spriteTwo) + dbSpriteHeight(spriteTwo);
    	}
    	for(int y = minY; y < maxY; y++)
    	{
    		int spriteOneCurrentPixY = y - dbSpriteY(spriteOne);
    		int spriteTwoCurrentPixY = y - dbSpriteY(spriteTwo);
    		for(int x = minX; x < maxX; x++)
    		{
    			int spriteOneCurrentPixX = x - dbSpriteX(spriteOne);
    			int spriteTwoCurrentPixX = x - dbSpriteX(spriteTwo);
    			if((x*y)<spriteOneVect.size() && (x*y)<spriteTwoVect.size();
    			{
    				if(spriteOneVect.at(spriteOneCurrentPixX*spriteOneCurrentPixY) == spriteTwoVect.at(spriteTwoCurrentPixX*spriteTwoCurrentPixY) && 
    					spriteOneVect.at(spriteOneCurrentPixX*spriteOneCurrentPixY) == 1)
    				{
    					return true;
    				}
    			}
    
    		}
    	}
    	return false;
    }



    EDIT:
    This will be a logic error. Inside the 2 loops for adding to the two vectors, i'm not so sure that the colors are being added correctly except for the character animations. THIS IS PROGRESS! So I have to get in touch with darkGDK and see if they can help me figuring out the width of each animation.
    Last edited by dhardin; 04-02-2010 at 09:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Going beyond bounds problem
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 01-03-2009, 09:41 AM
  2. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  3. out of bounds problem
    By chris285 in forum Game Programming
    Replies: 1
    Last Post: 04-26-2005, 09:00 AM
  4. Game matrix bounds checking
    By Panopticon in forum Game Programming
    Replies: 2
    Last Post: 02-04-2003, 09:30 PM
  5. checking array bounds
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-06-2002, 02:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21