Skip to main content

Project 4 - Particle Collision Simulation (Part-1)


In a simple particle collision simulation, the goal is to simulate n particles colliding with each other or with the walls of the container.

So how can we simulate these collisions?

To start with simulating these collisions, we will start to looking at what we need to know when simulating collisions.

For the sake of simplicity, let us assume that the particles are circular and that only elastic collision takes place.

1. Dimensions of the container which acts as the boundaries for the particles.
2. Position of each of the particles.
3. Velocity vector of each of the particles.
4. Mass of each of the particles.
5. Radius of each of the particles.

The position of the particles changes depending on the velocity of the particles and the velocity of the particles changes when a particle either collides with a wall or with another particle.

Now, we know how to calculate the new velocities when two particles collide. 
So, how do we check when two particles collide?

This is quite simple to do. 
We check whether any two particles touch or collide each other using their position and radius.

If the distance between the centres of the particles is less than or equal to the sum of the radius of the particles then the particles are colliding with each other
(or)
C1C2 = r1 + r2
where C1C2 = distance between centres of particle 1 and particle 2
and r1 and r2 are the radii of particle 1 and particle 2 respectively.

NOTE: We are not checking what happens when more that 2 particles collide simultaneously.

Once we know that two particles are colliding, we need to update the velocities of those particles. 


The angle brackets indicate the inner product (or dot product) of two vectors.  For more details about this formulate refer to the Wikipedia page.

The next step is to figure out when to check whether two particles are colliding and how the system should be updated.

Here we are going to be looking at two methods:
1. Time driven simulation
2. Event driven simulation

Time driven simulation

In a time driven simulation, we update the system every x amount of seconds.   

This is a very simple way to check for collisions, however, it comes with a few shortcomings:
1. If the number of particles are too high then the performance will be too low because the collision detection takes quadratic time.
2. If the rate at which we check for collisions is too high, then again the performance will be too low.
3. If the rate at which we check for collisions is too low, then we will have a lot of inaccuracies in our calculation. This is because, in an idea situation we want to check when two particles are colliding when the touch each other without overlapping.

The source code for the time driven simulation is available here.

In the next part of the project, we are going to be looking at the event driven simulation.

If you have any questions or have a project recommendation, feel free to leave a comment below.

Comments

Popular posts from this blog

Project 3 - Analysis of sorting algorithms

Sorting algorithms are one of the most basic as well as one of the most used algorithms. They form the basis for many other data structures and algorithms and are also a great way to learn to analyse algorithms.  In this post, I would like to perform my own analysis of these sorting algorithms to understand where and why various sorting algorithms should be used. My main focus is going to be practical analysis of these sorting algorithms and I am also going to be considering the simplicity of these algorithms. So first, let us think about what basis we are going to use for these sorting algorithms. To analyse any sorting algorithm, let us measure the time it takes to sort an array of integers. The array of integers that we are going to give to the sorting algorithms should be of the following types: Random arrays. Ex: [5, 2, 9, 7, 0, 4]. Sorted arrays. Ex: [3, 5, 7, 8, 11]. Sorted arrays in reverse order. Ex: [14, 11, 7, 3, 1]. Sorted arrays with a few random elements added to the ...

Project 6 - State Space Search - 8-Puzzle

State space search is a process which is used to create simple artificial intelligence. It can be used when the problem can be represented as a set of simple states and the player / agent is the only one who can affect the environment. It allows us to generate a path from the initial state to the goal state (of which there can be many depending on the problem). 8-Puzzle / Sliding Puzzle ,  N-queens  and  Route Finding  are some of the various problems which can be solved using state space search. Let us explore the process of solving the 8-Puzzle problem using various path finding algorithms and comparing how effective each of them is. To get started, we need to start by creating the 8-Puzzle game itself. First, we need a simple way to represent different states of the puzzle. A good way to do this is to store the state as a string. For example: Next up, we need a way to generate the actions that are possible from a particular state. We can  think of it as movin...

Project 1 - Browser linked list implementation

All of us use a browser to surf through the internet. In fact you are using one right now. Have you ever wondered how the forward and backward buttons of the browser work ? Or how the undo and redo functions of your text editor works ? The answer to this is a doubly linked list. A linked list consists of various individual nodes which store some data as well as a pointer to the next node. A doubly linked list has nodes which have pointers to the previous node as well. In a circular linked list, the last node points to the first node. Here is how the application is going to work: Whenever you go to a new website, a new node is added in front of the current node. And the forward and backward button traverse through the linked list.  If you wish to tinker with the code, here is the Github Repository . You can easily expand this program to act like undo and redo buttons of a text editor. Linked list are used in various other practical applications such as photo viewers and music player...