Bitboard based movement in Unity

10th March 2019

In this post, inspired by the 2nd section of an excellent course Mathematics for Computer Games Development using Unity by Penny de Byl, I'd like to show you how to traverse a game object across 8x8 bitboard in Unity.

In case you're not familiar with the bitboard data structure, I highly recommend to Google it up, it's a simple yet super powerful concept with you could achieve much more than I present in this post which is really just the tip of an iceberg.

I assume, you already have at least basic understanding of Unity engine, so without further ado, create yourself a new Unity project and put a Capsule into your default scene at [0,0,0], then create and attach to this game object a new C# script, let's call it BitboardMovement.cs.

In this script, first, declare yourself two unsigned longs for holding your current and one-step back positions of the game object on 8x8 bitboard and assign both of them a value of 1L:

ulong bitBoard = 1L;
ulong bitBoardLastState = 1L;

As you probably know already, ulong and long in C# are 64-bit integers and one bit can have a value of either 0 or 1. Now imagine all 64 bits arranged in a table with 8 rows and 8 columns. Actually, you don't have to imagine them as you can see them like that below. That's a bitboard:

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1

Before you start shifting that bits across the bitboard, write in the script a pair of new methods. First, a very simple one, to check if the bit at certain spot defined by row and column has the value of 1.

bool GetCellState(ulong bitboard, int row, int collumn)
   ulong mask = 1UL << (row * 8 + collumn);
   return ((bitboard & mask) != 0);

And second, to reverse order of bits. This comes useful when you shift your bits out of range so it overflows and you end up with bitboard of value 0.

In such case, you may do something different, but I choose this approach because I really like the effect as you come to the edge of bitboard and then jump outside you appears at the opposite position of your last state.

ulong ReverseBitsOrder(ulong bitBoard)
  ulong reversed = 0;
  for (ulong i = 0; i < 64; ++i)
    reversed <<= 1;
    reversed |= (bitBoard & 1);
    bitBoard >>= 1;
  return reversed;

The last method you need to add before you start shifting bits in the Update method is a method that actually changes the position of game object this script is attached to:

void MoveToPosition(ref ulong bitBoard, ref ulong bitBoardLastState)
  if (bitBoard == 0)
    bitBoard = ReverseBitsOrder(bitBoardLastState);

  int x, y;
  for (int r = 0; r < 8; r++)
    for (int c = 0; c < 8; c++)
       if (GetCellState(bitBoard, r, c))
         transform.position = new Vector3(r, 0, c);
         Debug.Log(string.Format("X:{0},Y:{1} | BB Bin:{2} | BB Dec:{3}", r, c, Convert.ToString((long)bitBoard, 2).PadLeft(64, '0'), bitBoard));

Notice both bitBoard and bitBoardLastState are passed as a reference because if we actually end up with zeroed bitboard, we want to fix that and modify the original variables.

And as I already wrote, the last thing you need to do is shift bits by 1 bit to left or 1 bit to right when left or right arrow is pressed or by 8 bits to left or 8 bits to right when up or down arrow is pressed, because it's obvious that shift by 8 bits in 8x8 bitboard means to move across the rows.

void Update()
  if (Input.GetKeyDown(KeyCode.LeftArrow))
    bitBoardLastState = bitBoard;
    bitBoard = bitBoard << 1;
    MoveToPosition(ref bitBoard, ref bitBoardLastState);
  else if (Input.GetKeyDown(KeyCode.RightArrow))
    bitBoardLastState = bitBoard;
    bitBoard = bitBoard >> 1;
    MoveToPosition(ref bitBoard, ref bitBoardLastState);
  else if (Input.GetKeyDown(KeyCode.DownArrow))
    bitBoardLastState = bitBoard;
    bitBoard = bitBoard >> 8;
    MoveToPosition(ref bitBoard, ref bitBoardLastState);
  else if (Input.GetKeyDown(KeyCode.UpArrow))
    bitBoardLastState = bitBoard;
    bitBoard = bitBoard << 8;
    MoveToPosition(ref bitBoard, ref bitBoardLastState);

You can download an example unity package here. It was made with Unity 2018.3.61f.

Building C++ projects in virtual Linux machine from Visual Studio 2017

10th February 2019

I came up with the idea to build C++ project pseudo-remotely in virtualized Linux machine from Visual Studio 2017 when I intended to make a native plug-in for Android game made with Unity.

That has to be a .so file, which stands for a shared object and it’s the Linux / Unix equivalent of dynamic link library - .dll. If you're interested, you can find more about this topic in Unity Docs.

To try out what this post is about, you're going to need:

Compile remotely from Visual Studio 2017 is really simple, but first, you need to set up your virtual Linux machine.

I’m not going to write in details here how to install Linux in Virtual Box, there’s plenty of tutorials cover that topic. So, in the following lines I’m going to assume you already have your virtual Linux prepared.

However, it is mandatory to have your virtual machine in the same network. To achieve that, you just need to bridge your virtual network adapter with your physical one.

In Virtual Box open the Settings of your virtual machine, select Network tab, tick Enable Network Adapter (in case it isn’t ticked already) and from lists below set

Attached to: Bridged Adapter
Name: {{ Your physical network adapter }}

Now run your virtual machine and in the Terminal type ifconfig to display the IP address of your virtual machine, so you can check if it’s in the same network as your physical machine. You're also going to need the address later in Visual Studio.

While you’re in the Terminal, install g++ compiler and OpenSSH server and allow port 22 by entering

apt-get install g++
apt-get install openssh-server
ufw allow 22

And that’s it. All you have to do now is to connect Visual Studio 2017 with your virtual machine which is a standard, few-clicks procedure you can find well-documented in Microsoft docs.

I also recommend to read out the following chapters - Deploy, run, and debug your Linux project and Linux Project Property Page Reference.

I had to learn how to learn

6th February 2019

In this very first post on my blog, I'd like to share with you some learning tips or let's say a set of rules which I've set for myself after a lot of mistakes I made during my own still continual learning process.

So far I finished 14 Udemy courses since September 2017 and thanks to the knowledge I gained by doing that I recently got a job as a programmer, which I was aiming to.

Learning itself is also a skill and better you master it the faster and more effective your learning will become.

Following this, I believe you'll get the most of every course. I do programming courses, but I think most of this is applicable in every field.

1. If there are challenges, never skip them

And if the challenge is too hard, at least try. If you succeed as an extra step, you can modify the task to challenge yourself (and learn) a little bit more.

2. Learn every single day at least an hour

Maybe you're saying now, that you don't have an hour a day, but I strongly believe everyone can find an hour for learning. Track down what you've been doing for a week and then see what you can get rid of to get your hour for learning. And of course it could happen you skip a day, it happens to me too of course, but it's occasionally.

3. No more than two courses at a time (maybe even just one)

I've been on that road for a while, doing even four or five courses at the same time. Some of them are still unfinished.

4. Plan

I have a special journal just for learning where I write down every Monday lessons for each day in a week and when I see on Sunday evening all planned lessons marked as done, It gives me a powerful motivation kick.

5. Make your own stuff

As you're going through the course, you're gaining knowledge and at some point, you're able to make more complex stuff on your own. For me, it's usually somewhere around the middle of a course, when I start working on some microproject completely on my own. This approach could really solidify what you’ve learned.

6. Specialize

Once I've been learning hacking, programming, 3D modeling, started to learn Japanese and I planned to learn how to play piano. Well, I know what a chord is, but I can't play, I can crack WiFi password as long as the hotspot remembers when king Richards III. was in charge and I know what neko means in Japanese.

Pretty useful stuff, right? Luckily I realize soon that none can be expert in everything and now I'm fully focused on programming.

7. Be humble and never stop learning

Even after many years of studying you should be aware there's always something new to learn and always will be, so never stop learning (yep, I wrote it twice, because I consider it as the most important thing of all).