# How to place objects from a 2d plane around a sphere?

I have a 2d plane which is the surface a planet. The planet is composed of square of varying heights, and I want to place them around a sphere so that these squares more or less make up the outside of the sphere.

This the created shape:

Here is my code for placing the objects, in which x and y are the coordinates on the 2d plane. I believe it makes sense, but it could also make none at all.

``````float ActualX = x * TerrainBase.transform.localScale.x;
float ActualY = y * TerrainBase.transform.localScale.z;

float d = Vector2.Distance(new Vector2(ActualX, ActualY), new Vector2()) / Draw.Radius;

Position.x = Mathf.Cos(d) * Draw.Radius + Draw.Position.x;
Position.z = Mathf.Sin(d) * Draw.Radius + Draw.Position.z;
Position.y = Mathf.Sqrt(Mathf.Pow(Draw.Radius, 2) - Mathf.Pow (Position.x, 2) - Mathf.Pow (Position.z, 2)) + Draw.Position.y;

Quaternion Rotation = Quaternion.LookRotation(Utility.GetNormalVector(Position, Draw.Position));
Draw.Map[x][y].Self = (GameObject)GameObject.Instantiate(TerrainBase, Position, Rotation);
``````

y and z have been flipped due to y being up and down in Unity.

ActualX and ActualY are the x and y scaled by the size of the TerrainBase (which is the object I’m using as a base).

d is the angle in radians from the center of the sphere because:
2 pi r = circumference
A circle has 2 pi degrees, so to find the angle you would calculate the distance (Vector2.Distance()), divide it by the circumference to find the percentage, then use that percentage time the full measure of a circle which is 2 pi, or simple divide the distance by the radius.

The x position is the cosine of that angle times the length (the Radius) and offset by the x position of the planet.
The y position is the sine of that angle times the same length and offset by the z position.
The z position can be found because simple algebra, because x^2 + y^2 + z^2 = r^2 for a sphere. Therefore z = sqrt(r^2 - x^2 - y^2).

Finally the rotation is found by using the normal vector of the difference between the position of the new piece of terrain and the center of the planet using Quartenion.LookRotation() and then the object is added to the world.