Structures on planet script

hello,
i want to make a planet overview where the planet is a sphere.

Now i want to zoom the sphere and have different landmarks or buildings on it can anyone point me to a script to calculate postions on the sphere to get the right rotation and position for the structures?

no one?

Then how can i get the postions on the surface of that planet? lets say i want to evenly divide the surface in a grid of 100 places how can i map that positions to the grid? From then i can do the rest on my own.

Depending on the how far the zoom starts and how near the player can get, zooming is going to have to work really differently. There just not enough information to give a concise answer.

If the range of zoom is going to be out from space viewing the globe of earth to street level as an exaggerated example, the least of your worries would be object placement, but knowing what to load and unload based on what’s in view, knowing what level of detail models to show, based off distance maybe. Purpose being to curb framerate lag, or prevent crashing from simply having too many objects to manage.

Actually that is only supposed to be a overview map not a real planet but just a simple planetmap with some objects drawn on it to select region. it will have at most maybe 1000 gameobjects in total (including children) so i load them all always.

like this but in 3d and so i can rotate the camera around it (and with maybe 30-40 gameobjects on it with 10 children each on the map, its supposed to be a bit more fancy than a simple 2d image):

http://static.giantbomb.com/uploads/original/1/16359/1337139-xenonauts_geoscape.jpg

I really can’t tell what you’re asking, but perhaps this article on spherical coordinates will help.

I tried to read it a bit deeper but i am not so sure if it really can help me.

Ok for understanding one little question: If i have a sphere and i want to have the black marked point. How can i calculate it when i assume its 3/4 from the top and 3/4 from the side?

The same question asked in other words:
Where the black spot on this 3D sun in my unity editor is i want to add with a script a black spot on the sun.
How can i calculate the position for this spot?
3148205--239165--unityquestion.png

This thread might help: Rotate sphere to look at camera based on lat lon - Unity Engine - Unity Discussions

Yeah, it sounds like you just want to convert from some sort of spherical coordinates (e.g. latitude/longitude) to cartesian (XYZ) coordinates.

That’s just a bit of trig, something like: x = r * cos(longitude); y = r * sin(latitude); z = r * sin(longitude). (Where r is the radius of your sphere.)

I am not sure i will take a look on it.

I like to try that.
How do i get the radius with the script?

After you get your position from the spherical coordinates. You can now create facing.

// after you set your position using latitude and longitude.
var upVector = transform.position - planet.position;

var lookVector = Random.insideUnitSphere; // this could be a players position, or what not.
lookVector = Vector3.Cross(upVector, lookVector); // this will give you the right direction.
lookVector = Vector3.Cross(upVector, lookVector); // this will change the right direction back to the correct forward direction.
transform.LookAt(transform.position + lookVector, upVector); // now do the look using the offset

OK, nutshell…

Random.insideUnitSphere creates a random xyz that is inside a normalized sphere. this means that the distance from 0 of any given point cannot be more than 1. Remember, this is anywhere inside the sphere.

Cross takes two directions, and gives you the third direction. So, if you gave it a point facing right, and a point facing forward, your result is either up or down, depending on which you put first in the function. We use cross to take the odd random direction, and give a direction that is exactly perpendicular to the up vector.

We use the second cross, to transform the “right” vector back to the forward vector. Since these are simple quick multiplication methods, the calls are extremely quick.

Lastly, we simply use Unity’s power to align the object to the points that we want it to be. Adding the look vector to the position, gives us a relative position to the object we are placing on the planet.

I didn’t test this, but I think the worst would be a syntax error on Random.insideUnitSphere, and it could be a method, ( I dont remember off hand) and the Cross could be backwards. (flip the look vector and up vector on one of them.

Vector math is simple… think numbers…
if I am at 0 and I want to get to 10, then 10 - 0 is my effective direction. so 10. This means that:

target - current = direction

It’s your sphere. You assign whatever radius you want.

I mean i need to insert the radius of the planet.

x = r * cos(longitude); y = r * sin(latitude); z = r * sin(longitude). (Where r is the radius of your sphere.)

so here i need to get the radius of the sphere.
i would guess its
r=scale*boundary of the 3d object /2
Boundary because not all objects are the same size when scale=1
I did something like this once but i dont fully remember the syntax.

is that too cumbersome would it work?

Please fill in:

float r=

Thankyou that will help too :slight_smile:

But… you have a planet right? The planet is a sphere. Surely you know the size of your planet?

That’s what you need to plug in.

1 Like

I am not completely sure if i know the size.
This script it is not for one planet but for all my planets i want to use it on different spheres and i am not sure if the gas giants as example have a bigger mesh, but i can try with the scale and look if it okay. Thankyou for your help :slight_smile:

If the scale works, then great. If it’s a sphere created using 3D Object->Sphere, then this should work.

Alternatively, if you’re using the same script for each planet, you can expose the radius in the inspector so you can manually set it there.

Or at script start, create an AABB around the sphere, get half the height or width, and there’s your radius.