[LibGDX] Box2D Random room generation

So I am working on a way to randomly generate square/rectangle rooms and join them all together but kind of unsure how to approach it as I have never done random level generation.

I do not have much code to show but here is basically the approach I am taking:

for (int i = 0; i < 4; i++) {
			polyShape.setAsBox(width, length,
					new Vector2((length - width) * MathUtils.cos(lastAngle), (length - width)
							* MathUtils.sin(lastAngle)), lastAngle);
			fd.shape = polyShape;
			wall = body.createFixture(fd);

			lastAngle += 90 * MathUtils.degRad;
		}

This creates 4 fixtures around the centre of the body, works fine as show below:

http://s26.postimg.org/y9thiw8jt/Room_Example.png

But say I want one of the walls to have a doorway? What would be the best approach? Only loop 3x and then create another 2 fixtures with a gap in the middle?

Then the problem goes…if I randomly generate these rooms, I need to find out where that gap is so I can have the next room through the door lol.

Sorry but level generation is all new to me :frowning:

Anyone got a way I can do this? I am thinking of making the walls like tiles:

F = fixture;
B = body;

FFFFFF
F F
F F
F B F
F F
F F
FFFFFF

Sort of like that, what I am doing atm to achieve this looks fricking horrible:

float roomSize = 5;
		float wallNum = -roomSize;
		for (float i = -roomSize; i < roomSize + 1; i++) {
			for(float y = -roomSize; y < roomSize + 1; y++){
				polyShape.setAsBox(size, size, new Vector2(wallNum, -roomSize), lastAngle);
				fd.shape = polyShape;
				wall = body.createFixture(fd);
				polyShape.setAsBox(size, size, new Vector2(-roomSize, wallNum), lastAngle);
				fd.shape = polyShape;
				wall = body.createFixture(fd);
				break;
				
			}
			polyShape.setAsBox(size, size, new Vector2(wallNum, roomSize), lastAngle);
			fd.shape = polyShape;
			wall = body.createFixture(fd);
			polyShape.setAsBox(size, size, new Vector2(roomSize, wallNum), lastAngle);
			fd.shape = polyShape;
			wall = body.createFixture(fd);
			wallNum++;
			
		}

Surely there is something I can do here, I have the center point + vector + the vector and angle of the shape. Is there a way to do this using trig? (Which I suck at).

You should make it as much OO (Object Oriented) as possible. Doing this in loops would be a real pain.

You should create a class room. It should contain boundaries of the room, as well as were the exists/doors reside.
When you want to add/connect another room, you create a new room and put doors depending on the first room’s position and stuff.

Here is something to spark imagination :smiley:


	public class Room {
		
		public float x0, y0, x1, y1;
		public List<Door> doors = new ArrayList<Door>(); 

		public Room(float x0, float y0, float x1, float y1) {
			// set the position of the room
		}
		
		public Room(Door door) {
			// set the position of the room according to were the door is.
		}
		
	}
	
	public class Door {
		
		public float x0, y0, x1, y1;

	}

Well yeah I have base classes for wall segment and room atm but I am mainly messing around with the code to try and figure out how to actually generate a room with very little code lol.