Sunday, April 26, 2015

Earth, Moon and Sun using JavaFX 3D

If you're impatient, download the Netbeans project.

Well, I finally was able to get some "Me-time" last weekend and I immediately wanted to try out all those things that I would think of when I'm really busy in office developing boring enterprise stuff. Out of many things I wanted to do, I really wanted to try out JavaFX 3D first. Now, this is because several times in past I've tried to start with JavaFX 3D and always Platform.isSupported(ConditionalFeature.SCENE3D) would return false on my 5 year old Dell laptop with GMA HD Graphics chipset, running Ubuntu. But after recent update to Java 8 Update 40, this changed and the statement started returning true. So I thought I'd give it a try. I'm a complete noob when it comes to 3D. So I had to go through some documentation and articles on developing 3D content specifically using JavaFX. And I thought I'll try to create a simple sun, earth and moon animation as it seemed to cover all the new stuff I've learned.


Only when I was half way through, I realized that I didn't know how to make the sun a real source of light. If you check out the video above you'd see that instead of Sun emanating the light out, the light is actually shone upon it making it look like another huge planet. This is because I've used a PointLight as the light source and I've put it approximately mid-way between Sun and Earth. JavaFX provides only 2 types of light sources: PointLight and AmbientLight. PointLight fit the bill for what I was trying to acheive (due to its directional nature within the scene). Any suggestions on how to make the sun actually look like a Sun are welcome.

I also noticed that the shadow of Moon or Earth doesn't fall on each other whenever there is a Solar or Lunar eclipse situations. 

Other than the Sun and shadows, I'm happy with the way Earth, Moon and the camera rotation turned out to be. The moon is phase locked with Earth (I'm not sure if it can been seen in the video).  The camera rotation works 180 degrees and the performance is quite good. Download the Netbeans project (link at top) if you're interested.