Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

hwo do i script a pixelated circle?

Discussion in 'Scripting' started by Kemorno, Apr 23, 2017.

  1. Kemorno

    Kemorno

    Joined:
    Apr 5, 2017
    Posts:
    18
    so i want to make a procedurally generated map, that its made out of cubes with 2 units of size, and i want to make it so the player can choose the shape of the arena and the size, and i want to know how i can make it so the game instantiates the wall prefab (cube with 2 units of size) in 2 dimensions, just like this circle http://i.imgur.com/NbsjTpf.png with the black lines being the radius, the red line being the walls(aka the instantiated gameobject) and the green point being the origin

    you dont need to give me the full code, i just want to know what calculations should i make.
     
  2. SGM3

    SGM3

    Joined:
    Jun 10, 2013
    Posts:
    81
    This should get you started:

    Code (CSharp):
    1.     public int Radius = 10;
    2.     public Sprite Wall;
    3.  
    4.     // Use this for initialization
    5.     void Start ()
    6.     {
    7.         DrawCircle();
    8.  
    9.     }
    10.  
    11.     private void DrawCircle()
    12.     {
    13.         int radiusSqrd = Radius * Radius;
    14.  
    15.         for (int x = -Radius; x <= Radius; x++)
    16.         {
    17.             int y = (int) (Mathf.Sqrt(radiusSqrd - x * x) + 0.5f);
    18.  
    19.             //This draws one half of the the circle
    20.             GameObject go = new GameObject(x + ", " + y);
    21.  
    22.             go.transform.position = new Vector2(x, y);
    23.             go.transform.parent = transform;
    24.  
    25.             SpriteRenderer sr = go.AddComponent<SpriteRenderer>();
    26.             sr.sprite = Wall;
    27.  
    28.  
    29.  
    30.             //This draws the other half of the circle
    31.             //Because squaring includes a ±
    32.             GameObject go2 = new GameObject(x + ", " + -y);
    33.  
    34.             go2.transform.position = new Vector2(x, -y);
    35.  
    36.             SpriteRenderer sr2 = go2.AddComponent<SpriteRenderer>();
    37.             sr2.sprite = Wall;
    38.  
    39.             go2.transform.parent = transform;
    40.  
    41.  
    42.         }
    43.     }
    This is the source, I just modified it for unity.
    http://groups.csail.mit.edu/graphics/classes/6.837/F98/Lecture6/circle.html

    Also more on the maths of a circle:
    http://www.mathwarehouse.com/geometry/circle/equation-of-a-circle.php


    The code I sent you has 'holes' and that is because if we were to solve for those missing coordinated, we will find that x has to be a rational number (technically this is wrong, but a circle in math does not have any straight edges), and integer data types are not capable of rational numbers. Moreover, the for loop is incremented by 1, therefore skipping any value that will result in the correct placing of the gameobjects.

    There may be a better way to do this, however I am approaching from a mathematical perspective. Hopefully someone could fill in the gaps, or provide a more efficient way of generating circles.
     
    Last edited: Apr 23, 2017
    Kemorno likes this.
  3. Kemorno

    Kemorno

    Joined:
    Apr 5, 2017
    Posts:
    18
    thank you very much, i'm trying to modify the code to make it draw the circle in a 4 way symmetry(already done it), and also trying to solve the gap part...
     
  4. Kemorno

    Kemorno

    Joined:
    Apr 5, 2017
    Posts:
    18
    kind of did it, the way it works now is, it makes the circle with a 8 way symmetry, so no more of those gaps, and in the end it checks if the game objects will not be on the same tile, by checking if x and z are equal to zero, and if it is equal to zero, it only instantiates the object by 4

    known issues:
    with certain values either the tiles overlap, or it creates a gap that is less than one tile meaning that the radius of the circle is not symmetric on 8 ways, only on 4.
    Code (CSharp):
    1. public class test : MonoBehaviour {
    2.  
    3.     public int Radius;
    4.     private int x;
    5.     private int z;
    6.     private int oldZ;
    7.     private bool dif2;
    8.     private bool dif1;
    9.     public float waitTime;
    10.     public GameObject wallTile;
    11.     public int size;
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.         x = 0;
    17.         z = Radius;
    18.         oldZ = z;
    19.         dif1 = true;
    20.         if (size >= 0)
    21.         {
    22.             StartCoroutine(DrawCircle());
    23.             Debug.Log("size must be greater than 0");
    24.         }
    25.     }
    26.  
    27.     IEnumerator DrawCircle()
    28.     {
    29.         int r2 = Radius * Radius;
    30.  
    31.  
    32.         Instantiate(wallTile, new Vector3(Radius-2, 0, 0), transform.rotation);
    33.         Instantiate(wallTile, new Vector3(-Radius+2, 0, 0), transform.rotation);
    34.         Instantiate(wallTile, new Vector3(0, 0, Radius-2), transform.rotation);
    35.         Instantiate(wallTile, new Vector3(0, 0, -Radius+2), transform.rotation);
    36.      
    37.  
    38.         while (dif1 == true)
    39.         {
    40.  
    41.  
    42.             bool difZ = false;
    43.  
    44.             x += size;
    45.             z = (int)(Mathf.Sqrt(r2 - x * x) + 0f);
    46.          
    47.             dif1 = z >= x;
    48.  
    49.             difZ = oldZ - z == size || oldZ - z == 0;
    50.  
    51.             if (difZ == false)
    52.                 {
    53.                     z -= size/2;
    54.                     dif2 = (z - x) >= 0;
    55.                 }
    56.  
    57.             if (dif2 == false && dif1 == false)
    58.             {
    59.                 StopCoroutine(DrawCircle());
    60.             }
    61.             if (dif2 == true && dif1 == true && x%2==0) //x%2==0 if it is odd
    62.             {
    63.                 Clone();
    64.  
    65.                 yield return new WaitForSeconds(waitTime);
    66.             }
    67.         }
    68.     }
    69.  
    70.     private void Clone()
    71.     {
    72.  
    73.      
    74.         transform.position = new Vector3(x, 0, z);
    75.  
    76.         Debug.Log(z-x);
    77.  
    78.         Instantiate(wallTile, new Vector3(transform.position.x, 0, transform.position.z), transform.rotation);
    79.         Instantiate(wallTile, new Vector3(transform.position.x, 0, -transform.position.z), transform.rotation);
    80.         Instantiate(wallTile, new Vector3(-transform.position.x, 0, -transform.position.z), transform.rotation);
    81.         Instantiate(wallTile, new Vector3(-transform.position.x, 0, transform.position.z), transform.rotation);
    82.         if ((z - x) != 0)
    83.         {
    84.             Instantiate(wallTile, new Vector3(transform.position.z, 0, transform.position.x), transform.rotation);
    85.             Instantiate(wallTile, new Vector3(transform.position.z, 0, -transform.position.x), transform.rotation);
    86.             Instantiate(wallTile, new Vector3(-transform.position.z, 0, -transform.position.x), transform.rotation);
    87.             Instantiate(wallTile, new Vector3(-transform.position.z, 0, transform.position.x), transform.rotation);
    88.         }
    89.         oldZ = z;
    90.     }
    91. }