Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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  
  Randomly generating maps like in minicraft  (Read 2189 times)
0 Members and 1 Guest are viewing this topic.
Offline chromejs10

Junior Newbie





« Posted 2012-03-12 08:20:32 »

Hey guys. As a quick background, I've been coding Java for quite a while now but wanted to mess around with game programming for fun. I've been reading up on using the LWJGL library.

Anyways, I was wondering how/what the logic is to create a dynamic map in a 2D world (of course 3D world would also be interesting to know!). Notch did this in his live stream of Minicraft and I was wondering for an explanation of how to do it and maybe some code snippets. This is my first post on here so I hope this is the right section.

Thanks to any answers and/or advice!
Offline GabrielBailey74
« Reply #1 - Posted 2012-03-12 08:45:21 »

This is a method from 'THECH3RN0' off of youtube, he used this in exactly what you want..
(Mentioned Notch alot during his video, saying that these methods are not ''exactly'' the same as his, but it's a Distant Graphic algorithm of some type.
Basicly using nothing but pixels, creating a 3D looking envionment/movement, while using no Graphics2D.

Code Snippet: (randomly generating 8x8 walls)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
      Level level = game.level;
      int size = 10;
      for (int xBlock = -size; xBlock <= size; xBlock++) {
         for (int zBlock = -size; zBlock <= size; zBlock++) {
            Block block = level.create(xBlock, zBlock);
            Block east = level.create(xBlock + 1, zBlock);
            Block south = level.create(xBlock, zBlock + 1);
            if (block.solid) {
               if (!east.solid) {
                  renderWall(xBlock + 1, xBlock + 1, zBlock, zBlock + 1,
                        0);
               }
               if (!south.solid) {
                  renderWall(xBlock + 1, xBlock, zBlock + 1, zBlock + 1,
                        0);
               }
            } else {
               if (east.solid) {
                  renderWall(xBlock + 1, xBlock + 1, zBlock + 1, zBlock,
                        0);
               }
               if (south.solid) {
                  renderWall(xBlock, xBlock + 1, zBlock + 1, zBlock + 1,
                        0);
               }
            }
         }


The Level class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
public class Level {

   public final int width, height;
   public Block[] blocks;

   public Level(int width, int height) {
      this.width = width;
      this.height = height;
      blocks = new Block[width * height];
      Random random = new Random();
      for (int y = 0; y < height; y++) {
         for (int x = 0; x < width; x++) {
            Block block = null;
            if (random.nextInt(4) == 0) {
               block = new SolidBlock();
            } else {
               block = new Block();
            }
            blocks[x + y * width] = block;
         }
      }
   }

   public Block create(int x, int y) {
      if (x < 0 || y < 0 || x >= width || y >= height) {
         return Block.solidWall;
      }
      return blocks[x + y * width];
   }

}


Now..
I really didn't want to post this next snippet after looking at it because it's so large..
But here's the method renderWall():
(TONS of fun math XD)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
/** NOTE: Reason this method is so chunky is due to all bugs being patched. */
   public void renderWall(double xLeft, double xRight, double zDistanceLeft,
         double zDistanceRight, double yHeight) {

      double upCorrect = 0.0625;
      double rightCorrect = 0.0625;
      double forwardCorrect = 0.0625;
      double walkCorrect = -0.0625;

      double xcLeft = ((xLeft / 2) - (right * rightCorrect)) * 2;
      double zcLeft = ((zDistanceLeft / 2) - (forward * forwardCorrect)) * 2;

      double rotLeftSideX = xcLeft * cosine - zcLeft * sine;
      double yCornerTL = ((-yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2;
      double yCornerBL = ((+0.5 - yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2;
      double rotLeftSideZ = zcLeft * cosine + xcLeft * sine;
      double xcRight = ((xRight / 2) - (right * rightCorrect)) * 2;
      double zcRight = ((zDistanceRight / 2) - (forward * forwardCorrect)) * 2;
      double rotRightSideX = xcRight * cosine - zcRight * sine;
      double yCornerTR = ((-yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2;
      double yCornerBR = ((+0.5 - yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2;
      double rotRightSideZ = zcRight * cosine + xcRight * sine;
      double tex30 = 0;
      double tex40 = 8;
      double clip = 0.5;

      if (rotLeftSideZ < clip && rotRightSideZ < clip) {
         return;
      }
      if (rotLeftSideZ < clip) {
         double clip0 = (clip - rotLeftSideZ)
               / (rotRightSideZ - rotLeftSideZ);
         rotLeftSideZ = rotLeftSideZ + (rotRightSideZ - rotLeftSideZ)
               * clip0;
         rotLeftSideX = rotLeftSideX + (rotRightSideX - rotLeftSideX)
               * clip0;
         tex30 = tex30 + (tex40 - tex30) * clip0;
      }

      if (rotRightSideZ < clip) {
         double clip0 = (clip - rotLeftSideZ)
               / (rotRightSideZ - rotLeftSideZ);
         rotRightSideZ = rotLeftSideZ + (rotRightSideZ - rotLeftSideZ)
               * clip0;
         rotRightSideX = rotLeftSideX + (rotRightSideX - rotLeftSideX)
               * clip0;
         tex40 = tex30 + (tex40 - tex30) * clip0;
      }

      double xPixelLeft = (rotLeftSideX / rotLeftSideZ * height + width / 2);
      double xPixelRight = (rotRightSideX / rotRightSideZ * height + width / 2);
      /* boundary issue */
      if (xPixelLeft >= xPixelRight) {
         return;
      }

      int xPixelLeftInt = (int) (xPixelLeft);
      int xPixelRightInt = (int) (xPixelRight);

      if (xPixelLeftInt < 0) {
         xPixelLeftInt = 0;
      }

      if (xPixelRightInt > width) {
         xPixelRightInt = width;
      }

      /* 4 corner pins */
      /* y corner divided by left side, multiplied by height + height / 2 */
      double yPixelLeftTop = yCornerTL / rotLeftSideZ * height + height / 2.0;
      double yPixelLeftBottom = yCornerBL / rotLeftSideZ * height + height
            / 2.0;
      double yPixelRightTop = yCornerTR / rotRightSideZ * height + height
            / 2.0;
      double yPixelRightBottom = yCornerBR / rotRightSideZ * height + height
            / 2.0;

      /* TEXTURE MODIFIERS */
      double tex1 = 1 / rotLeftSideZ;
      double tex2 = 1 / rotRightSideZ;
      double tex3 = tex30 / rotLeftSideZ;
      double tex4 = tex40 / rotRightSideZ - tex3;
      for (int x = xPixelLeftInt; x < xPixelRightInt; x++) {
         /* left = left, if less than right, keep rendering (if no inversion) */
         double pixelRotation = (x - xPixelLeft)
               / (xPixelRight - xPixelLeft);
         double zWall = (tex1 + (tex2 - tex1) * pixelRotation);
         if (zBufferWall[x] > zWall) {
            continue;
         }
         zBufferWall[x] = zWall;
         int xTexture = (int) ((tex3 + tex4 * pixelRotation) / zWall);
         double yPixelTop = yPixelLeftTop + (yPixelRightTop - yPixelLeftTop)
               * pixelRotation;
         double yPixelBottom = yPixelLeftBottom
               + (yPixelRightBottom - yPixelLeftBottom) * pixelRotation;
         /* top & bottom */
         int yPixelTopInt = (int) (yPixelTop);
         int yPixelBottomInt = (int) (yPixelBottom);

         if (yPixelTopInt < 0) {
            yPixelTopInt = 0;
         }
         if (yPixelBottomInt > height) {
            yPixelBottomInt = height;
         }
         for (int y = yPixelTopInt; y < yPixelBottomInt; y++) {
            double pixelRotationY = (y - yPixelTop)
                  / (yPixelBottom - yPixelTop);
            int yTexture = (int) (8 * pixelRotationY);
            // pixels[x + y * width] = xTexture * 100 + yTexture * 100 *
           // 256;
           pixels[x + y * width] = Texture.fill.pixels[(xTexture & 7)
                  + (yTexture & 7) * 8];
            zBuffer[x + y * width] = 1 / (tex1 + (tex2 - tex1)
                  * pixelRotation) * 8;
         }
      }
   }


Hope it helps you out some mate.

Offline chromejs10

Junior Newbie





« Reply #2 - Posted 2012-03-12 08:59:58 »

Hey thanks for the quick reply!

Funny enough I actually just went through his a lot of his videos on youtube. The math in this is definitely hard to grasp via the code. I think I'll have to watch this video more closely on YouTube and see if he offers decent explanations. I only watched through his creating the floor and ceiling. One thing I've found difficult about getting into the game development is that a lot of tutorials and online videos skim over what a lot of the things do. A lot of hand waving if you will.

I mentioned the Notch stuff since in Minicraft he seemed to create maps by what resembles a "render clouds" filter in Photoshop. Pretty neat! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jammas615

Junior Member


Projects: 1



« Reply #3 - Posted 2012-03-12 09:07:00 »

he seemed to create maps by what resembles a "render clouds" filter in Photoshop. Pretty neat! Smiley

You're thinking of perlin noise and generating height maps i think.

Offline GabrielBailey74
« Reply #4 - Posted 2012-03-12 09:07:07 »

lol, he said it took him about 4 hours to think up how to explain to us what that method does exactly (to learn+play with).

Offline Archibald

Junior Member


Projects: 1



« Reply #5 - Posted 2012-03-12 11:11:57 »

Can you drop the link to the video?
Offline GabrielBailey74
« Reply #6 - Posted 2012-03-14 07:07:05 »

Here's Episode 17: http://www.youtube.com/watch?v=WkBMoCbbgO0&feature=bf_prev&list=UUQ-W1KE9EYfdxhL6S4twUNw&lf=plcp
you could of course surf your way back to the beginning.
Still coming out, last one was.. march sixth this year?

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.

CopyableCougar4 (22 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (26 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
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!