Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Infinite" world problems
    primarykey
    data
    text
    <p>I'm creating a minecraft like voxel engine thing in xna, and have started about implementing "infinite" world but have ran into a few problems. One such problem is that the following line always seems to apply the oppisite (eg if the player moved X+ REGION_SIZE_X it would assign Direction.X_DECREASING instead of the expected Direction.X_INCREASING).</p> <pre><code> 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 &gt; Variables.REGION_SIZE_X) { direction = Direction.XIncreasing; player.Position = new Vector3(player.Origin.X, player.Position.Y, player.Position.Z); } else if (distancetraveledx &lt; -Variables.REGION_SIZE_X) { direction = Direction.XDecreasing; player.Position = new Vector3(player.Origin.X, player.Position.Y, player.Position.Z); } else if (distancetraveledz &gt; Variables.REGION_SIZE_Z) { direction = Direction.ZIncreasing; player.Position = new Vector3(player.Position.X, player.Position.Y, player.Origin.Z); } else if (distancetraveledz &lt; -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(); } } </code></pre> <p>So what this is doing is checking if the player has moved REGION_SIZE from it's origin and if it has, reset the component of the position which has moved over that boundary.</p> <p>This then calls the following function:</p> <pre><code> 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 &lt; future_regions.GetLength(0); j++) { for (short m = 0; m &lt; future_regions.GetLength(1); m++) { for (short i = 0; i &lt; 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; } </code></pre> <p>This actually moves the regions which causes the scrolling effect and the new regions which appear at the front. Which doesn't seem to work.</p> <p>I was thinking istead of actually "moving" the regions i could just assign different offsets and move in the world matrix but when I do so i just get a blue screen...</p> <p>Here is the rest of the code:</p> <p>Generator class function:</p> <pre><code> public virtual void build(ref Region[,,] regions) { for (short j = 0; j &lt; regions.GetLength(0); j++) { for (short m = 0; m &lt; regions.GetLength(1); m++) { for (short i = 0; i &lt; 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 &lt; Variables.REGION_SIZE_X; x++) { for (short y = 0; y &lt; Variables.REGION_SIZE_Y; y++) { for (short z = 0; z &lt; 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; } } } } } } } } } </code></pre> <p>and the code that checks if the region's buffers need to be rebuilt:</p> <pre><code> public void Update(World world) { this.world = world; for (short j = 0; j &lt; regions.GetLength(0); j++) { for (short m = 0; m &lt; regions.GetLength(1); m++) { for (short i = 0; i &lt; regions.GetLength(2); i++) { if (regions[j, m, i].Dirty &amp;&amp; regions[j, m, i].Generated) { regions[j, m, i].BuildVertexBuffers(); } } } } } </code></pre> <p>By the way, if Dirty is set to true this means that the buffers need to be regenerated.</p> <p>Any ideas why this is not creating new regions at the front and why it is not scrolling properly?</p> <p>EDIT: I was just thinking logically and that my idea with the changing the regions position in the array will not change it's world position, and like I said above with transforming them to the correct positions instead of copying them - that seems like the most logical step. Well the thing is I may have to copy some to different places in the regions array because the array may become spaghetti in no time if you just transform them...</p> <p>Thanks, Darestium</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload