• Advertisement
Sign in to follow this  

Unity Get Array of Points that Make a Circle And Displace Them

This topic is 1276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, so I'm trying to figure out a world generation algorithm for my game in Unity but I am having a hard time deciding how to implement it.
 
So few days ago, I stumbled on an old thread in this site which they achieve exactly what I am trying to do. They generate some circles and then use Perlin noise to mess with radius (or at least that's what I understood so far), resulting in irregular continents.
 
I tried using "if (x-x0)^2 + (y-y0)^2 <= r^2 then" but the thing is, I don't get the same result 
 
According to that thread, their way of drawing a circle is using function that has a theta, but I can't find a way of doing that.
So my question is: How Can I get an array of points that make up a circle (NOT CIRCUMFERENCE) so that I can get the same results as the image bellow?

 

The reason I don't ask in that thread its because its already too old and is locked.

Share this post


Link to post
Share on other sites
Advertisement

Are you talking about the parametric equation of a circle? You have to decide how many points your circle will have and then you can calculate theta accordingly, e.g.: 

Vector2[] circle = new Vector2[numVertices];
float delta = (2*PI) / numVertices;

for (int i=0; i < numVertices; i++)
{
   float t = delta * i;
   x = x0 + cos(t)*r;
   y = y0 + sin(t)*r;
   circle[i] = new Vector2(x,y);
}

Where x0,y0 are the coordinates of your center and r is the radius of the circle. As the for loop runs, t will be incremented until it covers the whole circle. The more points you calculate, the closer it will approximate a circle.

Edited by AvengerDr

Share this post


Link to post
Share on other sites

their way of drawing a circle is using function that has a theta, but I can't find a way of doing that.

 

Theta is an angle incremented from 0 to 2 Pi, commonly in a loop to generate (x,y) coordinate pairs.

 

Off the top of my head:

float delta = (some small value);
for(float theta = 0; theta < 2*Pi; theta += delta)
{
   float x = sin(theta);
   float y = cos(theta);
   // use (x,y) for something
}

ph34r.png by AvengerDr

Edited by Buckeye

Share this post


Link to post
Share on other sites

Thanks for your post, my problem is that using a parametric function, I just have the points surrounding the center, not actually the filled circle.

 

 

Share this post


Link to post
Share on other sites

If for each iteration in the loop you add a random displacement to the radius, you will get a circle with jagged segments - if you were to connect all the vertices to each other. In fact, this set of points could then be transformed into a polyline or spline and filled accordingly to your requirements.

Edited by AvengerDr

Share this post


Link to post
Share on other sites

You do know that there are other potentially easier ways to create continents, right? Such as height maps or incremental flood fill..

 

 

Looking at those images, I'm guessing they are using very close points, possibly with variations in the theta as well as the radius. If you are interested in this for mathematical reasons or whatever, then by all means, but if you just want nice continents, there are probably easier ways.

Share this post


Link to post
Share on other sites

I tried to take the challenge to make my own implementation, knowing nothing about the subject at all.  It looks nothing like the images you showed.  Then I noticed that they are using Perlin noise, and I was just using a simple random generator...  Basically using the method AvengerDr suggested, drawing a circle with various displacements, deformations.  (Basically drawing a jagged oval rather than a jagged circle.)

 

Edit: Looking at the picture you posted, it looks like they are not only using Perlin noise, but using spherical coordinates too.  (Mapping to a sphere.)

 

notamap.png

 

 

On the other hand, looking at the picture from the answer in the thread that you posted a link to, the initial shapes looks very much like what I get with my attempt.

 

 

Picture prom the post:

 

CK9dbvJ.png

 

 

Code (quick and dirty...)

- (void)drawRect:(NSRect)dirtyRect {

    [[NSColor blackColor] set];
    [NSBezierPath fillRect:dirtyRect];
    
    [[NSColor whiteColor] set];

    double centerX = [self bounds].size.width / 2.0f;
    double centerY = [self bounds].size.height / 2.0f;
    
    double x = 0.0f;
    double y = 0.0f;
    
    double radius = 100.0f;
    
    double delta = 0.05f;

    double randomValue = 0.0f;
    
    randomValue = (double)arc4random_uniform(10000) / 10000.0f;
    double scaleX = (randomValue + 0.1f);
    
    randomValue = (double)arc4random_uniform(10000) / 10000.0f;
    double scaleY = (randomValue + 0.1f);

    randomValue = (double)arc4random_uniform(10000) / 10000.0f;
    double clampToValue = randomValue * 200.0f;

    NSBezierPath *circle = [NSBezierPath bezierPath];
    BOOL isFirst = YES;
    double deviationRadius = 0.0f;

    for (double theta = 0.0f; theta < 2 * M_PI; theta += delta) {
        randomValue = ((double)arc4random_uniform(10000) / 10000.0f) - 0.5f;

        deviationRadius += randomValue * 100.0f;
        
        if ((clampToValue - (radius - deviationRadius)) < 0.0f) {
            deviationRadius += clampToValue;
        }
        
        x = sin(theta) * (radius + deviationRadius) * scaleX + centerX;
        y = cos(theta) * (radius + deviationRadius) * scaleY + centerY;

        if (isFirst) {
            [circle moveToPoint:NSMakePoint(x, y)];
            isFirst = NO;
        }
        else {
            [circle lineToPoint:NSMakePoint(x, y)];
        }
        
    }

    [circle closePath];
    [circle fill];
//    [circle stroke];
}

Edited by aregee

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Now

  • Advertisement
  • Similar Content

    • By RoKabium Games
      Another one of our new UI for #screenshotsaturday. This is the inventory screen for showing what animal fossils you have collected so far. #gamedev #indiedev #sama
    • By eldwin11929
      We're looking for programmers for our project.
      Our project is being made in Unity
      Requirements:
      -Skills in Unity
      -C#
      -Javascript
      -Node.js
      We're looking for programmers who can perform a variety of functions on our project.
      Project is a top-down hack-and-slash pvp dungeon-crawler like game. Game is entirely multiplayer based, using randomized dungeons, and a unique combat system with emphasis on gameplay.
      We have a GDD to work off of, and a Lead Programmer you would work under.
      Assignments may include:
      -Creating new scripts of varying degrees specific to the project (mostly server-side, but sometimes client-side)
      -Assembling already created monsters/characters with existing or non-existing code.
      -Creating VFX
      -Assembling already created environment models
      If interested, please contact: eldwin11929@yahoo.com
      This project is unpaid, but with royalties.
       
      ---
      Additional Project Info:
      Summary:
      Bassetune Reapers is a Player-verus-Player, competitive dungeon crawler. This basically takes on aspects of dungeon crawling, but with a more aggressive setting. Players will have the option to play as the "dungeon-crawlers" (called the 'Knights', or "Knight Class", in-game) or as the "dungeon" itself (literally called the 'Bosses', or "Boss Class", in-game). What this means is that players can choose to play as the people invading the dungeon, or as the dungeon-holders themselves.
      Key Features:
      -Intense, fast-paced combat
      -Multiple skills, weapons, and ways to play the game
      -Tons of different Bosses, Minibosses, creatures and traps to utilize throughout the dungeon
      -Multiple unique environments
      -Interesting, detailed lore behind both the game and world
      -Intricate RPG system
      -Ladder and ranking system
      -Lots of customization for both classes s of customization for both classes
    • By RoKabium Games
      Custom coffee mugs have arrived... More caffeine!
      Have a great weekend everyone! 
      #gamedev #indiedev #sama #caffeine
    • By Atwo Studios
       
      Hey guys,

      Anthony here from Atwo Studios bringing you some new updates for the new year!
      In this video I go over our game ROY, the new games and some general updates to the company!

      If you have not checked out ROY feel free to give it a try! Many people have said they enjoyed the game thus far!
      ROY: https://goo.gl/o6JJ5P
       
    • By Affgoo
      https://play.google.com/store/apps/details?id=com.NE.Alien
      still a lot of work to do, but its pretty stable  please let me know what you think <3
      Atlas Sentry is a game of destroy everything. Using your turret, simply swivel and shoot your way to victory, upgrading your weapons to unleash destruction on the variety of spaceships. The bigger your combo’s the more score you get! Earn silver as you play and then purchase new weapons and abilities to better deal with your enemy. Different enemies use different tactics and weapons, work out your own priorities in their destruction order. 

      Features: 
      **2 different game modes 
      **A level select mode with 20 difficult levels including a final boss, can you defeat it? **Arcade mode of endless destruction, how long will you last? 
      **High scores to compete against others, see who can take the top spot. 
       
  • Advertisement