Monday, 18 November 2013

Which way is up?

Not a hugely fruitful week.  I spent some time looking at building ui elements in Unity and by extension how manage game state.  I followed the xna gamestate previously and that was nice, it allowed menus to transition on and off nicely making the user experience very pleasant.  There's a good example of how replicate a manager class in Unity where you create an empty game object and attach a manager, as a MonoBehaviour to it so I'll probably follow that as best I can.

After that I started thinking about how handle movement of a game unit.  I recall reading about how Blizzard made Diablo, they knew the player would spend most of their time clicking and attacking so they got that low level system up and running as quick as possible so they could refine it.

0-7 as directions. sampling different points.

Without a regular grid, either square or hex, it's a mildly tricky problem.  I want to able to use the numpad for a lot of the movement.  So I find North from the center point of each cell then calculate if the angle toward is within a certain range,45degrees. In image_A I'm just using the center point and there 0 would be numpad8 and so on.  This is not so good for most cases, so I also then looked at the midpoint of the edge which you can see the results in image_B.  So I calculate it twice to see what fits best, in some case the center point and edge midpoint overlap.  In image_C I add one more point to sample, which is one third between the edge and the center point of the cell.  Sadly you still end up with broken cases to deal with as in image_D.

That probably didn't make much sense to read.  The gist of it though is when you look at these irregular cells, in your mind you may look and think "well that's obviously north, that's east" etc but actually translating that into something you can encode is not straightforward.  My next approach will be to use some fuzzy logic to define what cells best fits what direction, and in certain cases add a small delay where you can toggle between two destination until the one you want is highlighted.

(Unity 4.3 is supposedly out but my 4.2.2 install says it's up to date...hmm).

Monday, 11 November 2013

From XNA to Unity: That's no moon!

Some good Unity progress to report this week...here is the first public demo!  I got all the voronoi sphere generation code up and running in Unity along with the line and vertex color drawing.  Hurray!  Go check it out and play around with the sphere generation parameters.

Voronoi sphere in Unity 
Moving to Unity is proving to be a very good thing.  While I prefer writing this code with XNA and Visual Studio Express, once I'm into spawning units and creating interfaces I think that's when Unity will really come into it's own.   For this early procedural stuff I'm copying and pasting it into MonoDevelop for Unity once done.  That integration is a little awkward, the solution keeps refreshing and the syncing to dropbox, where I back it up, keeps throwing the odd file in use error.  It's a bunch of minor fiddly stuff though.  You can use Visual Studio with Unity but I was getting linebreak errors and other quirks, so it's seems best just to use MonoDevelop which is...well it's ok.  For the really important step though, the build & deploying of a game, Unity seems tons better.

Tortured indices
Couple of glitches for the week, got my indices messed up on the first attempt but that often seems to produce interesting results anyway.  Not entirely sure how Unity handle mouse picking, I think I'll need to create static rigid bodies for all of the voronoi regions, and I presume it has it's own octree implementation which is nice.  That crossover between XNA  and Unity might be a little tricky to translate.  Next step though is to get a unit up and moving around.  Then we can go exploring the surface of the sphere.

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.