-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# Curiosities | ||
|
||
[Go to the blog!](https://jesperkha.github.io/curiosities/) | ||
I often learn things I find interesting and useful, but despite my attempts at carrying these factoids with me at all times, it seems as though primitive human brain has a tendency to spontaneously forget things after a while. The solution? A blog containing explanations, diagrams, and code examples for various _curiosities_ I have found in the wild. | ||
|
||
**[Go to the blog ➜](https://jesperkha.github.io/curiosities/)** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Cloth simulation</title> | ||
<link rel="stylesheet" href="../templates/theme.css" /> | ||
<link rel="stylesheet" href="../templates/global.css" /> | ||
|
||
<link rel="preconnect" href="https://fonts.googleapis.com" /> | ||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> | ||
<link | ||
href="https://fonts.googleapis.com/css2?family=Fira+Code:[email protected]&family=Prata&family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&display=swap" | ||
rel="stylesheet" | ||
/> | ||
</head> | ||
<body> | ||
<div class="center"> | ||
<div class="nav"> | ||
<a href="javascript:history.back()">Go Back</a> | ||
<p>|</p> | ||
<a href="/curiosities">Index</a> | ||
<p>|</p> | ||
<p>Cloth simulation</p> | ||
</div> | ||
<h1>Euler's method and cloth</h1> | ||
<p>Making a simple cloth simulation is relatively straightforward. It involves estimating the position of multiple points in a grid using a method known as <a href="https://en.wikipedia.org/wiki/Euler_method">Euler's method</a>.</p> | ||
<p>Let's say we have a grid of points, where each point <code>p</code> has a position and previous position vector. We want to estimate where this point will be in the next time step <code>t</code>. To do this, we calculate a "velocity" as <code>vel = current_pos - old_pos</code>. We set the current position as <code>old_pos</code> and add the velocity to the current position.</p> | ||
<p>By also adding a small gravity constant to the points <code>y</code> velocity we get a falling grid of points:</p> | ||
<img src="../.github/clothsim/falling_points.gif"> | ||
<h2>Point recalibration</h2> | ||
<p>Now for the <em>harder</em> part. To simulate the elasticity, we need to make sure neighboring points try to stay together. To do this, we calculate the delta distance between the two points, find the desired distance between them, and compute an offset proportional to the displacement. Here's some pseudo code:</p> | ||
<div class="highlight py"><pre><span></span><span class="c1"># For each line connecting two points</span> | ||
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span> | ||
<span class="c1"># Get the delta position</span> | ||
<span class="n">dx</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">p1</span><span class="o">.</span><span class="n">x</span> <span class="o">-</span> <span class="n">line</span><span class="o">.</span><span class="n">p2</span><span class="o">.</span><span class="n">x</span> | ||
<span class="n">dy</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">p1</span><span class="o">.</span><span class="n">y</span> <span class="o">-</span> <span class="n">line</span><span class="o">.</span><span class="n">p2</span><span class="o">.</span><span class="n">y</span> | ||
<span class="n">dist</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">dx</span><span class="o">*</span><span class="n">dx</span> <span class="o">+</span> <span class="n">dy</span><span class="o">*</span><span class="n">dy</span><span class="p">)</span> | ||
|
||
<span class="c1"># The difference between the expected</span> | ||
<span class="c1"># distance and the actual distance.</span> | ||
<span class="n">diff</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">dist</span> <span class="o">-</span> <span class="n">dist</span> | ||
<span class="n">displacement</span> <span class="o">=</span> <span class="n">diff</span> <span class="o">/</span> <span class="n">dist</span> <span class="o">/</span> <span class="mi">2</span> | ||
<span class="n">offsetx</span> <span class="o">=</span> <span class="n">dx</span> <span class="o">*</span> <span class="n">displacement</span> | ||
<span class="n">offsety</span> <span class="o">=</span> <span class="n">dy</span> <span class="o">*</span> <span class="n">displacement</span> | ||
|
||
<span class="c1"># Apply the offset if the points are not pinned</span> | ||
<span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="o">.</span><span class="n">p1</span><span class="o">.</span><span class="n">pinned</span><span class="p">:</span> | ||
<span class="n">line</span><span class="o">.</span><span class="n">p1</span><span class="o">.</span><span class="n">x</span> <span class="o">+=</span> <span class="n">offsetx</span> | ||
<span class="n">line</span><span class="o">.</span><span class="n">p1</span><span class="o">.</span><span class="n">y</span> <span class="o">+=</span> <span class="n">offsety</span> | ||
<span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="o">.</span><span class="n">p2</span><span class="o">.</span><span class="n">pinned</span><span class="p">:</span> | ||
<span class="n">line</span><span class="o">.</span><span class="n">p2</span><span class="o">.</span><span class="n">x</span> <span class="o">-=</span> <span class="n">offsetx</span> | ||
<span class="n">line</span><span class="o">.</span><span class="n">p2</span><span class="o">.</span><span class="n">y</span> <span class="o">-=</span> <span class="n">offsety</span> | ||
</pre></div> | ||
<p>Notice how we added a check to see if a point is pinned. Pinned points should not be affected by any forces, so you can simply ignore them when updating point positions.</p> | ||
<p>With this new functionality forcing points to stay close to each other, line rendering, and some simple wind physics, we get a pretty good looking cloth simulation:</p> | ||
<img src="../.github/clothsim/final.gif"> | ||
<p>You can change the stiffness of the cloth by adjusting the number of times you run the point recalibration in with each iteration.</p> | ||
|
||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters