StateRecorder.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/Core/StreamIn.h>
  6. #include <Jolt/Core/StreamOut.h>
  7. JPH_NAMESPACE_BEGIN
  8. class Body;
  9. class Constraint;
  10. class BodyID;
  11. /// A bit field that determines which aspects of the simulation to save
  12. enum class EStateRecorderState : uint8
  13. {
  14. None = 0, ///< Save nothing
  15. Global = 1, ///< Save global physics system state (delta time, gravity, etc.)
  16. Bodies = 2, ///< Save the state of bodies
  17. Contacts = 4, ///< Save the state of contacts
  18. Constraints = 8, ///< Save the state of constraints
  19. All = Global | Bodies | Contacts | Constraints ///< Save all state
  20. };
  21. /// Bitwise OR operator for EStateRecorderState
  22. constexpr EStateRecorderState operator | (EStateRecorderState inLHS, EStateRecorderState inRHS)
  23. {
  24. return EStateRecorderState(uint8(inLHS) | uint8(inRHS));
  25. }
  26. /// Bitwise AND operator for EStateRecorderState
  27. constexpr EStateRecorderState operator & (EStateRecorderState inLHS, EStateRecorderState inRHS)
  28. {
  29. return EStateRecorderState(uint8(inLHS) & uint8(inRHS));
  30. }
  31. /// Bitwise XOR operator for EStateRecorderState
  32. constexpr EStateRecorderState operator ^ (EStateRecorderState inLHS, EStateRecorderState inRHS)
  33. {
  34. return EStateRecorderState(uint8(inLHS) ^ uint8(inRHS));
  35. }
  36. /// Bitwise NOT operator for EStateRecorderState
  37. constexpr EStateRecorderState operator ~ (EStateRecorderState inAllowedDOFs)
  38. {
  39. return EStateRecorderState(~uint8(inAllowedDOFs));
  40. }
  41. /// Bitwise OR assignment operator for EStateRecorderState
  42. constexpr EStateRecorderState & operator |= (EStateRecorderState &ioLHS, EStateRecorderState inRHS)
  43. {
  44. ioLHS = ioLHS | inRHS;
  45. return ioLHS;
  46. }
  47. /// Bitwise AND assignment operator for EStateRecorderState
  48. constexpr EStateRecorderState & operator &= (EStateRecorderState &ioLHS, EStateRecorderState inRHS)
  49. {
  50. ioLHS = ioLHS & inRHS;
  51. return ioLHS;
  52. }
  53. /// Bitwise XOR assignment operator for EStateRecorderState
  54. constexpr EStateRecorderState & operator ^= (EStateRecorderState &ioLHS, EStateRecorderState inRHS)
  55. {
  56. ioLHS = ioLHS ^ inRHS;
  57. return ioLHS;
  58. }
  59. /// User callbacks that allow determining which parts of the simulation should be saved by a StateRecorder
  60. class JPH_EXPORT StateRecorderFilter
  61. {
  62. public:
  63. /// Destructor
  64. virtual ~StateRecorderFilter() = default;
  65. ///@name Functions called during SaveState
  66. ///@{
  67. /// If the state of a specific body should be saved
  68. virtual bool ShouldSaveBody([[maybe_unused]] const Body &inBody) const { return true; }
  69. /// If the state of a specific constraint should be saved
  70. virtual bool ShouldSaveConstraint([[maybe_unused]] const Constraint &inConstraint) const { return true; }
  71. /// If the state of a specific contact should be saved
  72. virtual bool ShouldSaveContact([[maybe_unused]] const BodyID &inBody1, [[maybe_unused]] const BodyID &inBody2) const { return true; }
  73. ///@}
  74. ///@name Functions called during RestoreState
  75. ///@{
  76. /// If the state of a specific contact should be restored
  77. virtual bool ShouldRestoreContact([[maybe_unused]] const BodyID &inBody1, [[maybe_unused]] const BodyID &inBody2) const { return true; }
  78. ///@}
  79. };
  80. /// Class that records the state of a physics system. Can be used to check if the simulation is deterministic by putting the recorder in validation mode.
  81. /// Can be used to restore the state to an earlier point in time. Note that only the state that is modified by the simulation is saved, configuration settings
  82. /// like body friction or restitution, motion quality etc. are not saved and need to be saved by the user if desired.
  83. class JPH_EXPORT StateRecorder : public StreamIn, public StreamOut
  84. {
  85. public:
  86. /// Constructor
  87. StateRecorder() = default;
  88. StateRecorder(const StateRecorder &inRHS) : mIsValidating(inRHS.mIsValidating) { }
  89. /// Sets the stream in validation mode. In this case the physics system ensures that before it calls ReadBytes that it will
  90. /// ensure that those bytes contain the current state. This makes it possible to step and save the state, restore to the previous
  91. /// step and step again and when the recorded state is not the same it can restore the expected state and any byte that changes
  92. /// due to a ReadBytes function can be caught to find out which part of the simulation is not deterministic.
  93. /// Note that validation only works when saving the full state of the simulation (EStateRecorderState::All, StateRecorderFilter == nullptr).
  94. void SetValidating(bool inValidating) { mIsValidating = inValidating; }
  95. bool IsValidating() const { return mIsValidating; }
  96. /// This allows splitting the state in multiple parts. While restoring, only the last part should have this flag set to true.
  97. /// Note that you should ensure that the different parts contain information for disjoint sets of bodies, constraints and contacts.
  98. /// E.g. if you restore the same contact twice, you get undefined behavior. In order to create disjoint sets you can use the StateRecorderFilter.
  99. /// Note that validation is not compatible with restoring a simulation state in multiple parts.
  100. void SetIsLastPart(bool inIsLastPart) { mIsLastPart = inIsLastPart; }
  101. bool IsLastPart() const { return mIsLastPart; }
  102. private:
  103. bool mIsValidating = false;
  104. bool mIsLastPart = true;
  105. };
  106. JPH_NAMESPACE_END