Network programming in Java for Android

There are a number of ways to demonstrate networking features for mobile devices. Almost everyone nowadays has a smartphone that has access to the internet – allowing for developers to utilise these features to create interesting games and useful apps.

In my 3rd year of university I created an android game which makes use of several different networking features:

  • Local databases
  • Remote databases
  • Location services

Overview

The game itself is a simple clone of doodle jump. The game uses the accelerometer in the device to calculate the orientation of the device – this is how the player controls the game. Here is a short gif of the game being played:

ezgif-com-35b65d6b8b

Databases

When you play the game, your score is always automatically added to a database local to the device- This is the local highscores. From this point, you have a choice to add it to a remote database which is the global highscores.

To test the remote database, I used wampserver which hosts a server on the computer it is running on. From here, I connected my phone to the network and accessed the database. To communicate to the server, the app uses PHP files to retrieve and alter the database files.

Location services

To demonstrate the use of location services for a mobile device, the longitude and latitude co-ordinates of the current location is sent along with the players score to the database. Although this is not the ideal use for location services, it is used solely as a demonstration of the implementation.

After a location has been sent to the database, the player can access the database and click on any of the scores. When the player clicks on the score, the co-ordinates are sent to a maps activity, which shows the location on google maps.

DirectX11 Programming with Shaders

In order to demonstrate a number of different hlsl shaders in DirectX11, I created a 3D scene using a framework I was given by a lecturer in my 3rd year of university.

Aspects of the scene:

  • Basic vertex manipulation – Vertex shaders.
  • Shadows using shadow mapping – Depth shader, shadow shader.
  • Height map that makes use of tessellation – Hull shader, domain shader, pixel shader.
  • Gaussian blur post-processing – horizontal and vertical blur shaders.

Basic vertex manipulation shader:

To create a basic simulation of flowing water, a large plane can be passed through a vertex manipulation shader that makes use of a small formula that manipulates the Y co-ordinate of each vertex based on a cosine wave. The cosine formula is added to a time variable which gives movement to the wave. On top of this, a water texture is added to the plane. The final product is shown in the following gif:

ezgif-com-c9465e262a

 

Shadow Mapping:

To simulate shadows within the scene, a flat plane and a model are passed through a depth shader which calculates the depth of each pixel from the point of view of the light that casts the shadow.

The depth texture is given to the shadow shaders, along with the texture of each object and the position of the light. The resulting shadow map is then given to the pixel shader which determines that anything in the shadow map cannot be in shadow – thus the resulting pixels are lit – leaving the rest of the scene in shadow.

capture

 

Tessellated Height Map:

Tessellation is used in games to create very highly detailed textures. As an example, I passed a height map through a tessellator in order to show the difference that tessellation can make.

To begin with, the vertex shader receives and processes the control points, ready to send to the hull shader. The constant hull shader receives all of the control points and makes use of the tessellation factor that is passed in. It will then output the tessellation factor for each edge and interior of the patch. The control point hull shader sets up each control point for the tessellator. The tessellator now only has to subdivide each patch based on the tessellation factors that were calculated in the hull shader stage. The new vertices are then passed onto the domain shader stage.

The domain shader will act as a vertex shader and calculate the vertex manipulation based on a height map texture that is passed in. Using the tessellated vertices, the domain shader makes use of linear interpolation to calculate the position of the vertices, texture co-ordinates and normals of the plane. The texture co-ordinates are then used to increase the height of the Y vertices, based on the colour of the heightmap texture.

After the vertex manipulation, the pixel shader applies light calculations using a texture and two separate lights.

Single Tessellation:

capture2

8x Tessellation

Capture3.PNG

 

Post Processing – Gaussian Blur

Post-processing in general can be quite taxing on the performance of any program. To alleviate this slightly, the process of gaussian blur in this program down-samples the scene texture before blurring and up-samples the scene texture again after blurring.

The blurring process is also split into two separate shaders – horizontal blurring and vertical blurring. This significantly cuts down the amount of samples per pixel compared to if the process was done all in one.

The scene before and after the gaussian blur is applied:

Network programming in C++ with SFML

My assignment for network programming in 3rd year was to create a program which simulated a world with a number of movable objects, where multiple machines could interact with the world through a network.

I implemented this using the SFML framework and library, which I found much easier to use over Winsock. The SFML library also gave me an easy way to render a world.

The program uses a client-server based architecture, where many clients can connect to a singular server through both a local or wide area network. Messages between the clients and server are sent using a datagram transport – holding several different variables.

The variables being sent are:

  • Vector2 – for the players position
  • Boolean – whether the player is moving or not
  • Enum – Direction the player is moving in
  • Float – Velocity in the Y direction

To keep a consistent view of the game world, a prediction technique is used to update each players position. Using the three variables; the boolean that determines whether the player is moving or not, enum that determines the direction the player is moving in, and the float for the players Y velocity, the client program will always update the other players position based on the most recent packet that has been received. If a packet is lost, the prediction will keep movement constant, and if no packets are lost, the prediction is not needed and is overridden.

Here’s a short gif of the game being played over LAN:

ezgif-1390280178

Procedural Terrain Generation through faulting in DirectX11

Another technique I have used to create terrain procedurally is faulting. Faulting to create terrain is essentially just cutting a flat plane in half in a random direction, then raising one half and lowering the other half. This process is iterated until reasonably realistic terrain is created.

Here is an example I created using faulting:

Capture

Faulting is fast and efficient but to create realistic terrain over a large scale it will probably take thousands of iterations. However, if you were to create a game like Minecraft or something similar where the terrain does not need to be high resolution, faulting would be ideal.

Procedural Terrain Generation using mid-point displacement in DirectX11

For my 3rd year module ‘Procedural Methods’ I experimented with a couple of different procedural terrain generation techniques.

The first technique I used was mid-point displacement – using an algorithm called the ‘Diamond Square’ algorithm. This algorithm goes through a number of different steps and repeats itself until the terrain is fully generated. For more information on the diamond square algorithm click this link.

The initial terrain looks like this:

Capture1

Just now the terrain is far too large, the texture is stretched and the cliffs are far too pointy. To smooth out the terrain I created a simple noise map which is then blurred and multiplied with the terrain map.

After smoothing the terrain looks something like this:

Capture2

Finally, to make the terrain look more realistic and less 2-dimensional I added snow to the mountain using slope-based texturing. The implementation of this sounds complicated but in reality its quite simple.

First I created a normal map for the mountain by taking the tangent vector of each point in the height map. From there all I needed to do was calculate the slope by using the formula (1 – normalmap.Y). This will give us the slope of each point – the steeper the slope, the less snow there will be.

After adding the texture, the terrain looks like this:

Capture3