src/body/RigidBody.cpp
// Compute and set the center of mass, the mass and the local-space inertia tensor of the body using its colliders
/// This method uses the shape, mass density and transform of the colliders of the body to set
/// the total mass, the center of mass and the local inertia tensor of the body.
/// Note that calling this method will overwrite the
/// mass that has been set with the RigidBody::setMass(), the center of mass that has been
/// set with RigidBody::setCenterOfMass() and the local inertia tensor that has been set with
/// RigidBody::setInertiaTensorLocal().
Compute and set the center of mass, the mass and the local-space inertia tensor of the body using its colliders
This method uses the shape, mass density and transform of the colliders of the body to set
the total mass, the center of mass and the local inertia tensor of the body.
Compute and set the center of mass the mass and the local - space inertia tensor of the body using its colliders This method uses the shape mass density and transform of the colliders of the body to set the total mass the center of mass and the local inertia tensor of the body .
void RigidBody::updateMassPropertiesFromColliders() {
    const Vector3 oldCenterOfMassWorld = mWorld.mRigidBodyComponents.getCenterOfMassWorld(mEntity);
    Vector3 centerOfMassLocal = computeCenterOfMass();
    const Vector3 centerOfMassWorld = mWorld.mTransformComponents.getTransform(mEntity) * centerOfMassLocal;
    mWorld.mRigidBodyComponents.setCenterOfMassLocal(mEntity, centerOfMassLocal);
    mWorld.mRigidBodyComponents.setCenterOfMassWorld(mEntity, centerOfMassWorld);
    const BodyType type = mWorld.mRigidBodyComponents.getBodyType(mEntity);
    if (type == BodyType::DYNAMIC) {
        Vector3 linearVelocity = mWorld.mRigidBodyComponents.getLinearVelocity(mEntity);
        const Vector3& angularVelocity = mWorld.mRigidBodyComponents.getAngularVelocity(mEntity);
        linearVelocity += angularVelocity.cross(centerOfMassWorld - oldCenterOfMassWorld);
        mWorld.mRigidBodyComponents.setLinearVelocity(mEntity, linearVelocity);
    }
    RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body,
             "Body " + std::to_string(mEntity.id) + ": Set centerOfMassLocal=" + centerOfMassLocal.to_string(),  __FILE__, __LINE__);
    Vector3 inertiaTensorLocal;
    decimal totalMass;
    computeMassAndInertiaTensorLocal(inertiaTensorLocal, totalMass);
    mWorld.mRigidBodyComponents.setLocalInertiaTensor(mEntity, inertiaTensorLocal);
    if (type == BodyType::DYNAMIC) {
        Vector3 inverseInertiaTensorLocal(inertiaTensorLocal.x != decimal(0.0) ? decimal(1.0) / inertiaTensorLocal.x : 0,
                                          inertiaTensorLocal.y != decimal(0.0) ? decimal(1.0) / inertiaTensorLocal.y : 0,
                                          inertiaTensorLocal.z != decimal(0.0) ? decimal(1.0) / inertiaTensorLocal.z : 0);
        mWorld.mRigidBodyComponents.setInverseInertiaTensorLocal(mEntity, inverseInertiaTensorLocal);
    }
    RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body,
             "Body " + std::to_string(mEntity.id) + ": Set inertiaTensorLocal=" + inertiaTensorLocal.to_string(),  __FILE__, __LINE__);
    mWorld.mRigidBodyComponents.setMass(mEntity, totalMass);
    if (type == BodyType::DYNAMIC) {
        if (totalMass > decimal(0.0)) {
            mWorld.mRigidBodyComponents.setMassInverse(mEntity, decimal(1.0) / totalMass);
        }
        else {
            mWorld.mRigidBodyComponents.setMassInverse(mEntity, decimal(0.0));
        }
    }
    RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body,
             "Body " + std::to_string(mEntity.id) + ": Set mass=" + std::to_string(totalMass),  __FILE__, __LINE__);
}
void RigidBody :: updateMassPropertiesFromColliders ( ) { const Vector3 oldCenterOfMassWorld = mWorld . mRigidBodyComponents . getCenterOfMassWorld ( mEntity ) ; Vector3 centerOfMassLocal = computeCenterOfMass ( ) ; const Vector3 centerOfMassWorld = mWorld . mTransformComponents . getTransform ( mEntity ) * centerOfMassLocal ; mWorld . mRigidBodyComponents . setCenterOfMassLocal ( mEntity , centerOfMassLocal ) ; mWorld . mRigidBodyComponents . setCenterOfMassWorld ( mEntity , centerOfMassWorld ) ; const BodyType type = mWorld . mRigidBodyComponents . getBodyType ( mEntity ) ; if ( type == BodyType :: DYNAMIC ) { Vector3 linearVelocity = mWorld . mRigidBodyComponents . getLinearVelocity ( mEntity ) ; const Vector3 & angularVelocity = mWorld . mRigidBodyComponents . getAngularVelocity ( mEntity ) ; linearVelocity += angularVelocity . cross ( centerOfMassWorld - oldCenterOfMassWorld ) ; mWorld . mRigidBodyComponents . setLinearVelocity ( mEntity , linearVelocity ) ; } RP3D_LOG ( mWorld . mConfig . worldName , Logger :: Level :: Information , Logger :: Category :: Body , " " + std :: to_string ( mEntity . id ) + " " + centerOfMassLocal . to_string ( ) , __FILE__ , __LINE__ ) ; Vector3 inertiaTensorLocal ; decimal totalMass ; computeMassAndInertiaTensorLocal ( inertiaTensorLocal , totalMass ) ; mWorld . mRigidBodyComponents . setLocalInertiaTensor ( mEntity , inertiaTensorLocal ) ; if ( type == BodyType :: DYNAMIC ) { Vector3 inverseInertiaTensorLocal ( inertiaTensorLocal . x != decimal ( 0.0 ) ? decimal ( 1.0 ) / inertiaTensorLocal . x : 0 , inertiaTensorLocal . y != decimal ( 0.0 ) ? decimal ( 1.0 ) / inertiaTensorLocal . y : 0 , inertiaTensorLocal . z != decimal ( 0.0 ) ? decimal ( 1.0 ) / inertiaTensorLocal . z : 0 ) ; mWorld . mRigidBodyComponents . setInverseInertiaTensorLocal ( mEntity , inverseInertiaTensorLocal ) ; } RP3D_LOG ( mWorld . mConfig . worldName , Logger :: Level :: Information , Logger :: Category :: Body , " " + std :: to_string ( mEntity . id ) + " " + inertiaTensorLocal . to_string ( ) , __FILE__ , __LINE__ ) ; mWorld . mRigidBodyComponents . setMass ( mEntity , totalMass ) ; if ( type == BodyType :: DYNAMIC ) { if ( totalMass > decimal ( 0.0 ) ) { mWorld . mRigidBodyComponents . setMassInverse ( mEntity , decimal ( 1.0 ) / totalMass ) ; } else { mWorld . mRigidBodyComponents . setMassInverse ( mEntity , decimal ( 0.0 ) ) ; } } RP3D_LOG ( mWorld . mConfig . worldName , Logger :: Level :: Information , Logger :: Category :: Body , " " + std :: to_string ( mEntity . id ) + " " + std :: to_string ( totalMass ) , __FILE__ , __LINE__ ) ; }
Compute and set the center of mass, the mass and the local-space inertia tensor of the body using its colliders
This method uses the shape, mass density and transform of the colliders of the body to set
the total mass, the center of mass and the local inertia tensor of the body.
Compute and set the center of mass the mass and the local - space inertia tensor of the body using its colliders This method uses the shape mass density and transform of the colliders of the body to set the total mass the center of mass and the local inertia tensor of the body .
// Compute the local center of mass // Set the center of mass // If it is a dynamic body // Update the linear velocity of the center of mass // Compute the mass and local-space inertia tensor // If it is a dynamic body // Compute the inverse local inertia tensor // Set the mass // If it is a dynamic body // Compute the inverse mass