Search Unity

Structures on planet script

Discussion in 'Scripting' started by rainbow_design, Jul 16, 2017.

  1. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    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?
     
  2. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
  3. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    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.
     
    Last edited: Jul 17, 2017
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    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.
     
  5. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    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
     
    Last edited: Jul 17, 2017
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
  7. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    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?
    unityquestion.png
     
  8. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.)
     
  10. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    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?
     
  11. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    After you get your position from the spherical coordinates. You can now create facing.
    Code (csharp):
    1.  
    2. // after you set your position using latitude and longitude.
    3. var upVector = transform.position - planet.position;
    4.  
    5. var lookVector = Random.insideUnitSphere; // this could be a players position, or what not.
    6. lookVector = Vector3.Cross(upVector, lookVector); // this will give you the right direction.
    7. lookVector = Vector3.Cross(upVector, lookVector); // this will change the right direction back to the correct forward direction.
    8. transform.LookAt(transform.position + lookVector, upVector); // now do the look using the offset
    9.  
    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
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It's your sphere. You assign whatever radius you want.
     
  13. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    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 :)
     
  14. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    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.
     
    JoeStrout likes this.
  15. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    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 :)
     
  16. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    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.