Hello, I am working on building a procedural city with unity - Ive had the script split up the city area into smaller areas , which im placing buildings.
[Preview][1]
As you can see, the problem is that the buildings are forming inside each other so ive added this method before placing them to log the previous positions of each rectangle, and store them in a list.
void GetRandomIndustrialBiome(BiomeTile Tile ,IndustrialGenerator IndGen)
{
float radius = Tile.Size/2;
int numberofbuildings = Mathf.RoundToInt(Random.Range(1, 4));
Rect[] ForbiddenRegions = new Rect[numberofbuildings];
for (int i = 0; i < numberofbuildings; i ++)
{
int RandomX = Mathf.RoundToInt(Random.Range(-Tile.Size, Tile.Size));
int RandomY = Mathf.RoundToInt(Random.Range(-Tile.Size, Tile.Size));
int RandomYL = Mathf.RoundToInt(Random.Range(radius, radius * 2));
int RandomXL = Mathf.RoundToInt(Random.Range(radius, +radius * 2));
int Height = Mathf.RoundToInt(Random.Range(1, 4));
// WTF?
Rect CheckPosition = new Rect(RandomX + Tile.X, RandomYL + Tile.Y, RandomXL, RandomY);
bool isCollision = false;
for (int j = 0; j < ForbiddenRegions.Length; j++)
{
isCollision = RectangleIntersect(ForbiddenRegions*, CheckPosition);*
-
}*
-
if (!isCollision)*
-
{*
-
IndGen.GenerateBuilding(RandomX + Tile.X, RandomY + Tile.Y,Height,RandomYL, RandomXL,Quaternion.identity);*
_ ForbiddenRegions = CheckPosition;_
* }*
* }*
* }*
Here is the method im using to determine intersections:
bool RectangleIntersect(Rect a, Rect b)
* {*
* return !(a.xMax < b.x || a.x > b.xMax || a.y < b.yMax || a.yMax > b.y);*
* }*
I have been playing around with these values for quite some time and cant get it to work properly!
**Just to clarify:
I am trying to not get buildings to be placed ontop of each other, by means of holding all the positions of the previously placed buildings inside an array of rect, then before placing a new building, check the array for collisions.**
If anyone can help I would be extremely grateful!
Thanks!
_*[1]: http://imgur.com/MMW3w7B*_