Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 366 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 Sad

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline 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
Online 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 Cheesy
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<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;

   }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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 Cheesy
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<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.

"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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

UprightPath (13 views)
2014-09-20 20:14:06

BurntPizza (26 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (95 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29

BurntPizza (52 views)
2014-09-07 01:13:42

Longarmx (39 views)
2014-09-07 01:12:14

Longarmx (45 views)
2014-09-07 01:11:22
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!