| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2021 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <Jolt/ObjectStream/SerializableObject.h> |
| 8 | |
| 9 | JPH_NAMESPACE_BEGIN |
| 10 | |
| 11 | class StreamIn; |
| 12 | class StreamOut; |
| 13 | |
| 14 | /// Describes the mass and inertia properties of a body. Used during body construction only. |
| 15 | class JPH_EXPORT MassProperties |
| 16 | { |
| 17 | JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, MassProperties) |
| 18 | |
| 19 | public: |
| 20 | /// Using eigendecomposition, decompose the inertia tensor into a diagonal matrix D and a right-handed rotation matrix R so that the inertia tensor is \f$R \: D \: R^{-1}\f$. |
| 21 | /// @see https://en.wikipedia.org/wiki/Moment_of_inertia section 'Principal axes' |
| 22 | /// @param outRotation The rotation matrix R |
| 23 | /// @param outDiagonal The diagonal of the diagonal matrix D |
| 24 | /// @return True if successful, false if failed |
| 25 | bool DecomposePrincipalMomentsOfInertia(Mat44 &outRotation, Vec3 &outDiagonal) const; |
| 26 | |
| 27 | /// Set the mass and inertia of a box with edge size inBoxSize and density inDensity |
| 28 | void SetMassAndInertiaOfSolidBox(Vec3Arg inBoxSize, float inDensity); |
| 29 | |
| 30 | /// Set the mass and scale the inertia tensor to match the mass |
| 31 | void ScaleToMass(float inMass); |
| 32 | |
| 33 | /// Calculates the size of the solid box that has an inertia tensor diagonal inInertiaDiagonal |
| 34 | static Vec3 sGetEquivalentSolidBoxSize(float inMass, Vec3Arg inInertiaDiagonal); |
| 35 | |
| 36 | /// Rotate the inertia by 3x3 matrix inRotation |
| 37 | void Rotate(Mat44Arg inRotation); |
| 38 | |
| 39 | /// Translate the inertia by a vector inTranslation |
| 40 | void Translate(Vec3Arg inTranslation); |
| 41 | |
| 42 | /// Scale the mass and inertia by inScale, note that elements can be < 0 to flip the shape |
| 43 | void Scale(Vec3Arg inScale); |
| 44 | |
| 45 | /// Saves the state of this object in binary form to inStream. |
| 46 | void SaveBinaryState(StreamOut &inStream) const; |
| 47 | |
| 48 | /// Restore the state of this object from inStream. |
| 49 | void RestoreBinaryState(StreamIn &inStream); |
| 50 | |
| 51 | /// Mass of the shape (kg) |
| 52 | float mMass = 0.0f; |
| 53 | |
| 54 | /// Inertia tensor of the shape (kg m^2) |
| 55 | Mat44 mInertia = Mat44::sZero(); |
| 56 | }; |
| 57 | |
| 58 | JPH_NAMESPACE_END |