StateRecorder.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. /// User callbacks that allow determining which parts of the simulation should be saved by a StateRecorder
  22. class JPH_EXPORT StateRecorderFilter
  23. {
  24. public:
  25. /// Destructor
  26. virtual ~StateRecorderFilter() = default;
  27. /// If the state of a specific body should be saved
  28. virtual bool ShouldSaveBody([[maybe_unused]] const Body &inBody) const { return true; }
  29. /// If the state of a specific constraint should be saved
  30. virtual bool ShouldSaveConstraint([[maybe_unused]] const Constraint &inConstraint) const { return true; }
  31. /// If the state of a specific contact should be saved
  32. virtual bool ShouldSaveContact([[maybe_unused]] const BodyID &inBody1, [[maybe_unused]] const BodyID &inBody2) const { return true; }
  33. };
  34. /// 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.
  35. /// 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
  36. /// like body friction or restitution, motion quality etc. are not saved and need to be saved by the user if desired.
  37. class JPH_EXPORT StateRecorder : public StreamIn, public StreamOut
  38. {
  39. public:
  40. /// Constructor
  41. StateRecorder() = default;
  42. StateRecorder(const StateRecorder &inRHS) : mIsValidating(inRHS.mIsValidating) { }
  43. /// Sets the stream in validation mode. In this case the physics system ensures that before it calls ReadBytes that it will
  44. /// ensure that those bytes contain the current state. This makes it possible to step and save the state, restore to the previous
  45. /// step and step again and when the recorded state is not the same it can restore the expected state and any byte that changes
  46. /// due to a ReadBytes function can be caught to find out which part of the simulation is not deterministic.
  47. /// Note that validation only works when saving the full state of the simulation (EStateRecorderState::All, StateRecorderFilter == nullptr).
  48. void SetValidating(bool inValidating) { mIsValidating = inValidating; }
  49. bool IsValidating() const { return mIsValidating; }
  50. private:
  51. bool mIsValidating = false;
  52. };
  53. JPH_NAMESPACE_END