Skip to content

Commit faddc20

Browse files
Chandan-Bharadwajomichelstefaniapedrazzi
authored
Ensured correct mass is set during MoI matrix generation from bounding object #4906 (#6048)
* ensured correct mass is set during I matrix generation from bounding box * ensured correct mass is set during I matrix generation from bounding box * code refactoring #4906 * Update changelog-r2023.md logging fix for bug #4906 * Update src/webots/nodes/WbSolid.cpp Co-authored-by: Stefania Pedrazzi <[email protected]> * removing unused constant * refactoring of reference density in WbSolid.cpp * adjusted changes for clang format * Update docs/reference/changelog-r2023.md Co-authored-by: Stefania Pedrazzi <[email protected]> --------- Co-authored-by: Olivier Michel <[email protected]> Co-authored-by: Stefania Pedrazzi <[email protected]>
1 parent 433f2ba commit faddc20

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

docs/reference/changelog-r2023.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Released on ??
5454
- Fixed unwanted altitude change when reaching a target waypoint in `mavic2pro_patrol.c` ([#5981](https://github.com/cyberbotics/webots/pull/5981)).
5555
- Fixed the extern controller connection to a target Webots instance when a snap one is running ([#6002](https://github.com/cyberbotics/webots/pull/6002)).
5656
- Fixed the double downloading of meshes ([#6034](https://github.com/cyberbotics/webots/pull/6034)).
57+
- Fixed generation of moment of inertia matrix based on bounding box ([#6048](https://github.com/cyberbotics/webots/pull/6048)).
5758
- Fixed the loading of remote mesh bounding objects ([#6047](https://github.com/cyberbotics/webots/pull/6047)).
5859

5960
## Webots R2023a

src/webots/nodes/WbSolid.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ using namespace WbSolidUtilities;
7878
using namespace std;
7979

8080
const double WbSolid::MASS_ZERO_THRESHOLD = 1e-10;
81+
const double REFERENCE_DENSITY = 1000.0;
8182

8283
QList<const WbSolid *> WbSolid::cSolids;
8384

@@ -979,7 +980,7 @@ void WbSolid::adjustOdeMass(bool mergeMass) {
979980
} else {
980981
const double fieldDensity = p->density();
981982
if (fieldDensity >= 0.0)
982-
dMassAdjust(mOdeMass, (currentMass * fieldDensity) / 1000.0);
983+
dMassAdjust(mOdeMass, (currentMass * fieldDensity) / REFERENCE_DENSITY);
983984
}
984985

985986
memcpy(mMassAroundCoM, mOdeMass, sizeof(dMass));
@@ -1404,7 +1405,7 @@ void WbSolid::setInertiaMatrixFromBoundingObject() {
14041405
dMassSetZero(&dmass);
14051406

14061407
// Adds the masses of all the primitives lying in the bounding object
1407-
WbSolidUtilities::addMass(&dmass, boundingObject(), 1000.0);
1408+
WbSolidUtilities::addMass(&dmass, boundingObject(), REFERENCE_DENSITY);
14081409
memcpy(mReferenceMass, &dmass, sizeof(dMass));
14091410

14101411
// Computes the inertia matrix around the center of mass of the bounding object
@@ -1414,18 +1415,25 @@ void WbSolid::setInertiaMatrixFromBoundingObject() {
14141415
const WbField *const parameter = findField("physics", true)->parameter();
14151416
WbPhysics *const p = parameter ? static_cast<WbPhysics *>(static_cast<WbSFNode *>(parameter->value())->value()) : physics();
14161417

1417-
const double s = 1.0 / absoluteScale().x();
1418+
const double s0 = absoluteScale().x();
1419+
const double s = 1.0 / s0;
14181420
double s3 = s * s;
14191421
double s5 = s3;
14201422
s3 *= s;
14211423

1422-
if (p->mass() < 0.0) {
1423-
double boundingObjectMass = mReferenceMass->mass;
1424-
boundingObjectMass *= 0.001 * p->density();
1424+
// Sets the actual total mass to mReferenceMass
1425+
double boundingObjectMass = mReferenceMass->mass;
1426+
if (p->mass() > 0.0)
1427+
boundingObjectMass = s0 * s0 * s0 * p->mass();
1428+
else {
1429+
boundingObjectMass *= p->density() / REFERENCE_DENSITY;
14251430
p->setMass(boundingObjectMass * s3, true);
14261431
p->parsingInfo(tr("'mass' set as bounding object's mass based on 'density'."));
14271432
}
14281433

1434+
// Adjust the total according to mass and density fields
1435+
dMassAdjust(mReferenceMass, boundingObjectMass);
1436+
14291437
p->setDensity(-1.0, true);
14301438

14311439
const double *const I = mReferenceMass->I;
@@ -1806,7 +1814,7 @@ void WbSolid::createOdeMass(bool reset) {
18061814
const WbPhysics *const p = physics();
18071815
const bool customMass = p->mode() == WbPhysics::CUSTOM_INERTIA_MATRIX;
18081816
// needed for average density and average damping
1809-
WbSolidUtilities::addMass(mReferenceMass, boundingObject(), 1000.0, !customMass);
1817+
WbSolidUtilities::addMass(mReferenceMass, boundingObject(), REFERENCE_DENSITY, !customMass);
18101818

18111819
// Checks whether there is a valid inertia matrix, and uses it if so
18121820
if (customMass)
@@ -1835,8 +1843,8 @@ void WbSolid::createOdeMass(bool reset) {
18351843
if (fieldMass > 0.0) {
18361844
const double s2 = s * s;
18371845
actualMass = s * s2 * fieldMass;
1838-
} else if (fieldDensity != 1000.0)
1839-
actualMass *= 0.001 * fieldDensity;
1846+
} else if (fieldDensity != REFERENCE_DENSITY)
1847+
actualMass *= fieldDensity / REFERENCE_DENSITY;
18401848

18411849
// Adjust the total according to mass and density fields
18421850
dMassAdjust(mOdeMass, actualMass);

0 commit comments

Comments
 (0)