{"id":99,"date":"2011-01-15T18:33:14","date_gmt":"2011-01-15T16:33:14","guid":{"rendered":"http:\/\/blog.teleranek.org\/?p=99"},"modified":"2025-06-29T12:29:14","modified_gmt":"2025-06-29T10:29:14","slug":"particle-based-fluid-simulation","status":"publish","type":"post","link":"https:\/\/blog.teleranek.org\/?p=99","title":{"rendered":"Particle-based fluid simulation"},"content":{"rendered":"<p>I&#8217;ve recently stumbled upon this paper about fluid simulation &#8211; &#8220;<em>Particle-based Viscoelastic Fluid Simulation<strong>&#8220;<\/strong><\/em> by S. Clavet et. al.<\/p>\n<p>This paper just screams &#8220;implement me&#8221;, because its content lacks of unnecessary formalism and entire idea of particle simulation is explained with a pseudocode. So all I needed to do is just rewrite the pseudocode into an actionScript code&#8230;<br \/>\nSo this is the demo:<br \/>\n<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"800\" height=\"600\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"name\" value=\"pvsim\" \/><param name=\"bgcolor\" value=\"#0\" \/><param name=\"align\" value=\"middle\" \/><param name=\"src\" value=\"http:\/\/blog.teleranek.org\/wp-content\/uploads\/2011\/01\/final1.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"800\" height=\"600\" src=\"http:\/\/blog.teleranek.org\/wp-content\/uploads\/2011\/01\/final1.swf\" align=\"middle\" bgcolor=\"#0\" name=\"pvsim\"><\/embed><\/object><\/p>\n<p>There are bunch of parameters which controls the fluid properties &#8211; this is an advantage of using the particle fluid model.<br \/>\nMake\/grab option lets you add the particles, as well as grab them (when clicked in the center of the particle )<br \/>\nYield ratio influences stickyness or &#8220;stretchyness&#8221;. The lower the value, the stickiness or &#8220;stretchyness&#8221; is higher (of course if &#8220;sticky&#8221; or &#8220;stretchy&#8221; sliders are nonzero).<br \/>\n&#8220;Smooth jitter&#8221; and &#8220;smooth pulse&#8221; smoothes fluid velocity field, &#8220;smooth jitter&#8221; handles fast velocities and the other handles slow velocities respectively.<\/p>\n<p>There are many possibilities of how to optimize this, mainly PixelBender to calculate the distance field and alchemy to optimize some vector operations. I believe that there exists some tricks to optimize the goo rendering &#8211; it&#8217;s taking 50% of frame evaluation. Another subjects to optimize:<br \/>\n&#8211; highly unoptimized collision system<br \/>\n&#8211; getting rid of Vector&#8217;s splice function in spring adjusting function<br \/>\nMaybe somebody in the future (most probably &#8211; me;p) will &#8220;pixelbenderize&#8221; this thing, mainly because I&#8217;m curious how much faster can it be.<\/p>\n<p>Additionally I&#8217;ve written a &#8220;library&#8221; (1000 lines of code, the &#8220;library&#8221; term is an exaggeration) which lets you create your own goo, main class of the library is instantiated via<br \/>\n<code>var gg:GooGen=new GooGen( stage.stageWidth , stage.stageHeight );<\/code><br \/>\nThere is a bunch of methods for adding,deleting particles, and most important &#8211; for simulating them:<br \/>\n<code><br \/>\ngg.addParticle( x,y ); \/\/ add particle at x,y<br \/>\ngg.step(delta); \/\/ one step of simulation<br \/>\ngg.drawParticles(); \/\/ renders all particles, since GooGen extends Bitmap class<br \/>\ngg.getParticleAtPos(mouseX,mouseY); \/\/ gets particle at x,y<br \/>\ngg.addBody( x,y,width,height,rotation); \/\/ adds a wall. avoid adding too many walls, or optimize my code ;p<br \/>\n<\/code><\/p>\n<p>I don&#8217;t believe anyone will try to do something using this class, so I saved myself a trouble of writing any documentation :P Wide programmers will always figure out how to use it from the demo source I&#8217;m providing:<\/p>\n<p><a href=\"http:\/\/dl.dropbox.com\/u\/1268089\/viskoelastik\/FluidsViscoelastic.as\">demo source<\/a><br \/>\n<a href=\"http:\/\/dl.dropbox.com\/u\/1268089\/viskoelastik\/teleranek.zip\">GooGen &#8220;lib&#8221;<\/a><\/p>\n<p>Demo uses (as you can see) bit101&#8217;s minimal components.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve recently stumbled upon this paper about fluid simulation &#8211; &#8220;Particle-based Viscoelastic Fluid Simulation&#8220; by S. Clavet et. al. This paper just screams &#8220;implement me&#8221;, because its content lacks of unnecessary formalism and entire idea of particle simulation is explained with a pseudocode. So all I needed to do is just rewrite the pseudocode into [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.teleranek.org\/index.php?rest_route=\/wp\/v2\/posts\/99"}],"collection":[{"href":"https:\/\/blog.teleranek.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.teleranek.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.teleranek.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.teleranek.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":7,"href":"https:\/\/blog.teleranek.org\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/blog.teleranek.org\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions\/352"}],"wp:attachment":[{"href":"https:\/\/blog.teleranek.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.teleranek.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.teleranek.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}