MassProperties.h 2.0 KB

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