java - Perlin noise - What am I doing wrong? -
i started world generation , i've been looking tutorials on perlin noise everywhere sadly there not alot found on google. last days followed tutorial can't code work.
here java method.
private static double[][] createnoise(int xn, int yn, int sps) { int m = yn * sps; int n = xn * sps; double[][] u = new double[yn + 1][]; double[][] v = new double[yn + 1][]; double[][] x = new double[m][]; double[][] y = new double[m][]; double[][] z = new double[m][]; (int = 0; < m; i++) { x[i] = new double[n]; y[i] = new double[n]; z[i] = new double[n]; } (int = 0; < yn + 1; i++) { u[i] = new double[xn + 1]; v[i] = new double[xn + 1]; } (int = 0; < xn + 1; i++) { (int j = 0; j < yn + 1; j++) { u[i][j] = nextrandom(); v[i][j] = nextrandom(); } } double hx = xn / (n - 1); double hy = yn / (m - 1); (int = 0; < m; i++) { (int j = 0; j < n; j++) { x[i][j] = hx * j; y[i][j] = hy * i; } } (int = 0; < m; i++) { (int j = 0; j < n; j++) { int xc = (int)x[i][j]; int yc = (int)y[i][j]; if (x[i][j] % 1 == 0 && x[i][j] != 0 ) xc = xc - 1; if (y[i][j] % 1 == 0 && y[i][j] != 0 ) yc = yc - 1; double xr = x[i][j] - xc; double yr = y[i][j] - yc; double s11[] = {-xr, -yr}; double s21[] = {-xr, 1 - yr}; double s22[] = {1 - xr, 1 - yr}; double s12[] = {1 - xr, -yr}; double q11 = s11[0] * u[yc][xc] + s11[1] * v[yc][xc]; double q21 = s21[0] * u[yc + 1][xc] + s21[1] * v[yc + 1][xc]; double q22 = s22[0] * u[yc + 1][xc + 1] + s22[1] * v[yc + 1][xc + 1]; double q12 = s12[0] * u[yc][xc + 1] + s12[1] * v[yc][xc + 1]; z[i][j] = lerp(x[i][j], y[i][j], xc, xc + 1, yc, yc + 1, q11, q12, q21, q22); } } return z; }
the heightmap method returns sadly enough looks this
as can see, first row/column working, after algorithm seems fail. made sure method
nextrandom();
returns float value between -1 & 1.
thanks alot!
thanks user @gawi pointing out.
hx = xn / (n - 1);
will divide 2 integers, , return 0. can fix casting double it:
double hx = (double)xn / (n - 1);
then map works!
Comments
Post a Comment