Skip to content

Center of mass

Center of mass is the average position of all parts, weighted by mass. Forces applied here only move the assembly linearly (no rotational effect). COM is critical because forces applied elsewhere in the body will cause rotation around this point. This is why the physics engine tracks it precisely.

Center of mass computation

cpp
// Assembly.cpp - Computing center of mass
Vector3 Assembly::computeCenterOfMass() const {
    Vector3 totalMass = Vector3::zero();
    float totalMassScalar = 0.0f;
    
    for (const Body* body : this->bodies) {
        if (body->isPhysicsEnabled()) {
            Vector3 worldCoM = body->getCoM();
            float mass = body->getMass();
            
            // Weight each part's position by its mass
            totalMass += worldCoM * mass;
            totalMassScalar += mass;
        }
    }
    
    // Average position weighted by mass
    return totalMassScalar > 0 ? totalMass / totalMassScalar : Vector3::zero();
}

Open source, community maintained.