Network Physics Sim

This project was part of my courserwork for Paralel and Concurent programing. This project allows up to 4 computers to connect to erachother and simulate rigid objects colliding with eachother. The project was created using C++, DirectX11, Dear ImGUI and Flatbuffers

About

This projects allows up to 4 computers to connect and run rigid body simulations. Each PC can search for available PCs and connects automaticaly when one is found. The application allows the user/s to change the frame rate of the graphics, the simulation frequancy and the TPS of the network. The built in scenarios and camera options provide a useful tool to explore the enviroments and the collisions ocuring in the simulation.

Video



How it works

The application is split into 2 different projects. The Physics library and a seperate application project. The Library has the code responsible for object movement, intrsection and collision responces. The application project contains the code responsible for rendering the objects, seting up senarios, connecting and comunicating between diferent computers, seperating work loads over multiple CPU threads.

Physics Library

The Physics library contaisn multiple classes contain all the logic to moving and colliding difernet objects.

Each object that has any kind of physics capabilitys is a PhysicsObject. This class in responcible for managing the objects movement and collider; which I will expand upon later. The most important methods found in the PhysicsObject class are onUpdate and collide.
The onUpdate method is responcible for updating the objects poisition and velocity over the time-step based on the gravity it is found in. An important part of this method is the simType preamiter, which determines which type of integration method is used. The library supports Euler, Semi-Implicit Euler and RK4 integration methods.
The collide method is responcible for checking is there is a cillision between itself and another object. The first step in this method is checking if both the objects actualy contain colliders. After this it ensures both objects arnt stationary (can't move). After these steps it the method trys to aquire a lock on both objects to ensure nothing reads the object data while it is potentaily being changed. Affter aquring both locks the method checks if the two colliders are intersecting using the intersects method which is part of the Collider class. finaly if the colliders are colliding the methods calculates an appropriate responce nad updates both objects and relices the locks.

The other important class found in the library is the Collider class. this class contains all the information and methids required to check for collisions between 2 objects. This calss is a base class that is inherired by colliders for specific shapes. The derivitive classes are: Sphire, Cube, Plain and Capsule colliders. Each of these classes implement the intersects method which checks if it is colliding with another collider. The method uses different algorithms depending on the type of collider it is checking against. Due to the base collider class is not aware of the shape of the collider it is checking against, it uses a double dispatch system to ensure the correct algorithm is used. This saves on using runtime checks to decide what algorithm to use as this would couase performance issues when many objects are colliding at the same time.

Application

The application project is responsible for rendering the physics objects, setting up the scenarios, seperating work loads over multiple threads and managing the network. The apllication uses a few difernt libraries to acomplish this. The rendering is done using DirectX11, the UI is done using Dear ImGUI, data is serialized using flatbuffers and the physics by the physics library mentioned earlier.

The application is split across multiple threads. the main thread is responsible for rendering the objects and the UI as DirectX11 requires all rendering to be done the the same thread. The second thread is responsible for running the physics simulations. 3 other threads are used to manage the network. The first thread is responsible for listening for incomming connection requests and accepting them. The second thread is responcible for sending object data to the other connected PCs. and the final thread is responcible for recieving object data from the other connected PCs. A few more threas are used inside the physics loop to update the physics objects in paralel using a thread pool. Seporating these tasks other these threads allows fot the program to run more efficiently as well as controle the frequancy of each task. For example we could set the physics to run 30 times a second while the network sends data 10 times a second and the graphics to run at 120 frams pers second.
The network system is built using both TCP and UDP sockets. Both protocols are abstracrted behind two colasses called TCPSocket and UDPSocket to ease the process as Windows socket programing can be painfull. The network is a pear-2-pear system where each PC connects to eachother and send data about the objects to each PC. To setup the Network each PC starts a UDP socket to listen for PCs wanting to connect and a TCP socket listening for connection requests. When a Pc wants to find and connect to other PCs it will broadcast a message on a UDP socket. When a PC recives this message it will try to connect to the sender using a TCP socket. If the connection is successfull the two PCs will start sharing data about the current scenario loaded and any objects that are currently found in the scenario.