1
\$\begingroup\$

Super quick intro. I am making a terrain generator (With a MC exporter mostly to debug). For the islands I am creating a map with points, and create a Voronoi diagram from it. Each intersection (Where lines of the shapes meet) has a different height, depending on the distance to the coast. The coast itself is elevation=0. Here you can see what I meant. Where the wite lines meet is an intersection. Each one of the holds a different value.

However, whatever I try, I keep getting weird heightmaps. (I can't link more than two images, so I will explain). You can clearly see the difference between each site, yet they are slightly sloped.

In-game this looks like this: enter image description here

The red blocks that stick out is the height of the intersection. Sites are created by references to the intersections, since they have IDs.

The code I am using at the moment:

Point p = new Point(x, 1, z);

        double[] distances = new double[points.length];
        double[] percentages = new double[points.length];
        double[] elevations = new double[points.length];

        int max = 0;
        for(int i=0; i<points.length; i++)
            if(parent.getIntersections().fromIndex(points[i]).getElevation() > max)
                max = (int)parent.getIntersections().fromIndex(points[i]).getElevation();

        double totaldist = 0;
        for(int i=0; i<points.length; i++) {
            distances[i] = parent.getIntersections().fromIndex(points[i]).getLocation().surfaceDist(p);
            totaldist+=distances[i];
        }

        for(int i=0; i<points.length; i++){
            percentages[i] = (distances[i] / totaldist);
        }

        for(int i=0; i<points.length; i++)
            elevations[i] = (percentages[i] * (parent.getIntersections().fromIndex(points[i]).getElevation()));

        double heightTemp = 0;
        for(int i=0; i<elevations.length; i++)
            heightTemp += elevations[i];
        heightTemp = max-heightTemp+max;

        int height = (int) heightTemp - 3 + parent.getConfig().getSeaLevel() ;
        height = (height>255 ? 255 : height);

The code's quite weird and unorganized since I pretty much tried anything I could come up with. "points" is an array containing the reference IDs to the correct intersections "parent.getIntersections().fromIndex(int index)" is the way to retrieve an intersection from the index/reference id. "p" is the point to get the altitude/elevation from. "Intersection.getElevation()" returns the elevation (Anything above sea level).

Sorry if this is real vague, I have no clue on how to explain this properly.

\$\endgroup\$
2
  • \$\begingroup\$ Looking at this makes me really wish I understood voronoi \$\endgroup\$ – Shaun Wild May 27 '16 at 10:19
  • \$\begingroup\$ @ShaunWild It's not that complicated to understand, generating a diagram can also be done using some APIs! I started this project a few weeks ago, and did tons and tons of background reading. Go for it! \$\endgroup\$ – Daniël V May 27 '16 at 10:55
1
\$\begingroup\$

Solved

After thinking this through almost a complete day (I encountered the problem yesterday), I realized I had the function to determine the height of any given point using the distance to the ocean. Why would I need to take averages and complicated calculations if I could just check the distance to the nearest coast, and then use the formula?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.