Sunday, April 07, 2019

Network Time Sync

Synchronizing the time between client and server is pretty complex, I initially naively thought that using System.getCurrentTimeMillis() would somehow magically solve my problem. I realized quickly that this was not going to work as this is dependent on the current system time setting.

The most appropriate solution to the problem is to use a common NTP server and ensure that the time is set right, but this is rarely a viable option when you have to deal with consumer level apps. We have to therefore resort to re-implementing or approximating the time sync.

After searching through the web, this is the simplest one i could find and is what worked immediately and fixed our issues with time sync.

TimeSync Equations
Based on the above equation, we are able to calculate the time difference between client and server time stamps and hence reduce the errors in physics calculations. 

Monday, January 21, 2019

Thinking in ECS

If you have been following the new developments in the Unity Game engine, you would have found that they have been focusing on performance and modular code. One of the outcomes of this is a new way of writing code called "Entity Component System" where "Systems" operate on "Entities" which have various "Components" within them.  The ECS name is specific to Unity naming convention this "paradigm" also goes by the name of "Data oriented Programming".

This could easily be mapped to how "C" programming works, or in general any functional programming works i guess..

"Systems" == "Functions"
"Entities" == "Structures"
"Components" == "Data Types / Unions"

If we had to write a game engine in C i believe any reasonable programmer would write it this way, there would be an array of structures which contain the "position, rotation, scale" information of each object and there would be a update function that would loop through them and update the values as required.

I understand that that its' a stretch to call ECS the new functional programming because the systems are still classes, but it seems really easy to grasp the ECS concept when you look at it from the "C" programmer perspective. This is the trick i used to wrap my head around the whole ECS system.

Yet another way to look at it:
If you are a shader programmer or have spent any time trying to look at how GLSL/Compute shaders work, ECS can be mapped to a piece in glsl world as follows

"Systems" == "shader (FS/VS)"
"Entities" == "Input / Output Structures"
"Compoenents" == "Data types"

One of the main reasons these ECS systems make sense is that they can be pushed to GPU for running in parallel on thousands of these objects at once.