Jump to content

c++ SDL pixel collision

- - - - -

  • Please log in to reply
No replies to this topic

#1
atod

atod

    Newbie

  • Members
  • PipPip
  • 13 posts
Hi i want to make a pixel perfect collision but sdl's working against me! :p

I have a function before this one that checks if the entities boxes collide so it doesent do pixelcollision all the time.
My problem is that the game compile and run but once i collide the game crashes and i go in to debug mode where i see my variables r, g, b, a and they have the value 0 for some strange reason. I am quite sure my calculations of getting the "collide overlaping rectangle" are correct its something weird about getting the pixel i think.
My pictures are png.

here is me pixel collision function:
bool EntityData::getCollisionOverlap(const SDL_Rect& boxA, const SDL_Rect& boxB, const EntityData& entA, const EntityData& entB)
{
	int aLeft = entA._src.x;
	int aRight = entA._src.x + entA._src.w;
	int aTop = entA._src.y;
	int aBottom = entA._src.y + entA._src.h;
	
	int bLeft = entB._src.x;
	int bRight = entB._src.x + entB._src.w;
	int bTop = entB._src.y;
	int bBottom = entB._src.y + entB._src.h;

	int aOverLeft = 0;
	int aOverRight = 0;
	int aOverTop = 0;
	int aOverBottom = 0;

	int bOverLeft = 0;
	int bOverRight = 0;
	int bOverTop = 0;
	int bOverBottom = 0;

	//Left
	if(boxA.x < boxB.x)
		aOverLeft = boxB.x - boxA.x;

	if(boxA.x > boxB.x)
		bOverLeft = boxA.x - boxB.x;

	aLeft += aOverLeft;
	bLeft += bOverLeft;

	//Right
	if(boxA.x + boxA.w > boxB.x + boxB.w)
		aOverRight = (boxA.x + boxA.w) - (boxB.x + boxB.w);

	if(boxA.x + boxA.w < boxB.x + boxB.w)
		bOverRight = (boxB.x + boxB.w) - (boxA.x + boxA.w);

	aRight += aOverRight;
	bRight += bOverRight;

	//Top
	if(boxA.y < boxB.y)
		aOverTop = boxB.y - boxA.y;

	if(boxA.y > boxB.y)
		bOverTop = boxA.y - boxB.y;

	aTop += aOverTop;
	bTop += bOverTop;

	//Bottom
	if(boxA.y + boxA.h > boxB.y + boxB.h)
		aOverBottom = (boxA.y + boxA.h) - (boxB.y + boxB.h);

	if(boxA.y + boxA.h < boxB.y + boxB.h)
		bOverBottom = (boxB.y + boxB.h) - (boxA.y + boxA.h);

	aBottom += aOverBottom;
	bBottom += bOverBottom;

	//std::cout << "aRight: " << aRight << std::endl;
	std::cout << "aTop: " << aTop << std::endl;
	//std::cout << "aBottom: " << aBottom << std::endl;
	std::cout << "entA._pic->w: " << entA._pic->w << std::endl;

	SDL_Surface* surfaceA = entA._pic;
	SDL_Surface* surfaceB = entB._pic;
	int y = 0;
	for(int x = 0; aTop + y < aBottom; x++)
	{		
		Uint32 pixel = getPixel(surfaceA, aLeft+x, aTop+y);
		Uint8 r = 0;
		Uint8 g = 0;
		Uint8 b = 0;
		Uint8 a = 0;

		std::cout << "pixel: " << pixel << std::endl;
		SDL_GetRGBA(pixel, surfaceA->format, &r, &g, &b, &a);
		
		
		if(a == SDL_ALPHA_OPAQUE)
		{
			pixel = getPixel(surfaceB, bLeft+x, bTop+y);
			SDL_GetRGBA(pixel, surfaceB->format, &r, &g, &b, &a);
			if(a == SDL_ALPHA_OPAQUE)
				return true;
		}		

		if((aLeft + x) == aRight)
		{
			x = 0;
			y++;
		}
	}

	return false;
}

here is getPixel function:
Uint32 EntityData::getPixel(SDL_Surface* surface, int x, int y)
{
	Uint32 *pixels = (Uint32 *)surface->pixels;
	return pixels[ ( y * surface->w ) + x ];
	//return ( y * surface->w ) + x;
}

Why doesent SDL_GetRGBA work for me??

I realise this code might be a little messy but i would really appreciate the help thank you!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users