Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Body/MassProperties.cpp
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#include <Jolt/Jolt.h>56#include <Jolt/Physics/Body/MassProperties.h>7#include <Jolt/Math/Matrix.h>8#include <Jolt/Math/Vector.h>9#include <Jolt/Math/EigenValueSymmetric.h>10#include <Jolt/ObjectStream/TypeDeclarations.h>11#include <Jolt/Core/StreamIn.h>12#include <Jolt/Core/StreamOut.h>13#include <Jolt/Core/InsertionSort.h>1415JPH_NAMESPACE_BEGIN1617JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(MassProperties)18{19JPH_ADD_ATTRIBUTE(MassProperties, mMass)20JPH_ADD_ATTRIBUTE(MassProperties, mInertia)21}2223bool MassProperties::DecomposePrincipalMomentsOfInertia(Mat44 &outRotation, Vec3 &outDiagonal) const24{25// Using eigendecomposition to get the principal components of the inertia tensor26// See: https://en.wikipedia.org/wiki/Eigendecomposition_of_a_matrix27Matrix<3, 3> inertia;28inertia.CopyPart(mInertia, 0, 0, 3, 3, 0, 0);29Matrix<3, 3> eigen_vec = Matrix<3, 3>::sIdentity();30Vector<3> eigen_val;31if (!EigenValueSymmetric(inertia, eigen_vec, eigen_val))32return false;3334// Sort so that the biggest value goes first35int indices[] = { 0, 1, 2 };36InsertionSort(indices, indices + 3, [&eigen_val](int inLeft, int inRight) { return eigen_val[inLeft] > eigen_val[inRight]; });3738// Convert to a regular Mat44 and Vec339outRotation = Mat44::sIdentity();40for (int i = 0; i < 3; ++i)41{42outRotation.SetColumn3(i, Vec3(reinterpret_cast<Float3 &>(eigen_vec.GetColumn(indices[i]))));43outDiagonal.SetComponent(i, eigen_val[indices[i]]);44}4546// Make sure that the rotation matrix is a right handed matrix47if (outRotation.GetAxisX().Cross(outRotation.GetAxisY()).Dot(outRotation.GetAxisZ()) < 0.0f)48outRotation.SetAxisZ(-outRotation.GetAxisZ());4950#ifdef JPH_ENABLE_ASSERTS51// Validate that the solution is correct, for each axis we want to make sure that the difference in inertia is52// smaller than some fraction of the inertia itself in that axis53Mat44 new_inertia = outRotation * Mat44::sScale(outDiagonal) * outRotation.Inversed();54for (int i = 0; i < 3; ++i)55JPH_ASSERT(new_inertia.GetColumn3(i).IsClose(mInertia.GetColumn3(i), mInertia.GetColumn3(i).LengthSq() * 1.0e-10f));56#endif5758return true;59}6061void MassProperties::SetMassAndInertiaOfSolidBox(Vec3Arg inBoxSize, float inDensity)62{63// Calculate mass64mMass = inBoxSize.GetX() * inBoxSize.GetY() * inBoxSize.GetZ() * inDensity;6566// Calculate inertia67Vec3 size_sq = inBoxSize * inBoxSize;68Vec3 scale = (size_sq.Swizzle<SWIZZLE_Y, SWIZZLE_X, SWIZZLE_X>() + size_sq.Swizzle<SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Y>()) * (mMass / 12.0f);69mInertia = Mat44::sScale(scale);70}7172void MassProperties::ScaleToMass(float inMass)73{74if (mMass > 0.0f)75{76// Calculate how much we have to scale the inertia tensor77float mass_scale = inMass / mMass;7879// Update mass80mMass = inMass;8182// Update inertia tensor83for (int i = 0; i < 3; ++i)84mInertia.SetColumn4(i, mInertia.GetColumn4(i) * mass_scale);85}86else87{88// Just set the mass89mMass = inMass;90}91}9293Vec3 MassProperties::sGetEquivalentSolidBoxSize(float inMass, Vec3Arg inInertiaDiagonal)94{95// Moment of inertia of a solid box has diagonal:96// mass / 12 * [size_y^2 + size_z^2, size_x^2 + size_z^2, size_x^2 + size_y^2]97// Solving for size_x, size_y and size_y (diagonal and mass are known):98Vec3 diagonal = inInertiaDiagonal * (12.0f / inMass);99return Vec3(sqrt(0.5f * (-diagonal[0] + diagonal[1] + diagonal[2])), sqrt(0.5f * (diagonal[0] - diagonal[1] + diagonal[2])), sqrt(0.5f * (diagonal[0] + diagonal[1] - diagonal[2])));100}101102void MassProperties::Scale(Vec3Arg inScale)103{104// See: https://en.wikipedia.org/wiki/Moment_of_inertia#Inertia_tensor105// The diagonal of the inertia tensor can be calculated like this:106// Ixx = sum_{k = 1 to n}(m_k * (y_k^2 + z_k^2))107// Iyy = sum_{k = 1 to n}(m_k * (x_k^2 + z_k^2))108// Izz = sum_{k = 1 to n}(m_k * (x_k^2 + y_k^2))109//110// We want to isolate the terms x_k, y_k and z_k:111// d = [0.5, 0.5, 0.5].[Ixx, Iyy, Izz]112// [sum_{k = 1 to n}(m_k * x_k^2), sum_{k = 1 to n}(m_k * y_k^2), sum_{k = 1 to n}(m_k * z_k^2)] = [d, d, d] - [Ixx, Iyy, Izz]113Vec3 diagonal = mInertia.GetDiagonal3();114Vec3 xyz_sq = Vec3::sReplicate(Vec3::sReplicate(0.5f).Dot(diagonal)) - diagonal;115116// When scaling a shape these terms change like this:117// sum_{k = 1 to n}(m_k * (scale_x * x_k)^2) = scale_x^2 * sum_{k = 1 to n}(m_k * x_k^2)118// Same for y_k and z_k119// Using these terms we can calculate the new diagonal of the inertia tensor:120Vec3 xyz_scaled_sq = inScale * inScale * xyz_sq;121float i_xx = xyz_scaled_sq.GetY() + xyz_scaled_sq.GetZ();122float i_yy = xyz_scaled_sq.GetX() + xyz_scaled_sq.GetZ();123float i_zz = xyz_scaled_sq.GetX() + xyz_scaled_sq.GetY();124125// The off diagonal elements are calculated like:126// Ixy = -sum_{k = 1 to n}(x_k y_k)127// Ixz = -sum_{k = 1 to n}(x_k z_k)128// Iyz = -sum_{k = 1 to n}(y_k z_k)129// Scaling these is simple:130float i_xy = inScale.GetX() * inScale.GetY() * mInertia(0, 1);131float i_xz = inScale.GetX() * inScale.GetZ() * mInertia(0, 2);132float i_yz = inScale.GetY() * inScale.GetZ() * mInertia(1, 2);133134// Update inertia tensor135mInertia(0, 0) = i_xx;136mInertia(0, 1) = i_xy;137mInertia(1, 0) = i_xy;138mInertia(1, 1) = i_yy;139mInertia(0, 2) = i_xz;140mInertia(2, 0) = i_xz;141mInertia(1, 2) = i_yz;142mInertia(2, 1) = i_yz;143mInertia(2, 2) = i_zz;144145// Mass scales linear with volume (note that the scaling can be negative and we don't want the mass to become negative)146float mass_scale = abs(inScale.GetX() * inScale.GetY() * inScale.GetZ());147mMass *= mass_scale;148149// Inertia scales linear with mass. This updates the m_k terms above.150mInertia *= mass_scale;151152// Ensure that the bottom right element is a 1 again153mInertia(3, 3) = 1.0f;154}155156void MassProperties::Rotate(Mat44Arg inRotation)157{158mInertia = inRotation.Multiply3x3(mInertia).Multiply3x3RightTransposed(inRotation);159}160161void MassProperties::Translate(Vec3Arg inTranslation)162{163// Transform the inertia using the parallel axis theorem: I' = I + m * (translation^2 E - translation translation^T)164// Where I is the original body's inertia and E the identity matrix165// See: https://en.wikipedia.org/wiki/Parallel_axis_theorem166mInertia += mMass * (Mat44::sScale(inTranslation.Dot(inTranslation)) - Mat44::sOuterProduct(inTranslation, inTranslation));167168// Ensure that inertia is a 3x3 matrix, adding inertias causes the bottom right element to change169mInertia.SetColumn4(3, Vec4(0, 0, 0, 1));170}171172void MassProperties::SaveBinaryState(StreamOut &inStream) const173{174inStream.Write(mMass);175inStream.Write(mInertia);176}177178void MassProperties::RestoreBinaryState(StreamIn &inStream)179{180inStream.Read(mMass);181inStream.Read(mInertia);182}183184JPH_NAMESPACE_END185186187