Tuesday 12 January 2016

Project 2: Cloth Simulation

Cloth simulation using opengl and c#.

My previous project was about learning the graphics api opengl (lighting/models/projections/shaders) and I feel like if I wanted to carry it on I would have to start again from scratch using things I learned the first time around.
Instead I am focusing on some other concepts which have been on my bucket list for a while now, cloth simulations.
In the beginning there were lines and dots.
My first task was how to effectively render a "cloth" matrix (?) so that I could visually see the system, this is actually quite a challenge when opengl is focused on 3d not 2d.

I set up the model-view-projection matrix using an Orthographic Projection.
Then since the original coordinate system goes (0, +Y, 0) up the screen, (+X, 0, 0) along the screen it was not intuitive to debug so I applied a scale of (0, -1, 0) and a translation, because (0, 0, 0) is the center of the screen in opengl, to transform it into Cartesian coordinates.

Ok... so now I can draw some dots and lines in the right place.

My next task was figuring out the formulas that I needed to use to calculate the forces acting on each dot and how they would interact with each other.
There is A LOT of information on the internet about cloth simulations so if you're looking for an in-depth overview of it look elsewhere. Personally I started with this useful snippet of a paper which can be found here [1]. 
After a while there came physics.
I knew that cloths can be simulated as a set of point masses connected by springs, thankfully forces on springs are fairly simple to calculate (shown above).
So I set up my system where each point mass knows about it's neighbors (similar to the game of life) information and made sure all the calculations happen in the right order.

The most frustrating part about this ordeal was the coefficients, since my system isn't "real" what should Ks and Kd be? In the end I just plugged in several different values and went with the ones which looked the most realistic (still not finished with that bit).
This was a huge step because if the damping isn't perfect then the points oscillate out of control (practically undamped) or just stay still (overdamped), damping.

The top left and right points are "pinned" so the force the exert on their neighbors is proportional to the distance between them and some large constant X. Still not sure about this one.

Anyway here is my current product, there is still a lot to do with shear and linear springs (I think?), making the forces propagate across the system nicely, lighting and filling in the lines.



It was fun to see the points fly wildly about dragging other points with it on it's quest for freedom. Maybe I should implement spring breaking sometime...


No comments:

Post a Comment

The difference between class and object.

 The difference between class and object is subtle and misunderstood frequently. It is important to understand the difference in order to wr...