Skip to content

Gyakobo/Cross-Hairs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Cross-Hairs

image image image image image image image

Author: Andrew Gyakobo

Note

This is a project which I presented and subsequently won 1st place in the 2021 Hackathon @ NJIT.

Warning

This project is very raw and indelicate, and honestly could be furnished slightly more. Most prominently I want to point out that I should have definitely utilized malloc and allocated and managed memory way more efficiently. Apart from that there's a ton of memory leaks and code errors. Moreover, I ought to have used a struct or a class for each particle in this engine.

Introduction

To start off, I always had a passion for mathematics, physics and programming. The world always seemed quantifiable and boring to me, thus I always reverted to coding. I specifically loved exploiting game and physics engines. Making up glamorous formulas predicting the gravitational trajectory of an object always fascinated me. Having heard of the Hackathon, I was sort of pushed into making my own physics from scratch.

This project tries to simulate fluid dynamics, and fluid viscosity. In particular I drew inspiration from the game Noita (cool game btw) to develop this project. More details can be found on this video

Methodolgy

The game is divided into small grids which pertain to a specific.

  • The dynamics of this game are painfully simple. Each particale in the grid follows a specific behaviour and interacts with other particles which also conform to the same move set.

In particular each particle moves in the following manner:

  1. If there simply isn't anything underneath a select particle then the said particle just moves down:

  →  

 
  1. If there's however another object underneath the existing one then the particle moves to the left of the object:

  →  

 
  1. And subsequently the element would seek to occupy the next available space on the right:

  →  

 
  1. Lastly, if all the avaiable space below are occupied then the particle in question would remain at the same place. There is however another methodolgy that I glossed over. There exists an additional step on making the viscosity aspect of this game even more prominent adding another relation: each particle that has nowhere to go would simply 'slide' down left and cocomitantly right if there's already a particle on the left:

  →  

  →  

What I'd have done differently

  1. Most definitely I should have just instantiated a struct struct Particle and created them with malloc and freed memory with free respectively. The contents of this data structure would be as follows: the coordinate int x, y and the color char color.

Note

From a memory management perspective, you ought to have the smallest element first in the struct in order to minimize memory usage. This way a select struct object would be of size 9 bytes.

struct Particle {
    char color; /* 1 byte */
    int x, y; /* 4 bytes per variable */
}
  1. The particle grid could also be operated with the use of threads. Let say we have an adustable 640 x 480 grid. For now it's very easy to work with this grid, however, as soon as you add more elements or increase the grid size, there becomes an overhead and latency increases. This is where you should use multi-threading.

The OpenMP - open Multi-processing library

Just as a side note the OpenMP library comprises of the following parts. Also feel free to download, edit, commit and leave feedback to the project.

For more details about parallelism in C please refer to my previous multi-threading project where I utilized and explained the <omp.h> C library.

Compiler Directives

#pragma omp parallel
#pragma omp critical
#pragma omp barrier
#pragma omp master

Functions

#include <omp.h>
int omp_get_thread_num()
int omp_get_num_threads()

Compiling and Linking

gcc -fopenmp # C compiler
g++ -fopenmp # C++ compiler

Environmental variables

export OMP_NUM_THREADS=8
export OMP_NESTED=TRUE

How to execute

To execute this project please first give exec permissions to build.sh and of course run it:

$ sudo chmod +x build.sh
$ sudo ./build.sh

License

MIT