Terrain Generation

hi all

how to make this shape if any one can help

1-norm

i found this equation :
centerpoint (x1, y1) and a radius r, the height of the hill at the point (x2, y2) is equivalent to:

equation

how to use it to form this terrain

can and how if i can using it in this code

// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/IKB1hWWedMk

int cols, rows;
int scl = 20;
int w = 2000;
int h = 1600;

float flying = 0;

float[][] terrain;

void setup() {
  size(600, 600, P3D);
  cols = w / scl;
  rows = h/ scl;
  terrain = new float[cols][rows];
 colorMode(HSB, 100, 100, 100);
}


void draw() {

  flying -= 0.1;

  float yoff = flying;
  for (int y = 0; y < rows; y++) {
    float xoff = 0;
    for (int x = 0; x < cols; x++) {
      terrain[x/2][y/2] = map(noise(xoff, yoff), 0, 1, -100, 100);
      xoff += 0.2;
    }
    yoff += 0.2;
  }



  background(0);
// stroke(255);
  //noFill();
//noStroke();
  translate(width/2, height/2+50);
  rotateX(PI/3);
  translate(-w/2, -h/2);
  for (int y = 0; y < rows-1; y++) {
   // beginShape(TRIANGLE_STRIP);
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < cols; x++) {
      
      float z = terrain[x][y]; 
      fill(z+20, 100, 100);    
      vertex(x*scl, y*scl, z);
      
      float z1 = terrain[x][y+1];
      fill(z1+20, 100, 100);        
      vertex(x*scl, (y+1)*scl, z1);
      //rect(x*scl, y*scl, scl, scl);
    }
    endShape();
  }
}

thanks for all

1 Like

I couldn’t do it


// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/IKB1hWWedMk

int cols, rows;
int scl = 20;
int w = 2000;
int h = 1600;

float flying = 0;

float[][] terrain;

void setup() {
  size(1600, 900, P3D);
  cols = w / scl;
  rows = h/ scl;
  terrain = new float[222][222];
  colorMode(HSB, 100, 100, 100);

  // -----
  flying -= 0.1;

  float x1= 230; // w/2; 
  float y1= 230; // h/2; 
  float r = 5.015;  

  float yoff = 0;
  int y3=0; 
  for (int y = -100; y < 100; y++) {
    float xoff = 0;
    int x3=0; 
    for (int x = -100; x < 100; x++) {
      float a1 = ( pow2 ( xoff-x1 )) +  ( pow2 (yoff-y1) ) ; 
      float b1 = (r*r) - a1; // map(noise(xoff, yoff), 0, 1, -100, 100);

      terrain[x3][y3] =  map(b1, -100000, 100000, -3000, 3000);
      println(b1); 

      xoff += 4;
      x3++;
    }
    yoff += 4;
    y3++;
  }
}

float pow2 ( float in_) {
  return 
    in_*in_;
}


void draw() {
  background(0);
  lights();

  // stroke(255);
  //noFill();
  //noStroke();
  translate(width/2, height/2-222, -600);
  rotateX(PI/3);
  translate(-w/2, -h/2);
  for (int y = 0; y < rows-1; y++) {
    // beginShape(TRIANGLE_STRIP);
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < cols; x++) {

      float z = terrain[x][y]; 
      fill(z+20, 100, 100);    
      //noStroke(); 
      vertex(x*scl, y*scl, z);

      float z1 = terrain[x][y+1];
      fill(z1+20, 100, 100);        
      vertex(x*scl, (y+1)*scl, z1);
      //rect(x*scl, y*scl, scl, scl);
    }
    endShape();
  }
}
//

1 Like

it is more than great that you give it a try :heart:

i played with this line once got some change

float z = terrain[x^2][y^2]; 

and once again with this got some change

terrain[x^2][y^2] = map(noise(xoff, yoff), 0, 1, -100, 100);

i am still learning i found this site few time a go

https://mathcs.holycross.edu/~spl/old_courses/126_spring_2004/handouts/02.25.04.html

its great maybe the equation above for the c++ here were i found it

https://www.stuffwithstuff.com/robot-frog/3d/hills/hill.html

your result is close

Hello @jafal,

The first equation you have is not correct.
The equation of a sphere is:
image

So you actually need a z² or to take the square root of the right side of your equation.

To implement it, you simply need to compute the z value of your grid depending on the x, y coordinates.

BUT you also need to check if you are in the sphere boundary. The equation is valid only for x and y coordinate within the sphere. Outside the sphere you want the height equal to 0.

Hope this piece of code helps you:

import peasy.PeasyCam;

PeasyCam cam;
float[][] terrain;

void setup() {
  size(800, 600, P3D);
  
  cam = new PeasyCam(this, 400);
  
  terrain = new float[300][300];
  colorMode(HSB, 100, 100, 100);

  float x0= 150;
  float y0= 150;
  float r = 100;  

  for (int x = 0; x < terrain.length; x++) {
    for (int y = 0; y < terrain[0].length; y++) {
      if (r * r - ((x- x0) * (x - x0) + (y- y0) * (y - y0)) > 0) {
        terrain[x][y] = sqrt(r * r - ((x- x0) * (x - x0) + (y- y0) * (y - y0)));
      } else {
        terrain[x][y] = 0;
      }
    }
  }
}

void draw() {
  background(20);
  lights();

  translate(-150, 0, -600);
  rotateX(PI/3);
  translate(0, 0);
  for (int y = 0; y < terrain[0].length - 1; y++) {
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < terrain.length; x++) {
      fill(220, 40, 40);
      noStroke();
      vertex(x, y, terrain[x][y]);
      vertex(x, y + 1, terrain[x][y + 1]);
    }
    endShape();
  }
}

image

4 Likes

Hello @jb4x

it is charming and great i have said to @Chrisir maybe its for c++ as apart of the code

many thanks for you … i am totally new and fresh :smile: to processing and my way for understanding is to study sketches for a certain issue and make comparing

God with you warm greeting

1 Like

i just wonder why it is without mesh and how to make it looks like the red mesh ?

Thanks for you

It’s because of noStroke()

2 Likes

hi
the photo i post is with stroke i add it

@Chrisir
peace upon you

I have been studying your code for one night and feel that the issue is how the grid constructed.

See the equation here gave a result with Mr jb4x code

  terrain[x][y] = sqrt(r * r - ((x- x0) * (x - x0) + (y- y0) * (y - y0)));

i added the same equation with your code gave a close result because the dome appeared
And the square base remains to complete the shape

 terrain[x3][y3] = (r * r - ((xoff-x1 ) * (xoff- x1) +   (yoff- y1) * (yoff - y1)));
     

I tried to solve the problem, but I could not

Do you have any suggestions?

// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/IKB1hWWedMk

int cols, rows;
int scl = 25;
int w = 2000;
int h = 2200;

float flying = 0;

float[][] terrain;

void setup() {
  size(800, 900, P3D);
  cols = w / scl;
  rows = h/ scl;
  terrain = new float[222][222];
  colorMode(HSB, 100, 100, 100);

  // -----
  flying -= 0.1;

  float x1= 230; // w/2; 
  float y1= 230; // h/2; 
  float r = 5.015;  

  float yoff = 0;
  int y3=0; 
  for (int y = -100; y < 100; y++) {
    float xoff = 0;
    int x3=0; 
    for (int x = -100; x < 100; x++) {
     // float a1 = ( pow2 ( xoff-x1 )) +  ( pow2 (yoff-y1) ) ; 
      //float b1 = (r*r) - a1; // map(noise(xoff, yoff), 0, 1, -100, 100);
  terrain[x3][y3] = (r * r - ((xoff-x1 ) * (xoff- x1) +   (yoff- y1) * (yoff - y1)));
     // terrain[x3][y3] =  map(b1, -100000, 100000, -3000, 3000);
     // println(b1); 

      xoff += 4;
      x3++;
    }
    yoff += 4;
    y3++;
  }
}

float pow2 ( float in_) {
  return 
    in_*in_;
}


void draw() {
  background(0);
  lights();

  // stroke(255);
  //noFill();
  //noStroke();
  translate(width/2, height/2-222, -600);
  rotateX(PI/3);
  translate(-w/2, -h/2);
  for (int y = 0; y < rows-1; y++) {
    // beginShape(TRIANGLE_STRIP);
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < cols; x++) {

      float z = terrain[x][y]; 
      fill(z+20, 100, 100);    
      //noStroke(); 
      vertex(x*scl, y*scl, z);

      float z1 = terrain[x][y+1];
      fill(z1+20, 100, 100);        
      vertex(x*scl, (y+1)*scl, z1);
      //rect(x*scl, y*scl, scl, scl);
    }
    endShape();
  }
}
//

jb4x code is much better than mine

In his code there is one line noStroke()

Comment it out or replace it with stroke(0);

Chrisir

hi this is same jb4x code but more More flexible

int cols, rows;
int scl =15;
int w = 2000;
int h = 1600;

float flying = 0;

float[][] terrain;
import peasy.PeasyCam;

PeasyCam cam;
void setup() {
  size(800, 800, P3D);
  cam = new PeasyCam(this, 400);
  cols = w / scl;
  rows = h/ scl;
  terrain = new float[cols][rows];
}


void draw() {

  flying -= 0.1;

  float yoff = flying;
  for (int y = 0; y < rows; y++) {
    float xoff = 0;
    for (int x = 0; x < cols; x++) {
 if (2222 - ((x- 53) * (x - 53) + (y- 53) * (y - 53)) > 0) {
   terrain[x][y] = sqrt(2222 - ((x- 53) * (x - 53) + (y- 53) * (y - 53)))*12;
   
      
      
} 
     // terrain[x][y] = map(noise(xoff, yoff), 0, 1, -100, 100);
      xoff += 0.2;
    }
    yoff += 0.2;
  }



  background(0,222,1);
 // stroke(2,5,255);
 stroke(5);
 strokeWeight(7);
  noFill();
scale(.2);
  translate(width/2, height/2+50);
  rotateX(PI/3);
  translate(-w/2, -h/2);
  for (int y = 0; y < rows-1; y++) {
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < cols; x++) {
      vertex(x*scl, y*scl, terrain[x][y]);
      vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
      //rect(x*scl, y*scl, scl, scl);
    }
    endShape();
  }
}

1 Like

1 Like

@jb4x

many thanks for you

2 Likes

1 Like