- Home /
creating a procedural sphere
hello, I am trying to create a procedural sphere for a space game, i want a planet that the further you go the less vertices it has. This is most likely not the best way to do it but it was the only way i could think of. It consists of creating a series of circles, both "poles" would be a single vertex, then apropiately conecting the vertices in triangles. the creation of the circles is where I am currently stuck at. to create them i used the following:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Create_Sphere : MonoBehaviour {
public int lons;
public int lats;
public float radius;
public List <Vector3> points = new List<Vector3>();
void Start ()
{
CreateCircle();
}
void CreateCircle ()
{
points.Add(new Vector3(0, 0, 0));
for (int i = 0; i < lons * 2; i++)
{
//calculate the angle between points
float ang = 180/ lons;
//calculate the points x and z position
float x = radius * Mathf.Cos(i * ang);
float z = radius * Mathf.Sin(i * ang);
//add the points to a List
points.Add(new Vector3(x, 0, z));
}
//visualy display the points
DisplayPoints();
}
void DisplayPoints ()
{
for (int i = 0; i < points.Count; i++)
{
GameObject dspl = GameObject.CreatePrimitive(PrimitiveType.Cube);
dspl.transform.position = points [i];
}
}
}
the problem is, though it does create them at an apropiate distance from the center but as if it had the wrong angle, for example, when creating 2, the ang variable says 180 but it seams to create it at 120 or so. but when there are 3, ang var says 120, but they are on something like a 20 deg. no idea what is going on so thanks a lot to everyone for reading this question and sorry for making it so long.
p.s. all i am trying to create for now is the circle
points.Add(new Vector3(x, 0, z));
This does not seem right for points on a sphere. ALL Cartesian (x,y,z) coordinates should change for each lat/long.
http://en.wikipedia.org/wiki/Spherical_coordinate_system check out the section on "Cartesian coordinates", it will have the formula you are looking for, that converts from spherical(Radius, lat,long) to cartesian(x,y,z) coordiantes.
(using this formula, you will want to create two loops(nested). One for lat, one for long.)
oh, yea ik, thanks for pointing that out. For now im trying to create just a cicle, before i move one to create the whole sphere, to keep it simple. Sory for not specifying.
Your answer
Follow this Question
Related Questions
How to keep an object within a circle/sphere (radius) - NO RECTANGLE 3 Answers
How to find the radius area of linerencderer drawn circle and move objects inside ? 1 Answer
irregularities with procedurally generated sphere 1 Answer
Clamp x,y within circle, z to sphere 0 Answers
How to create a terrain in a sphere with Perlin Noise 0 Answers