I have read somewhere that sprites can detect the background color, but I don't remember where and how it's working. What I'm trying to do is just a function to detect if the sprite is on a special surface, that has a special color, instead of using collision dedection between objects. Help is preciated! Thanks!


1 Answer 1


You can render your entire scene to a render target instead of directly to screen. A render target is also a texture and you can query the color of each pixel by getting the data from the texture. See this StackOverflow answer on how to get the color of a pixel in a texture.


  • The color data is a 1 dimensional array so you need to do a tiny bit of math to go from 2D to 1D to get the correct pixel data.
  • Getting the color data like this can be slow, but don't worry about it until you experience actual performance issues. If you only do this once per frame you should be alright.

Other resources:

You can find a tutorial for XNA here that uses a texture as a color key for collision detection. A lot of the techniques you need are explained there in detail so be sure to check it out!

  • \$\begingroup\$ I know about render target, but I was looking for something simplier, just to check what the color is at a sprites position!? \$\endgroup\$
    – 3D-kreativ
    Aug 24, 2012 at 17:17
  • \$\begingroup\$ If you wan't to check the color you have to get the data back from the GPU memory to your normal memory. I'm afraid I don't know any other way, and I don't think there is. \$\endgroup\$
    – Roy T.
    Aug 24, 2012 at 18:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .