So I’m making an asteroids like game for practice in XNA. I have a Large texture I’m using for a texture atlas as described in R.B.Whitaker’s wiki here: http://rbwhitaker.wikidot.com/texture-atlases-1
I’ve now branched off from the wiki and am attempting to make collision detection for my ship and the alien. The problem is I need the current sprite from the atlas as a separate texture2d to where I can do accurate collision detection. I’ve read examples using the texture2d.GetData method, but I can’t seem to get a working implementation. Any detailed implementations of that method or other options would be greatly appreciated.
Use
Texture.GetData<Color>()on your atlas texture to get the array of colors representing individual pixels:When you need a rectangle of data from the texture, use a method as such:
The method above throws index out of range exception if the
rectangleis out of bounds of the original texture.widthparameter is the width of the texture.Usage with
sourceRectangleof a specific sprite:If you really need another texture (though I imagine you actually need color data for per pixel collision), you could do this:
Side note – If you need to use color data from textures constantly for collision tests, save it as an array of colors, not as textures. Getting data from textures uses the GPU while your CPU waits, which degrades performance.