Planet Generation

I’m doing a planet generation program.
My code:

import peasy.*;

PVector resolution = new PVector(200,200);
PVector[][] basicGlobe = new PVector[round(resolution.x) + 1][round(resolution.y) + 1];

float radius = 400;
PVector[][] lithosphere = new PVector[round(resolution.x) + 1][round(resolution.y) + 1];

int lithoSeed;

PeasyCam camera;

void setup(){
  size(600,600,P3D);
  camera = new PeasyCam(this,0,0,0,600);
  
  lithoSeed = round(random(-2147483647,2147483647));
}

void draw(){
  for(int i = 0;i < resolution.x + 1;i++){
    float latitude = map(i,0,resolution.x,0,TAU);
    for(int j = 0;j < resolution.y + 1;j++){
      float longitude = map(j,0,resolution.y,0,PI);
      
      float x = cos(latitude) * cos(longitude);
      float y = sin(latitude);
      float z = cos(latitude) * sin(longitude);
      basicGlobe[i][j] = new PVector(x,y,z);
    }
  }
  
  background(0);
  noStroke();
  fill(127);
  pointLight(255,255,255,-1000,0,0);
  
  beginShape(TRIANGLE_STRIP);
  noiseSeed(lithoSeed);
  for(int i = 0;i < resolution.x + 1;i++){
    float latitude = map(i,0,resolution.x,0,TAU);
    for(int j = 0;j < resolution.y + 1;j++){
      float longitude = map(j,0,resolution.y,0,PI);
      
      float x = cos(latitude) * cos(longitude);
      float y = sin(latitude);
      float z = cos(latitude) * sin(longitude);
      x *= map(noise(basicGlobe[i][j].x,basicGlobe[i][j].y,basicGlobe[i][j].z),0,1,400,500);
      y *= map(noise(basicGlobe[i][j].x,basicGlobe[i][j].y,basicGlobe[i][j].z),0,1,400,500);
      z *= map(noise(basicGlobe[i][j].x,basicGlobe[i][j].y,basicGlobe[i][j].z),0,1,400,500);
      
      lithosphere[i][j] = new PVector(x,y,z);
      
      if(i != 0){
        PVector v1 = lithosphere[i - 1][j];
        PVector v2 = lithosphere[i][j];
        
        vertex(v1.x,v1.y,v1.z);
        vertex(v2.x,v2.y,v2.z);
      }
    }
  }
  endShape();
}

There is a “gap” in the middle of my planet.


How do I fix that?

Hi,

Nice work already! :slight_smile:

Your issue is probably due to the fact that the noise is not looping correctly when the surface loops.

This happens when the first and last noise value mismatch :

Screenshot from 2020-10-28 18-30-50

And here an animated code version for fun ;):

int resolution = 20;
float radius = 100;
float offset = 0;

void setup() {
  size(500, 500);
}

void draw() {
  background(255);

  translate(width/2, height/2);
  
  float firstRadius = radius;
  float lastRadius = radius;

  strokeWeight(2);
  noFill();
  stroke(0);

  beginShape();
  for (int i = 0; i <= resolution; i++) {
    float angle = i * (TWO_PI / resolution);
    float dRadius = radius + noise(i + offset) * radius;
    
    if (i == 0) firstRadius = dRadius;
    else if (i == resolution) lastRadius = dRadius;
    
    vertex(cos(angle) * dRadius, sin(angle) * dRadius);
  }
  endShape();
  
  stroke(255, 0, 0);
  strokeWeight(5);
  line(firstRadius, 0, lastRadius, 0);
  
  float gap = abs(firstRadius - lastRadius);
  fill(255, 0, 0);
  textAlign(RIGHT, CENTER);
  text("Gap : " + floor(gap), min(firstRadius, lastRadius) - 50, 0);
  
  offset += 0.01;
}

This video might help :

1 Like

No, when I set the sphere fill to low alpha, I saw a large flat (plate? what must I call it?) inside the planet.
Guess I will watch more Coding Train video to find out my answer.

Thanks anyway!