- Home /
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: http://gyazo.com/d2b7f5dd956ba373b3a2bc03578678cf.png
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.
In the likely event this process doesn't make any sense, please help me anyway. I tried.
Thanks for any help!
I don't see a question here. Are you looking for a solution to place your planes evenly around a sphere?
Not sure I undertand what you want. Do you want to place columns on a sphere so you basically get a Terrain on a sphere by having columns of different height? But you don't scale the column yet to get different heights, right? Also, have you considered generating the $$anonymous$$esh ins$$anonymous$$d of instantiating columns?
Take a look at this question:
http://answers.unity3d.com/questions/410992/how-do-i-get-raycasts-to-cast-symmetrically.html
Part way down the page you will see the function UniformPointsOnSphere(). This will allow you find all the positions.
I can't mark anyone else as the answerer since no one else actually posted an answer, so I marked myself. Sorry robertbu
Answer by Kytuzian · Jan 02, 2014 at 03:43 AM
Yes thank you, I was looking for a solution for just that. I suppose the question was quite clear, as I meant that I had a bunch of rectangular prisms all set with 2d coordinates that I wanted to transform into 3d coordinates around a sphere.
I used the link from robertu.
Thanks!
Your answer
Follow this Question
Related Questions
How do you make a sphere from 6 planes? 1 Answer
How do you create a sphere form 6 planes? 0 Answers
Align an axis to a plane 0 Answers