I came across this little snippet of code* to find the next power of 2 and found it quite helpful – from what I understand it's also very efficient compared to other methods.
public int NextPowerOfTwo(int inInt)
inInt = ( inInt >> 1 ) | inInt;
inInt = ( inInt >> 2 ) | inInt;
inInt = ( inInt >> 4 ) | inInt;
inInt = ( inInt >> 8 ) | inInt;
inInt = ( inInt >> 16 ) | inInt;
inInt++; // Val is now the next highest power of 2.
Link to original article regarding technique and actually a more detailed explanation of the method – Acius' Snippets.
So here is my version I'm going to make public.
Added + and – for the speed increase and decrease (on the right)
Added frame forward and frame back into the bottom controls (also + and – .. maybe I should change that)
Download the latest zip packages here:
So I created a quick planet texture (loosely based on a moon type texture) and added some functionality to make the ships to move towards the planets with a slight shift along the tangent – which basically means that fleets sent out and fleets incoming wont line up on top of each other so you can distinguish between the two.
The real big change is a added functionality to center and scale the camera viewport based on the map size so in theory this visualizer can handle any map of any size and it will stay centered and not clip off the screen.
Right now there seems to be a selective Unity bug that wont let me display instanced objects in the web viewer :'(. I bugged the Unity guys and hopefully they'll release a patch soonish.
Added GUI text components for the planets and the fleets. I tried with 3D text but the results are true 1:1 pixel ratio for the text rendering so you get aliasing artifacts.
To get the GUI text in the right position I had to do a double transform. Once to Camera pixel space and then to camera viewport space (basically 0,0 to 1,1 extents representation of the viewport)
Vector3 screenPos = gameCamera.ScreenToViewportPoint(gameCamera.WorldToScreenPoint(pos));
Oh and I whipped up a quick ship model to use rather than the capsule I was using before
So in the time while I waited for GoDaddy to update my wordpress I've managed to get a pretty basic implementation of the visualizer in Unity 3.0.
Right now it splits the game data string into rounds and creates a unique 'planet wars' object per round (List<PlanetWars>) – cloning the planets for each round and updating the owner and number of ships as well as creating the unique fleets for each round.
The 'CreateScene' function takes that array and creates a collection of GameObjects for the planets and fleets as needed.
It's creating and destroying the whole scene (planets, fleets etc) each round which isn't really a problem but pretty inefficient.
I need to add text rendering to show the numbers on the planets and fleets to show ship counts.
Unity 3.0 is pretty straight forward to get something like this up and running, however I am having a problem with Monodevelop on my PC. It doesn't seem to want to debug properly. I mean I can start the debug session and everything it's just if I modify any of the source code Unity looses all responsiveness and stops being able to progress. I think it has something to do with the recompile process because it only happens when I make a change to one of the scripts :'(.