Multiplayer Galaga

Summary

Introduction

Galaga is an arcade classic. It is a fixed shooter arcade developed and published by Namco in Japan and published by Midway in North America in 1981. During the Interactive Entertainment Technology course we were asked to make a replica of this genre and to add the multiplayer to the game.

Almost all of the students in the class (me included) chose to use Unity3d as an engine for development. Mainly for two reasons:

  • Time bounds. We had “only” three months. Although it seems an infinite time, especially if you work in a game company where a title (possibly a AAA title) is released every 9-12 months, it was not for us as we had concurrently other 5 subjects and other running projects (e.g. Real-time Animation) which were really challenging.
  • Easiness of development. Unity is a very powerful and flexible game engine, it increases drastically the productivity if compared to alternative methods using OpenGL and C++ for example.

The Assets for the game are taken from the Unity Assets Store. I found this tutorial, it’s a space shooter tutorial to learn how to use Unity. Given that at the time I started (September 2014) I was a beginner in Unity I thought it was a nice idea to base my project on this tutorial to learn and to re-use most of the assets.

Swarm in Formation

The swarm has been implemented using splines. In Unity there is a handy tool called iTween an interpolation system that takes one value and animates it to another over a given amount of time. Although I used a custom method to animate the enemy, I thought it was useful to have a WYSIWYG spline editor.

Galaga Spline Path 1

 

Given the keyframes set in the editor I used this simple method for the interpolation:

private static Vector3 CubicLerp(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, float t)
{
   var t2 = t * t;
   var a0 = v3 - v2 - v0 + v1;
   var a1 = v0 - v1 - a0;
   var a2 = v2 - v0;
   var a3 = v1;

   return (a0 * t * t2 + a1 * t2 + a2 * t + a3);
}

Here is the link to the Spline class on GitHub if you are interested in seeing the full code implementation.

Network Architecture

The network architecture has been implemented using a simple Client-Server model, with the server being on the same client network. Drawbacks of this approach are scalability, fault-tolerance and the constraint in which players are obliged to be on the same network. I have not adopted any lag compensation algorithm. Network lag can cause game states to diverge making it difficult to maintain the illusion of a single shared space leading to a bad game experience. That makes sense only in big distributed game systems with lot of players. Galaga being a game with maximum two players per instance does not require such complexity.

The Network layer in Unity uses UDP, which is an unreliable, un-ordered protocol but it can be used to send ordered packets reliably, just like TCP does. This technique is called Reliable Delta Compression. To do this, Unity internally uses ACKs and NACKs to control packet transmission, ensuring no packets are dropped. The downside to using reliable ordered packets is that if a packet is dropped or delayed, everything stops until that packet has arrived safely.

This can cause transmission delays where there is significant network lag. In opposition to Reliable Delta Compression there is the Unreliable mode. Unreliable transmission is useful when it is known that data will change every frame anyway, so the effects of a missed packet will soon be fixed by the next one. This approach is preferred when quick, frequent updates are more important than missed packets.

In Unreliable mode, Unity will send packets without checking that they have been received. This means that it does not know which information has been received and so it is not safe to send only the changed data; the whole state is sent with each update. I used this approach in my implementation because the scene changes frequently, setting the packet send rate to 60 per second. The result is a pleasant and the game experience is smooth. Such approach works well as long as the players are within the same network but it has the drawback of not being scalable. The high packet send rate is the main bottleneck thus the players won’t run a smooth game if they play over Internet.

The game could have been more robust by implementing prediction, lag compensation and a fault tolerance mechanisms. Client-side Prediction means predicting the client’s expected movement. Lag compensation is used when packets get dropped on the way to the client. This would normally cause the players’ movement to pause and then jump to the newest position when the new packet finally arrives. Interpolating between the last known position and the new one, where packets were dropped, is smoother.

Video