0
\$\begingroup\$

I am making a 2d top-down tile based game, which is randomly generated each new world.

What I want to do is create a Polygon (or other more appropriate shape I don't know of) for each landmass not disrupted by land, basically each unseparated landmass/island, I want to do this for where my player is to spawn in the world, I want to have it so it has a higher chance of spawning on the biggest landmass in the world, rather than spawning on a tiny several tile island. So, all I need to know is how would I create some sort of shape system to recognise different islands.

The reason I suggest a Polygon is because the player would randomly spawn in that area so it has to be accurate to the land, it can't start of by spawning in water nearby.

If a Polygon is obsolete for this idea, and another system would be better let me know.

\$\endgroup\$
  • \$\begingroup\$ This sounds like a simple connected component search problem, which we've answered here before. Where have you run into difficulty implementing it? \$\endgroup\$ – DMGregory Sep 17 '19 at 19:13
  • \$\begingroup\$ @DMGregory I haven't tried anything yet, but I don't actually know where to start, my quesion was asking for ideas to my problem \$\endgroup\$ – Tyler Harrison Sep 18 '19 at 15:42
0
\$\begingroup\$

For the main island I would do something like this. I would choose how many sides the polygon has to have. Then, I would choose a value, how far the farthest point of the island can be. Then I would create an array of coordinates of my polygon like this:

public class Island{

    public static void main(String[] args){

        int numberOfPoints = 10;
        float [][] islandCoordinates = float[numberOfPoints][2];//array of coordinates
        float maxDistance = 1000;

        float stepAngle = 2 * Math.PI / numberOfPoints;

        for(int i = 0;  i < numberOfPoints; i++){
            float dist = maxDistance * Math.random();
            float xcoordinate = Math.cos(stepAngle * i) * dist;
            float ycoordinate = Math.sin(stepAngle * i) * dist;

            islandCoordinates[i][0] = xcoordinate;
            islandCoordinates[i][1] = ycoordinate;
        }            

    }

}

This would make the island look very simple, but I think with playing with the values you could make it a lot nicer.

Good luck!

\$\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.