Color Key Collision Detection Help

Started by
3 comments, last by GameDev.net 17 years, 1 month ago
Hi, can someone show me how to do "color key" collision detection. I have a tile of type LPDIRECT3DTEXTURE9, and I need to get the color of the x, y pixel so I can check for collision, anyone can show me how? Thanks
Advertisement
I don't know how color collision works, but if you want to get pixel values from a texture, you simply need to call LockRect on it and get its D3DLOCKED_RECT.
Then you can access the texture bits as you would with a normal array (size increment depends on texture format, of course).

Hope this helps :)
Let say I loaded a 640x 480 image.bmp image as texture. How would I go about getting the Color value at that pixel x y from the D3DLOCKED_RECT object? Sorry, I am very new at Window Programming, sample code would help. Thanks!
something like this should do the trick:

D3DXCOLOR IMAGE[256 * 256]; // assumes source image is R8G8B8D3DLOCKED_RECT pLockedRect;if(FAILED(Texture->LockRect(0, &pLockedRect, NULL, D3DLOCK_READONLY)))	return false;for (int y=0; y<IMAGE_SIZE; y++){    for (int x=0; x<IMAGE_SIZE; x++){	D3DXCOLOR color = *((DWORD*)(((BYTE*)pLockedRect.pBits) +   pLockedRect.Pitch*y + x*sizeof(DWORD)));	IMAGE[x + y*IMAGE_SIZE] = color * 255;	}}Texture->UnlockRect(0);


Hope this helps :)
can you explain what does this mean in details.
D3DXCOLOR color = *((DWORD*)(((BYTE*)pLockedRect.pBits) + pLockedRect.Pitch*y + x*sizeof(DWORD)));

I know you convert pLockedRect.pBits to BYTEs and pLockedRect.pBits represent the texture in bits.

what does pLockedRect.Pitch*y + x*sizeof(DWORD)) do??

This topic is closed to new replies.

Advertisement