1

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?

3 Answers3

0

Create a camera that rotates instead of rotating all textures / make a rendertarget. It basically views the sprites, instead of tweaking everything else.

There are plenty of tutorials on the internet, but I'll show you something:

class Camera
{
    public Matrix transform; // The transformed matrix

    public void Update(float rotation)
    {
        // you can add the positions here, x y z. Play around with it.
        // x should be half of the screen width,
        // y the half of the screen height, creating a "orgin" where it's rotating.
        transform = Matrix.CreateTranslation(0, 0, 0) * Matrix.CreateRotationZ(rotation);
    }
}

Put this in your Draw method, it uses the camera's viewport: sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, camera.transform);

Also: I don't really know much about camera's. But this is a basic one.

joppiesaus
  • 5,471
  • 3
  • 26
  • 36
  • I've actually got a camera already up and working, but we'll be manipulating multiple ships on the screen flying around, so we can't just turn the screen. But thanks for the idea! – user3001120 Aug 19 '14 at 00:55
  • Oh, if you want a cool camera tutorial/code (this is what we used), check this out: http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/ it's really cool stuff. – user3001120 Aug 19 '14 at 00:56
  • @user3001120 Thanks! But: Can't you just use cos and sin for rotations? – joppiesaus Aug 19 '14 at 09:35
0

You will have to find "center of mass" aka barycenter. it's averageing of all x and y coordinates. and you will have origin that you need.

this is code i found some times ago, but never tested... i guess it's also from stackoverflow.

Point bary = Point.Zero;
foreach (Point p in list)
{
    bary.X += p.X;
    bary.Y += p.Y;
}
bary.X = (bary.X + list.Count / 2) / list.Count;
bary.Y = (bary.Y + list.Count / 2) / list.Count;
return bary;

try this way

Davor Mlinaric
  • 1,989
  • 1
  • 19
  • 26
0

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 ;]

  • This worked. I'd seen people make comments like this before and I'd tried to add Color.Transparent but I guess I always added it in the wrong spot. Thanks for the help, and yes, May the best spaceship game win ;) Thanks again! – user3001120 Aug 19 '14 at 16:33