title: Calculating the moment of inertia of a 2D capsule
body: <p>I am trying to calculate the moment of inertia around the center of mass of a 2D capsule which I divided into a rectangle and two semicircles. <strong>C</strong> is the center of mass and the axis of rotation passes through this point.</p>
  
  <p><a href="https://i.stack.imgur.com/TXjUT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TXjUT.png" alt="2D Capsule"></a></p>
  
  <p>I know that the moment of inertia for a semicircle around an axis perpendicular to its plane passing through the center of its full circle is <strong>1/2 MR²</strong> where <strong>M</strong> is the mass of the semicircle and <strong>R</strong> is the radius (correct me if I am wrong). Here, for the two semicircles, it is points <strong>A</strong> and <strong>B</strong>. For a rectangle, the moment of inertia passing through its centroid is <strong>1/12 M(L² + W²)</strong> where <strong>M</strong> is mass of the rectangle and <strong>L</strong> and <strong>W</strong> are its dimensions. Here, the centroid is the same as center of mass <strong>C</strong>.</p>
  
  <p>According to the <a href="https://en.wikipedia.org/wiki/Parallel_axis_theorem" rel="nofollow noreferrer">Parallel Axis Theorem</a>, the moment of inertia around an axis parallel to the existing axis is the existing moment of inertia added to the mass multiplied with the squared perpendicular distance between the two axes. Here, for the semicircles, around <strong>C</strong> it would be <strong>1/2 MR² + M(AC² or BC²)</strong>.</p>
  
  <p>The total moment of inertia for the capsule would be the sum of its constituent moments of inertia, so it would be <strong>Rectangle's + 2 * (Semicircle's)</strong>. Based on this, this is what I did, but I don't think I got it right because my capsule is jittering all over the place and not rotating as intended.</p>
  
  <pre><code>float momentOfInertiaOfRectangle = (massOfRectangle / 12.0f) * ((extentLength * extentLength) + (extentWidth * extentWidth));
  float momentOfInertiaOfSemiCircle = massOfSemicircle * ((0.5f * m_CapsuleRadius * m_CapsuleRadius) + (extentLength * extentLength * 0.25f));
  float totalMomentOfInertia = momentOfInertiaOfRectangle + (momentOfInertiaOfSemiCircle * 2.0f);
  </code></pre>
  
  <p>Please tell me if I did anything wrong and clarify this for me. Thank you.</p>