Skip to content

Commit

Permalink
custom shapes: omit unnecessary null tests and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jan 27, 2024
1 parent 2de4c45 commit 42ac8b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,21 +230,18 @@ public float scaledVolume() {
public void setScale(Vector3f scale) {
super.setScale(scale);

if (unscaledHe != null) {
unscaledHe.mult(scale, scaledHe);

float a = scaledHe.x;
float b = scaledHe.y;
float c = scaledHe.z;
/*
* The moments of inertia of a uniformly dense box
* with mass=1, around its center of mass:
*/
float ix = (b * b + c * c) / 3f;
float iy = (a * a + c * c) / 3f;
float iz = (a * a + b * b) / 3f;
this.setScaledInertia(ix, iy, iz);
}
unscaledHe.mult(scale, scaledHe);
float a = scaledHe.x;
float b = scaledHe.y;
float c = scaledHe.z;
/*
* The moments of inertia of a uniformly dense box
* with mass=1, around its center of mass:
*/
float ix = (b * b + c * c) / 3f;
float iy = (a * a + c * c) / 3f;
float iz = (a * a + b * b) / 3f;
setScaledInertia(ix, iy, iz);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,24 +257,21 @@ public float scaledVolume() {
public void setScale(Vector3f scale) {
super.setScale(scale);

if (unscaledHe != null) {
unscaledHe.mult(scale, scaledHe);
scaledHe.mult(scaledHe, squaredScaledHe);

float a2 = scaledHe.x * scaledHe.x;
float b2 = scaledHe.y * scaledHe.y;
float c2 = scaledHe.z * scaledHe.z;
/*
* see https://adamheins.com/blog/ellipsoidal-shell-inertia
*
* the moments of inertia of a uniformly dense ellipsoid
* with mass=1, around its center of mass:
*/
float ix = inertiaFactor * (b2 + c2);
float iy = inertiaFactor * (a2 + c2);
float iz = inertiaFactor * (a2 + b2);
this.setScaledInertia(ix, iy, iz);
}
unscaledHe.mult(scale, scaledHe);
scaledHe.mult(scaledHe, squaredScaledHe);
float a2 = squaredScaledHe.x;
float b2 = squaredScaledHe.y;
float c2 = squaredScaledHe.z;
/*
* see https://adamheins.com/blog/ellipsoidal-shell-inertia
*
* the moments of inertia of a uniformly dense ellipsoid
* with mass=1, around its center of mass:
*/
float ix = inertiaFactor * (b2 + c2);
float iy = inertiaFactor * (a2 + c2);
float iz = inertiaFactor * (a2 + b2);
setScaledInertia(ix, iy, iz);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,24 +257,21 @@ public float scaledVolume() {
public void setScale(Vector3f scale) {
super.setScale(scale);

if (unscaledHe != null) {
unscaledHe.mult(scale, scaledHe);
scaledHe.mult(scaledHe, squaredScaledHe);

float a2 = scaledHe.x * scaledHe.x;
float b2 = scaledHe.y * scaledHe.y;
float c2 = scaledHe.z * scaledHe.z;
/*
* see https://adamheins.com/blog/ellipsoidal-shell-inertia
*
* the moments of inertia of a uniformly dense ellipsoid
* with mass=1, around its center of mass:
*/
float ix = inertiaFactor * (b2 + c2);
float iy = inertiaFactor * (a2 + c2);
float iz = inertiaFactor * (a2 + b2);
this.setScaledInertia(ix, iy, iz);
}
unscaledHe.mult(scale, scaledHe);
scaledHe.mult(scaledHe, squaredScaledHe);
float a2 = squaredScaledHe.x;
float b2 = squaredScaledHe.y;
float c2 = squaredScaledHe.z;
/*
* see https://adamheins.com/blog/ellipsoidal-shell-inertia
*
* the moments of inertia of a uniformly dense ellipsoid
* with mass=1, around its center of mass:
*/
float ix = inertiaFactor * (b2 + c2);
float iy = inertiaFactor * (a2 + c2);
float iz = inertiaFactor * (a2 + b2);
setScaledInertia(ix, iy, iz);
}

/**
Expand Down

0 comments on commit 42ac8b9

Please sign in to comment.