# [FUN/WGEN] Create a world from trigonometric functions!

Discussion in 'Resources' started by codename_B, Oct 6, 2011.

Not open for further replies.
1. Offline

### codename_B

At the minute this is just a proof-of-concept but I'd rather release it and see if anyone can make something of it  The main class
Code:
```package de.bananaco.bananatrig;

import java.util.Random;

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.generator.ChunkGenerator;

public class TrigGenerator extends ChunkGenerator {
byte[] result;

//This needs to be set to return true to override minecraft's default behaviour
@Override
public boolean canSpawn(World world, int x, int z) {
return true;
}
//This converts relative chunk locations to bytes that can be written to the chunk
public int xyzToByte(int x, int y, int z) {
return (x * 16 + z) * 128 + y;
}

@Override
public byte[] generate(World world, Random rand, int chunkx, int chunkz) {
result = new byte;
for(int x=0; x<16; x++)
for(int z=0; z<16; z++) {
int trueX = chunkx*16+x;
int trueZ = chunkz*16+z;
//    for(int y=0; y<64; y++)
//    result[xyzToByte(x,y,z)] = (byte) Material.STATIONARY_WATER.getId();
double h = Function.get(trueX, trueZ)*127;
for(int y=0; y<h; y++)
result[xyzToByte(x,y,z)] = (byte) Material.STONE.getId();
//This will set the floor of each chunk at bedrock level to bedrock
result[xyzToByte(x,0,z)] = (byte) Material.BEDROCK.getId();
}
return result;
}

}
```
And of course, the FUNCTION class containing the static methods to give us our stuff.

Code:
```package de.bananaco.bananatrig;

public class Function {
// Converts the degrees to a normalised radian for use with Math.sin()
public static double normalise(int x) {

int conv = x % 360;
if(conv >= 1)
x = x % 360;
}

private static double sinOctave(int x, int z, int octave) {
return (Math.sin(normalise(x/octave))+Math.sin(normalise(z/octave)));
}

private static double cube(double x) {
return x*x*x;
}

public static double get(int x, int z) {
double calc;
x=x;
z=z;

calc =
Math.sin(normalise(x/4))
+Math.sin(normalise(z/5))
+Math.sin(normalise(x-z))
+(Math.sin(normalise(z))/(Math.sin(normalise(z))+2))*2
+Math.sin(normalise((int) (Math.sin(normalise(x)+Math.sin(normalise(z)))+Math.sin(normalise(z))))+Math.sin(normalise(z/2))
+Math.sin(normalise(x)
+Math.sin(normalise(z)))
+Math.sin(normalise(z))
+Math.sin(normalise(z))
+Math.sin(normalise(x)))
+Math.sin(normalise((x+z)/4))
+Math.sin(normalise((int) (x+Math.sin(normalise(z)*Math.sin(normalise(x))))));

calc = Math.abs(calc/6);
if(calc>1)
calc = 1;
return calc;
}

}
```
Using this its possible to (very quickly) generate some complex (repeating) terrain - and while it will obviously get samey after 1000 blocks or so, it's far better than just a plan flatmap.

I would recommend this to anyone who's tried flatmaps, gotten bored, and wants to mess about, and understand a little about SINE waves.

I mean, how cool is that? Converting a wave into 3 dimensional terrain? XD      EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 20, 2016
#1
Phiwa and thehutch like this.
2. Offline

### slipcor

hey code, add the parameters to your images! so ppl can see directly how to make that #2
3. Offline

### vaiquero

Well, I am currently making a whole new map generator for my server "The Wastelands". I'll try it out and see if I can modify it to something even better.

#3
4. Offline

Loving it!

#4
5. Offline

### gamerguy14

You should try it with other functions.

#5
6. Offline

### thehutch

Hmm Tan would look wierd and wouldnt there be gaps in it? since Tan doesnt touch the values +-1 ?

#6
7. Offline

### Forge_User_78080094

Wow you're awesome! How did you ever came up with that code =O