Adding values to a list with an array - c#

I'm trying to make a list using this class:
public class Edges
{
public int no_piece;
public int edge;
public int[,] edge_pixels;
}
This is to assemble a puzzle. I am trying to save the characteristics of each piece this way: no_piece - the number of the piece /// edge - which edge (top as '0', left as '1', bottom as '2' and right as '3') /// edge_pixels - the number of pixels for each channel, for example:
edge_pixels[0,x] would have the values of each pixel of an edge, of n piece);
This is the way I tried;
List<Edges> edges_list = new List<Edges>();
for (i = 0; i < number of pieces; i++)
{
for (y = vector_coordinates[i,1]; y < vector_coordinates[i,3]; y++)
{//vectors with top and bottom coordinates of the piece in the original image
for (x = vector_coordinates[i,0]; x < vector_coordinates[i,2]; x++)
{// the same goes for x but left and right
if (y == vector_coordinates[i, 1]) //top
{
for (aux_rgb = 0; aux_rgb < 3; aux_rgb++)
{
Edges edge = new Edges();
edge.no_piece = i;
edge.edge = 1;
edge.edge_pixels[aux_rgb, aux_count_pixel] = (int)dataPtr[aux_rgb];
edges_list.Add(edge);
}
}
aux_count_pixel++;
}
(...)
But it doesn't work and I donĀ“t understand why. I'm not sure if I made myself clear. Thank you

Try following.
List<Edges> edges_list = new List<Edges>();
for (i = 0; i < number of pieces; i++)
{
for (y = vector_coordinates[i,1]; y < vector_coordinates[i,3]; y++)
{//vectors with top and bottom coordinates of the piece in the original image
for (x = vector_coordinates[i,0]; x < vector_coordinates[i,2]; x++)
{// the same goes for x but left and right
if (y == vector_coordinates[i, 1]) //top
{
Edges edge = new Edges();
edges_list.Add(edge);
edge.no_piece = i;
for (aux_rgb = 0; aux_rgb < 3; aux_rgb++)
{
edge.edge = 1;
edge.edge_pixels[aux_rgb, aux_count_pixel] = (int)dataPtr[aux_rgb];
}
}
aux_count_pixel++;
}

Related

Problem with world generation in unity c#

I want to create a world generation using two noise maps (altitude noise and moisture noise). I create maps of these noises and set them in the inspector to values between 0 and 1.
I want to get result like this:
If Elevation < 1000
{
If Moisture < 50: Desert
Else Forest
}
And it seems that I did it as it should, but for some reason the generation does not work correctly:
Here is my code:
for (int x=0; x < tilemap.Width; x++)
{
for (int y = 0; y < tilemap.Height; y++)
{
// Get height at this position
var height = noiseMap[y * tilemap.Width + x];
var moisureHeight = moisureMap[y * tilemap.Width + x];
// Loop over our configured tile types
for (int i = 0; i < TileTypes.Length; i++)
{
var TileType = TileTypes[i];
for (var j = 0; j < TileType.MoisureValues.Length; j++)
{
// If the height is smaller or equal then use this tiletype
if (height <= TileType.Height)
{
if (moisureHeight <=TileType.MoisureValues[j].MoisureHeight)
{
tilemap.SetTile(x, y, (int)TileType.MoisureValues[j].GroundTile);
break;
}
}
}
}
}
}

IndexOutOfRangeException: Array Index is out of Range Unity

I was trying to build a Pathfinding A* code, but keep getting an Array Index error. I have tried tweaking with the x and y, but no luck so far.
Here is the code.
public int columns = 9; //Columns and rows to set up the board
public int rows = 9; //This number are the usable tiles, the complete board is 11 by 11
//With a border of 1 tile
public Node[,] graph;
//Class Node
public class Node
{
public List<Node> neighbours; //List of all the neighbours nodes (4Directions)
public Node()
{
neighbours = new List<Node>();
}
}
//Sets up the outer walls and floor (background) of the game board.
void BoardSetup()
{
for (int x = -1; x < columns + 1; x++)
{
for (int y = -1; y < rows + 1; y++)
{
Edited out Code, don't think has anything to do with my problem.
}
}
}
//Creates the graph to use Pathfinding
void GeneratePathfindingGraph()
{
//Create graph
graph = new Node[columns, rows];
for (int x = -1; x < columns + 1; x++)
{
for (int y = -1; y < rows + 1; y++)
{
graph[x, y] = new Node();
Add the 4 cardinal adjacent tiles
if (x != - 1)
graph[x, y].neighbours.Add(graph[x - 1, y]);
if (x != columns - 1)
graph[x, y].neighbours.Add(graph[x + 1, y]); //Where I am getting the error
if (y != - 1)
graph[x, y].neighbours.Add(graph[x, y - 1]);
if (y != rows - 1)
graph[x, y].neighbours.Add(graph[x, y + 1]);
}
}
}
//SetupScene initializes our level and calls the previous functions to lay out the game board
public void SetupScene(int level)
{
//Creates the outer walls and floor.
BoardSetup();
//Creates the Node map to use Pathfinding
GeneratePathfindingGraph();
}
}
I have edited out most of the code in this class, I don't think it has much to do with the problem.
Can you change all the initialization in the loop like x = -1;to x = 0;
Arrays always start's with a ZERO.

Rotating matrices

Objectives
Imagine that, we have matrix like
a11 a12 a13
a21 a22 a23
a31 a32 a33
What I want to do is, from textbox value rotate this matrix so that, for example if I write 2 and press rotate, program must keep both diagonal values of matrix (in this case a11, a22, a33, a13, a31) and rotate 2 times clockwise other values. So result must be like
a11 a32 a13
a23 a22 a21
a31 a12 a33
It must work for all N x N size matrices, and as you see every 4 rotation takes matrix into default state.
What I've done
So idea is like that, I have 2 forms. First takes size of matrix (1 value, for example if it's 5, it generates 5x5 matrix). When I press OK it generates second forms textbox matrix like that
Form 1 code
private void button1_Click(object sender, EventArgs e)
{
int matrixSize;
matrixSize = int.Parse(textBox1.Text);
Form2 form2 = new Form2(matrixSize);
form2.Width = matrixSize * 50 + 100;
form2.Height = matrixSize *60 + 200;
form2.Show();
//this.Hide();
}
Form 2 code generates textbox matrix from given value and puts random values into this fields
public Form2(int matrSize)
{
int counter = 0;
InitializeComponent();
TextBox[] MatrixNodes = new TextBox[matrSize*matrSize];
Random r = new Random();
for (int i = 1; i <= matrSize; i++)
{
for (int j = 1; j <= matrSize; j++)
{
var tb = new TextBox();
int num = r.Next(1, 1000);
MatrixNodes[counter] = tb;
tb.Name = string.Format("Node_{0}{1}", i, j);
Debug.Write(string.Format("Node_{0}{1}", i, j));
tb.Text = num.ToString();
tb.Location = new Point(j * 50, i * 50);
tb.Width = 30;
tb.Visible = true;
this.splitContainer1.Panel2.Controls.Add(tb);
counter++;
}
}
}
Form 2 has 1 textbox for controlling rotation (others are generated on the fly, programmatically). What I want to do is, when I enter rotation count and press Enter on this textbox, I want to rotate textbox matrix as I explained above. Can't figure out how to do it.
Copy both diagonals to separate arrays, then rotate your matrix and replace diagonals. Below code shows each step:
class Program
{
static void Main(string[] args)
{
int matrixSize = 3;
string[,] matrix = new string[matrixSize,matrixSize];
//create square matrix
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
matrix[x, y] = "a" + (x + 1).ToString() + (y + 1).ToString();
}
}
Console.WriteLine(Environment.NewLine + "Base square matrix");
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(matrix[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
//copy diagonals
string[] leftDiagonal = new string[matrixSize];
string[] rightDiagonal = new string[matrixSize];
for (int x = 0; x < matrixSize; x++)
{
leftDiagonal[x] = matrix[x, x];
rightDiagonal[x] = matrix[matrixSize - 1 - x, x];
}
Console.WriteLine(Environment.NewLine + "Diagonals");
for (int x = 0; x < matrixSize; ++x)
{
Console.Write(leftDiagonal[x] + " " + rightDiagonal[x] + Environment.NewLine);
}
Console.ReadKey();
//rotate matrix
string[,] rotatedMatrix = new string[matrixSize, matrixSize];
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
rotatedMatrix[x, y] = matrix[matrixSize - y - 1, x];
}
}
Console.WriteLine(Environment.NewLine + "Rotated");
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(rotatedMatrix[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
//rotate matrix again
string[,] rotatedMatrixAgain = new string[matrixSize, matrixSize];
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
rotatedMatrixAgain[x, y] = rotatedMatrix[matrixSize - y - 1, x];
}
}
Console.WriteLine(Environment.NewLine + "Rotated again");
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(rotatedMatrixAgain[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
//replace diagonals
for (int x = 0; x < matrixSize; x++)
{
rotatedMatrixAgain[x, x] = leftDiagonal[x];
rotatedMatrixAgain[matrixSize - 1 - x, x] = rightDiagonal[x];
}
Console.WriteLine(Environment.NewLine + "Completed" + Environment.NewLine);
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(rotatedMatrixAgain[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
}
}
I don't know C#, so I can only give a suggestion in pseudocode:
Input: An N by N matrix in
Output: The input matrix rotated as described in the OP out
for i = 1 to N
for j = 1 to N
if N - j != i and i != j // Do not change values on either diagonal
out[j][N-i] = in[i][j]
else
out[i][j] = in[i][j]
Disclaimer: This algorithm is untested. I suggest you use a debugger to check that it works as you want.
This seems like quite an unorthodox UI presentation, but you're not too far off in terms of being able to achieve your functionality. Instead of a linear array, a rectangular array will make your job much easier. The actual rotation could be implemented with a for loop repeating a single rotation (which would be implemented as in the case 1 code), but I've decided to combine them into the four possible cases. This actually allows you to enter a negative number for number of rotations. Which reminds me, you really should do more error checking. At least protect against int.Parse throwing an exception both places it's used (with a try catch block or by switching to int.TryParse) and make sure it returns a meaningful number (greater than 0, possibly set a reasonable maximum other than int.MaxValue) for matrixSize in button1_Click.
namespace RotatingMatrices
{
public class Form2 : Form
{
// note these class fields
private TextBox[,] matrixNodes;
private int matrixSize;
public Form2(int matrSize)
{
InitializeComponent();
// note these inits
matrixSize = matrSize;
matrixNodes = new TextBox[matrixSize, matrixSize];
Random r = new Random();
// note the new loop limits
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
var tb = new TextBox();
int num = r.Next(1, 1000);
// note the change in indexing
matrixNodes[i,j] = tb;
tb.Name = string.Format("Node_{0}_{1}", i, j);
Debug.Write(string.Format("Node_{0}_{1}", i, j));
tb.Text = num.ToString();
tb.Location = new Point(j * 50, i * 50);
tb.Width = 30;
tb.Visible = true;
this.splitContainer1.Panel2.Controls.Add(tb);
}
}
}
private void buttonRotate_Click(object sender, EventArgs e)
{
string[,] matrix = new string[matrixSize, matrixSize];
int rotations = (4 + int.Parse(textBoxRotations.Text)) % 4; // note the addition of and mod by 4
switch(rotations)
{
case 1: // rotate clockwise
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrix[j, matrixSize - i - 1] = matrixNodes[i, j].Text;
}
}
break;
case 2: // rotate 180 degrees
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrix[i, j] = matrixNodes[matrixSize - i - 1, matrixSize - j - 1].Text;
}
}
break;
case 3: // rotate counter-clockwise
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrix[i, j] = matrixNodes[j, matrixSize - i - 1].Text;
}
}
break;
default: // do nothing
return;
}
// restore diagonals
for(int i = 0; i < matrixSize; i++)
{
matrix[i, i] = matrixNodes[i, i].Text;
matrix[i, matrixSize - i - 1] = matrixNodes[i, matrixSize - i - 1].Text;
}
// write strings back to text boxes
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrixNodes[i, j].Text = matrix[i, j];
}
}
}
}
}
I decided to tackle the issue using a listView instead of a text box, which makes the logic easier for me. Using this method, I was able to think of the matrix as successive boxes. I start on the outside and move in toward the middle, changing the size of my box each time.
Also, rather than using two forms, I use one. At the top I have a textbox where the user enters the size they want the array to be, and a button labeled "Fill" (button2). And at the bottom I have a textbox where the user enters the degree of rotation. When they click "Rotate," it kicks off a process of adding values to linked lists, combining and shifting the list, and then writing back out to the matrix. I'm sure I made it more convoluted than it has to be, but it was a great learning exercise.
After looking over jerry's code above, I think I'm going to look into rectangular arrays. :)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Recycle
{
public partial class Form1 : Form
{
public int size;
public LinkedList<string> topRight = new LinkedList<string>();
public LinkedList<string> bottomLeft = new LinkedList<string>();
public LinkedList<string> myMatrix = new LinkedList<string>();
public LinkedList<string> shiftMatrix = new LinkedList<string>();
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
listView1.Clear();
size = int.Parse(textBox2.Text);
int c = 0;
int q = 0;
int w = 0;
string[] content = new string[size];
Random rnd = new Random();
for (c = 0; c < size; c++)
{
listView1.Columns.Add("", 25);
}
for (q = 0; q < size; q++)
{
for (w = 0; w < size; w++)
{
content[w] = rnd.Next(9,100).ToString();
}
ListViewItem lvi = new ListViewItem(content);
listView1.Items.Add(lvi);
}
}
public bool iseven(int size)
{
if (size % 2 == 0)
{
return true;
}
else
{
return false;
}
}
public void button1_Click(object sender, EventArgs e)
{
if (listView1.Items.Count < 3)
{
MessageBox.Show("Matrix cannot be rotated.");
return;
}
bool even = false;
int shift = int.Parse(textBox1.Text); //amount to shift by
int box = listView1.Items.Count - 1; //size of box
int half = Convert.ToInt32(listView1.Items.Count / 2);
int corner = 0; //inside corner of box
if (shift > listView1.Items.Count)
{
shift = shift % ((listView1.Items.Count - 2) * 4);
}
do
{
eachPass(shift, box, corner);
++corner;
--box;
} while (box >= half + 1);
}
public void eachPass(int shift, int box, int corner)
{
int x;
int i;
//Read each non-diagonal value into one of two lists
for (x = corner + 1; x < box; x++)
{
topRight.AddLast(listView1.Items[corner].SubItems[x].Text);
}
x = box;
for (i = corner + 1; i < box; i++)
{
topRight.AddLast(listView1.Items[i].SubItems[x].Text);
}
for (x = box - 1; x > corner; x--)
{
bottomLeft.AddLast(listView1.Items[box].SubItems[x].Text);
}
x = corner;
for (i = box - 1; i > corner; i--)
{
bottomLeft.AddLast(listView1.Items[i].SubItems[x].Text);
}
string myTest = "";
//join the two lists, so they can be shifted
foreach (string tR in topRight)
{
myMatrix.AddLast(tR);
}
foreach (string bL in bottomLeft)
{
myMatrix.AddLast(bL);
}
int sh;
//shift the list using another list
for (sh = shift; sh < myMatrix.Count; sh++)
{
shiftMatrix.AddLast(myMatrix.ElementAt(sh));
}
for (sh = 0; sh < shift; sh++)
{
shiftMatrix.AddLast(myMatrix.ElementAt(sh));
}
//we need the sizes of the current lists
int trCnt = topRight.Count;
int blCnt = bottomLeft.Count;
//clear them for reuse
topRight.Clear();
bottomLeft.Clear();
int s;
//put the shifted values back
for (s = 0; s < trCnt; s++)
{
topRight.AddLast(shiftMatrix.ElementAt(s));
}
for (s = blCnt; s < shiftMatrix.Count; s++)
{
bottomLeft.AddLast(shiftMatrix.ElementAt(s));
}
int tRn = 0;
int bLn = 0;
//write each non-diagonal value from one of two lists
for (x = corner + 1; x < box; x++)
{
listView1.Items[corner].SubItems[x].Text = topRight.ElementAt(tRn);
++tRn;
}
x = box;
for (i = corner + 1; i < box; i++)
{
listView1.Items[i].SubItems[x].Text = topRight.ElementAt(tRn);
++tRn;
}
for (x = box - 1; x > corner; x--)
{
listView1.Items[box].SubItems[x].Text = bottomLeft.ElementAt(bLn);
++bLn;
}
x = corner;
for (i = box - 1; i > corner; i--)
{
listView1.Items[i].SubItems[x].Text = bottomLeft.ElementAt(bLn);
++bLn;
}
myMatrix.Clear();
shiftMatrix.Clear();
topRight.Clear();
bottomLeft.Clear();
}
}
}

How can I create a square hextilemap?

I have this code:
int tX = 1;
for (int y = 0; y < ROWS; y++)
{
for (int x = 0; x < tX; x++)
{
Tile t = new Tile()
{
Texture = tile,
Position = new Microsoft.Xna.Framework.Point(x, y),
Troops = rnd.Next(1, 4),
OwnedByPlayerIndex = 0
};
t.Tap += tile_Tap;
if (t.Position.Y < ROWS)
tiles.Add(t);
}
tX += 2;
tX = (int)MathHelper.Clamp(tX, 0, COLS);
}
And what im trying to do is create a map within a rect, limiting the map by number of rows and cols.
But it does not work as it does not follow up and finishes the last corner, leaving it uncomplete
You appear to have some redudant logic in there, and your inner loop is not iterating over all columns. If you want to fill the entire rectangle then you don't need tX at all. Example:
for (int y = 0; y < ROWS; y++)
{
for (int x = 0; x < COLS; x++)
{
Tile t = new Tile()
{
Texture = tile,
Position = new Microsoft.Xna.Framework.Point(x, y),
Troops = rnd.Next(1, 4),
OwnedByPlayerIndex = 0
};
t.Tap += tile_Tap;
tiles.Add(t);
}
}
Additionally, from your screenshot it looks like if the entire bottom row were filled then the lower staggered hexes would overlap the bottom of the red rectangle. If you don't want to add those lower hexes then you'll need to add them conditionally:
...
if (y > 0 || 0 == (x & 1))
tiles.Add(t);

"Infinite" world problems

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).
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();
}
}
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.
This then calls the following function:
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;
}
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.
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...
Here is the rest of the code:
Generator class function:
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;
}
}
}
}
}
}
}
}
}
and the code that checks if the region's buffers need to be rebuilt:
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();
}
}
}
}
}
By the way, if Dirty is set to true this means that the buffers need to be regenerated.
Any ideas why this is not creating new regions at the front and why it is not scrolling properly?
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...
Thanks, Darestium
Just looking at this part
float distancetraveledx = player.Origin.X - player.Position.X;
You're subtracting position from origin, you should probably reverse the order and do this
float distancetraveledx = player.Position.X - player.Origin.X;
For example, if the player has moved from (0, 0, 0) to (10, 0, 0), the player has moved 10 units in the X direction, but your 'distancetraveledx' would give you 0 - 10, or -10.
Unrelated to your question:
Your variables will be easier to use if you follow CamelCase, with the first letter lower or upper-case:
float distanceTraveledX = player.Position.X - player.Origin.X;
In C# the convention is to have class names with the first letter capitalized, and variables with the first letter lower-case, like such:
MyClass myClass;

Categories