Skip to content

Commit

Permalink
rebuilding site Wed, Aug 21, 2024 11:08:45 AM
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Aug 21, 2024
1 parent 0d6d6e9 commit 4249a76
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion posts/2024/08/simd-matters/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ <h2 id="graph-coloring">Graph coloring</h2>
<p><img src="/images/graph_color.png" alt="Large Pyramid Graph Coloring"><em>Graph coloring of the large pyramid</em></p>
<p>These colors group together constraints that can be solved simultaneously using SIMD. For example, color 1 has 2524 contact constraints. Each of these constraints is between two bodies. The graph coloring ensures that none of the same bodies appear more than once in all 2524 contact constraints. This means all 2524 constraints can be solved simultaneously.</p>
<p>But isn&rsquo;t graph coloring very complex and slow? Contacts come and go all the time in rigid body simulation. Do I need to recompute the graph colors every time a contact is added or removed?</p>
<p>First of all, there is a lot on intimidating <a href="https://en.wikipedia.org/wiki/Four_color_theorem">theory</a> around graph coloring and it seems at first that some complex algorithms must be applied to do graph color properly. This is not true at all! A simple <a href="https://en.wikipedia.org/wiki/Greedy_algorithm">greedy algorithm</a> is sufficient for game physics.</p>
<p>First of all, there is a lot of intimidating <a href="https://en.wikipedia.org/wiki/Four_color_theorem">theory</a> around graph coloring and it seems at first that some complex algorithms must be applied to do graph color properly. This is not true at all! A simple <a href="https://en.wikipedia.org/wiki/Greedy_algorithm">greedy algorithm</a> is sufficient for game physics.</p>
<p>Box2D maintains a <a href="https://en.wikipedia.org/wiki/Bit_array">bitsets</a> for each graph color. Each bit corresponds to a body index. When a contact constraint is created, the graph color bitsets are examined. The constraint is assigned to the first color with a bitset that doesn&rsquo;t have either body bit set to 1. Once the constraint is assigned to a color, those two body bits are set to 1. This is a very fast operation.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">b2AddContactToGraph</span>(b2ConstraintGraph<span style="color:#f92672">*</span> graph, b2Contact<span style="color:#f92672">*</span> contact)
</span></span><span style="display:flex;"><span>{
Expand Down

0 comments on commit 4249a76

Please sign in to comment.