Sunday, July 19, 2015

Simulating Physics!

I tried do this for a while now and I have to admit it was more challenging than I thought. I had to revise some topics on physics just to get a clear handle on theories I was trying to simulate. Overall, it was a fun project and I am glad it came out just well.

My intentions were to simulate physics based environment where object would be acted upon my real-world-like physical forces. I know there are libraries out there that does this for us and also provide better performance and speed. But, I wanted to try it on my own.

Here's how I divided my work:

1. The Core
The Core package consisted mostly of physical quantities such as Position, Velocity and Acceleration. They mostly had the simple mathematics in them, that did most of the dirty works.

2. The Environment
The environment provides room for existence to the elements and it was my main engine where the elements were added and were acted up. Environment had its own properties like gravity, air frictions and boundary.

This piece of code represents general work in this module:

for element in self.elements:
    if element != selectedParticle:

3. The Element
Element was my abstract representation of real-world object which had properties like mass, color, velocity, position and many others. Elements were added to the environment and where subjected to forces in the environment.

4. Objects
I started with a Circle, that represented a ball like object, and worked my way up-to simulating square blocks. Circles, besides representing a real-world ball, acted as pressure-points for other objects. Pressure-points were useful because these were the actual points that worked as points for exchanging forces upon collision which simulated all sorts of interesting properties like translation and rotation easily.

For example a bar consisted of two pressure points at either end. This would allow us to easily simulate bar-like properties especially rotation experienced due to difference in direction of force at either end of the bar.

The point of collision besides those pressure points could be interpolated easily. Square had a similar layout. It had these bars as its edges, protecting it from all angles.

4. Collision Handling
Collision Handling was modeled using the principle of conservation of linear momentum combined with law of conservation of energy. There was a small problem I face while implementing this approach to my models. The problem was the behavior of  objects upon interaction while there velocity neared zero. There was only one force acting that time, and it was gravity which was constantly pulling objects downwards. This was a problem, because when the objects were on top of each other, the object would collapse, unrealistically, because they had no force left in them to counteract gravity. There was a simple workaround to the problem, but it was not a wise one. Whenever two object came in contact with one another, a small force(in all four direction) would always be acting to prevent collapsing thus separating objects apart.

This was the model I went with. I am planning to share the code in near future as well. Here's a small video of the program in action:

It's not much, but it was a fun :) Let me know if you have any suggestions or feedback. Thanks!