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