Browsed by
Category: Coding

Tilemap bitmasking

Tilemap bitmasking

As a game developer, bitmasks are useful when it comes to representing a set of data with single digit in both 3d and 2d games. An illustration of this in use in three dimensions can be found within the well known voxel game minecraft. The current status of a block may be represented with a bitmask value to indicate whether it is open or closed when considering blocks adjacent to it. For a given voxel if one (or however many) side(s) is open but surrounding sides closed, perhaps you wish to show a grass texture or only render one side – that voxel would have a bitmask value based on what surrounds it and we can use that to determine how it should render.  If the voxel is completely surrounded, it has a different bitmask value and perhaps you wish not to even render voxels with a bitmask value that indicates it should not be visible. In this usage scenario, what this bitmask value tells you is whether the voxels on each side are open or closed – and this is all saved with a single bit. In other words, through a single bit you are storing a set of data that would otherwise require more memory!

In a 2d game this has all sorts of usage scenario’s too. For example, when it comes to tilemaps and placing tiles there is a huge benefit to storing data represented as a bitmask value. For example, a fence – having a single fence tile may have a stand alone sprite to display, however, placing two fence pieces beside each other may require two different tiles drawn to line up. This means that an algorithm will have to determine the surrounding sides to decide which fence piece should be rendered (i.e. an end piece, middle piece, L shape, etc). We can use a simple calculation to determine this for a 2d tilemap:

With this simple equation, we will then know which tile to display. Below you can see it in action in my game Attack of the Dead – I’ve replaced the wall tiles with some dummy tiles to show the bitmask value for illustration. The variables left, down, right, and up are simple boolean values to indicate whether there is a tile at these positions surrounding the tile you are calculating for.

The textures to display the tile may be named according to the bitmask value for easy organization, or alternatively, you may assign a tile to display based on the bitmask value of that tile and organize your atlas based on the bitmask value.

If you need a quick 4 tile bitmask chart, I’ve thrown one together on JSFiddle below. You can easily modify this for using 8 tiles or whatever suits your needs.

RijndaelManaged, AesManaged, and AesCryptoServiceProvider – SimpleAccountLocker App

RijndaelManaged, AesManaged, and AesCryptoServiceProvider – SimpleAccountLocker App

I recently wanted to write a small app to become more familiar with .NET encryption/decryption libraries. As a result, I created a quick little account locker app which stores manually entered account data locally. Sure, there are great services available which do this already and automate the process, i.e. last pass, for saving passwords securely across multiple devices – however, SimpleAccountLocker is an extremely minimal app which provides the basis to store data locally if you do not wish to save off-site; likewise, it was a fun little way to play with some security libraries.

You can view the app on my GitHub: https://github.com/MrChrisHammond/SimpleAccountLocker

To begin, .NET offers many different encryption classes, however, for this app’s purpose, the main AES classes I tested out include:

– RijndaelManaged
– AesManaged
– AesCryptoServiceProvider

All three of these are based on AES – a specification for encryption created by the US National Institute of Standards and Technology (NIST) in 2001. This method of encryption is symmetric and allows data to be encrypted and later decrypted with a key and initialization vector. This is helpful when you have data you wish to store securely but have a later need to decrypt and read it – for example, an instant message. Nonetheless, within the aforementioned classes are a few differences. For instance, RijndaelManaged allows you to set a different block size whereas AesManaged maintains the same fixed blocksize of 128 so-as not to compromise security. However, AesManaged is actually based on RijndaelManaged.

In terms of compliance, AesCryptoServiceProvider uses a library which is FIPS compliant1 whereas RijndaelManaged and AesManaged do not.  If you are not familiar with FIPS, you might be wondering – what is it? The Federal Information Processing Standard is a set of rules which sets the requirements on approving cryptographic modules. Although this is a US government standard, here in Canada the Communications Security Establishment (CSE) uses FIPS 140-1 and 2 as part of its certification2.

In code, all three of these can be used to encrypt data with an ICryptoTransform transformation, CryptoStream decorator, and using a key and Initialization Vector (IV). Below is a  comparison of AesCryptoServiceProvider vs RijndaelManaged within SimpleAccountLocker.

 

Boids – 2D Unity Approach

Boids – 2D Unity Approach

Boid’s model was created in 1986 by Craig Reynolds to simulate the behaviour of birds as they group together in a flock. The model has three key methods to mimic real life bird flock behaviour. These methods include:

a) Maintaining a heading based on the averaged direction of surrounding boids.
b) Maintaining a separation so boids do not overlap.
c) The boids must maintain a cohesiveness with the flock by steering towards the centre position of surrounding flockmates.

The behaviour also operates similar to schools of fish or other wildlife. As a game developer, this can be useful when implementing enemy mob behaviour and in my case, I used this approach to simulate flocks of zombies in 2d space as seen in the mp4 above.

The concept isn’t too difficult, below I’ve posted two implementations. I’ve implemented it using rigidbody2d but I’ve also included the code for using it without rigidbody force.

  1. Boid Class for use with Rigidbody2D
  1. Boid Class for use with kinematic
Simple Unity Android VR controller setup for Xbox 360 Controllers

Simple Unity Android VR controller setup for Xbox 360 Controllers

It is no longer required to download the Android VR SDK to build a mobile VR game or application for Unity. Instead, you can set your game up in the Unity build settings and make use of UnityEngine.VR.

Creating a simple player controller may be a bit tricky and so I hope this post will save some developers a head ache when it comes to moving a player around with a gamepad in Android based VR projects.

In this setup, I’m using the following:
1 x Android 6.0 Phone
1 x XBOX 360 Wired USB Controller (but you can use any Android VR controller)
1 x Generic Android VR Headset

To begin, it’s important to understand that you have to create a parent and child arrangement of GameObject’s to control a VR player. Whereas generally a simple FPS controller might be arranged with a Camera as a child to a Capsule, with Unity VR you’ll have to child the Camera to a Camera parent Transform and that Camera parent will be a child to your main player. Even more, the Body of the player will have to be a separate child Transform of the parenting Main Player GameObject if your player has a body. Why? This is done to control the movement of the Camera since moving the Camera Transform directly in VR seems to result in odd behaviour/non-functioning movement at the time of this post.

Here is how your basic Main Player hierarchy should look:

Step 2:
Attach a camera to the Camera GameObject.

Step 3:
In your input settings, add two new axes. For the two different Android phones I tested, the Xbox 360 controller I had seemed to map to these axis:

Step 4:

Create a new script and call it SimpleVRController.cs Use the code below then assign the proper transforms in the inspector.

Unity 2D Target Scanner

Unity 2D Target Scanner

Target scanners are essential for any game where you have an enemy mob that actively looks for prey. One way to scan for targets is to cast a circular physics ray based on the radius area you would like to scan.

Below is a quick and helpful intro I’ve put together for anyone who needs a quick and easy target scanner to notify another script via event subscription. It searches for targets based on the tags set through the Unity Inspector.

How to use from another script:

 

Final notes:

This is a simple solution if you only require a few dozen scanners running at a time or less. If you have many more running concurrently then using tags may be slower and I would recommend scanning based on Unity Layers. You can set which layer to RayCast2D on or alternatively, you can set in Project Settings -> Physics 2D and set which physics layers interact with one another.