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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making a sphere out of points

Discussion in 'Scripting' started by Richop, Dec 6, 2016.

  1. Richop

    Richop

    Joined:
    Mar 18, 2016
    Posts:
    136
    Hi,

    So I've been exploring different methods of making the verts of a sphere, I've got a really simple method that creates a 2d circle of points, then spins it round by an amount of circles / 180, basically making a terry's chocolate orange, or for anyone without a reference an inflatable beach ball. This isn't the most ideal method for this task as I end up with overlapping verts at the poles. So I either create a list and search through for identical verts or figure out a better way to make a sphere.

    I've seen a few different methods around, one being the Icososphere and the dart throwing one, I'd like to ideally make an algorithm that distributes points evenly across a sphere, as with my current method there's always the same bunching of circles, as the radius gets smaller on the sphere towards the poles there are still the same amount of points as the band in the middle.

    Does anyone know any better way to make spheres? All methods welcome!

    The Terry's chocolate orange script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class SphereSimpleWorking3 : MonoBehaviour
    6. {
    7.     public GameObject cubes;
    8.  
    9.     private GameObject[] objects = null;
    10.     public int numberOfObjectsinPool = 20;
    11.  
    12.     public int numberOfObjects = 20; // Number of objects around circle
    13.     public int numberOfCircles = 3; // How many rotations around a sphere
    14.  
    15.     public float radius = 5f; // Radius of main circle
    16.     public Transform parent;
    17.     private int currentRes;
    18.     private int increment;
    19.  
    20.     private float sphereAngle = 0f;
    21.     private Vector3 currentPoint;
    22.     public GameObject target;
    23.  
    24.     public List <Vector3> vertList = new List<Vector3> ();
    25.  
    26.     void Start()
    27.     {
    28.         objects = new GameObject[numberOfObjectsinPool];
    29.         int increment = 0;
    30.  
    31.         for (int i = 0; i < numberOfObjectsinPool; i++) // Start up main objects
    32.         {
    33.             objects[i] = Instantiate (cubes) as GameObject;
    34.             objects[i].transform.parent = parent.transform;
    35.             objects[i].SetActive (false);
    36.         }
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.         if (currentRes != numberOfObjects * numberOfCircles)
    42.         {
    43.             ClearObjs ();
    44.             vertList.Clear ();
    45.             increment = 0;
    46.  
    47.             StartCoroutine("StartMakingSphere");
    48.         }
    49.     }
    50.  
    51.     void ClearObjs()
    52.     {
    53.         for (int i = 0; i < numberOfObjectsinPool; i++)
    54.         {
    55.             objects[i].SetActive(false);
    56.         }
    57.     }
    58.  
    59.     IEnumerator StartMakingSphere()
    60.     {
    61.         currentRes = numberOfObjects * numberOfCircles;
    62.  
    63.         for (int j = 0; j < numberOfCircles; j++)
    64.         {
    65.             for (int i = 0; i < numberOfObjects; i++)
    66.             {
    67.                 float angle = (i * Mathf.PI * 2 / numberOfObjects);
    68.                 float x = Mathf.Cos (angle) * radius;
    69.                 float y = Mathf.Sin (angle) * radius;
    70.                 Vector3 pos = new Vector3 (0, x, y);
    71.                 ActivateObj(pos, i);
    72.                 //    vertList.Add(pos); // This is adding the same position to the list as it's always made in same place in world and then it'srotated.
    73.                 transform.parent = gameObject.transform;
    74.                 yield return new WaitForSeconds (0.1f);
    75.             }
    76.             yield return new WaitForSeconds (0.1f);
    77.             sphereAngle = 180 / numberOfCircles;
    78.             transform.RotateAround(Vector3.zero, Vector3.up, sphereAngle);
    79.         }
    80.  
    81.         yield return null;
    82.         //    SearchList();
    83.     }
    84.  
    85.     void ActivateObj(Vector3 pos, int i)
    86.     {
    87.  
    88.         //        objects[increment].name = i + "" + j;
    89.         objects[increment].transform.position = pos;
    90.         objects[increment].SetActive(true);
    91.         vertList.Add(pos);
    92.         increment++;
    93.         if (increment > (numberOfObjectsinPool))
    94.         {
    95.             increment = 0;
    96.         }
    97.     }
    98.  
    99.     void SearchList()
    100.     {
    101.         Debug.Log ("i'm doing the Calculations for you now...");
    102.         for (int i = 0; i < vertList.Count; i++)
    103.         {
    104.             if (vertList[i] == Vector3.zero) // How do I search through the list to find if there is an overlapping vert?
    105.             {                                 // Or should I dismiss the poles by half of objects in circle, first and middle taken out after first loop around
    106.                 Debug.Log("Same");
    107.             }
    108.         }
    109.     }
    110. }
    111.  
     
  2. Richop

    Richop

    Joined:
    Mar 18, 2016
    Posts:
    136
    I think I thought of another way of doing it, draw a line from 0,1 on one axis ie Y, these are the poles.
    Split line into sections to make rings, the radius of the rings is what I can't work out, outside of a sphere is like half a wave going up, so could use a 1/2 a wave function to work out the radius for the rings or something.

    How ever I'm unsure of how to much change or whether to increase/decrease the amount of verts as you go up the sphere, this might not be the best way to make an even spacing of points on a sphere, totally unsure.
     
  3. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
  4. Richop

    Richop

    Joined:
    Mar 18, 2016
    Posts:
    136