• Create Account

Banner advertising on our site currently available from just \$5!

# MacD

Member Since 30 Mar 2010
Offline Last Active Jan 05 2013 10:21 AM

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

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?

### [.net] C# CF 3.5 why isn't this loop being entered?

27 April 2010 - 01:47 PM

```int[,] gridArray;
int gridArraySize = 0;
void gridOKBtn_Click(object sender, EventArgs e)
{
if ((gridWidthTb.Text.Length == 0) || (gridXSpacingTb.Text.Length == 0) || (gridYSpacingTb.Text.Length == 0) || (fromCentreRb.Checked == false && fromTopleftRb.Checked == false) )
{
MessageBox.Show("missing grid variables!");
}
else
{
int width = int.Parse(gridWidthTb.Text);
int xspacing = int.Parse(gridXSpacingTb.Text);
int yspacing = int.Parse(gridYSpacingTb.Text);
int halfwidth = ((int)(this.ClientSize.Width * 0.5));
int halfheight = ((int)(this.ClientSize.Height * 0.5));
//AddGridLine(gridArray, 1 , width, 240, 0, 240, 800, 0); //this does get called

if (fromCentreRb.Checked) //calc gridlines from centre
{
//for x to right of centre
for (int xstart = halfwidth; xstart >= this.ClientSize.Width; xstart = (xstart + xspacing))
{
int ystart = 0;
int yend = this.ClientSize.Height;
int xend = xstart;

MessageBox.Show(gridArray.GetLength(0).ToString());
AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
MessageBox.Show("added " + xstart.ToString() + ", " + ystart.ToString() + ", " + xend.ToString() + ", " + yend.ToString());
}
//for xlines Left of centre line
for (int xstart = (halfwidth - xspacing); xstart <= 0; xstart = (xstart - xspacing))
{
int ystart = 0;
int yend = this.ClientSize.Height;
int xend = xstart;

AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
}
//for y down from centre
for (int ystart = halfheight; ystart >= this.ClientSize.Height; ystart = (ystart + yspacing))
{
int yend = ystart;
int xstart = 0;
int xend = this.ClientSize.Width;

AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
}
//for y up from centre
for (int ystart = (halfheight - yspacing); ystart <= 0; ystart = (ystart - yspacing))
{
int yend = ystart;
int xstart = 0;
int xend = this.ClientSize.Width;

AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
}
//MessageBox.Show(gridArray.GetLength(0).ToString()); //this gets called too
}
else  //calc gridlines from topleft
{
for (int xstart = 0; xstart >= this.ClientSize.Width; xstart = (xstart + xspacing))
{
int xend = xstart;
int ystart = 0;
int yend = this.ClientSize.Height;

MessageBox.Show("xlines");
AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
MessageBox.Show("xlinesend");
}
//then do y lines
for (int ystart = 0; ystart >= this.ClientSize.Height; ystart = (ystart + yspacing))
{
int yend = ystart;
int xstart = 0;
int xend = this.ClientSize.Width;

MessageBox.Show("ylines");
AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
MessageBox.Show("ylinesend");
}
}
//MessageBox.Show(gridArray.GetLength(0).ToString());
this.Invalidate();
}
}

```
As I said, the Textbox.Text gets transfered/captured, and the AddGridLine() method works (if I put it in the IF statement, before the FOR loop). The first MessageBox, just before the first ELSE, gets called too (duh, really). If I put anything (msgbox, a AddGridLine() call) after the IF stament, before the first FOR loop, that gets called too! But anything IN the FOR loops? No go. Why? I just can't understand.... is it my itterator? Naw, cause that would mean the loop still should get called once, and it doesn't. *sigh*. I'm really starting to feel dumb :)

### [.net] C# CF3.5 Cannot get portion of image to draw using DrawImage()

30 March 2010 - 05:25 PM

Hi, guys! Just joined up after lurking for a while. I have limited programming experience, so please bear with me :) I'm trying to build a simple C# game for my mobile phone, so I'm using .net CF 3.5 on my Touch HD (WM 6.1). The basic game works, but now I'm trying to add a score counter using numbers from a texture sheet I made. Looking around, this should be simple: just use an e.Graphics.DrawImage() call in the counter's OnPaint (the counter is a custom controll derived from UserControl), using the (bitmap, destRect, scrRect, gfxUnit) overload. This should do the trick and MSDN tells me this call is supported in CF 3.5. Everything seems to work (the incrementing to change the scrRect to crop the number I want, the placement of the counter), but for some reason the actual bitmap-part won't show! It just displays the background :( Oddly enough, to troubleshoot, I placed a "e.Graphics.Clear(Color.Beige);" just above the DrawImage() in the control's OnPaint, and now, exactly where I would want to see a nice bit of bitmap containing my cropped number, I see a beige box :( Here's the counter class:
```sing System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Drawing;

namespace Mash2

public class SideScrollScoreBox : System.Windows.Forms.UserControl
{
//vars
public Bitmap scoreSheet;
public Rectangle bitmapPart;
public Rectangle locRect;

//accessors
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
{
//Do nothing
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.Clear(Color.Beige);
e.Graphics.DrawImage(scoreSheet, locRect, bitmapPart, GraphicsUnit.Pixel);
}

//constructors
public SideScrollScoreBox(Bitmap scoreSheet)
{
this.scoreSheet = scoreSheet;
}
}
}
```
One of the counters is initialised like so (in the form):
```private static Bitmap blueNumbersJ = new Bitmap(@"Program Files\\Mash2\\jpgs\\numbersheet_blue.jpg");
public SideScrollScoreBox redScoreBox = new SideScrollScoreBox(redNumbersJ);
```
with it being set up further in a method DrawBackground():
```//define lower picbox scoreboard
//SideScrollScoreBox redScoreBox = new SideScrollScoreBox(redNumbersJ); //done as public static uptop
redScoreBox.Location = new Point(23, 703);
redScoreBox.Size = new Size(70, 70);
redScoreBox.bitmapPart = new Rectangle(0, 0, 48, 64);
redScoreBox.locRect = new Rectangle(redScoreBox.Location.X, redScoreBox.Location.Y, redScoreBox.Size.Width, redScoreBox.Size.Height);