Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMacD

Posted 03 November 2012 - 12:42 AM

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 Posted Image), 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:

Posted Image

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:

Posted Image

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:
Posted Image

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:
Posted Image

With a couple of octaves extra I get:
Posted Image

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?

#3MacD

Posted 30 October 2012 - 05:06 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 Posted Image), 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:

Posted Image

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:

[source lang="java"]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); } }[/source]


However, this just gets me this:

Posted Image

After some thinking, I changed
[source lang="java"]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);[/source]

into
[source lang="java"]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);[/source]


and got this:
Posted Image

So I thought that I should scale something, and did it directly in the noise lookup:
noise = SimplexNoise4D.noise(nx, ny, nz, nw);
became:
[source lang="java"]noise = SimplexNoise4D.noise(nx/(tilewidth * numberOfTilesWidthInt), ny/(tileheight * numberOfTilesHeightInt), nz/(tilewidth * numberOfTilesWidthInt), nw/(tileheight * numberOfTilesHeightInt));[/source]


And now I do get terrain:
Posted Image

With a couple of octaves extra I get:
Posted Image

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.

[source lang="java"]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); } }[/source]
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?

#2MacD

Posted 30 October 2012 - 05:02 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 Posted Image), 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:

Posted Image

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:

[source lang="java"]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); } }[/source]


However, this just gets me this:

Posted Image

After some thinking, I changed
[source lang="java"]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);[/source]

into
[source lang="java"]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);[/source]


and got this:
Posted Image

So I thought that I should scale something, and did it directly in the noise lookup:
noise = SimplexNoise4D.noise(nx, ny, nz, nw);
became:
[source lang="java"]noise = SimplexNoise4D.noise(nx/(tilewidth * numberOfTilesWidthInt), ny/(tileheight * numberOfTilesHeightInt), nz/(tilewidth * numberOfTilesWidthInt), nw/(tileheight * numberOfTilesHeightInt));[/source]


And now I do get terrain:
Posted Image

With a couple of octaves extra I get:
Posted Image

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.

[source lang="java"]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); } }[/source]
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?

#1MacD

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 Posted Image), 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:

Posted Image

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:

Posted Image

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:
Posted Image

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:
Posted Image

With a couple of octaves extra I get:
Posted Image

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?

PARTNERS