# MacD

Member

8

122 Neutral

• Rank
Newbie
1. ## Why is this terrain using 4d simplex noise not tiling?

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...
2. ## Why is this terrain using 4d simplex noise not tiling?

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!
3. ## Why is this terrain using 4d simplex noise not tiling?

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?
4. ## [.net] C# CF 3.5 why isn't this loop being entered?

Yup, that was it. Thanks for looking through this; I guess I'm still not an experienced enough coder to catch on to things like that. I'd screwed around with everything in that loop statement, EXCEPT for that :) Now I hope you've taught me another troubleshooting tip. Thanks for the lesson, Mike :)
5. ## [.net] C# CF 3.5 why isn't this loop being entered?

Hi again! This has literally been costing me sleep. I have a simple program I'm going to post to xda dev (and here, if people want it), basically looping through some user input. The program is a simple layout planner for use on a mobile device, so you can draw lines and boxes, create a grid and (on doubleclick) readout your x/y coords, all to help you plan out your application. This all works, except for the gridcreation. I'm using a method (AddGridLines(), copied from a AddLines() which works great) which I've basically copy/pasted/modified from something I wrote earlier, which works perfectly. The code takes TextBox input and creates lines from it which it displays. The code should have been generic enough to add to a grid generating input UI, but I made a few mistakes and decided: "if it's not broke don't fix it", so I just reworked the AddLines() into AddGridLines() instead of looping and using AddLines. The problem is that the method itself, the one it calls to, does seem to work. The loop which should call it, however, never seems to be entered! It's basically a for loop in an if loop. Using MessageBoxes, testing the method seperatley and other debug code, I KNOW the method it calls (AddGridLine()), works. As does the rendering code: if called seperately, I can display, hide and unhide the gridline it creates. I also know that the IF loop is entered. It is just that the FOR loop within the IF loop NEVER seems to be entered :( And I just cannot figure out why. I've even changed the conditional and the itterative statement, to see if that makes a difference...it doesn't seem to :( So, here's the code...please help prove I'm an idiot who made a simple mistake! 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 { //first add xlines 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 :)
6. ## [.net] C# CF3.5 Cannot get portion of image to draw using DrawImage()

*thud* *thud* *thud* That's the sound of my head hitting my desk. You were absolutely right about where I was pointing my destRect...I thought I was being clever with the programatic referencing, and I'd done just about everything to troubleshoot this, but everything seemed correct, the bitmap was displaying correctly in a test picbox, the increments were incrementing properly.... Sigh :) Thank you bundles for looking over my code ... I was just blind from looking over it myself :) Now to add animation and I'll have a nice little moving score counter :)
7. ## [.net] C# CF3.5 Cannot get portion of image to draw using DrawImage()

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); this.Controls.Add(redScoreBox); this.Controls.SetChildIndex(redScoreBox, 4); redScoreBox.BringToFront(); There's some logic to move along the .X of bitmapPart, but that seems to work fine. The problem is that the DrawImage() call doesn't seem to work. Is this a known problem (ie cf3.5 can't crop images, despite what MSDN says) or am I doing something dramatically wrong? PS: how do you get those nice codeboxes? <code></code> just doesn't look as nice!
8. ## Need recommendations for beginning with C#

I found the O'Reilly "Programming C#" (I have the c# 3.0, fifth edition, but there'll be an uptodate one available) book to be a good start for c#. Much better than those "c# in 21 days/24 hours" books. A bit basic maybe for someone with a lot of programming experience, but it does touch a few topics you mentioned. Of course, this place has a whole c# workshop forum attached, so you could check there?