Inaccurate operation with density in UBodySetup's CalculateMass

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;
}

Steps to Reproduce

  1. Create a custom physical material with a density of 0.05g/cm³ (50kg/m³), and a RaiseMassToPower value of 1.0
  2. Place a 1m³ object in the scene
  3. Assign the created custom material to the cube
  4. Notice the mass when selecting the object in the editor: It will display 90kg instead of 50kg.
  5. Additionally, fetching the mass through UBodySetup’s CalculateMass will return 90kg instead of 50kg. UPrimitiveComponent’s GetMass will return the correct value of 50kg.

Hello [mention removed]​

Thank you for reaching out and bringing this issue to our attention.

Also, thank you for the well-thought-out repro project, the analysis, and the suggested fix.

I was able to reproduce the described behavior using your repro project.

I’ll investigate a bit more before opening an issue report, and will come back once I have something to share.

All the best,

[mention removed]​

Hello [mention removed]​

Thank you again for reporting this issue - it was a good catch!

I’ve made a JIRA report at: Unreal Engine Issues and Bug Tracker (UE\-341342)

Please note that it can take some time to be made publicly accessible.

We don’t provide updates on EPS, but if you would like to track the resolution, check the link for the status.

I believe we don’t have further actionable issues, so I’ll close the case.

Feel free to reply if you have anything else to add.

All the best,

[mention removed]​