Technical Insights on Doing Interaction and Physics with HoloLens
Games provide a good way to test the limits of what’s possible. Not only do applications need to be easy to use, fast and connected to the rest of the world, they also often require features that push the boundaries of a platform.
We chose a game that everyone knows: using a catapult to destroy an object. Many famous games for mobile devices use this concept, and most of them operate in two-dimensional space. Our game would be completely constructed in three-dimensional space, which adds a further layer of implementation complexity.
The goal in the game is to have breakable objects. We decided that the best visual style to use is the voxel visual style. When using voxel visual style, each 3D object is composed from cubes (voxels) that fill up the volume that should be taken by the object. This gives the objects a blocky look and feel. It also makes it easy to do the breaking of the objects to smaller parts when they get hit. The gallery below contains images of models that we created and used in the game:
Our idea was to have a vehicle (a boat) that will circle around an island and destroy structures on it. What we were aiming to get was something like this:
The players get rewarded for breaking the structures. Bonus points are earned if the structure is destroyed in less than 5 hits. The more hits it takes the player to destroy the structure, the less bonus it gets. Once a structure has been destroyed enough, the player will advance to the next level. We consider that a structure has been destroyed enough if the number of voxels it is composed of or its height falls below a certain percent.
We decided to use Unity as a game engine. Unity has a native support for HoloLens since version 5.5. Microsoft also provides a lot of useful utilities in its open sourced and free library HoloToolkit available on GitHub. All of the scripts that need to be written for a project in Unity, can be written in C#, and there is also a great integration with Visual Studio.
The voxel models, could be created with various software. We decided to use a software that is specially meant for creating voxel models – MagicaVoxel. It was really easy to pick up and use, and has sped up our development time. It also provides the means to export the created models in various formats.
The voxel models created in MagicaVoxel needed to be imported in Unity. During the import process we wanted to setup the physics for the imported model and also attach some predefined and custom components that will affect the gameplay (colliders, rigid bodies, components for calculating points and destruction status, etc.). For this reason, we created a custom importer. Our importer automatically created prefabs that can be just dragged and dropped in the game. One imported object can be seen on the picture below:
Implementing the gameplay mechanics was actually pretty simple to do. The real challenge was configuring the physics. Unity provides a good physics simulation engine that could be used. There were several important points here:
- Performance – how many physics objects can be used without deteriorating the performance a lot? Keep in mind that all of the breakable models were composed of cubes. If not careful, a simple looking model would contain thousands of cubes. That would have big impact on performance. Our tests have shown that HoloLens is powerful enough to support physics simulation, but only for simple scenarios. This means that we should restrict ourselves to models that contain no more than 300 cubes. That’s kind of the sweet spot. We also tested with larger number (around 2000), but once a complex simulation starts (an explosion happens), the game just froze for a few seconds before continuing. This is not what we want.
- Scaling – when developing games in Unity for HoloLens you have to keep in mind that the game objects will be placed in the real world. They need to be in the right size. In order to achieve correct scaling, we needed to keep in mind that 1 unit in Unity is 1 meter in the real world. That meant that the objects, when placed in a scene were very small. To get the demonstrate the problem, look at the following picture:
As it can be seen, the objects need to be really small (around 30cm high). This means that a single voxel (cube) should be very small (1cm or less). This affected the physics simulations. Even the simplest buildings – towers with cubes stacked one over another – would collapse instantly. We had to modify the physics settings and the gravity to make the simulation work. This is a trial and error process, but once we got the settings right, everything worked as expected and looked natural.
- Model constitution – when we talk about importing models composed out of cubes, you would think that it would be fairly easy to just import them and attach rigid body and collider components. Basically that is true. However what about the case when there is a cube that doesn’t have cubes below it. Think of, balconies or bridges. How do we resolve this issue? Turns out, it is very easy. Unity provides the joints as a way to connect several physics objects together. We used the Fixed Joint component to connect every hanging cube to its neighbors. This way the cube will have something to hang on. These fixed joints can be broken if a excessive force affects the objects (explosion for example). This can be seen on the image below:
The cube that is marked with magenta is hanging. It is connected to the adjacent cubes (marked with orange) with fixed joints. The same infrastructure is established for every cube that is hanging.
The game is planned to be released in the app store around April / May 2017.
Follow us on Twitter for updates and news.