Augmented Reality Solar System

Did you ever want to control the universe? In this tutorial, I’m going to show you how you can make your desk the center of the universe. You are going to set up a solar system and place the sun, earth and moon in it. Also, we are going to let them circle around each other like they do in reality.

Things you need

I also highly recommend to work through the stretchy line tutorial before starting this tutorial. The reason is, that the solar system builds upon the knowledge of the stretchy line example.

Set up the basics

You should have opened a new project with a new scene and already imported the ARUnity package. If you need support doing this please see Getting started with ARToolKit for Unity.

We now set up an AR scene as in the Getting started with ARToolKit for Unity tutorial with the difference that this scene will contain three ARMarkers and three ARTrackedObjects.

  1. Create a new GameObject called ARToolKit
  2. Drag the following scripts onto it
    • ARController
    • 3x ARMarker
      • The Type of all ARMarker is NFT
  1. Change the Marker Tag of the first ARMarker script to sun and set the NFT dataset name to Sun_poster_150ppi
  2. Change the Marker Tag of the second ARMarker script to earth and set the NFT dataset name to Earth_150ppi
  3. Change the Marker Tag of the third ARMarker script to moon and set the NFT dataset name to Moon_South_Pole_150ppi
Unity3D solar system scene set up

That is how the scene should look like

You can find filter options for each ARMarker script. You may want to play around with those to remove jittering.
  1. Create another GameObject called ARSceneRoot and drag AROrigin script to it
  • Set Layer to AR foreground
  • As child to Scene root create three GameObjects and rename them to Sun, Earth, Moon
  • Drag ARTrackedObject script to those three objects
    • Inside the ARTrackedObject script in the Sun, Earth and Moon GameObject set the Marker tag to sun, earth, and moon accordingly. (This is how you establish the link between the ARMarker and the GameOjects that will be placed upon this marker)
    • Verify that Got marker is true for all three
  • As child to ARSceneRoot create a Camera object
    • Set culling mask to AR foreground
    • Drag ARCamera to this camera object

If you need help during these steps please consult Getting started with ARToolKit for Unity  as the steps above are all described there in detail.

Add the planets

Now we are going to add and style the actual planets.

For the planets add one sphere as a child to the Sun, Earth and Moon GameObject each.

  1. Add a new material to the sun sphere and make it a yellow color.
    1. Select Project/Favorites/All Materials
    2. Right click in the middle area Create/Material
    3. Select a yellowish color for Albedo and Emission
  2. For the earth, I got the material from the Unity Store: https://www.assetstore.unity3d.com/en/#!/content/23399 (it is free)
  3. To style the moon I took the earth material from the Unity Store and replaced the texture with this MoonMap

Create a material

More realistic planets

There are better models to make the solar system more realistic in the Unity Store. But they cost some money. Decide for yourself if you want to use them:

  • https://www.assetstore.unity3d.com/en/#!/content/13783
  • https://www.assetstore.unity3d.com/en/#!/content/4436
  • https://www.assetstore.unity3d.com/en/#!/content/56766

The sun also needs to emit some light so let us do that.

  1. Add a light as child object of the sun sphere
  2. Make the type a point light
  3. Place the light in the center of the sphere
  4. Also, add a Halo to the sphere:
    1. Select SunSphere and check the Halo checkbox
    2. Enter an orange/yellowish color
    3. Set the size of the halo, I used 0.32

Setting the size brings me to the fact that we need to change the default sizes of our spheres: (x/y/z)

  1. Sun Scale: 0.3/0.3/0.3
  2. Earth Scale: 0.12/0.1085/0.12
  3. Moon Scale: 0.03/0.03/0.03

Also, we need to set the position of the planets. If we leave them to default they will be placed with their center in the middle of the marker and that is not what we want.

  1. Sun Position: 0/0/ -0.175
  2. Earth Position: 0/0/ -0.175
  3. Moon Position: 0/0/ -0.175

Get the planets into the correct distance

Now we are going to measure the distance between the planets (well between the markers that are linked to the planets). This is done in a similar way as in the stretchy line tutorial.

1. Create a new script called distanceLine

2. Override the functions: Start, OnMarkerFound, OnMarkerLost, OnMarkerTracked

Start() is called by Unity once the application starts. OnMarkerFound(), OnMarkerLost() and OnMarkerTracked() is called by ARTrackedObject.

3. In Start() we look-up our line object and store a reference to it.

4. In OnMarkerFound() and OnMarkerLost() we lookup if the found/lost marker is the one we are looking for and store/release references to this marker.

5. To let the script know which markers we are looking for we define two static string members which Unity will show as text entry fields for this function. We will use this text fields and enter earth, sun, moon tags accordingly later.

6. In OnMarkerTracked() we call a private function to measure the distance and draw a line between the two markers. We use the line as an indicator if the markers have the correct distance between each other.

7. drawLine() implementation:

  1. First, make a sanity check if we have two markers and if the two markers are the ones we are looking for
  2. Then get the position of the first marker and use it as the start position. Then get the position of the second marker and use as the target position
  3. To draw the line, we use etPosition(0,startPosition) and setPosition(1,targetPosition). 0 marks the start and 1 the target coordinate of the line.
  4. To measure the distance, we use the Unity function Vector.Distance()
8. We also want the line to disappear if only one of the two markers is visible and reappear if  all two are visible.

  1. Add line.setWidth(0f,0f) as last call in the OnMarkerLost() function
  2. Add line.setWidth(0.01f,0.01f) at the end of drawLine()

Start the rotation when the distance is right

We are going to set-up another component that keeps track of the distance changes and kicks off the rotation of the planets. Let’s do this by creating our own component as a C# script.

1. Create another component and call it rotateAroundObject

2. Let us call rotateAroundObject every time we notice that the distance changes:

1. Add an event broadcast to the distanceLine component at the end of drawLine() function

2. Implement the event broadcast receiver method in the rotateAroundObject component

3. Additionally, we pass the measured distance to this event

3. To know which object to circle around define a public member that is used to enter the tag of the GameObject that we want to circle around. While you are at this, define two more members to store the valid min and max distance.

4. Alright, in OnLineChange() we now look up the object to circle around, check if our distance is correct and call startRotation()

We can optimize this a bit if we do not look up the rotateObject every time the line changes but in the OnStart() function. So let’s do that.

  1. Override the OnStart() function
  2. Copy the lookup of the rotateObject over to to the OnStart function
  3. Also, we implement that the position of the planet that circles is moved onto <<Is this needed?>>

Now let’s implement startRotation()

This is only three lines of code:

  1. Call the Unity3D function transform.rotateAround()
    1. Pass to it the position of the object that we are rotating around
    2. The rotation mask. This defines around which axis the rotation is done
    3. And the rotation speed

At this state, the planet is already circling around the other planet. To make it a bit more realistic you can also add the feature to circle in an elliptic way. You can see how that is done in code snippet.

To be able to contol the speed the planets circle around I added another public member called rotation speed. That member takes a number value and is multiplied with the time that has passed. Using this you can have the planes circle slow or fast around each other.

Wire everything together

Now we are going to put everything in place and make the solar system work:

  1. Add two Unity3D Line Renderer as children of ARSceneRoot and name them DistanceLine and DistanceLineMoonEarth:
    • Create an empty GameObject
    • Add the unity component Line Renderer to it
  2. Tag the DistanceLine with the name distanceLine and add the Distance Line script to it.
  3. Tag the DistanceLineMoonEarth with the name earthMoonDistance and add the Distance Line script to it.
  4. Configure the DistanceLine script:
    1. Event Receiver: EarthSphere
    2. Line Tag: distanceLine
    3. Marker 1 Tag: sun
    4. Marker 2 Tag: earth
  5. Configure the DistanceLineMoonEarth script:
    1. Event Receiver: MoonSphere
    2. Line Tag: earthMoonDistance
    3. Marker 1 Tag: earth
    4. Marker 2 Tag: moon
  6. Add the RotateAroundObject component to the EarthSphere and MoonSphere
  7. Configure the RotateAroundObject at EarthSphere:
    1. Rotation Speed: 7
    2. Rotation Object: sun
    3. Valid Distance Min: 0.3
    4. Valid Distance Max: 0.4

You can play around with this values. Just the Rotation Object needs to be the name of the object we are rotating around.

  1. Configure the RotateAroundObject at MoonSphere:
    1. Rotation Speed: 100
    2. Rotation Object: earth
    3. Valid Distance Min: 0.1
    4. Valid Distance Max: 0.13′

Screenshot how to wire up the distance line

 

 

 

 

Screenshot how to configure the rotate around script

That’s it now you have everything in place and the solar system is ready to run. Let me know if you encounter any issues, have any question or general feedback.

Thanks for reading I hope to see you around soon.

Download to the Solar system unity export. (This also includes the function to fade out the lines once the correct distance is reached, which I didn’t cover in this tutorial.)

2 Comments

  1. asdfghjkl January 21, 2017 Reply
  2. Matus April 21, 2017 Reply

Leave a Reply