19 Replies - 7193 Views - Last Post: 11 September 2011 - 03:54 PM
#1
Collision detection based on pixel colors ,good idea?
Posted 21 July 2011 - 07:06 AM
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
Replies To: Collision detection based on pixel colors ,good idea?
#2
Re: Collision detection based on pixel colors ,good idea?
Posted 21 July 2011 - 08:33 AM
Look into AABB collision detection, it is much more appropriate for this particular project and relatively simple to implement.
Good luck!
#3
Re: Collision detection based on pixel colors ,good idea?
Posted 22 July 2011 - 02:21 AM
stayscrisp, on 21 July 2011 - 08:33 AM, said:
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
#4
Re: Collision detection based on pixel colors ,good idea?
Posted 24 July 2011 - 05:43 AM
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
#5
Re: Collision detection based on pixel colors ,good idea?
Posted 24 July 2011 - 09:04 AM
#6
Re: Collision detection based on pixel colors ,good idea?
Posted 24 July 2011 - 10:22 AM
gamazone, on 24 July 2011 - 01:43 PM, said:
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?
#7
Re: Collision detection based on pixel colors ,good idea?
Posted 24 July 2011 - 11:07 PM
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..
#8
Re: Collision detection based on pixel colors ,good idea?
Posted 25 July 2011 - 05:09 AM
gamazone, on 24 July 2011 - 11:07 PM, said:
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
#9
Re: Collision detection based on pixel colors ,good idea?
Posted 25 July 2011 - 06:41 AM
This post has been edited by ButchDean: 25 July 2011 - 08:15 AM
Reason for edit:: I'm insane.
#10
Re: Collision detection based on pixel colors ,good idea?
Posted 26 July 2011 - 05:09 AM
ButchDean, on 25 July 2011 - 06:41 AM, said:
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.
#11
Re: Collision detection based on pixel colors ,good idea?
Posted 26 July 2011 - 05:40 AM
#12
Re: Collision detection based on pixel colors ,good idea?
Posted 26 July 2011 - 05:48 AM
#13
Re: Collision detection based on pixel colors ,good idea?
Posted 29 July 2011 - 06:51 AM
#14
Re: Collision detection based on pixel colors ,good idea?
Posted 10 September 2011 - 02:22 PM
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
#15
Re: Collision detection based on pixel colors ,good idea?
Posted 11 September 2011 - 11:25 AM
Thanks for your post, i am done with Collision detection part , its working fine, its simple but lengthy.
|
|

New Topic/Question
Reply



MultiQuote






|