Jump to content
  • Advertisement
Sign in to follow this  

2d pixel perfect collision detection

This topic is 2647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can't find any tutorial describing pixel perfect collisions... I would be very grateful for giving any information or links on how to create pixel perfect 2d collision. At least, essential functions to achieve this would be appreciated ^^

Share this post

Link to post
Share on other sites
I've found a sample code provided by Evil Steve Here but I can't make it work ;/
I want to set true in CollisionMask1 array for these pixels that are opaque in the rectangle of X: 160 - 240 and Y: 80 - 160
bool* CollisionMask1 = new bool[80 *80];

RECT rect;
SetRect(&rect, 160, 80, 240, 160);
// Lock the texture to access the pixel data
Input::Obj().Texture->LockRect(0, &rct, &rect, D3DLOCK_READONLY);
// Process the texture, line by line
for(UINT y=0; y<80; ++y)
DWORD* pBits = (DWORD*)((BYTE*)rct.pBits + y*rct.Pitch);
for(UINT x=0; x<80; ++x)
CollisionMask1[y*80 + x] = (pBits[x]&0xff000000) != 0; // Do whatever here
if(CollisionMask1[3200]) PostQuitMessage(0); //assuming that the sprite's width and height is 80 pixels the centre of it should be 3200 but program doesn't quit

I would be very grateful for any help and any advise would be mostly appreciated

Share this post

Link to post
Share on other sites

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 = new ResolveTexture2D(graphics.GraphicsDevice, graphics.GraphicsDevice.PresentationParameters.BackBufferWidth, graphics.GraphicsDevice.PresentationParameters.BackBufferHeight, 1, graphics.GraphicsDevice.PresentationParameters.BackBufferFormat);

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;
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 -




Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!