{"id":2456,"date":"2024-12-08T00:18:47","date_gmt":"2024-12-08T05:18:47","guid":{"rendered":"https:\/\/www.lilithebowman.com\/blog\/?p=2456"},"modified":"2024-12-08T10:21:49","modified_gmt":"2024-12-08T15:21:49","slug":"simulating-particle-physics-2d","status":"publish","type":"post","link":"https:\/\/www.lilithebowman.com\/blog\/2024\/12\/simulating-particle-physics-2d\/","title":{"rendered":"Simulating Particle Physics (2D)"},"content":{"rendered":"\n<p>I decided to have some fun with Unity tonight so I&#8217;ve been having fun rendering to a <code>Texture2D<\/code> some particles bouncing around with an eye towards making a 2D fluid simulation.<\/p>\n\n\n\n<p>It took most of the night wrestling with Unity&#8217;s <code>Texture2D<\/code> class to even get it to render anything in the first place.<\/p>\n\n\n\n<p>Once that was tamed, the particles themselves were easy!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class SimulatedParticle {\n\tpublic Vector2 position;\n\tpublic Vector2 velocity;\n\tfloat gravityConstant = -1;\n\tint boundaryX;\n\tint boundaryY;\n\tfloat drag = -0.5f;\n\tpublic SimulatedParticle (int width, int height) {\n\t\tposition = new Vector2(Random.Range(0, width), Random.Range(0, height));\n\t\tvelocity = Vector2.zero;\n\t\tvelocity.x = Random.Range(-1, 1);\n\n\t\tboundaryX = width;\n\t\tboundaryY = height;\n\t}\n\n\tpublic void Move() {\n\t\tvelocity.y += gravityConstant;\n\n\t\tposition.x += velocity.x;\n\t\tposition.y += velocity.y;\n\n\t\t\/\/ If we hit the sides, bounce back\n\t\tif (position.x &lt; 0) velocity.x = -velocity.x + drag;\n\t\tif (position.x &gt; boundaryX) velocity.x = -velocity.x + drag;\n\n\t\t\/\/ if we hit the top or bottom, bounce back\n\t\tif (position.y &lt; 0) velocity.y = -velocity.y + drag;\n\t\tif (position.y &gt; boundaryY) velocity.y = -position.y + drag;\n\t}\n}<\/code><\/pre>\n\n\n\n<p>I rendered out the colour based on the velocity of the particle. It defaults to green fully on. The velocity affects the red and blue channels.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"720\" style=\"aspect-ratio: 1280 \/ 720;\" width=\"1280\" controls src=\"https:\/\/www.lilithebowman.com\/blog\/wp-content\/uploads\/2024\/12\/2024-12-08-00-15-13.mp4\"><\/video><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ render the particle's current position\nrenderTexture.SetPixel(\n\t(int) particle.position.x,\n\t(int) particle.position.y,\n\tnew Color(\n\t\tMathf.Abs(particle.velocity.y),\n\t\t1,\n\t\tMathf.Abs(particle.velocity.x)\n\t)\n);<\/code><\/pre>\n\n\n\n<p>Right now it&#8217;s late and I have to go to bed, but this is interesting. I&#8217;ll continue to play with this idea later.<\/p>\n\n\n\n<p>I created a GitHub Repository that you can check out. <a href=\"https:\/\/github.com\/lilithebowman\/Fluid-Simulation-Experiments\">https:\/\/github.com\/lilithebowman\/Fluid-Simulation-Experiments<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I decided to have some fun with Unity tonight so I&#8217;ve been having fun rendering to a Texture2D some particles bouncing around with an eye towards making a 2D fluid simulation. It took most of the night wrestling with Unity&#8217;s Texture2D class to even get it to render anything in the first place. Once that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2459,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2456","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-daily-musings"],"_links":{"self":[{"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/posts\/2456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/comments?post=2456"}],"version-history":[{"count":6,"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/posts\/2456\/revisions"}],"predecessor-version":[{"id":2465,"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/posts\/2456\/revisions\/2465"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/media\/2459"}],"wp:attachment":[{"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/media?parent=2456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/categories?post=2456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lilithebowman.com\/blog\/wp-json\/wp\/v2\/tags?post=2456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}