#### Archived

This topic is now archived and is closed to further replies.

# Get Pixel Colors...

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

## Recommended Posts

Hey, Well, I am working on an asteroids clone and I have been trying to make my collision detection more exact. I decided to do the pixel perfect way. Now, my only question is when I load an image. How would I get whether a pixel is black or not. Black is my transparent color. I am using DirectX with 16 bit surfaces. Well if anyone could help me out I would appreciate it. Thanks. Later, James

##### Share on other sites
You basically compare the two pixels from both buffers to see if any two pixels (one from each buffer) have colors different than 0.

So say pixel from ship buffer has 0 (black) and pixel from another ship buffer has different than 0 then that''s not a collision.

e.g.

pixels: 0 - 0 -- no collision
0 - not zero -- no collision
not zero - not zero -- collision

so you can test it like this:

  for (y=collisionRect.top; y

data1 and data2 are the pixel buffers which contain the data from the sprites from both objects

collisionRect is the rectangle where the two rectangles have collided...

If you need more help let me know...

##### Share on other sites
Well, the question I am asking is how would I fill the Data1 and Data2 arrays with whether a pixel is black or not. I already know how a pixel perfect collision works. Yet, I don't know how to determine what color a pixel is, of course, all I need to know is if it is black or not. I have my collision code all ready, all I need to figure out how to do is, fill my arrays with the pixel information. Like I said though, I don't know how to determine if a pixel is black or not. Do you understand what I mean? Well if you could help I would appreciate it, thanks.

Later

Edited by - JSCFaith on November 10, 2001 4:02:09 PM

##### Share on other sites
Yor pixel is black if the valew is 0 (0x000000)
EX:
if( Data1[x][y] == 0 )
{
// Black
} else
{
// not
}

##### Share on other sites
How, do you go through the entire image and check each pixel to see if it is black and then put whether it as black in the array? For example, when I load an image, I create an array the size of the image. Then I go through the image, one pixel at a time. How do I check each pixel of the image? That is what I don't understand. When you load an image, you get an LPDIRECTDRAWSURFACE7, now how do I check each pixel of that image?

Edited by - JSCFaith on November 10, 2001 4:11:39 PM

Edited by - JSCFaith on November 10, 2001 4:13:26 PM

##### Share on other sites
Hi,

This is actually more a directx related thing

First you just create a surface as usual. Then you can use the directxsurface-function Lock(), to get a pointer to that surface. Then you just put some values there, and call Unlock() to let Directx know you''re done with the surface. You could do this like this:

DDSURFACEDESC2 ddsd;void *lpSurface;// get a pointerddsd.dwSize = sizeof(DDSURFACEDESC2);if (lpDDSMySurface->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL)!=DD_OK){   // something went wrong...}// put some values in memory*((unsigned short *)lpSurface) = 0xffff; // set the first pixel to white, for example (convert to pointer to short because of 16 bpp mode)// donelpDDSMySurface->Unlock(NULL);[code]Good luck,Nick    `

##### Share on other sites
OK thanks, but now I have one more question. Using the exampe below, how would I get to the next pixel? lpSurface++;? Thats what you do with strings, but I want to make sure. Well if that is the case, does that go to the next x or y? Thanks a lot

Later.

##### Share on other sites
Thanks nickm I was wondering that myself!

##### Share on other sites
Hmm... I''m not sure I understand you... You don''t have to FILL any array...you just calculate the collision rectangle... and then access both sprites'' data buffer using that rectangle and just check of both pixels at the same position in those buffers are not black then a collision has occured...

##### Share on other sites
But when do I fill the buffers for the player and the enemy (or the asteroid)?
In my game I have enemies of different size. Should I create a buffer (UCHAR player_collmap[100][100];, UCHAR enemy1_collmap[100][100];, UCHAR enemy2_collmap[100][100];, ...) for each player/enemy before the game starts?

Because when I create the buffer at runtime (when the collision per boundary boxes occured), it gets too slow (because of the damn GetPixel(); ).

But creating the buffers before the game starts, would mean, that I create many buffers, which consume (too much?) memory.

PS: Sorry for my bad english.

www.hw-board.de/cpp

• 13
• 12
• 23
• 10
• 19