In UBodySetup::CalculateMass, the following code is present:
// physical material - nothing can weigh less than hydrogen (0.09 kg/m^3)
float DensityKGPerCubicUU = 1.0f;
float RaiseMassToPower = 0.75f;
if (PhysMat)
{
DensityKGPerCubicUU = FMath::Max(0.00009f, PhysMat->Density * 0.001f);
RaiseMassToPower = PhysMat->RaiseMassToPower;
}
The idea being to set a sensible lower bound for object density. However, the calculation for `DensityKGPerCubicUU` is incorrect.
As stated in the code, the density of hydrogen is 0.08988g/L, correctly approximated to 0.09 kg / m³. DensityKGPerCubicUU is kg / cm³, and the conversion from m³ to cm³ should be reducing the value in the FMath::Max statement by a factor of 1e-6 (one million). However, it’s only being reduced by a thousand in the problematic statement.
While this doesn’t seem to affect the actual physics (which use UPrimitiveComponent->GetMass()), it still causes the following problems for us:
- The in-editor mass estimate uses CalculateMass and displays an inaccurate mass to our designers
- In many cases, we need to get the mass of objects which have physics simulation turned off (it gets turned on later on). This is not possible using the regular GetMass function, we need to use CalculateMass
- In some cases, objects get modified during gameplay and we need to access the initial mass. We do this using CalculateMass on the class default object, GetMass isn’t possible here.
The suggested fix would be as follows:
// physical material - nothing can weigh less than hydrogen (0.09 kg/m^3)
float DensityKGPerCubicUU = 1.0f;
float RaiseMassToPower = 0.75f;
if (PhysMat)
{
DensityKGPerCubicUU = FMath::Max(0.00000009f, PhysMat->Density * 0.001f);
RaiseMassToPower = PhysMat->RaiseMassToPower;
}