pvigier's blog

computer science, programming and other ideas

Beginner's Guide to Game Networking

During the last two weeks, I was working on the network engine of my game. And before that, I knew nothing about game networking. Thus, I read a lot of articles and did a lot of experiments to understand all the concepts and be able to code my own network engine.

In this guide, I want to share with you the different concepts you must learn before to write your own game engine and the best resources and articles to learn them.

There are mainly two possible network architectures: peer-to-peer and client-server. In the peer-to-peer architecture, data is exchanged between any pair of connected players while in the client-server architecture, data is only exchanged between players and the server.

While the peer-to-peer architecture is still used in some games, client-server is the standard as it is easier to implement, it requires less bandwidth and it is easier to prevent cheating. Thus, in this guide, we will focus on client-server architecture.

More precisely, we will be interested in authoritative servers, it means that the server is always right. For instance, if a player thinks it is at coordinates (10, 5) but the server tells him that it is at (5, 3), then the client should update its position to that of the server, and not the opposite. Using an authoritative server enables us to detect cheating more easily.

There are mainly three components in game networking:

  • Transport protocol: how to transport the data between clients and the server?
  • Application protocol: what to send from clients to the server and from the server to clients and in which format?
  • Application logic: how to use the exchanged data to update clients and the server?

It is really important to understand the role of each part and its challenges.

Read more ...

Tags: vagabond game-engine


Vagabond – Choosing a GUI Library

The goal of this week was to find a GUI library for the in-game interface. While making some custom widgets may seem easy, writing a general, production-ready, full-fledged GUI library is notoriously difficult. Thus there are few open-source and well-maintained GUI libraries that work with OpenGL or SFML.

In this article, I will enumerate the alternatives I considered and give some details about them. Finally, I will select one and explain my choice.

Read more ...

Tags: vagabond game-engine cpp


Vagabond – Audio Engine

After the graphics engine and the physics engine, this week, I have worked on the audio engine.

To play sounds and music I use the audio part of SFML which is really good. It is a thin wrapper around OpenAL. It supports WAV, OGG/Vorbis and FLAC. Moreover, it also supports spatialization and it is really easy to use.

There are only two things for which an audio engine is needed.

The first one is to make sure that the total number of sounds and music never exceeds 256 (which is an internal limit according to SFML documentation). To do that, the audio engine refuses to play a new sound or music if the limit has already been reached. And as soon as a sound or a music finishes, it removes its instance to be able to play a new one.

The second thing is being able to pause all sounds and music, play other sounds and music and then be able to resume all the sounds and music that have been paused. This use case happens when you are in-game and some sounds or music are playing. Then you go to the pause menu, all the game simulation is paused, so the sounds and music should be paused too. Some sounds are played by the user interface in the pause menu. Finally, when the player returns to the game, the simulation resumes and all the sounds and music should be resumed too.

To achieve that, I will use two abstractions, one for each of the two issues, respectively the audio engine and the audio scene.

Let us dive into the details of these abstractions.

Read more ...

Tags: vagabond game-engine


Vagabond – 2D Physics Engine

This week, I worked on the physics engine. It was the first time, I wrote a physics engine thus it took me a lot of time to design an algorithm that works well. In this article, I will try to share with you the issues I faced and then my current design of the physics engine.

Read more ...

Tags: vagabond game-engine


Quadtree and Collision Detection

This week was short as I was still working on the 2D light system on Monday and Tuesday. I spent the rest of my time working on a quadtree implementation.

In this article, I will share with you my implementation and my thoughts while designing it.

Firstly, I want to argue why I decided to implement a quadtree.

A Quadtree is a space partitioning data structure. Its main advantage against other data structures is its versatility. It offers good performance for insertion, removal and lookup. Thus, we can use it in a dynamic context where the data often change. Moreover, it is quite easy to understand and implement.

If you are totally new to space partitioning, I advise you to read this article by Robert Nystrom. If you want a gentler introduction to quadtrees, you can read this article or this one.

In my game, there are several places where using a quadtree is an instant win:

  • During collision detection, using a quadtree is way more efficient than the brute-force approach (testing all pairs). It is not the most efficient approach though, see this article if you want an overview of possible approaches and benchmarks. But, I will start using this in the first version of my physics engine. I may change later and use a more specialized algorithm if I need to.
  • In the scene graph, to perform culling, I can use a quadtree to find all the nodes that are visible.
  • In the light system, I can use a quadtree to find the walls that are intersecting the light visibility polygon.
  • In an AI system, I can use a quadtree to find all the objects or enemies that are close to an entity.
  • etc.

As you can notice, quadtrees are very versatile. They are a nice tool to have in your toolbox.

You can find all the code that I will show next on GitHub.

Read more ...

Tags: vagabond game-engine cpp