• Create Account

## Why is this terrain using 4d simplex noise not tiling?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1MacD  Members

122
Like
0Likes
Like

Posted 30 October 2012 - 03:37 PM

Hi, guys!

I'm a bit of a lurker and have enjoyed and learned a lot from these forums. Usually I try to work everything out from what I find around here, but now I'm completely stuck.

I'm making my second game for android (shameless plug for Ringi, my first one ), so I'm programming in Java.

I'm trying to make a world map which is made up of tiles and wraps around in x and y directions. The tiling manager is up and running and now I'm trying to get it filled with something resembling terrain. The terrain is created once at the start of the game, so processing time isn't much of an issue and it will be postprocessed after creation to create something stylised/original.

Anyway, after some research I think tiling 4D simplex noise is what I want to use for this; I've found and read all I could find, including Gustavson's paper and GameDev's own JTippetts many posts on the subject. But I cannot, for the life of me, get it to tile. I get a decent terrain, but it just is not tiling:

Now, remember, I'm creating all the tiles at the start of a new game, so I have one method which loops through all the tiles (row per row) and in the same way looks up a pixel to put on the tile. After reading all I could find, I would have expected the following code to work and create a terrain which tiles in the x and y direction. Remember, this is how I thought it should work:

int[] noisedpixelsarray = new int[tilewidth * tileheight];
int absx = 0;
int absy = 0;

double s;
double t;
double m_loopx0;
double m_loopx1;
double m_loopy0;
double m_loopy1;
double dx;
double dy;

double nx;
double ny;
double nz;
double nw;

for (int i = 0; i<numberOfTilesHeightInt; i++)
{
for (int j = 0; j<numberOfTilesWidthInt; j++)
{
for (int localy = 0; localy < tileheight; localy++)
{
for (int localx = 0; localx < tilewidth; localx++)
{
absx = localx + (j * tilewidth);
absy = localy + (i * tileheight);

s = absx / (tilewidth * numberOfTilesWidthInt);
t = absy / (tileheight * numberOfTilesHeightInt);

//m_loop specifies the part of the map to loop
m_loopx1 = (double)(tilewidth * numberOfTilesWidthInt);
m_loopx0 = 0;
m_loopy1 = (double)(tileheight * numberOfTilesHeightInt);
m_loopy0 = 0;

dx = (m_loopx1-m_loopx0);
dy = (m_loopy1-m_loopy0);
//s=s* ((m_mapx1-m_mapx0)/(m_loopx1-m_loopx0));
//t=t*((m_mapy1-m_mapy0)/(m_loopy1-m_loopy0));
//these are commented out because if the map lenght is equals to the range over which you want the terrain to loop you get s=s*1=s, so we don't need this

nx = m_loopx0+ Math.cos(s * 2 * Math.PI)*dx/(2*Math.PI);
ny = m_loopy0+ Math.cos(t * 2 * Math.PI)*dy/(2*Math.PI);
nz = m_loopx0+ Math.sin(s * 2 * Math.PI)*dx/(2*Math.PI);
nw = m_loopy0+ Math.sin(t * 2 * Math.PI)*dy/(2*Math.PI);

noise = SimplexNoise4D.noise(nx, ny, nz, nw);

// Adjust range to [0, 1]
noise = ((noise + 1) / 2);

// Convert noise to colour
alpha = 255;
red = 0;
green = (int) (noise * 255);
blue = 0;
if (green > 155)
{
green = 0;
blue = 255;
}

// Bounds check colour. The persistence of octaves doesn't sum to one,
// this will catch any values the fly outside the valid range [0, 255].
//if (red > 255) red = 255;
//else if (red < 0) red = 0;
if (green > 255) green = 255;
else if (green < 0) green = 0;
//if (blue > 255) blue = 255;
//else if (blue < 0) blue = 0;

this.tileColor=Color.argb(alpha, red, green, blue);

noisedpixelsarray[localx + (localy * tilewidth)] = tileColor;
//saving the found noise pixel to a 1D pixel array which we later place into a bitmap
}
}

tmptmpTileBitmap.setPixels(noisedpixelsarray, 0, tmpTileBitmap.getWidth(), 0, 0, tmpTileBitmap.getWidth(), tmpTileBitmap.getHeight());

//draw the colour noisemap in grayscale:
_tmpTileCanvas.drawBitmap(tmptmpTileBitmap, 0, 0, tmpPaint);
//and draw on h-index-w-index text:
_tmpTileCanvas.drawText("w" + String.valueOf(j) + "-" + String.valueOf(i), tmpTileBitmap.getWidth() * 0.5f, tmpTileBitmap.getHeight() * 0.5f, penPaint);

saveTile(tmpTileBitmap, j, i);
}
}

However, this just gets me this:

After some thinking, I changed
nx = m_loopx0+ Math.cos(s * 2 * Math.PI)*dx/(2*Math.PI);
ny = m_loopy0+ Math.cos(t * 2 * Math.PI)*dy/(2*Math.PI);
nz = m_loopx0+ Math.sin(s * 2 * Math.PI)*dx/(2*Math.PI);
nw = m_loopy0+ Math.sin(t * 2 * Math.PI)*dy/(2*Math.PI);

into
nx = absx+ Math.cos(s * 2 * Math.PI)*dx/(2*Math.PI);
ny = absy+ Math.cos(t * 2 * Math.PI)*dy/(2*Math.PI);
nz = absx+ Math.sin(s * 2 * Math.PI)*dx/(2*Math.PI);
nw = absy+ Math.sin(t * 2 * Math.PI)*dy/(2*Math.PI);

and got this:

So I thought that I should scale something, and did it directly in the noise lookup:
noise = SimplexNoise4D.noise(nx, ny, nz, nw);

became:

noise = SimplexNoise4D.noise(nx/(tilewidth * numberOfTilesWidthInt), ny/(tileheight * numberOfTilesHeightInt), nz/(tilewidth * numberOfTilesWidthInt), nw/(tileheight * numberOfTilesHeightInt));

And now I do get terrain:

With a couple of octaves extra I get:

Which is fine as things go; I can now make terrain .... but it doesn't tile!

So, from the original code I changed m_loopx0 (which is a constant, being afaik the start of the part of the noisemap you want to tile) to absx and absy, which is the absolute x/y coordinate of the pixel we're doing the lookup for (in the megatile made from the smaller tiles), which of course changes linearly.
Couple that with a downscaling to the nx/ny/nz/nw WITHIN the noise lookup function and I finally get terrain, but non-tiling!

So, below is my code as it is now. It also includes the code with which I add two extra octaves and a simple colour mapping to get some water.

int[] noisedpixelsarray = new int[tilewidth * tileheight];
int absx = 0;
int absy = 0;

double s;
double t;
double m_loopx0;
double m_loopx1;
double m_loopy0;
double m_loopy1;
double dx;
double dy;

double nx;
double ny;
double nz;
double nw;

for (int i = 0; i<numberOfTilesHeightInt; i++)
{
for (int j = 0; j<numberOfTilesWidthInt; j++)
{
for (int localy = 0; localy < tileheight; localy++)
{
for (int localx = 0; localx < tilewidth; localx++)
{
absx = localx + (j * tilewidth);
absy = localy + (i * tileheight);

s = absx / (tilewidth * numberOfTilesWidthInt);
t = absy / (tileheight * numberOfTilesHeightInt);

//m_loop specifies the part of the map to loop
m_loopx1 = (double)(tilewidth * numberOfTilesWidthInt);
m_loopx0 = 0;
m_loopy1 = (double)(tileheight * numberOfTilesHeightInt);
m_loopy0 = 0;

dx = (m_loopx1-m_loopx0);
dy = (m_loopy1-m_loopy0);

//s=s* ((m_mapx1-m_mapx0)/(m_loopx1-m_loopx0)); //= 1 for delta map x / delta range of noisemap x
//t=t*((m_mapy1-m_mapy0)/(m_loopy1-m_loopy0)); //= 1

nx = absx + Math.cos(s * 2 * Math.PI)*dx/(2*Math.PI);
ny = absy + Math.cos(t * 2 * Math.PI)*dy/(2*Math.PI);
nz = absx + Math.sin(s * 2 * Math.PI)*dx/(2*Math.PI);
nw = absy + Math.sin(t * 2 * Math.PI)*dy/(2*Math.PI);

noise = SimplexNoise4D.noise(nx/(tilewidth * numberOfTilesWidthInt), ny/(tileheight * numberOfTilesHeightInt), nz/(tilewidth * numberOfTilesWidthInt), nw/(tileheight * numberOfTilesHeightInt));
////noise = (noise - (int) noise) * 1f;
noise += SimplexNoise4D.noise(nx/(tilewidth * numberOfTilesWidthInt * 0.5), ny /(tileheight * numberOfTilesHeightInt * 0.5), nz /(tilewidth * numberOfTilesWidthInt * 0.5), nw /(tileheight * numberOfTilesHeightInt * 0.5)) * 0.3;
noise += SimplexNoise4D.noise(nx/(tilewidth * numberOfTilesWidthInt * 0.05), ny /(tileheight * numberOfTilesHeightInt * 0.05), nz /(tilewidth * numberOfTilesWidthInt * 0.05), nw /(tileheight * numberOfTilesHeightInt * 0.05)) * 0.15;

// Adjust range to [0, 1]
//noise = ((noise + 1) / 2);
noise = ((noise + 1.45) / 2.9);
//noise = (noise - (int) noise) * 1f;

// Convert noise to colour
alpha = 255;
red = 0;
//green = (int) (128 + (noise*127) + (noise * 64) + (noise * 32));
green = (int) (noise * 255);
blue = 0;
if (green > 155)
{
green = 0;
blue = 255;
}

// Bounds check colour. The persistence of octaves doesn't sum to one,
// this will catch any values the fly outside the valid range [0, 255].
//if (red > 255) red = 255;
//else if (red < 0) red = 0;
if (green > 255) green = 255;
else if (green < 0) green = 0;
//if (blue > 255) blue = 255;
//else if (blue < 0) blue = 0;

this.tileColor=Color.argb(alpha, red, green, blue);

noisedpixelsarray[localx + (localy * tilewidth)] = tileColor;
}
}

tmptmpTileBitmap.setPixels(noisedpixelsarray, 0, tmpTileBitmap.getWidth(), 0, 0, tmpTileBitmap.getWidth(), tmpTileBitmap.getHeight());

//draw the colour noisemap in grayscale:
_tmpTileCanvas.drawBitmap(tmptmpTileBitmap, 0, 0, tmpPaint);
//and draw on h-index-w-index text:
_tmpTileCanvas.drawText("w" + String.valueOf(j) + "-" + String.valueOf(i), tmpTileBitmap.getWidth() * 0.5f, tmpTileBitmap.getHeight() * 0.5f, penPaint);

saveTile(tmpTileBitmap, j, i);
}
}

I'm sorry for the long post, but I really hope someone can figure out what I've done wrong here and how I can get the kind of terrain I'm looking for, but tiling, as it should using 4D simplex noise.

BONUS:
Thanks for reading all this, and I hope you'll excuse me, but I have one more small question which fits into the topic, so I thought it would be better to ask it here than to spam the forum with a new topic.
Once I get the tiling working, I of course want to get some "random" terrain going every time the terrain generation is run. Am I correct in thinking I can achieve this by getting a random (largish) number and adding that to the bounds of what the terrain tiles across? So basically just get the number and add that to x0, x1, y0 and y1 so that I'm basically looking at a different patch of noise?

Edited by MacD, 03 November 2012 - 12:42 AM.

### #2JTippetts  Moderators

12320
Like
1Likes
Like

Posted 30 October 2012 - 04:41 PM

It's a little tough reading your code, since it seems the forums ate your newlines, so correct me if I'm wrong about what you are trying to do.

1) You have an array of tiles, tiles indexed on i and j.
2) Each tile is a chunk of noise, the individual pixels of which are indexed on localx and localy.
3) You want to calculate each pixel of the tiles such that the whole map tiles seamlessly with itself

For reference, here is how the 2D tiling noise in ANL is constructed:
 {
size_t w=a.width();
size_t h=a.height();
static double pi2=3.141592*2.0;
size_t x,y;
for(x=0; x<w; ++x)
{
for(y=0; y<h; ++y)
{
double p=(double)x / (double)w;
double q=(double)y / (double)h;
double r;
double nx,ny,nz,nw,nu,nv,val=0.0;
double dx, dy, dz;
dx=ranges.loopx1-ranges.loopx0;
dy=ranges.loopy1-ranges.loopy0;
p=p*(ranges.mapx1-ranges.mapx0)/(ranges.loopx1-ranges.loopx0);
q=q*(ranges.mapy1-ranges.mapy0)/(ranges.loopy1-ranges.loopy0);
nx=ranges.loopx0 + cos(p*pi2) * dx/pi2;
ny=ranges.loopx0 + sin(p*pi2) * dx/pi2;
nz=ranges.loopy0 + cos(q*pi2) * dy/pi2;
nw=ranges.loopy0 + sin(q*pi2) * dy/pi2;
val=m.get(nx,ny,nz,nw);

a.set(x,y,val);
}
}


Now, comparing your code, these lines where you calculate your noise input coords look suspicious to me:

nx = absx + Math.cos(s * 2 * Math.PI)*dx/(2*Math.PI);
ny = absy + Math.cos(t * 2 * Math.PI)*dy/(2*Math.PI);
nz = absx + Math.sin(s * 2 * Math.PI)*dx/(2*Math.PI);
nw = absy + Math.sin(t * 2 * Math.PI)*dy/(2*Math.PI);


absx and absy are calculated as the global pixel coordinates of the sample, so they are going to change in value as the pixel coords change. In my original implementation, I use loopx0 and loopy0 here, which do not change. The only way the coordinates change is through the value of s and t, which should be calculated from the pixel coords.

In my implementation, I specify the ranges over which the noise loops explicitly, rather than tying the range to the tile coordinates. Similarly, I specify the ranges over which the tiles are mapped explicitly. The ranges loopx0 to loopx1 and loopy0 to loopy1 specify the range of noise mapped to the entire map. The ranges mapx0 to mapx1 and mapy0 to mapy1 specify the range of noise (a sub-range of the larger looping range) to map to a given tile. This would be calculated by dividing the larger loop range up into as many equal-sized pieces as there are tiles.

As far as getting different terrain, I implement a seeding technique where a seed is passed as another input to the noise function, and is hashed together with the input coordinates. Change the seed, get a different output.

Edited by JTippetts, 30 October 2012 - 04:41 PM.

### #3MacD  Members

122
Like
0Likes
Like

Posted 30 October 2012 - 05:28 PM

Thank you for looking it over. Thank you even more for the reply!

I knew I was doing something bad by replacing the static loopxy0 with a variable absxy, but it was the only thing which led to a resulting terrain. Looking at your explanation, am I correct that loopxy0 will always be 0 (if you take the noise from the range 0 to (tilewidth*NoOfTiles)?

Then I could define loopx/y/0/1 and dx and dy outside of the whole loop, and then define mapx and mapy per tile, instead of per pixel, but I would have to do the second s and t calculations (which I had commented out)?

I'm just trying to get this to work, so for now all I'm trying is to get a tiling from 0 to (tilewidth*NumberOfTiles) ... wouldn't that also mean that in
p=p*(ranges.mapx1-ranges.mapx0)/(ranges.loopx1-ranges.loopx0);
q=q*(ranges.mapy1-ranges.mapy0)/(ranges.loopy1-ranges.loopy0);

the loop1-loop0 is always constant, and map1-map0 (call this: dxmap) is also always a tilewidth or a tileheight (because for each tile, mapx0 AND mapx1 shift a tile's width?

Hmm, I think I'm starting to get it: and I think I understand now why using absx/y DID give me a terrain where loopxy0 didn't! I'll give it a shot right now!

PS: sorry about the code; it looked fine in preview I added a couple of carriage returns in one code block and now it seems to display ... ok. When I post the page looks ok too, but when I reload the page, everything is back to one-liners!

Edited by MacD, 30 October 2012 - 07:30 PM.

### #4Bacterius  Members

13102
Like
1Likes
Like

Posted 30 October 2012 - 11:44 PM

The reason it doesn't work is because simplex noise tiles over a continuous range, however when you cross the axes your terrain tiles automatically go from 0 to 9 instead of going -1, -2, ... the transition from 0 to 9 is not continuous and this breaks the tiling. Taking the absolute value fixes the issue, but it has its limitations too (e.g. the axis "mirrors" the terrain).

Edited by Bacterius, 30 October 2012 - 11:45 PM.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #5JTippetts  Moderators

12320
Like
1Likes
Like

Posted 02 November 2012 - 11:03 AM

Yes, loopx0 and loopx1 are constant. If you were to map the range specified by loop to the entire map all at once, that map would tile with itself. mapx0 and mapx1 are specified as sub-ranges of loop, and correspond to each sub-chunk of the map. So if the loop range is 0 to 1 and there are 10 sub-chunks across the map, mapx0 for the first chunk would be 0, mapx1 would be 0.1. For the second chunk, mapx0 would be 0.1, mapx1 would be 0.2. But for all chunks, loop would be the same.

Now, there is a problem with mapping the range [0, tilewidth*numberoftiles] to loop. Say your tile width is 64 and you have 100 tiles. This means the looping range would be 0 to 6400. Mapping this range of noise does not produce anything meaningful, basically just white noise, unless you turn the frequency of the noise function WAY down. This is because, in Perlin noise (gradient as well as simplex, even the value-noise variant) there is roughly 1 "feature" per integral coordinate at the default frequency of 1. This means that in the range 0,6400 there would be produced approximately 6400 "hills" or "valleys". The output would be very much like the third screenshot in your original post. You need to turn down the range specified in loop to correspond what you wish to map.

### #6MacD  Members

122
Like
0Likes
Like

Posted 03 November 2012 - 12:37 AM

Pffffttt. Finally got it running!

My main problem was that I didn't really grok the distinctions and relations between the mapping of the total loop, the resulting noisemap and the final tiles I wanted. I was trying a lot of wierd things with the mapx/y chunks, partial mapping etc. I think I saw pics of every way this could go wrong

Anyway, in the end it was quite easy: I just set the submap to the size of the loop and grabbed chunks of that using an offset:

for (int i = 0; i<numberOfTilesHeightInt; i++)
{
for (int j = 0; j<numberOfTilesWidthInt; j++)
{
for (int localy = (i * tileheight); localy < ((i+1) * tileheight); localy++)
{
for (int localx = (j * tilewidth); localx < ((j+1) * tilewidth); localx++)
{
s = (double) localx / (double) m_dxloop;
t = (double) localy / (double) m_dyloop;

nx = m_loopx0 + Math.cos(s * 2 * Math.PI)*m_dxloop/(2*Math.PI);
ny = m_loopy0 + Math.cos(t * 2 * Math.PI)*m_dyloop/(2*Math.PI);
nz = m_loopx0 + Math.sin(s * 2 * Math.PI)*m_dxloop/(2*Math.PI);
nw = m_loopy0 + Math.sin(t * 2 * Math.PI)*m_dyloop/(2*Math.PI);

noise = SimplexNoise4D.noise(nx* scalingfactor, ny * scalingfactor, nz * scalingfactor, nw * scalingfactor);
noise = ((noise + 1) / 2);
noisedpixelsarray[(localx - (j * tilewidth))+ ((localy - (i*tileheight)) * tilewidth)] = tileColor;
}
}
noiseTileBitmap.setPixels(noisedpixelsarray, 0, tilewidth, 0, 0, tilewidth, tileheight);

//draw the colour noisemap in grayscale:
tmpTileCanvas.drawBitmap(noiseTileBitmap, null, dstRect, tmpPaint);
//and draw on h-index-w-index text:
_tmpTileCanvas.drawText("w" + String.valueOf(j) + "-" + String.valueOf(i), tmpTileBitmap.getWidth() * 0.5f, tmpTileBitmap.getHeight() * 0.5f, penPaint);

saveTile(tmpTileBitmap, j, i);
}
}

Anyway, thanks for helping me out! Now I'm off to find some good octaves and making some postprocessing routines

PS: wtf? CODE tags eat my newlines again?!? It looks OK in preview, but as soon as I post and reload the page, it's all one line? Well, let's see what removing the tags does...

Edited by MacD, 03 November 2012 - 12:40 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.