- Home /
Procedurally Generate Field of Squares
I'm not sure at all how to go about this but what I want to do is generate a field of squares of random sizes but all fit within an allotted area (a larger square). The meshes generated are supposed to be planes but have to fit within a larger invisible plane, how can I go about doing this?
EDIT: Is there a way I can hold an array of arrays of Vector2's (the arrays of Vector2's would hold the four vertices of the squares, and the larger one all of the squares, that way I can check what positions for new vertices are available and which aren't)?
Answer by slayer29179 · Jun 24, 2013 at 09:25 PM
Why not use an Integer or Vector2 for the size of the larger square and add the random X and Y size to each number. For example:
int large_size_x = 0; //Lets say your large grid is 30 in size
int large_size_y = 0;
if (large_size_x < 30 && large_size_y < 30)
{
var xNum = Random.Range(1, 20); //Size of the actual grid
var chosenx = xNum; //Add the size of your smaller grid to the large overall variable.
//Spawn your grid here and use local transform to edit the size
var small_grid = Instantiate (gridObject, transform.position, Quaternion.identity);
large_size_x += chosenx;
small_grid.transform.position.x += chosenx;
}
Not Tested
Hope this gives you some grounds to go off :)
The main problem though is making sure the sizes of all of the squares fits within the larger area and also making sure they don't overlap, but thanks.
EDIT: Or maybe I was a bit confused as to what you were trying to say.
I think you may be confused :)
The two top large_size variables are there as the overall size of all your small grids added up.
And then you use the If statement against the two large_size variables to deter$$anonymous$$e what size you have now.
Every time you spawn you increase the variables by the size of the grid.
For example, if I spawned a grid which was 5x5. I would increase large_size_x and y by 5 each. Then I would keep increasing until I reached 30 on my large_size_x and y.
I hope this explains it slightly better :)