Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Centroid Question
#6
The mass computation in my physics engine is based upon the following code by Randy Gaul. 

https://github.com/RandyGaul/ImpulseEngi...er/Shape.h
Code: (Select All)
void ComputeMass( real density )
  {
    // Calculate centroid and moment of interia
    Vec2 c( 0.0f, 0.0f ); // centroid
    real area = 0.0f;
    real I = 0.0f;
    const real k_inv3 = 1.0f / 3.0f;

    for(uint32 i1 = 0; i1 < m_vertexCount; ++i1)
    {
      // Triangle vertices, third vertex implied as (0, 0)
      Vec2 p1( m_vertices[i1] );
      uint32 i2 = i1 + 1 < m_vertexCount ? i1 + 1 : 0;
      Vec2 p2( m_vertices[i2] );

      real D = Cross( p1, p2 );
      real triangleArea = 0.5f * D;

      area += triangleArea;

      // Use area to weight the centroid average, not just vertex position
      c += triangleArea * k_inv3 * (p1 + p2);

      real intx2 = p1.x * p1.x + p2.x * p1.x + p2.x * p2.x;
      real inty2 = p1.y * p1.y + p2.y * p1.y + p2.y * p2.y;
      I += (0.25f * k_inv3 * D) * (intx2 + inty2);
    }

    c *= 1.0f / area;

    // Translate vertices to centroid (make the centroid (0, 0)
    // for the polygon in model space)
    // Not really necessary, but I like doing this anyway
    for(uint32 i = 0; i < m_vertexCount; ++i)
      m_vertices[i] -= c;

    body->m = density * area;
    body->im = (body->m) ? 1.0f / body->m : 0.0f;
    body->I = I * density;
    body->iI = body->I ? 1.0f / body->I : 0.0f;
  }
Reply


Messages In This Thread
Centroid Question - by TerryRitchie - 07-13-2024, 06:00 PM
RE: Centroid Question - by Pete - 07-13-2024, 06:26 PM
RE: Centroid Question - by bplus - 07-13-2024, 07:58 PM
RE: Centroid Question - by TerryRitchie - 07-13-2024, 09:20 PM
RE: Centroid Question - by Pete - 07-13-2024, 09:44 PM
RE: Centroid Question - by justsomeguy - 07-13-2024, 11:56 PM
RE: Centroid Question - by bplus - 07-14-2024, 12:00 AM
RE: Centroid Question - by aadityap0901 - 07-14-2024, 06:14 AM
RE: Centroid Question - by TerryRitchie - 07-14-2024, 01:14 PM
RE: Centroid Question - by Kernelpanic - 07-14-2024, 01:11 PM
RE: Centroid Question - by bplus - 07-14-2024, 03:42 PM
RE: Centroid Question - by Kernelpanic - 07-14-2024, 06:24 PM
RE: Centroid Question - by bplus - 07-14-2024, 06:55 PM
RE: Centroid Question - by Kernelpanic - 07-15-2024, 03:07 PM
RE: Centroid Question - by Pete - 07-14-2024, 07:15 PM
RE: Centroid Question - by vince - 07-15-2024, 03:46 AM
RE: Centroid Question - by bplus - 07-15-2024, 01:02 PM
RE: Centroid Question - by Pete - 07-16-2024, 12:33 AM



Users browsing this thread: 6 Guest(s)