C# XNA等距平铺碰撞

C# XNA等距平铺碰撞,c#,xna,collision,tile,isometric,C#,Xna,Collision,Tile,Isometric,我是一名C#/XNA的学生,最近我一直在开发一个等距瓷砖引擎,到目前为止,它工作得相当好。但我在试图找出如何进行碰撞时遇到了问题,这就是我的平铺引擎目前所做的: 从图像绘制世界并根据图像上的颜色放置平铺。例如,红色会画一块草砖。(瓷砖为64x32) 摄影机跟随播放器,我的绘制循环只绘制摄影机看到的内容 这就是我的游戏的样子,如果这有什么帮助的话: 我不知道哪种碰撞最有效。我应该做碰撞点,或相交或任何其他类型的碰撞。我在某个地方读到过,你可以做Worldtoscreen/Screentowo

我是一名C#/XNA的学生,最近我一直在开发一个等距瓷砖引擎,到目前为止,它工作得相当好。但我在试图找出如何进行碰撞时遇到了问题,这就是我的平铺引擎目前所做的:

  • 从图像绘制世界并根据图像上的颜色放置平铺。例如,红色会画一块草砖。(瓷砖为64x32)

  • 摄影机跟随播放器,我的绘制循环只绘制摄影机看到的内容

这就是我的游戏的样子,如果这有什么帮助的话:

我不知道哪种碰撞最有效。我应该做碰撞点,或相交或任何其他类型的碰撞。我在某个地方读到过,你可以做Worldtoscreen/Screentoworld,但我远没有经验,不知道这是如何工作的,也不知道代码会是什么样子

这是我的代码图瓷砖等:

class MapRow
    {
        public List<MapCell> Columns = new List<MapCell>();
    }



    class TileMap
    {
        public List<MapRow> Rows = new List<MapRow>();

        public static Texture2D image;
        Texture2D tileset;


        TileInfo[,] tileMap;


        Color[] pixelColor;


        public TileMap(string TextureImage, string Tileset)
        {
            tileset = Game1.Instance.Content.Load<Texture2D>(Tileset);

            image = Game1.Instance.Content.Load<Texture2D>(TextureImage);
            pixelColor = new Color[image.Width * image.Height]; // pixelColor array that is holding all pixel in the image
            image.GetData<Color>(pixelColor); // Save all the pixels in image to the array pixelColor

            tileMap = new TileInfo[image.Height, image.Width];

            int counter = 0;
            for (int y = 0; y < image.Height; y++)
            {
                MapRow thisRow = new MapRow();
                for (int x = 0; x < image.Width; x++)
                {
                    tileMap[y, x] = new TileInfo();


                    if (pixelColor[counter] == new Color(0, 166, 81))
                    {
                        tileMap[y, x].cellValue = 1;//grass

                    }
                    if (pixelColor[counter] == new Color(0, 74, 128))
                    {

                        tileMap[y, x].cellValue = 2;//water
                    }

                    if (pixelColor[counter] == new Color(255, 255, 0))
                    {
                        tileMap[y, x].cellValue = 3;//Sand
                    }


                    tileMap[y, x].LoadInfoFromCellValue();//determine what tile it should draw depending on cellvalue
                    thisRow.Columns.Add(new MapCell(tileMap[y, x]));


                    counter++;
                }
                Rows.Add(thisRow);
            }
        }


        public static int printx;
        public static int printy;

        public static int squaresAcross = Settings.screen.X / Tile.TileWidth;
        public static int squaresDown = Settings.screen.Y / Tile.TileHeight;

        int baseOffsetX = -32;
        int baseOffsetY = -64;

            public void draw(SpriteBatch spriteBatch)
        {
            printx = (int)Camera.Location.X / Tile.TileWidth;
            printy = (int)Camera.Location.Y / Tile.TileHeight;

            squaresAcross = (int)Camera.Location.X / Tile.TileWidth + Settings.screen.X / Tile.TileWidth;
            squaresDown = 2*(int)Camera.Location.Y / Tile.TileHeight + Settings.screen.Y / Tile.TileHeight + 7;


            for (printy = (int)Camera.Location.Y / Tile.TileHeight; printy < squaresDown; printy++)
            {
                int rowOffset = 0;
                if ((printy) % 2 == 1)
                    rowOffset = Tile.OddRowXOffset;

                for (printx = (int)Camera.Location.X / Tile.TileWidth; printx < squaresAcross; printx++)
                {
                    if (tileMap[printy, printx].Collides(MouseCursor.mousePosition))
                        Console.WriteLine(tileMap[printy, printx].tileRect);

                    foreach (TileInfo tileID in Rows[printy].Columns[printx].BaseTiles)
                    {

                        spriteBatch.Draw(

                            tileset,
                            tileMap[printy, printx].tileRect = new Rectangle(
                                (printx * Tile.TileStepX) + rowOffset + baseOffsetX,
                                (printy * Tile.TileStepY) + baseOffsetY,
                                Tile.TileWidth, Tile.TileHeight),
                            Tile.GetSourceRectangle(tileID.cellValue),
                            Color.White,
                            0.0f,
                            Vector2.Zero,
                            SpriteEffects.None,
                            tileID.drawDepth);





                    }



                }

            }

            }

        }
类映射行
{
公共列表列=新列表();
}
类TileMap
{
公共列表行=新列表();
公共静态纹理2D图像;
纹理2D瓷砖组;
TileInfo[,]tileMap;
颜色[]像素颜色;
公共TileMap(字符串纹理图像、字符串Tileset)
{
tileset=Game1.Instance.Content.Load(tileset);
image=Game1.Instance.Content.Load(TextureImage);
pixelColor=新颜色[image.Width*image.Height];//保存图像中所有像素的pixelColor数组
image.GetData(pixelColor);//将图像中的所有像素保存到数组pixelColor
tileMap=新的TileInfo[image.Height,image.Width];
int计数器=0;
对于(int y=0;y
为什么不像普通的基于瓷砖的游戏那样画东西,然后将相机旋转45度?当然,您需要将图形设置得有点奇怪,但这样可以更容易地处理平铺。

但是如果你喜欢你的方式,那么我建议用简单的数学来计算“右边的瓷砖”、“左边的瓷砖”、“向上的瓷砖”和“向下的瓷砖”,你知道,玩家周围的瓷砖(或其他瓷砖)。您可以简单地处理列表,通过一些数学,基本的数学,如获取下一个磁贴,非常简单。

编辑
您可以使用如下代码获取玩家下一个位置的平铺值:

tileMap[Math.Floor((player.y+playerVelociy.Y)/tileHeight)]
       [Math.Floor((player.x+playerVelocity.X)/tileWidth)]
在这段代码中,我假设第一个平铺位于0,0,并且您正在向右和向下绘制。(如果没有,则将Math.Floor更改为Math.Ceil)
链接可以帮助您了解这个想法,但是在AS3.0中,只是语法不同。

是的,我想我可以做到(alt nr.2)。但这将使用一个正常的矩形碰撞?既然我的瓷砖是等距的,这意味着角是透明的,那么程序不会也认为角也是