Rotating the whole level

限于喜欢 提交于 2019-12-11 12:54:04

问题


I have a XNA programm which draws a field (50x50) of a texture

The code of the drawing looks like that:

namespace Village
{
    public class DrawLogic
    {

        internal static void DrawCreatures(GameTime gameTime, GraphicsDevice graphicsDevice, SpriteBatch spriteBatch, Dictionary<string, Texture2D> creatureTextures, List<Creature> list, Coordinate current)
        {
            spriteBatch.Begin();
            foreach (var item in list)
            {
                Vector2 origin = new Vector2(0, 0);
                if (item.Selected)
                {
                    spriteBatch.Draw(creatureTextures["selected"], new Vector2((item.Coordinates.X * 32) - current.X, (item.Coordinates.Y * 32) - current.Y), new Rectangle(0, 0, creatureTextures["human"].Width, creatureTextures["human"].Height), Color.White, 0.0f, origin, 1f, SpriteEffects.None, 1f);
                }
                else
                {
                    spriteBatch.Draw(creatureTextures["human"], new Vector2((item.Coordinates.X * 32) - current.X, (item.Coordinates.Y * 32) - current.Y), new Rectangle(0, 0, creatureTextures["human"].Width, creatureTextures["human"].Height), Color.White, 0.0f, origin, 1f, SpriteEffects.None, 1f);
                }
            }
            spriteBatch.End();
        }

        internal static void DrawObjects(GameTime gameTime, GraphicsDevice graphicsDevice, SpriteBatch spriteBatch, Dictionary<string, Texture2D> creatureTextures, List<IHarvestable> list, Coordinate current)
        {
            spriteBatch.Begin();
            foreach (var item in list)
            {
                Vector2 origin = new Vector2(0, 0);

                spriteBatch.Draw(creatureTextures["tree"], new Vector2((item.Coordinates.X * 32) - current.X, (item.Coordinates.Y * 32) - current.Y), new Rectangle(0, 0, creatureTextures["tree"].Width, creatureTextures["tree"].Height), Color.White, 0.0f, origin, 1f, SpriteEffects.None, 1f);

            }
            spriteBatch.End();
        }

        internal static void DrawMap(GameTime gameTime, GraphicsDevice graphicsDevice, SpriteBatch spriteBatch, Dictionary<string, Texture2D> worldTextures, Block[,] map, Coordinate current)
        {
            spriteBatch.Begin();

            for (int i = 0; i < map.GetLength(0); i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                {
                    if (map[i, j].GetType() == typeof(Grass))
                    {


                        Vector2 origin = new Vector2(0, 0);
                        spriteBatch.Draw(worldTextures["grass"], new Vector2((i * 32) - current.X, (j * 32) - current.Y), new Rectangle(0, 0, worldTextures["grass"].Width, worldTextures["grass"].Height), Color.White, 0.0f, origin, 1.0f, SpriteEffects.None, 1f);
                    }
                }
            }

            spriteBatch.End();
        }
    }
}

My Question ist now, how can i rotate the whole map?

It should look like that:

I don't know whats the best way to do this.

Should I rotate everyone of my textures and put them in the right order, or is there a way to rotate the whole level?

I think if I could rotate the whole level, the walking algorithm of my creatures would me much easier, because it wouldn't be different then my current algorithm.

The other way (rotating every single texture) would be much harder.. I think.

Thanks in advance!


回答1:


The way I would do it is to apply a rotation matrix to the projection matrix before you draw the map. You can see a small how-to in the XNA documentation here.

If you're looking for an isometric projection (which I think is what you're actually looking for), the tile engine tutorial on XNA Resources goes into it here.

I'm not sure I see how that would simplify your "walking algorithm," though, as rotating the display in this regard is for visual purposes only and has no impact on the data behind the scenes. Could you go into further detail in why you think it would change things?




回答2:


You can use the overloaded method spriteBatch.Begin() that takes a transform matrix. You can set a matrix that you create with Matrix.CreateRotationZ() and Matrix.CreateTranslation. Combine them by multiplying them. If you want to rotate about a certain point, you have to apply a translation that repositions the according point to the origin, rotate and translate back.




回答3:


Do not try to rotate the world — that's impossible. Instead transform your point of view.



来源:https://stackoverflow.com/questions/10065072/rotating-the-whole-level

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!