Note that there are some explanatory texts on larger screens.

plurals
  1. POProcedural terrain texture with lines and zones
    primarykey
    data
    text
    <p>I am currently making a program to procedurally generate 2d terrain maps, with different technics such as perlin noise, simplex, voronoi, fractal noise, etc. on a size-defined image to be able to use it in my games requiring a 2d terrain.</p> <p>I've come across the "Modelling fake planets" section of <a href="http://paulbourke.net/fractals/noise" rel="nofollow">http://paulbourke.net/fractals/noise</a> and I need to make it on a 2d texture, and not on a 3d world like it is explained.</p> <p>Now I'm trying to</p> <ol> <li>create a line from point 'X' to point 'Y'</li> <li>That line will define a zone with a boolean value for left or right of the line to be "darker".</li> <li>Doing that for a number of iteration to create a texture.</li> <li>Using the RGB value of the final image to change stuffs such as forests, lakes, etc.</li> </ol> <p><strong>this would work this way:</strong></p> <p>overrides with this method below,</p> <p><a href="http://img35.imageshack.us/img35/24/islf.png" rel="nofollow">http://img35.imageshack.us/img35/24/islf.png</a></p> <p>I used my high school maths powers to create a code sample but it's not really working...</p> <p><strong>Questions:</strong></p> <ol> <li>How should i change it so it works instead of just being failing?</li> <li>Is there a simpler way than using what i am using?</li> </ol> <p><strong>Java file:</strong> if i need an example on how i will proceed, here it is:</p> <pre><code>package Generator; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random; import VectorialStuffs.Vector2; public class Linear { public static BufferedImage generateImage(Dimension dim, int iterations) { BufferedImage image = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); //point X and point Y Vector2 pointX; Vector2 pointY; //difference between those Vector2 diff; Vector2 side; double slope; //random Random rand = new Random(); boolean direction; //the orientation of the dark zone. (left/right) for (int i = 0; i &lt; iterations; ++i) { pointX = new Vector2(0, 0); pointY = new Vector2(0, 0); direction = rand.nextBoolean(); System.out.println(direction); side = new Vector2(0, 0); //there are 4 sides of the image. while (side.x == side.y) { side.x = rand.nextInt(3); //0 - 1 - 2 - 3 side.y = rand.nextInt(3); } switch(side.x) //not the x coord, the X point! ;D { //x = random and y = 0 case 0: pointX.x = rand.nextInt(dim.width); pointX.y = 0; break; //x = max and y = random case 2: pointX.x = dim.width; pointX.y = rand.nextInt(dim.height); break; //x = random and y = max case 1: pointX.x = rand.nextInt(dim.width); pointX.y = dim.height; break; //x = 0 and y = random case 3: pointX.x = 0; pointX.y = rand.nextInt(dim.height); break; } switch(side.y) //not the y coord, the Y point! ;D { //x = random and y = 0 case 0: pointY.x = rand.nextInt(dim.width); pointY.y = 0; break; //x = max and y = random case 2: pointY.x = dim.width; pointY.y = rand.nextInt(dim.height); break; //x = random and y = max case 1: pointY.x = rand.nextInt(dim.width); pointY.y = dim.height; break; //x = 0 and y = random case 3: pointY.x = 0; pointY.y = rand.nextInt(dim.height); break; } diff = new Vector2((pointY.x - pointX.x), (pointY.y - pointX.y)); slope = diff.y / diff.x; Graphics graph = image.getGraphics(); if (direction) //true = right | false = left { int start; //the start x coordinate, on the line then increases until reaching the end of the image int end = dim.width; graph.setColor(Color.red); graph.fillRect(pointX.x - 8, pointX.y -8, 16, 16); graph.setColor(Color.yellow); graph.fillRect(pointY.x - 8, pointY.y -8, 16, 16); for (int times = 0; times &lt; dim.height; ++times) //horizontal drawer { System.out.println(times); start = (int)((times-diff.y)/slope + diff.y); //this is where it goes wrong? for (int value = start; value &lt; end; ++value) { graph.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), 100)); graph.fillRect(value, times, 1, 1); } } graph.dispose(); } else { int start; //the start x coordinate, on the line then increases until reaching the end of the image int end = dim.width; graph.setColor(Color.red); graph.fillRect(pointX.x - 8, pointX.y -8, 16, 16); graph.setColor(Color.yellow); graph.fillRect(pointY.x - 8, pointY.y -8, 16, 16); for (int times = 0; times &lt; dim.height; ++times) //horizontal drawer { System.out.println(times); start = (int)((times-diff.y)/slope); for (int value = end; value &lt; start; --value) { graph.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), 100)); graph.fillRect(value, times, 1, 1); } } graph.dispose(); } } return image; } } </code></pre> <p><strong>Note:</strong> In this case vector2 is just a class with X and Y, which can be accessed (this is probably going to be temporary).</p> <p><strong>Startup part to avoid you losing time:</strong></p> <pre><code>terrainImage = Linear.generateImage(size, 1); //size being a Dimension. -&gt; "new Dimension(256, 256)" if (terrainImage != null) { Icon wIcon = new ImageIcon(terrainImage); JOptionPane.showMessageDialog(null, "message", "title", JOptionPane.OK_OPTION, wIcon); } </code></pre> <p><strong>//edit</strong> here is the code that needs improvement:</p> <pre><code>if (direction) //true = right | false = left { int start; //the start x coordinate, on the line then increases until reaching the end of the image int end = dim.width; graph.setColor(Color.red); graph.fillRect(pointX.x - 8, pointX.y -8, 16, 16); graph.setColor(Color.yellow); graph.fillRect(pointY.x - 8, pointY.y -8, 16, 16); for (int times = 0; times &lt; dim.height; ++times) //horizontal drawer { System.out.println(times); start = (int)((times-diff.y)/slope + diff.y); //this is where it goes wrong? for (int value = start; value &lt; end; ++value) { graph.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), 100)); graph.fillRect(value, times, 1, 1); } } graph.dispose(); } else { int start; //the start x coordinate, on the line then increases until reaching the end of the image int end = dim.width; graph.setColor(Color.red); graph.fillRect(pointX.x - 8, pointX.y -8, 16, 16); graph.setColor(Color.yellow); graph.fillRect(pointY.x - 8, pointY.y -8, 16, 16); for (int times = 0; times &lt; dim.height; ++times) //horizontal drawer { System.out.println(times); start = (int)((times-diff.y)/slope); for (int value = end; value &lt; start; --value) { graph.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), 100)); graph.fillRect(value, times, 1, 1); } } graph.dispose(); } </code></pre> <p>i can't get it to work like i showed in the picture above, all it does is either nothing, or offset from the 2 points. Also, sometimes it freezes for no reason, so idk what will happen if i make more iterations of this :/</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload