What I am currently doing is spawning some rooms
which are basically just rectangles. What I want to do now is just to separate them so that they are not overlapping.
I'm working on a small SDL based framework.
What I'm looking for is some simple algorithm or way that I can achieve this.
What I currently have is the following:
static void SeparateRooms(Room& r1, Room& r2)
{
if ( utils::IsOverlapping(r1.GetRect(), r2.GetRect()) )
{
constexpr int moveIncrement{ 3 };
if (r1.GetArea() > r2.GetArea())
{
const Vector2f smallToBigVector{ r1.GetPosition() - r2.GetPosition() };
const Vector2f smallToBigNormalized{ smallToBigVector.Normalized() };
r1.m_Rect.left += moveIncrement * smallToBigNormalized.x;
r1.m_Rect.bottom += moveIncrement * smallToBigNormalized.y;
}
else
{
const Vector2f smallToBigVector{ r2.GetPosition() - r1.GetPosition() };
const Vector2f smallToBigNormalized{ smallToBigVector.Normalized() };
r2.m_Rect.left += moveIncrement * smallToBigNormalized.x;
r2.m_Rect.bottom += moveIncrement * smallToBigNormalized.y;
}
}
}
This works, but only when the rectangles are not surrounding another rectangle. Is there some tweak that I can do to make this work or another way I can approach the problem?