Search Unity

FX Planet Orbit

Discussion in 'Made With Unity' started by ForceX, Jul 17, 2011.

  1. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100


    ForceX Planet Orbit v1.1.0 (Feb 4 2012) : Transform method

    ForceX planet orbit is an easy to implement planetary orbit simulation. Setup is a snap:

    1. Create a Parent object for your planet to orbit.
    2. Create an object to be your planet and apply the FX_Planet_Orbit script.
    3. (With your planet selected) In the inspector set your "Parent Mass" to be your Parent object from step 1.
    4. Adjust the settings to your liking. (Calculations are based off of standard "Earth" times)
    5. Run


    This demo simulates Earth and our moon at x200000 normal speed.The orbit is about 2.6 min a year and will rotate 365 times during that time and the moon will orbit the earth once every 27.3 days.



    Internet Explorer 9 users. If you are having problems downloading files from the fourm please enable Compatibility View
     

    Attached Files:

    Last edited: Jan 11, 2014
  2. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    Reserved for instructions
     
  3. psyclone

    psyclone

    Joined:
    Nov 17, 2009
    Posts:
    245
    Very Nice, and quite useful.

    Thanks you very much
     
  4. wiseowlstudios

    wiseowlstudios

    Joined:
    Feb 13, 2011
    Posts:
    741
    can you also make moons orbit planets eg sun as parent planet as child moons as child of planet

    edit-nm it looks like thats what on the webdemo
     
    Last edited: Jul 19, 2011
  5. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    @WiseOwlStudios: Yes you can. Check out the web player and you will see a sim of the earth and moon orbiting the sun.
     
  6. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    Update v1.0.1(3.4)

    This version is compatible with Unit 3.4.

    Resolved:
    1. Fixed warning message about out dated scripting method.
     
  7. zencoder

    zencoder

    Joined:
    Apr 20, 2009
    Posts:
    26
    How do we open the editor GUI? I downloaded the project and imported it. I can run it in uity 3.5, but I can't open the FX_Orbit_Editor. Is there a special key that I need to press to get it to call the OnInspectorGUI function?

    Doh! I figured it out. Its not a game function its really specs you edit in the unity game editor. You click the earth for example and then you can edit its properties like "axial tilt."

    I have to say you did a really good job on this project.
     
    Last edited: Jan 10, 2012
  8. zencoder

    zencoder

    Joined:
    Apr 20, 2009
    Posts:
    26
    One last thought. This is important to my project on solar and lunar eclipses. We don't get an eclipse each month because of the moon's titled orbit about the ecliptic.


    The moons orbit about the earth is tilted 5 degrees. Because of this tilt we only have an eclipse at two possible points around the earth's orbit. In other words the lunar orbit is inclined by about 5 degrees on the ecliptic.



    How would we implement this in the FX orbit project?

    Lunar Nodes

     
    Last edited: Jan 12, 2012
  9. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    I haven't worked with the script in a while but I think you could add an if moon Boolean, then if true up date the orbit along its local axis. That way you could set any non planer orbit angle you want. I could look into this.
     
  10. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    @ zencoder : I added a quick implementation for your non planar orbit. You can enable it by selecting "Non Planar Orbit" in the inspector.. This is not currently orbiting around an axis with an angle offset. What this is doing is offsetting the objects Y position by an amount of "Orbit Angle". This amount is applied to a Sine wave value of -1 to 1 based off the objects current position in the 360 deg orbit. So depending on the size of your planets / moons you man need a very small "Orbit Angle" value (like in the example file) or large if you use big planets.

    The limit of this is that you cannot create an orbit that is perpendicular to the parent orbit. But for slight wobbles i think it'll work just fine.

    This should get you going until i have more time to figure out how to do this based on an angle.
     
    Last edited: Jan 13, 2012
  11. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    Update v1.0.3(3.4)

    New features:
    1. Non Planar Orbit

    Fixes:
    1. Counters will now correctly count local planetary time. Currently all values start from 0 at run time. Future release will allow for custom time offsets time scrubbing during runtime. Counters still have a functionality limit with the "Time Multiplier". It is recommended to keep this value <= 5000000 for a standard earth year orbital period. At this rate the earth will complete one orbit in about 4 seconds.
     
    Last edited: Jan 14, 2012
  12. zencoder

    zencoder

    Joined:
    Apr 20, 2009
    Posts:
    26
    ForceX, really cool! I am downloading and testing now.
     
  13. Lenn Dolling

    Lenn Dolling

    Joined:
    Jan 15, 2012
    Posts:
    15
    Line 86 has a var that is missing a "n" character for it to compile. :)

    Testing as well.
     
  14. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    @SkyboardSoftware : Thanks for the bug report. Should be squashed now in 1.0.3a
     
  15. Lenn Dolling

    Lenn Dolling

    Joined:
    Jan 15, 2012
    Posts:
    15
    Very sweet stuff. If you want to see what I am doing with it.. check out.. .early stages

    http://ltsc.skyboardsoftware.com

    all beta in dev. MMOG :) Using photon cloud. I managed to create all the planets but all with the old version of the script....

    I have been working on getting camera scripts to maintain smooth planet following...

    ** Edit **

    I was thinking that perhaps in the

    main calling script during the orbit updating to create a filter around the desired vector...

    a simple filter that would trap any out of range values and just continue on a probability vector....

    var DesiredPos : Vector3 = rot * Vector3(0, 0, 1) + Parent.position;
    var DesiredDir : Vector3 = (DesiredPos - Parent.position).normalized;

    I attempted to put in a lastPos check that would check for a Y transform that was larger than a 1.5 via a Lerp of the Y position purposed change and last value....as I was finding that the ThisTransform.transform.position.y caused some jumping on some frames......

    something along the lines of doing a Abs on the 2 Y's current and desired and if > 1.5 just keep last....I was thinking this would solve some flickering...hmmm
     
    Last edited: Jan 20, 2012
  16. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    @ SkyboardSoftware: I think i see the flickering your talking about. In your project when you set the FOV all the way to the top to where you can see the moon, it appears that moon is jumping from one spot to another. This is due to it's position being set at multiple locations in the script. Such "my code transform.position = value 1, then later on again my code transform.position = value 2". I believe this is coming from your ThisTransform.transform.position.y. Because the position.y value is being controlled by the UpdateOrbit() function any other attempt to change the value of the objects Y position will cause position jitters, because it will set it's position twice per frame. If the reason you are attempting to clamp the planets Non Planar Orbit is because it's position is going to hi/low then all you need to do is lower it's Orbit Angle value. Orbit Angle is a bad name because angles have nothing to do with it, it is simply setting a height value multiplied by -1 to 1, where -1 or 1 is determined by it's current location in a 0-360 deg orbit. 360deg or 0deg = -1, 180deg = 1.

    Also just to help with script speed. If your typing inside the FX_Planet_Orbit.js script it is not necessary to access the transform of ThisTransform by typing ThisTransform.transform, as ThisTransform is cached as a transform in the start() function. You can access all the transform scripting methods simply by ThisTransform.position, ThisTransform.eularAngles, ThisTransform.localScale. ect. This will prevent GameObject --> Transform look-ups every frame.
     
    Last edited: Jan 21, 2012
  17. Lenn Dolling

    Lenn Dolling

    Joined:
    Jan 15, 2012
    Posts:
    15
    I know I was trying to put a kind of smoother in.... and was not able to just because I think I am trying to half and endless Half.... as the Master orbit Slice gets advanced I was checking the last Advancement and then checking.. but due to the fact that even if I override the Position there would be no gain as then the next frame would just move that much more... I think... hmm.

    yes.. I was thinking that perhaps the rotationProcess that fires off after the orbit setting was the culprit for the flickering.. but I had to adjust the sizes to allow +1.0 Earth Size.... vs 0.12 ... but I know in your example you have the moon and the earth both at 0.12....

    i have boosted the values 10x. as this is the cause I do believe.... I have although timed off every frame and yes have found that some math calculations will collapse from the large values I think of what gets ran through the scene and then exceptions out.... and then recoverers the very next....


    but ... I do have have wish's.. if there is list to write on... but what I will need have is a usable solar system that I can use you know in a Master Client Relationship..... where the Master process waits for the advancement of "time" to then allow a scrubbing of the solar system...


    ahhah just thinking about how to get it all together in a MMO style that is usable. So many things to get working in space. like wow. as I scratch my head.
     
    Last edited: Jan 24, 2012
  18. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    Update 1.0.4

    Changes:
    1. Minor code changes.
    2. Inspector hides information if it's unavailable due to selected options.
    3. Removed diameter value. Planets no longer sized through this script.
    4. Option to have "Orbital Offset" affect the current timing offset of a year.

    @SkyboardSoftware : If i understand correctly you would like to be able to do either:
    A: "Set a time" start the client and have the planets be represented by that time.
    B: Track universe time so when a client enters the client side software will update the positions of the planets to represent the current time.

    If A : This is something i haven't worked out yet, but i have my theories.
    If B : You can just pass the current position values of the planets to the client.

    The "rotationProcess" only effects the rotation of the object and not it's position.
     
    Last edited: Jan 21, 2012
  19. Lenn Dolling

    Lenn Dolling

    Joined:
    Jan 15, 2012
    Posts:
    15

    hey your work on this script is great. testing. whoo hooo
     
  20. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    Update 1.0.5

    New Features:
    1. Planetary rotation now has both "Auto" "Manual" settings.

    Changes:
    1. Removed old unused time calculations during Start() function.
     
  21. Lenn Dolling

    Lenn Dolling

    Joined:
    Jan 15, 2012
    Posts:
    15
    I am going to try to do that. Hold a time value on the game logic server that then sends to the clients the active time of the game...

    whoo hoo 1.0.5
     
  22. zencoder

    zencoder

    Joined:
    Apr 20, 2009
    Posts:
    26
    ForceX,

    I am very impressed. The orbit angle in 1.0.5 seems to work beautifully. Great job! I'll be tying this project to data from nasa to build a solar / lunar eclipse simulator. http://eclipse.gsfc.nasa.gov/solar.html This will take time to be able to calculate the positions and start the simulation based on universal time. The idea being you can enter a date and watch the eclipse on the simulator.

    I plan on exporting this for test on the iphone. I noticed that the shadows generated in unity pro are not supported in the iphone version. http://unity3d.com/unity/licenses

    This forces me to use a projector for the shadows to actually show the eclipse on the iphone / ipad. http://unity3d.com/support/documentation/Components/class-Projector.html

    I must say you did a great job. There are only a few things to add to make it a perfect eclipse simulator. There are 4 types of solar eclipses.



    1. Total: The moon completely obscures the sun.
    2. Partial: The moon obscures most of the sun / but its not a perfect aligment.
    3. Annular: The suns halo is seen around the moon. Annular means a ring around something.
    4. Hybrid: From different locations on earth we see two types of eclipses. From some locations the eclipse appears to be total, on other locations it appears annular.



    There are two variables that create these 4 combinations. One is the orbit angle which you have already added. The other is the apogee / perigee. Calculating lunar perigee, I'm not sure how to do. The moon actually moves closer and further from earth in its orbit. When the moon is near perigee (closest in its approach to earth) we get a total eclipse. When the moon is close to apogee (furthest from earths orbit) we get an annular eclipse.

    http://www.fourmilab.ch/earthview/pacalc.html

    http://science.nasa.gov/science-news/science-at-nasa/2011/16mar_supermoon/

     
    Last edited: Jan 29, 2012
  23. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    Update 1.0.6

    New Features;
    1. Draw orbit: This will draw the planets orbit with a circle.

    Changes:
    1. Added Orbit Position Offset Z to the inspector.

    Fixes:
    1. Corrected an issue with adding "FX_Planet_Orbit" script to a new object generating a null reference error due to no name being entered in the inspector.

    @zencoder: Thanks for your feedback with the script. Your ideas/needs are helping to make this a feature rich package. For the apogee / perigee you could try to adjust the "Orbit Position Offset X Z" values. This will move the orbits center making the moon move closer and further away. If that's not the correct effect then I think adding a non spherical/elliptical orbit would be able to solve for that. This would generate an orbit that's more like a squashed circle. This is something that I've been wanting to do but just haven't gotten around to it just yet. I think i know how to do it. If i manage to make it work it will be in the next update.

    Edit:
    Ok i watched your embedded video and it looks like its a combination of both an non spherical orbit and an orbit that is not centered with the earth. ATM you can adjust the orbits center point but not the orbits shape.
     
    Last edited: Jan 29, 2012
  24. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    Update 1.0.7

    New Features:
    1. Orbit path texture: The orbit path can now have a texture assigned to it in the inspector. See first post for example (texture included).
     
  25. zencoder

    zencoder

    Joined:
    Apr 20, 2009
    Posts:
    26
    @ForceX,

    What you are doing is really great. I'm trying to see if unity script is capable of simulating real planetary orbit. It requires some intense math that may not be possible in unity script. If its not possible I might be able to fudge the orbit a bit by sticking the earth and moon in the proper position and letting a basic orbit continue.

    On the bright side if you can get it to work with keplers laws of planetary motion there is probably a job waiting for you at Nasa. Everything in the universe follows Kepler's laws. The moons orbit is no exception. If you can nail this then you have an accurate simulation that can be used as a part of any scientific project with an need to simulate real orbits.

    Keplers laws of planetary motion
    http://en.wikipedia.org/wiki/Kepler%27s_laws_of_planetary_motion



     
    Last edited: Jan 29, 2012
  26. zencoder

    zencoder

    Joined:
    Apr 20, 2009
    Posts:
    26
    I've downloaded 1.0.7. I'm still learning unity so this resource is awesome. I did want to be able to draw the orbit so I'm looking into how you did it. Again, great job!
     
  27. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    @zencoder: Yea i'm not sure about the accuracy of using the script for exact simulations. It's mainly just fuzzy math that makes things go in circles. :)

    I found the code to draw a linerenderer in a circle here on the forum some where. Then i just modified it to work with the orbit script to get the results i wanted. If i wanted i could have done it my self, but why reinvent the wheel.. HAHA pun intended, you know... wheels are round, planets go in round orbits.. I kill me.
     
  28. zencoder

    zencoder

    Joined:
    Apr 20, 2009
    Posts:
    26
    @ForceX,

    No reason to reinvent any wheels. I did look at your code which led me to the MathF scripting reference.
    http://unity3d.com/support/documentation/ScriptReference/Mathf.html

    Here I see most of the functions we would need to implement Kepler's laws. I'm still learning unity and reading through your code, so it will be awhile before I come up to speed.

    Found two other projects that may be helpful.

    http://forum.unity3d.com/threads/31363-orbit-script
    http://forum.unity3d.com/threads/40976-Planetary-gravity-approximation

    Your code has the moon's orbit done nicely, the orbit about something orbiting. This code shows how to do an ellipse.

    http://www.unifycommunity.com/wiki/index.php?title=Simple_planetary_orbits
     
    Last edited: Jan 30, 2012
  29. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    Nice finds.. These make a great resource.
     
  30. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    Update 1.1.0

    Changes:
    1. Changed the way planets orbit a parent mass. This now allows a planets orbit to be perpendicular to it's parent's orbit. -|-
    2. Orbit Angle is actually an angle and no longer a vertical offset amount. Object size no longer matters.

    Suspended:
    1. Allow Start Orbital Offset To Effect Year: Has been disabled for now. Will return later.
     
  31. zencoder

    zencoder

    Joined:
    Apr 20, 2009
    Posts:
    26
    Awesome! Downloading and testing now...
     
  32. Lenn Dolling

    Lenn Dolling

    Joined:
    Jan 15, 2012
    Posts:
    15
    Last edited: Feb 11, 2012
  33. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,051
    Just to confirm, this script doesn't replicate the force of gravity on an object does it? In other words, if I wanted to have a player controlled object like a space ship fall into orbit with a planet during the game, this script wouldn't work for that, right?
     
  34. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    @Rajmahal: That is correct. This does not use any gravity simulation. The only thing this script does do is setup an object to orbit another object at a rate of speed. I have not done what you are attempting to do, but I would think all that would need to be done is create or mod the existing script to give planets "gravity. This could be something simple like an OnCollisionStay that would pull any objects into it once they come into a collider volume. Or something complex that uses physics to simulate a gravity pull (I cannot help with this option).

    Hope this answered your question. If you have any other questions feel free to ask.
     
    Last edited: Mar 30, 2012
  35. booticus

    booticus

    Joined:
    Jan 1, 2013
    Posts:
    3
    Hey is there any way to set up a fake solar system programatically? I'll admit it's been a while since I've fiddled with Unity, so sorry if it's an obvious question. I'd just love to be able to create a few random planets with a few random moons around each planet.

    Appreciate any help! :)

    -Mario
     
  36. KingShizel

    KingShizel

    Joined:
    Oct 29, 2012
    Posts:
    17
    Its funny seeing this, I did a project for my chem teacher that was a sim of our solar system. She teaches like every science at our school so i thought it would be nice for her to use it in a class or something but she said it was a waste becuase she said im stupid and no one would ever like to see that. Now 3 months later here something is and everyone likes it. Now i bet if she saw this she would fell like a dumb person.
     
  37. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    @boticus : While this script does not procedurally generate a solar system, it can how ever be used to drive the planets of one. First you would obviously need a script to generate your system. Then you would attach this script to any planets or moons and set there parent mass that they would orbit. Thirdly you could setup random properties for your orbits.

    Sorry i couldn't be more helpful. Unfortunately procedural generation is outside the scope of this particular script.
     
  38. laakerules

    laakerules

    Joined:
    Aug 29, 2012
    Posts:
    153
    Umm pretty nice, but one thing the earths orbit is an excact circle around the sun, its actually a eliptical path with the sun closer to one side, kinda like such


    You should add support for the elliptical system for true orbits.
     
  39. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    @laakerules : You can easily do this by adjusting the orbit center offset in the inspector. This will allow you to adjust the orbits center point. Open the demo scene and look at the moon. Here you will see that it's orbit center offset.z is -0.5. When you play the scene you can see the moons orbit path is closer to the earth on one side than the other. What this script can't do however is non circular orbits aka a squished or elongated circle. I believe that would require a true physics based solution.

    Hope this helps.
     
    Last edited: Jan 21, 2013
  40. laakerules

    laakerules

    Joined:
    Aug 29, 2012
    Posts:
    153
    Okay, i aint gonna use this cause i doubt a medieval fps multiplayer game would need it but other than that looks nice.
     
  41. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    @laakerules : Your right i cant see much purpose for it in a medieval fps game either. If you ever try your hand at a space related game please feel free to use this in your project.
     
  42. booticus

    booticus

    Joined:
    Jan 1, 2013
    Posts:
    3
    Thanks! I'll try my hand at it!
     
  43. hike1

    hike1

    Joined:
    Sep 6, 2009
    Posts:
    401
    How do I add a planet? I ctrl-D Earth, renamed it Mars, moved it out 50 million miles, it spins but doesn't orbit the sun.
     
  44. hike1

    hike1

    Joined:
    Sep 6, 2009
    Posts:
    401
    K, now it works, I changed 'orbital distance' to 10 (from 5) and unchecked 'keep local time', as orbital period is 687 days.
    Also added a FPS controller on the same plane, turned off gravity, backwards speed, and fall speed. Next I need
    some Wing Commander asteroids.
    Thanks, ForceX!
     
  45. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    @hike1 : Sounds like you got a lot going on in that scene :) Glad you got it working the way you needed.

    Edit: If you are needing some good asteroids for visual flare i would suggest checking out Darkcoder's Space Graphics Toolkit. This kit has some nice billboard style asteroids that look incredible. I thought they were 3D at first but they are a textured quad with normal maps that make them look 3D. Very resource friendly.

    You can see an example of his asteroids in this image.

    The only thing is i do not believe they are collideable in there vanilla state.
     
    Last edited: Feb 9, 2013
  46. hike1

    hike1

    Joined:
    Sep 6, 2009
    Posts:
    401
  47. Jessicajrs

    Jessicajrs

    Joined:
    Nov 14, 2013
    Posts:
    10
    Hello my friend!
    Thanks for this amazing package! Really appreciate it!

    I'm trying to figure out how to achieve something like this:

    http://www.rkm.com.au/animations/animation-graphics/Solar-System-Planets.png

    I mean, I want the planets to orbit the star for a while (each one with a different orbit velocity) and then they align and the animation stops!

    I made it in video a few times doing this: I start all planets in the desired final position (aligned) and then I run the animation for a few seconds and the planets orbits will not be aligned anymore.
    After that I invert the timeline settings so that they begin non aligned and end up aligned (and the animation stops).

    Now, I have no idea how to do this on unity! Someone has any idea?
     
  48. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    I suppose you could create a script that allows the planets to orbit for a set amount of time then after that time has passed disable the orbit script once the planets have reached a pre defined position in their orbit.

    Something like this. I dont have access to unity or my scripts so you will probably need to change the Planets.Orbit with the correct var name from the orbit script that contains the orbit position.

    Code (csharp):
    1.  
    2. var StopTime : float; // The amount of time you want the planets to animate / orbit
    3. var StopAngle : float; // The position in the orbit you want the planets to stop
    4. var Planets : FX_Orbit[]; //The planets
    5.  
    6. function Update(){
    7. if(Time.time >= StopTime){ // Check if the current time is >= StopTime
    8.   for(var i : int = 0; i < Planets.Length; i++){ // Check the position of all the planets in their orbit
    9.     if(Planets[i].Orbit >= StopAngle  Planets[i].Orbit <= StopAngle + 2){
    10.       Planets[i].enabled = false; // Disable the orbit script stopping the planets.
    11.     }
    12.   }
    13. }
    14. }
    15.  
     
  49. TomatoOrgyLT

    TomatoOrgyLT

    Joined:
    Aug 10, 2012
    Posts:
    6
    I love what you made. However I get weird ussues when I build my project. The graphical orbit circles in the editor look fine but when the project is built and I open the .exe file the circles look all purple. By the way I get 3 warnings when I build my project showing Assets/FX_Planet_Orbit/Editor/FX_Orbit_Editor.js(22,18 ): BCW0012: WARNING: 'UnityEditor.EditorGUIUtility.LookLikeInspector()' is obsolete. LookLikeControls and LookLikeInspector modes are deprecated. The same error is in the line (43, 34) and (130,42). Why does that happen? :(
     
  50. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,100
    The errors are because in unity 4.3 those custom editor look like instructions are depreciated / obsolete. You can simply comment them out. The purple / pink may be because the shader used for the orbit rings is not included in your build. Unity does not include shaders that have not existed in the scene pre build time. You can fix this by assigning any shaders to be included in your build by going to

    Edit --> Project Settings --> Graphics --> Always Include Shaders
     
unityunity