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

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

Thread Status:
Not open for further replies.
  1. Offline


    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
    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
        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;
        public byte[] generate(World world, Random rand, int chunkx, int chunkz) {
        result = new byte[32768];
        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.

    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;
    		return Math.toRadians(x);
    	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;
    		calc =
    				+Math.sin(normalise((int) (Math.sin(normalise(x)+Math.sin(normalise(z)))+Math.sin(normalise(z))))+Math.sin(normalise(z/2))
    				+Math.sin(normalise((int) (x+Math.sin(normalise(z)*Math.sin(normalise(x))))));
    		calc = Math.abs(calc/6);
    			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
    Phiwa and thehutch like this.
  2. Offline


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


    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.
  4. Offline


    Loving it!
  5. Offline


    You should try it with other functions.
  6. Offline


    Hmm Tan would look wierd :p and wouldnt there be gaps in it? since Tan doesnt touch the values +-1 ?
  7. Wow you're awesome! How did you ever came up with that code =O
Thread Status:
Not open for further replies.

Share This Page