MassProperties.h 2.0 KB

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