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

Pathfinding and AI using C++

An extremely important technique used in modern games is path-finding. In second year of university I implemented a number of pathfinding techniques such as Dijkstra’s Algorithm, Lee algorithm and the A* algorithm.

Below is a demonstration of my implementation of the Lee Algorithm, using C++ and the SFML library for graphics:

ezgif-2742356885.gif

The program works by initially placing a start and end point on a grid, and drawing a maze between the points. Once a maze is created, the spacebar is pressed and a path is drawn between the two points.

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