title: Soft body engine in C++
body: <p>I am attempting to make an elementary soft body engine in C++ using SDL2. It works by considering all the vertices of the soft body to be interconnected by springs of same length and rigidity (with the same spring constant <code>k</code> and length <code>natural_length</code>). To make it more realistic, I also introduced a damping constant <code>c</code>.<br />
  However, I encountered a frustrating problem. I have been trying to debug it for past 6-7 hours but to no avail. The soft body encounters many strange bugs which I don't understand</p>
  <ul>
  <li>Firstly, the &quot;soft body&quot; is not at all &quot;soft&quot;. It becomes a crumpled mess of points every time. I have tried calculating the force of neighbouring points instead only, but it still becomes a crumpled mess.</li>
  <li>The soft body flies up to the top corner (origin) every time even though I haven't implemented any external forces.</li>
  </ul>
  <p>Both of these bugs are visible in this image -  <img src="https://i.stack.imgur.com/Far1X.jpg" alt="bug" /></p>
  <p>The 2 following functions (they are in the same class as with all the variables, hence need not take in any arguments) are the actual simulation part of the code. (I have omitted the rest of the code as it is unnecessary.)<br />
  I used a <code>vector</code> of <code>SDL_Points</code> to store every point and a <code>vector</code> of <code>Vector</code> to store their velocities. If you are wondering what <code>Vector</code> is, it is simply a <code>struct</code> I created which simply has 2 <code>float</code> members <code>x</code> and <code>y</code>.<br />
  The <code>acceleratePoints()</code> function assigns velocity and position to each point and <code>checkCollision()</code> well... checks for collisions with the walls of the window, which has width <code>scr_w</code> and height <code>scr_h</code>.</p>
  <pre><code>    void acceleratePoints()
      {
          vector&lt;SDL_Point&gt; soft_body_copy=soft_body;
          vector&lt;Vector&gt; velocity_copy=velocity;
          for(int i=0;i&lt;soft_body.size();++i)
          {
              for(int j=0;j&lt;soft_body.size();++j)
              {
                  if(i!=j)
                  {
                      Vector d={(soft_body[j].x-soft_body[i].x)/100.0,(soft_body[j].y-soft_body[i].y)/100.0};
                      float t=atan2(d.y,d.x);
                      float disp=fabs(magnitude(d))-natural_length/100.0;
                      velocity_copy[i].x+=(k*disp*cos(t))/10000.0;
                      velocity_copy[i].y+=(k*disp*sin(t))/10000.0;
                      velocity_copy[i].x-=c*velocity_copy[i].x/100.0;
                      velocity_copy[i].y-=c*velocity_copy[i].y/100.0;
                      soft_body_copy[i].x+=velocity_copy[i].x;
                      soft_body_copy[i].y+=velocity_copy[i].y;
                  }
              }
              soft_body=soft_body_copy;
              velocity=velocity_copy;
          }
      }
      void checkCollision()
      {
          for(int k=0;k&lt;soft_body.size();++k)
          {
              if(soft_body[k].x&gt;=scr_w||soft_body[k].x&lt;=0)
              {
                  velocity[k].x*=e;
                  soft_body[k].x=soft_body[k].x&gt;scr_w/2?scr_w-1:1;
              }
              if(soft_body[k].y&gt;=scr_h||soft_body[k].y&lt;=0)
              {
                  velocity[k].y*=e;
                  soft_body[k].y=soft_body[k].y&gt;scr_h/2?scr_h-1:1;
              }
          }
      }
  </code></pre>
  <p>The <code>magnitude()</code> function returns the magnitude of a <code>Vector</code>.<br />
  The values for coefficient of restitution <code>e</code>, damping constant <code>c</code> and spring constant <code>k</code>, which I used for the image are 0.5, 10 and 100 respectively.
  Thank you for taking the time to read this! Help would be greatly appreciated.</p>
  <h3>Edit</h3>
  <p><a href="http://pastebin.com/eEgmqB6J" rel="nofollow noreferrer">Here</a> is the entire code if anyone wants to test it. You'll need SDL and a folder 'img' with a '.bmp' file in 'img/point.bmp'.</p>