I've stumbled upon a problem while working on my simple game and have been stuck on it for quite a while, I hope anyone can offer me an elegant solution or a step into the right direction.
I currently have a simple top down scroller, the player is for now just a white cube, and around the map are rotating 45 degree light cones made with Krypton. The player has to avoid being seen by the lights, so I'm trying to create a pixel-collision method to fire an event for when the player falls in range of the lit up area.
Krypton has a property that returns the bounding rectangle around a light, but I can't use that for the collision since it will draw a huge square around the entire zone of the light, so the player would get detected even if you stand behind the light. I want accuracy.
I've also tried drawing a triangle around the lit up area, but the problem here was it wasn't as accurate as I would want it to be, because some of the light shines through corridors, which I can't accurately pick up with a static triangle, plus the triangle goes through walls since the range of the light is actually larger than half the map, but is mostly just blockaded by shadow hulls. The player has to be able to hide from said lights behind walls. Here's an image example of what I mean. Image
So I figured the best way to do this would be to check if the color of one of the pixels of the player has changed to a certain .. RGB value. This would require some hard coding but it's a simple game, so I would not mind this approach. But here I run into some trouble.
I read about the Texture2D.GetData<>
method, I can do this however it returns a color array of the actual imported texture, which is always white. It doesn't return the post-processed results. Which makes sense because there can be multiple textures in the scene.
The next one I tried was GraphicsDevice.GetBackBufferData<>
which returns all the pixels of the viewport, which immediately brings my FPS down to none, there's overloads to only retrieve pixels in a certain range but they never seem to work for me, as I'm always surprised with the out of index exception. Here's what I try when using this method:
Color[] colors = new Color[10]
GraphicsDevice.GetBackBufferData<Color(colors, 0, 10);
(Just an example, but I can't seem to understand why this does not work.)
So to cut to the chase more or less, I'm looking for a method to retrieve a color array of a certain area or coordinate of my view port, so I can compare all the colors in said array to see if they match.
Apologies if it's a bit long, but I would greatly appreciate any advice or guidance as I don't know what I'm missing, I've followed most tutorials regarding this and they all seem to compare the color values of the textures two colliding textures, but if I'm not mistaken the texture itself never actually changes colors, since it's just an imported asset that gets drawn. I figure I need to retrieve the colors of WHERE the texture is drawn to properly check if there has been a change.
Kind regards
EDIT:
I got it to work, the rectangle as argument in GetBackBufferData works well, disabling MultiSampling makes the lag non existent. Thank you for the suggestions, @Bjarke Søgaard and @thomas88wp.
Color colorArray = new Color[player.Bounds.Width * player.Bounds.Height];
Color[] colors = new Color[player.Bounds.Width * player.Bounds.Height];
GraphicsDevice.GetBackBufferData<Color>(rec, colors, 0, colors.Count());