Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 [LibGDX] Box2D Random room generation  (Read 579 times) 0 Members and 1 Guest are viewing this topic.
Gibbo3771
 « Posted 2014-02-11 15:59:17 »

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:

 1  2  3  4  5  6  7  8  9 `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:

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

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Gibbo3771
 « Reply #1 - Posted 2014-02-12 13:16:27 »

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:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 `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).

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
trollwarrior1
 « Reply #2 - Posted 2014-02-12 13:21:48 »

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
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 `   public class Room {            public float x0, y0, x1, y1;      public List doors = new ArrayList();       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;   }`
 Games published by our own members! Check 'em out!
Gibbo3771
 « Reply #3 - Posted 2014-02-12 13:25:54 »

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
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 `   public class Room {            public float x0, y0, x1, y1;      public List doors = new ArrayList();       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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 BurntPizza (28 views) 2015-04-23 03:42:11 theagentd (32 views) 2015-04-22 16:23:07 Riven (45 views) 2015-04-16 10:48:47 Duke0200 (56 views) 2015-04-16 01:59:01 Fairy Tailz (40 views) 2015-04-14 20:13:12 Riven (42 views) 2015-04-12 21:36:37 bus hotdog (58 views) 2015-04-10 02:39:32 CopyableCougar4 (63 views) 2015-04-10 00:51:04 BurntPizza (64 views) 2015-04-06 22:06:58 ags1 (65 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 16x wessles 15x alwex 11x 65K 11x kingroka123 11x kevglass 8x Rayvolution 8x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x Olo 7x ra4king 7x Ecumene 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org