A Flight Simulator in LibGDX?

Category: Articles Published: Sunday, 04 January 2015 Written by Troy Peterson

So we've pretty much established that LibGDX is an awesome framework for 2d games... The Shaft and LoggerBill are both basic 2d games and so is our up coming puzzle game that we hope to release soon... But just how does LibGDX fare as a 3d game development framework? I've been wondering that. So, as a side project during 3 days of my Christmas vacation I decided to dive in and try to write a 3d landscape graphics engine for a flight simulator... To see if it could be done.  There are some Android flight simulators out there, but I think they're all done in C++. Can LibGDX handle it?

Maybe... Yes!

The 3d functionality in LibGDX is quite complete and well thought out.  The biggest issue with doing 3d graphics using Java is the context switch to native code whenever you need to pass on anything to OpenGL... Luckily, LibGDX helps us here immensely by using the same approach that it does for 2d graphics - batches. The 3d rendering commands use something called a "ModelBatch" which is anagolous to a SpriteBatch to accumulate all of your drawing commands together and send them to the low level OpenGL libraries all at once at the end. This makes it natrually faster and far superior to trying to do all of this in Java and OpenGL without any sort of batching mechanism.

But the big area that it falls down in is documentation. Xoppa has written a great series of tutorials on the libGDX 3d framework, but they only scratch the surface... And if you do not know the techniques behind 3d graphics in general it can be hard to grasp. Further to this, if you read anything on the web about 3d techniques most of them are based on raw OpenGL so they assume you are dealing with the matrix math yourself... Libgdx provides wonderful abstractions of cameras and models so that you don't need to deal with the matrixes directly... it can get very confusing.

In the end i managed to toss together a very basic 3d height map engine that reads in a .png file and builds a quad-tree hight map from it... and then flies over it. I used a heightmap of the UK that I found and in 3 days (with a few hours wasted on dead  ends) I got a basic proof of concept working...  sort of. Performance on Android is still atrocious... I get 60+ frames per second on desktop, but android drops to 30 fps at best.. hovering more consistantly around 14 fps (Updated Jan 11, 2015 - I was playing around with it a bit and without any significant changes I somehow managed to get it to consistantly do 60+fps on Andoird too. Not sure what I changed, but it works great!)... But I do believe it is possible with tweaking to get this up to 60+fps on Android... so yes, I would say that I _think_ libgdx can handle some fairly complex 3d rendering and could possibly be used to write a flight simulator... but it is a little painful.. there's no doubt that it would be better just to do is in C++ (NDK)...

The code is in no state for release and probably never will be... but I'll share some screenshots.

I started with simple flat shading:


Then added smooth shading... There is a slight problem with my normals calculation that is causing the un-intended grid effect... It shouldn't be difficult to fix that, but I havn't done it yet. It makes it look like your flying over a huge globe.


And finally... some textures. The grid problem is still there...




There's obviously lots more to do... Like a proper sky... but this shows promise and potential. It's been a fun project for the past 3 days, I don't know if I will get back to it... Pehaps some day.

Hits: 6216