Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/CalculateSolverSteps.h
9912 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2023 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Physics/PhysicsSettings.h>
8
9
JPH_NAMESPACE_BEGIN
10
11
/// Class used to calculate the total number of velocity and position steps
12
class CalculateSolverSteps
13
{
14
public:
15
/// Constructor
16
JPH_INLINE explicit CalculateSolverSteps(const PhysicsSettings &inSettings) : mSettings(inSettings) { }
17
18
/// Combine the number of velocity and position steps for this body/constraint with the current values
19
template <class Type>
20
JPH_INLINE void operator () (const Type *inObject)
21
{
22
uint num_velocity_steps = inObject->GetNumVelocityStepsOverride();
23
mNumVelocitySteps = max(mNumVelocitySteps, num_velocity_steps);
24
mApplyDefaultVelocity |= num_velocity_steps == 0;
25
26
uint num_position_steps = inObject->GetNumPositionStepsOverride();
27
mNumPositionSteps = max(mNumPositionSteps, num_position_steps);
28
mApplyDefaultPosition |= num_position_steps == 0;
29
}
30
31
/// Must be called after all bodies/constraints have been processed
32
JPH_INLINE void Finalize()
33
{
34
// If we have a default velocity/position step count, take the max of the default and the overrides
35
if (mApplyDefaultVelocity)
36
mNumVelocitySteps = max(mNumVelocitySteps, mSettings.mNumVelocitySteps);
37
if (mApplyDefaultPosition)
38
mNumPositionSteps = max(mNumPositionSteps, mSettings.mNumPositionSteps);
39
}
40
41
/// Get the results of the calculation
42
JPH_INLINE uint GetNumPositionSteps() const { return mNumPositionSteps; }
43
JPH_INLINE uint GetNumVelocitySteps() const { return mNumVelocitySteps; }
44
45
private:
46
const PhysicsSettings & mSettings;
47
48
uint mNumVelocitySteps = 0;
49
uint mNumPositionSteps = 0;
50
51
bool mApplyDefaultVelocity = false;
52
bool mApplyDefaultPosition = false;
53
};
54
55
/// Dummy class to replace the steps calculator when we don't need the result
56
class DummyCalculateSolverSteps
57
{
58
public:
59
template <class Type>
60
JPH_INLINE void operator () (const Type *) const
61
{
62
/* Nothing to do */
63
}
64
};
65
66
JPH_NAMESPACE_END
67
68