Collision detection based on pixel colors ,good idea?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 11510 Views - Last Post: 11 September 2011 - 03:54 PM Rate Topic: -----

#1 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Collision detection based on pixel colors ,good idea?

Posted 21 July 2011 - 07:06 AM

Hi All,

I am developing tetris clone, Using SDL. I need to detect collision between the different objects.
For this i have planned to do it based on colors of pixels , scenario is as follows:-

The object descending , and i simply need to check if the next place to which it will be moved is already
occupied or not. My all objects are of green color, and initially the surface is of black color.

So before moving an object(Sprite) to next area of the surface, i check whether any of the pixel of that area is green colored or not, if yes, that means already an object is present there. if not that means i can move my object on that area.

This approach seems fine to me,but is it reliable approach?? and i can easily get the color of pixel as given here
http://lazyfoo.net/S...son31/index.php , but i am not able to get the desired result.

Please assist on this.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Collision detection based on pixel colors ,good idea?

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Collision detection based on pixel colors ,good idea?

Posted 21 July 2011 - 08:33 AM

Nope, don't do this. It is a real performance hog and this can be done much simpler. Also why wouldn't you use something as simple as bounding box collision considering your tetris blocks are all going to be square.

Look into AABB collision detection, it is much more appropriate for this particular project and relatively simple to implement.

Good luck! :)
Was This Post Helpful? 0
  • +
  • -

#3 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: Collision detection based on pixel colors ,good idea?

Posted 22 July 2011 - 02:21 AM

View Poststayscrisp, on 21 July 2011 - 08:33 AM, said:

Nope, don't do this. It is a real performance hog and this can be done much simpler. Also why wouldn't you use something as simple as bounding box collision considering your tetris blocks are all going to be square.

Look into AABB collision detection, it is much more appropriate for this particular project and relatively simple to implement.

Good luck! :)

OK!! i will give it a try and get back to you... Thanx
Was This Post Helpful? 0
  • +
  • -

#4 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: Collision detection based on pixel colors ,good idea?

Posted 24 July 2011 - 05:43 AM

Hi,

Right now i am detecting collision based on pixel color . But i am not getting desired results:

1] when i read the pixel , its returning 0, even though its green.

2]Also,I am reading the different four pixels one after the other Consecutively

3] Is reading the SDL surface again and again is problematic?



Please find the code chunk below:-
This is the condition for Collision Detection for sprite horizontal I(four blocks)
if((getpixel(destrect.x+5,destrect.y+destrect.h+4)==0))// only this part works correctly.
	   {	
	      if((getpixel(destrect.x+15,destrect.y+destrect.h+4)==0))
		  {		
             if((getpixel(destrect.x+25,destrect.y+destrect.h+4)==0))
			 {
			    if((getpixel(destrect.x+35,destrect.y+destrect.h+4)==0))
				{ return;
				}
			 }
		  }
	    }

	   collide=true;   
	//  count=0;
	   break;
}
//getpixel is defined as:-
Uint32 sprite::getpixel(int x,int y)
{
    int bpp = screen->format->BytesPerPixel;
   // Here p is the address to the pixel we want to retrieve 
	SDL_LockSurface(screen);
   Uint8 *p = (Uint8 *)screen->pixels + y* screen->pitch + destrect.x * bpp;

	


    switch(bpp) {
    case 1:
        return *p;

    case 2:
        return *(Uint16 *)p;

    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
            return p[0] << 16 | p[1] << 8 | p[2];
        else
            return p[0] | p[1] << 8 | p[2] << 16;

    case 4:
        //return *(Uint32 *)p;
			SDL_UnlockSurface(screen);
			return *(Uint32 *)p;

	//return pixels[y* screen->w + destrect.x];

    default:
        return 0;  //     /* shouldn't happen, but avoids warnings 
    }
}





Please assist on this

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Collision detection based on pixel colors ,good idea?

Posted 24 July 2011 - 09:04 AM

Hmm, should I use my invisibility for good or for evil?...
Was This Post Helpful? 2
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Collision detection based on pixel colors ,good idea?

Posted 24 July 2011 - 10:22 AM

View Postgamazone, on 24 July 2011 - 01:43 PM, said:

Hi,

Right now i am detecting collision based on pixel color . But i am not getting desired results:

1] when i read the pixel , its returning 0, even though its green.

2]Also,I am reading the different four pixels one after the other Consecutively

3] Is reading the SDL surface again and again is problematic?



Please find the code chunk below:-
This is the condition for Collision Detection for sprite horizontal I(four blocks)
if((getpixel(destrect.x+5,destrect.y+destrect.h+4)==0))// only this part works correctly.
	   {	
	      if((getpixel(destrect.x+15,destrect.y+destrect.h+4)==0))
		  {		
             if((getpixel(destrect.x+25,destrect.y+destrect.h+4)==0))
			 {
			    if((getpixel(destrect.x+35,destrect.y+destrect.h+4)==0))
				{ return;
				}
			 }
		  }
	    }

	   collide=true;   
	//  count=0;
	   break;
}
//getpixel is defined as:-
Uint32 sprite::getpixel(int x,int y)
{
    int bpp = screen->format->BytesPerPixel;
   // Here p is the address to the pixel we want to retrieve 
	SDL_LockSurface(screen);
   Uint8 *p = (Uint8 *)screen->pixels + y* screen->pitch + destrect.x * bpp;

	


    switch(bpp) {
    case 1:
        return *p;

    case 2:
        return *(Uint16 *)p;

    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
            return p[0] << 16 | p[1] << 8 | p[2];
        else
            return p[0] | p[1] << 8 | p[2] << 16;

    case 4:
        //return *(Uint32 *)p;
			SDL_UnlockSurface(screen);
			return *(Uint32 *)p;

	//return pixels[y* screen->w + destrect.x];

    default:
        return 0;  //     /* shouldn't happen, but avoids warnings 
    }
}





Please assist on this

Thanks

Have you even tried what stayscrisp suggested? :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#7 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: Collision detection based on pixel colors ,good idea?

Posted 24 July 2011 - 11:07 PM

Hi,

not yet...actually my this code had a bug..now i found the bug... and its working fine , it will be slow as suggested by you guys.... and how much slow ?? this i wan't to figure out actually..as for implementing AABB i need to track every sprites coords..so i think my way is simple!!

and about the suggestion of staycrisp, i come to know its even better to implement the game using
grid based collision detection!!just don't know what it is !! will google it,

any ways , thanks you guys for your attention..
Was This Post Helpful? 0
  • +
  • -

#8 hype261  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 29
  • Joined: 02-March 11

Re: Collision detection based on pixel colors ,good idea?

Posted 25 July 2011 - 05:09 AM

View Postgamazone, on 24 July 2011 - 11:07 PM, said:

and about the suggestion of staycrisp, i come to know its even better to implement the game using
grid based collision detection!!just don't know what it is !! will google it,


Grid based collision detection is very simple method of doing collision detection which might suite your game perfectly. Basically all for your level you have a 2D array representing the game world. As your objects move they update this array with which elements are currently occupied. For a square you would do something like this...

occupied[x][y] = true;
occupied[x + 1][y] = true;
occupied[x][y + 1] = true;
occupied[x + 1][y + 1] = true;



When you move your object it will have to test its next position is clear prior to moving below and the move and then clear the elements of the array for its previous position. I have used this method not for collision detection, but for influence mapping, but in my case I was using a char instead of a bool as my data type and then doing bit shifting to mark the elements appropriatly. This allowed me to store 8 different pieces of information in only 1 bytes worth of data.

This post has been edited by hype261: 25 July 2011 - 05:14 AM

Was This Post Helpful? 1
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Collision detection based on pixel colors ,good idea?

Posted 25 July 2011 - 06:41 AM

I read something else, it is a good idea!

This post has been edited by ButchDean: 25 July 2011 - 08:15 AM
Reason for edit:: I'm insane.

Was This Post Helpful? 0
  • +
  • -

#10 hype261  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 29
  • Joined: 02-March 11

Re: Collision detection based on pixel colors ,good idea?

Posted 26 July 2011 - 05:09 AM

View PostButchDean, on 25 July 2011 - 06:41 AM, said:

I read something else, it is a good idea!


Are you actually saying that collision detection by colors is a good thing??? There are a bunch of problems with doing this method. First it doesn't scale well. For a simple game and I mean really simple this might work, but for any game that has actions taking place off screen it won't work because you aren't going to render them. Secondly you are going to have to keep a copy of your back buffer in video ram to prevent stalling the GPU while you do all your texture look ups. Thirdly, if two objects in the game world happen to share the same color then they won't collide at all. There are much easier and more reliable methods to doing collision detection.
Was This Post Helpful? 0
  • +
  • -

#11 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Collision detection based on pixel colors ,good idea?

Posted 26 July 2011 - 05:40 AM

@hype261 I am pretty sure meant that your idea was the good one :) Which is true, tile based collision is probably the most reliable solution.
Was This Post Helpful? 1
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Collision detection based on pixel colors ,good idea?

Posted 26 July 2011 - 05:48 AM

View Poststayscrisp, on 26 July 2011 - 01:40 PM, said:

@hype261 I am pretty sure meant that your idea was the good one :) Which is true, tile based collision is probably the most reliable solution.

Exactly, stayscrisp. :)
Was This Post Helpful? 0
  • +
  • -

#13 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: Collision detection based on pixel colors ,good idea?

Posted 29 July 2011 - 06:51 AM

@hye261... My this method for collision detection(CD) was strictly meant for tetris game, not for other games,that would be insane. Also thanx for the description you have added for grid based method, its really useful as i just another way of implementing(CD) is clicked in my mind!! :) :smile2:
Was This Post Helpful? 0
  • +
  • -

#14 dazpants1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-September 11

Re: Collision detection based on pixel colors ,good idea?

Posted 10 September 2011 - 02:22 PM

Hi,

It looks like you're trying to do the same as i tried on one of my first games.

If i understand you correctly then you want to do collision detection by tracking what color a pixel is on the screen and use this to determine if there has been a collision.

After searching around a lot, i did find something that worked for me.

Fist of all i needed to get a copy of the screen -

if (isfirstrun)
{
backBufferData.Dispose(); 
backBufferData = new ResolveTexture2D(graphics.GraphicsDevice, graphics.GraphicsDevice.PresentationParameters.BackBufferWidth, graphics.GraphicsDevice.PresentationParameters.BackBufferHeight, 1, graphics.GraphicsDevice.PresentationParameters.BackBufferFormat);
graphics.GraphicsDevice.ResolveBackBuffer(backBufferData); 
}


Then determine what the pixel colour is at my collision point -

Color selectedColor3;
Color selectedColor4;

Color[] retrievedColor3;
Color[] retrievedColor4;

GetEnemyCorners(enemypos, enemytexture);
backBufferData.GetData<Color>(0, enemytoprightRectangle, retrievedColor3, 0, 1);
backBufferData.GetData<Color>(0, enemybottomrightRectangle, retrievedColor4, 0, 1);
selectedColor3 = retrievedColor3[0];
selectedColor4 = retrievedColor4[0];
if (selectedColor3 == Color.Red || selectedColor4 == Color.Red || EnemyCross(enemypos, enemytexture))
enemypos -= Vector2.Multiply(enemy1horizontalvelocity, (float)gameTime.ElapsedGameTime.TotalSeconds);


private void GetEnemyCorners(Vector2 enemypos, Texture2D enemy1texture)
{
enemytopleftRectangle = new Rectangle((int)enemypos.X + Goldie.Game1.playerenemyoffset, (int)enemypos.Y, 1, 1);
enemytoprightRectangle = new Rectangle((int)enemypos.X + (Goldie.Game1.enemyframeSize.X-Goldie.Game1.goldframeSize.X), (int)enemypos.Y, 1, 1);
enemybottomrightRectangle = new Rectangle((int)enemypos.X + (Goldie.Game1.enemyframeSize.X - Goldie.Game1.goldframeSize.X), (int)enemypos.Y + Goldie.Game1.goldframeSize.Y, 1, 1);
enemybottomleftRectangle = new Rectangle((int)enemypos.X + Goldie.Game1.playerenemyoffset, (int)enemypos.Y + Goldie.Game1.goldframeSize.Y, 1, 1);

retrievedColor3 = new Color[1];
retrievedColor4 = new Color[1];
}


Then there is the actual collision detection -

if (EnemyCollide(player1pos, enemypos, player1texture, enemytexture))
{ 
"what you want to do if collision happens."
}

protected bool EnemyCollide(Vector2 player1pos, Vector2 enemypos, Texture2D player1texture, Texture2D enemytexture)
{
player1rect.X = (int)player1pos.X + Goldie.Game1.playerenemyoffset;
player1rect.Y = (int)player1pos.Y;
player1rect.Width = Goldie.Game1.player1frameSize.X - (Goldie.Game1.playerenemyoffset * 2);
enemyrect.X = (int)enemypos.X+Goldie.Game1.playerenemyoffset;
enemyrect.Y = (int)enemypos.Y;
enemyrect.Width = Goldie.Game1.enemyframeSize.X-(Goldie.Game1.playerenemyoffset*2);
enemyrect.Height = enemytexture.Height;

if (enemyrect.Intersects(player1rect))
return true;
else
return false;
}


Now as i said, i'm not expert but this is the starting point i have used.

It may make more sense if you seen the screen shots of my Goldie game at -

http://www.agressive...ure-gallery.php

Ta

Daz
Was This Post Helpful? 0
  • +
  • -

#15 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: Collision detection based on pixel colors ,good idea?

Posted 11 September 2011 - 11:25 AM

Hi ,

Thanks for your post, i am done with Collision detection part , its working fine, its simple but lengthy.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2