• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
MacD

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

5 posts in this topic

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 [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]), 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:

[img]http://img.photobucket.com/albums/v506/MacD/terrain/litingterrain_3octaves_tile.png[/img]

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:

[img]http://img.photobucket.com/albums/v506/MacD/terrain/litingterrain_flat.png[/img]

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:
[img]http://img.photobucket.com/albums/v506/MacD/terrain/litingterrain_noisy.png[/img]

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

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


And now I do get terrain:
[img]http://img.photobucket.com/albums/v506/MacD/terrain/litingterrain_oneoctave.png[/img]

With a couple of octaves extra I get:
[img]http://img.photobucket.com/albums/v506/MacD/terrain/litingterrain_3octaves.png[/img]

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
0

Share this post


Link to post
Share on other sites
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:
[code]
{
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);
}
}
[/code]

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

[code]
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);
[/code]

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
1

Share this post


Link to post
Share on other sites
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
[CODE]
p=p*(ranges.mapx1-ranges.mapx0)/(ranges.loopx1-ranges.loopx0);
q=q*(ranges.mapy1-ranges.mapy0)/(ranges.loopy1-ranges.loopy0);
[/CODE]
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 [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] 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
0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0