«Бесконечные» мировые проблемы - PullRequest
1 голос
/ 15 декабря 2011

Я создаю майнкрафт, похожий на воксельный движок в xna, и начал внедрять «бесконечный» мир, но столкнулся с несколькими проблемами.Одна из таких проблем состоит в том, что следующая строка, кажется, всегда применяет противоположное (например, если игрок переместил X + REGION_SIZE_X, он назначит Direction.X_DECREASING вместо ожидаемого Direction.X_INCREASING).

    Thread regionLoader;

    public void CheckPlayer()
    {
        Direction direction = Direction.MAX;

        float distancetraveledx = player.Origin.X - player.Position.X;
        float distancetraveledy = player.Origin.Y - player.Position.Y;
        float distancetraveledz = player.Origin.Z - player.Position.Z;

        if (distancetraveledx > Variables.REGION_SIZE_X)
        {
            direction = Direction.XIncreasing;
            player.Position = new Vector3(player.Origin.X, player.Position.Y, player.Position.Z);
        }
        else if (distancetraveledx < -Variables.REGION_SIZE_X)
        {
            direction = Direction.XDecreasing; 
            player.Position = new Vector3(player.Origin.X, player.Position.Y, player.Position.Z);
        }
        else if (distancetraveledz > Variables.REGION_SIZE_Z)
        {
            direction = Direction.ZIncreasing;
            player.Position = new Vector3(player.Position.X, player.Position.Y, player.Origin.Z);
        }
        else  if (distancetraveledz < -Variables.REGION_SIZE_Z)
        {
            direction = Direction.ZDecreasing;
            player.Position = new Vector3(player.Position.X, player.Position.Y, player.Origin.Z);
        }
        if (direction != Direction.MAX)
        {
            regionManager.direction = direction;

            regionLoader = new Thread(new ThreadStart(regionManager.ShiftRegions));
            regionLoader.Start();
        }
    }

Так что же этовыполняется проверка того, переместил ли игрок REGION_SIZE из своего источника и, если он это сделал, сбросит компонент позиции, которая перешла эту границу.

Затем вызывается следующая функция:

    public void ShiftRegions()
    {
        // Future and current arrays are to avoid moving elements twice (or maybe I am thinking about it in the wrong way...)
        future_regions = new Region[(int)Variables.RENDER_DISTANCE, (int)Variables.RENDER_DISTANCE, (int)Variables.RENDER_DISTANCE];

        for (short j = 0; j < future_regions.GetLength(0); j++)
        {
            for (short m = 0; m < future_regions.GetLength(1); m++)
            {
                for (short i = 0; i < future_regions.GetLength(2); i++)
                {
                    future_regions[j, m, i] = new Region(world, new Vector3i(new Vector3(j, m, i)));

                    switch (direction)
                    {
                            // This appears to work as XDecreasing
                        case Direction.XIncreasing:
                            // If it is not at the back
                            if (j != future_regions.GetLength(0) - 1)
                            {
                                // Make the regions look like they are scrolling backward
                                future_regions[j, m, i] = regions[j + 1, m, i];
                                future_regions[j, m, i].Index = new Vector3i((uint)j, (uint)m, (uint)i);
                                // In the next call the vertex buffer will be regenerated thus placing the "blocks" in the correct position
                                future_regions[j, m, i].Dirty = true;
                            }
                            // If it is the front most region to which the player is traveling ing
                            if (j == 0)
                            {
                                // New the region setting the Generated flags to false thus allowing it to be regenerated
                                future_regions[j, m, i] = new Region(world, new Vector3i(new Vector3(j, m, i)));
                            }
                            // If it is at the back of the regions
                            if (j == future_regions.GetLength(0) - 1)
                            {
                                //store region
                            }

                            break;
                        case Direction.XDecreasing:

                            break;
                    }
                }
            }
        }


        generator.build(ref future_regions);
        direction = Direction.MAX;

        this.regions = future_regions;
    }

Это фактически перемещает области, которые вызывают эффект прокрутки, и новые области, которые появляются спереди.Который, кажется, не работает.

Я думал, что вместо того, чтобы фактически "перемещать" регионы, я мог бы просто назначить различные смещения и перемещаться в матрице мира, но когда я делаю это, я просто получаю синий экран ....

Вот остаток кода:

Функция класса генератора:

    public virtual void build(ref Region[,,] regions)
    {


        for (short j = 0; j < regions.GetLength(0); j++)
        {
            for (short m = 0; m < regions.GetLength(1); m++)
            {
                for (short i = 0; i < regions.GetLength(2); i++)
                {
                    OutputBuffer.Add("Generating...");

                    if (regions[j, m, i].Generated == false)
                    {
                        regions[j, m, i].Dirty = true;
                        regions[j, m, i].Generated = true;

                        for (short x = 0; x < Variables.REGION_SIZE_X; x++)
                        {
                            for (short y = 0; y < Variables.REGION_SIZE_Y; y++)
                            {
                                for (short z = 0; z < Variables.REGION_SIZE_Z; z++)
                                {
                                    regions[j, m, i].Blocks[x, y, z] = new Block();

                                    Vector3i relativeBlock = new Vector3i(new Vector3(x + Variables.REGION_SIZE_X * j, y + Variables.REGION_SIZE_Y * m, z + Variables.REGION_SIZE_Z * i));

                                    if (x == 0 || x == 16 || x == 32 || z == 0 || z == 16 || z == 32 || y == 0 || y == 16 || y == 32)
                                    {
                                        regions[j, m, i].Blocks[x, y, z].BlockType = BlockType.dirt;
                                    }
                                    else
                                    {
                                        regions[j, m, i].Blocks[x, y, z].BlockType = BlockType.grass;

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

и код, который проверяет необходимость восстановления буферов региона:

    public void Update(World world)
    {
        this.world = world;

        for (short j = 0; j < regions.GetLength(0); j++)
        {
            for (short m = 0; m < regions.GetLength(1); m++)
            {
                for (short i = 0; i < regions.GetLength(2); i++)
                {
                    if (regions[j, m, i].Dirty &&
                        regions[j, m, i].Generated)
                    {
                        regions[j, m, i].BuildVertexBuffers();
                    }
                }
            }
        }
    }

Кстати, если для параметра Dirty установлено значение true, это означает, что буферы должны быть восстановлены.

Любые идеи, почему это не создает новые области на фронте и почему это не такправильная прокрутка?

РЕДАКТИРОВАТЬ: Я просто думал логически, и что моя идея с изменением положения регионов в массиве не изменит его мировое положение, и, как я сказал выше, с преобразованием их в правильные положения вместокопирование их - это кажется самым логичным шагом.Дело в том, что мне, возможно, придется скопировать некоторые в разные места в массиве регионов, потому что массив может быстро стать спагетти, если вы просто преобразуете их ...

Спасибо, Darestium

1 Ответ

0 голосов
/ 17 января 2012

Просто смотрю на эту часть

float distancetraveledx = player.Origin.X - player.Position.X;

Вы вычитаете позицию из начала координат, вам, вероятно, следует изменить порядок и сделать это

float distancetraveledx = player.Position.X - player.Origin.X;

Например, если игрок перешел с (0, 0, 0) на (10, 0, 0), игрок переместился на 10 единиц в направлении X, но ваш «distancetraveledx» даст вам 0 - 10 или -10.

Не имеет отношения к вашему вопросу: Ваши переменные будут проще использовать, если вы будете следовать CamelCase , с первой буквой в нижнем или верхнем регистре:

float distanceTraveledX = player.Position.X - player.Origin.X;

В C # соглашение состоит в том, чтобы имена классов с первой буквой были заглавными, а переменные с первой буквой в нижнем регистре, например:

MyClass myClass;

...