Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question

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.

Notes:

  • 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!

share|improve this answer
    
I know about render target, but I was looking for something simplier, just to check what the color is at a sprites position!? –  3D-kreativ Aug 24 '12 at 17:17
    
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. –  Roy T. Aug 24 '12 at 18:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.