Search code examples
c#xnarotationrendertarget

xna 4.0 c# rotate a collection of images


Me and my team are working on a game in xna right now. However we've run into a problem. It is an open space sim where players will be able to build their own ships (top down view) room by room. Each room has a texture2d, however since the game is top-down, we will need to be able to "turn the ship".

We're running into a problem rotating the collection of texture2ds that make up the ship. In our old code, we tried to rotate all the rooms around a central origin as the location of the bridge. As we had problems with this, we looked online and decided to try our hand at RenderTargets (draw the whole ship onto an RT, then we could just rotate the RT image when it's drawn). Our problem is that the RT draws a big purple background and, since our ships aren't always perfectly filled in rectangles, there's always a purple background somewhere poking out.

The question is: What is the best way to rotate a collection of texture2ds around a single origin in xna as if they were a single image?


Solution

  • Your problem is almost word-for-word the exact same problem I had until recently.

    I had a collection of compartments (for each of my spaceships) rotating around an origin, but this resulted in:

    1. Large overhead for calculating and reposition each and every compartment
    2. Small gaps between compartments, probably due to the inaccuracies of the float

    I recommend you go back to the RenderTarget approach. To get rid of the purple background, use:

    GraphicsDevice.SetRenderTarget(render);
    GraphicsDevice.Clear(Color.Transparent);
    

    Much less overhead and a lot prettier without the gaps between compartments.

    All the best to you and your team, may the best spaceship game win ;]