Sorry if the title isn't clear, I'm not really sure how to word this. I'm very new to this, so if I'm not clear on something just ask and I can try and word it better.
I am using binary space partitioning for procedural level generation in a 2d game. I have a binary tree where each node contains it's parent, a rectangle named room, childA, and childB. childA.room and childB.room are the results of splitting room. The sum of childA and childB's room sizes is equal to the room's size, but childA and childB's room sizes are not guaranteed to be equal. The axis that the rectangle is cut across will most likely be different each time, but again that's not guaranteed.
So far, all of this is fine. I'm getting rectangles with resulting sizes that I am happy with. Now, I'm stuck on the (seemingly) trivial part of positioning the rectangles. I assume that one of the rectangles doesn't even need to be moved. Below is the closest I've got, it works correctly on the first split but not on any splits after. Again, I'm sorry if I missed any important information or was unclear, just ask.
public List<BinaryNode> SplitNode(BinaryNode nodeToSplit, Vector2 splitParam)
{
var nodeList = new List<BinaryNode>();
var tempSplitParam = new Vector2();
var TempChildA = new BinaryNode();
var TempChildB = new BinaryNode();
TempChildA.parent = nodeToSplit;
TempChildB.parent = nodeToSplit;
tempSplitParam = Vector2.zero;
tempSplitParam.x = Random.Range(splitParam.x, splitParam.y); //select the percentage that each child room gets to keep, somewhere between the splitParams x and y
tempSplitParam.y = 1 - tempSplitParam.x; //x + y will always = 1. child a gets x%, child b gets y%, all the space is used.
//randomly decide whether to split on the x or y, only on the FIRST cut.
if (Random.value > .5 && nodeToSplit.parent == null)//y axis
{
TempChildA.room.size = new Vector2(Mathf.Round(nodeToSplit.room.size.x * tempSplitParam.x), nodeToSplit.room.size.y);
TempChildB.room.size = new Vector2(Mathf.Round(nodeToSplit.room.size.x * tempSplitParam.y), nodeToSplit.room.size.y);
TempChildB.room.position = new Vector2(TempChildA.room.size.x + TempChildB.room.position.x, TempChildA.room.position.y);
} else
if(nodeToSplit.parent == null)//x axis
{
TempChildA.room.size = new Vector2(nodeToSplit.room.size.x, (Mathf.Round(nodeToSplit.room.size.y * tempSplitParam.x)));
TempChildB.room.size = new Vector2(nodeToSplit.room.size.x, (Mathf.Round(nodeToSplit.room.size.y * tempSplitParam.y)));
TempChildB.room.position = new Vector2(TempChildB.room.position.x, TempChildA.room.size.y + TempChildB.room.position.x);
} else
if (nodeToSplit.room.width > nodeToSplit.room.height) //cut across the y axis if the room is longer than it is tall
{
TempChildA.room.size = new Vector2(Mathf.Round(nodeToSplit.room.size.x * tempSplitParam.x), nodeToSplit.room.size.y);
TempChildB.room.size = new Vector2(Mathf.Round(nodeToSplit.room.size.x * tempSplitParam.y), nodeToSplit.room.size.y);
TempChildB.room.position = new Vector2(TempChildA.room.size.x + TempChildB.room.position.x, TempChildA.room.position.y);
} else //cut across the x axis otherwise
{
TempChildA.room.size = new Vector2(nodeToSplit.room.size.x, (Mathf.Round(nodeToSplit.room.size.y * tempSplitParam.x)));
TempChildB.room.size = new Vector2(nodeToSplit.room.size.x, (Mathf.Round(nodeToSplit.room.size.y * tempSplitParam.y)));
TempChildB.room.position = new Vector2(TempChildB.room.position.x, TempChildA.room.size.y + TempChildB.room.position.x);
}
nodeToSplit.childA = TempChildA;
nodeToSplit.childB = TempChildB;
nodeList.Add(TempChildA);
nodeList.Add(TempChildB);
return nodeList;
}