Monday, 4 November 2013

From XNA to Unity

I spent part of the week trying out Unity and it's definitely a goer.  The main issue I investigated was being able to generate meshes when the game runs for building maps.  The mesh class is thankfully open for adding your own triangle and line vertex arrays.  There's a great set of videos walking through parts of the process.  That was another nice thing about Unity, it's obviously quite popular now and there's a ton of help out there for all sorts of areas you might need help on.  I went through the opening tutorial, where you make ball roll around, and the final build step already seems tons better than the XNA equivalent.  Plus it supports Mac & Linux as well and a bunch of mobile platforms too.  It looks pretty great.

I wrote a quick Matrix class to replicate some the XNA methods I was used to, namely the CreateRotation X/Y/Z methods and CreateTranslation method.  You can download that class from my site.  Unity totally has the same functionality for transforming, rotating & scaling, it's just down to habit on my part that I like those methods separated out.

One major bump in the road though was the convexhull/triangulation library I was using.  Knowing this was going to be important I built the library in MonoDevelop and added the dll to the Unity project pretty early on and it compiled no problem.  Fast forward and couple of days though, when I went to actually use the code it threw an error.  Oh...crap.  The MiConvex code was written in .net 4.0 and Unity uses 3.5.  So I find the setting to change the build target, and I'm already starting to feel out of my depth, and it wouldn't compile as it's using some specific 4.0 features.  Double...crap.  I looked into trying to rewrite the few parts that used the specific 4.0 features but it was just above my skill level to decipher.  Triple...crap.  It's really clever code, and the only reason I'd managed to get the voronoi sphere working before was because I'd found that code to handle the tricky generation part.  So I was back to square one all of a sudden.

Went back to blindly stumbling around the web trying to think of my next step.  I had a light bulb moment when I found a site illustrating different algorithms for building a convex hull.  Stepping through the incremental version, which is this nice simple brute force approach, it finally clicked how I might go about replicating the triangulation...and thus the voronoi sphere.  Check this out!



Success!  I wrote my own convex hull triangulation algorithm.  Let me repeat that, I wrote my own convex hull triangulation algorithm.  I'm pretty sure I got enough experience points to level up in programming...I put the talent point into 'luck'.

It actually works super fast for number of points I'll need, showing off the animation because it looks shiny.  Also shows that even if I where to slow it down and it took 5/10seconds  to build a level, if you can turn that into a nice animation for player it's a good trade off.  Plus it only has to do it once at the beginning of the game.  That build is still done in XNA and I'll hopefully get that into Unity shortly.  That's probably the pattern I'll follow, where it's a little easier to do the more code focused work in XNA and then port it across, and then I can use all the nice Unity features, like particles, input etc for the rest.