Dynamically change or destroy Texture2D for drawing and collision detection

I am using XNA for a 2D project. I have a problem and I donโ€™t know how to solve it. I have a texture (image) that is drawn on the screen, for example:

|+++|+++| |---|---| |+++|+++| 

Now I want to destroy part of this structure / image so that it looks like this:

 |+++| |---|---| |+++|+++| 

so that now a new collision works for a new image.

What is the best way to solve this problem:

  • Change the whole texture with another texture that is transparent in the places where it was destroyed.
  • Use some tricks with spriteBatch.Draw (sourceRectangle, destinationRectangle) to get the desired rectangles, and also check for a collision with this somehow.
  • Divide the texture into 4 smaller textures, each of which will be responsible for its own drawing / collision detection.
  • Use another way of smart assistants that I do not know about.

Any help would be greatly appreciated. Let me know if you need more clarification / examples.

EDIT: To clarify, I will give an example use for this. Imagine a 4x4 wall that, when fired, destroys the 1x1 part a bit.

+4
source share
4 answers

I ended up choosing option 3. Basically, I have a Tile class that contains texture and size. Size n means that there are n * n subtiles in this tile. I also have an array that keeps track of which tiles are destroyed or not. My class looks like this in pseudo code:

  class Tile texture dimention int [,] subtiles; //0 or 1 for each subtile public Tile() // constructor subtiles = new int[dimention, dimention]; intialize_subtiles_to(1); public Draw() // this is how we know which one to draw //iterate over subtiles for(int i.. for(int j ...) if(subtiles[i,j] == 1) Vector2 draw_pos = Vector2(i*tilewidth, j*tileheight) spritebatch.Draw(texture, draw_pos) 

Similarly, I have a collision method that will check for a collision:

  public bool collides(Rectangle rect) //iterate over subtiles for i... for j.. if(subtiles[i,j]==0) continue; subtile_rect = //figure out the rect for this subtile if(subtile_rect.intersects(rect)) return true; return false; 

And so on. You can imagine how to โ€œdestroyโ€ certain substructures by setting their corresponding value to 0 and how to check if the entire fragment is destroyed. Provided by this technique, all litter will have the same texture. So far I canโ€™t come up with a simpler solution.

0
source

I will take the third option:

3 . Divide the texture into 4 smaller textures, each of which will be responsible for this pattern / collision detection.

This is not difficult. This is basically the same as the TileSet structure. However, you will need to modify your code to fit this approach. Read a little about Tiles on: http://www-cs-students.stanford.edu/~amitp/gameprog.html#tiles Many sites and books have talked about tiles and how to use them to create game worlds. But you can use this logic to everything that all compost from small parts.

Let me notice other options:

1 . Change the whole texture with another texture that is transparent in the places where it is destroyed.

No. In another image, all different positions are bad. Should you need to change the texture? Do you repeat all images again?

2 . Use some trompe l'oeil with spriteBatch.Draw (sourceRectangle, destinationRectangle) to get the desired rectangles, as well as make a collision with this somehow.

Unfortunately, this does not work, because spriteBatch.Draw only works with Rectangles :(

4 Use another smart ass way to know.

I can not imagine any magic. Perhaps you can use another image to create masks. But it is extremely expensive.

+1
source

Check out this article on Ziggyware. This is a Deformable Terrain and may be what you are looking for. Essentially, this method includes the settings for the pixels you want to hide so that they are transparent.

0
source

Option number 3 will work.

A more reliable system (if you do not want to be limited to frames) will use collision detection on pixels. The process basically works as follows:

  • Calculate a bounding box (or circle) for each object
  • Check if two objects overlap
  • For each overlap, blit sprites onto a hidden surface, comparing pixel values โ€‹โ€‹as they move. If the pixel is already set when you try to draw a pixel from the second sprite, you have a collision.

Here is a good example of XNA (in fact, another Ziggyware article): Two-pixel conflict detection

Some more links:

Can anyone explain pixel conflict detection

XNA dual disk clash

0
source

All Articles