Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
SpringPart.h
Go to the documentation of this file.
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
8#ifndef JPH_PLATFORM_DOXYGEN // Somehow Doxygen gets confused and thinks the parameters to CalculateSpringProperties belong to this macro
9JPH_MSVC_SUPPRESS_WARNING(4723) // potential divide by 0 - caused by line: outEffectiveMass = 1.0f / inInvEffectiveMass, note that JPH_NAMESPACE_BEGIN already pushes the warning state
10#endif // !JPH_PLATFORM_DOXYGEN
11
14{
15public:
25 inline void CalculateSpringProperties(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, float inFrequency, float inDamping, float &outEffectiveMass)
26 {
27 outEffectiveMass = 1.0f / inInvEffectiveMass;
28
29 // Soft constraints as per: Soft Contraints: Reinventing The Spring - Erin Catto - GDC 2011
30 if (inFrequency > 0.0f)
31 {
32 // Calculate angular frequency
33 float omega = 2.0f * JPH_PI * inFrequency;
34
35 // Calculate spring constant k and drag constant c (page 45)
36 float k = outEffectiveMass * Square(omega);
37 float c = 2.0f * outEffectiveMass * inDamping * omega;
38
39 // Note that the calculation of beta and gamma below are based on the solution of an implicit Euler integration scheme
40 // This scheme is unconditionally stable but has built in damping, so even when you set the damping ratio to 0 there will still
41 // be damping. See page 16 and 32.
42
43 // Calculate softness (gamma in the slides)
44 // See page 34 and note that the gamma needs to be divided by delta time since we're working with impulses rather than forces:
45 // softness = 1 / (dt * (c + dt * k))
46 mSoftness = 1.0f / (inDeltaTime * (c + inDeltaTime * k));
47
48 // Calculate bias factor (baumgarte stabilization):
49 // beta = dt * k / (c + dt * k) = dt * k^2 * softness
50 // b = beta / dt * C = dt * k * softness * C
51 mBias = inBias + inDeltaTime * k * mSoftness * inC;
52
53 // Update the effective mass, see post by Erin Catto: http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=4&t=1354
54 //
55 // Newton's Law:
56 // M * (v2 - v1) = J^T * lambda
57 //
58 // Velocity constraint with softness and Baumgarte:
59 // J * v2 + softness * lambda + b = 0
60 //
61 // where b = beta * C / dt
62 //
63 // We know everything except v2 and lambda.
64 //
65 // First solve Newton's law for v2 in terms of lambda:
66 //
67 // v2 = v1 + M^-1 * J^T * lambda
68 //
69 // Substitute this expression into the velocity constraint:
70 //
71 // J * (v1 + M^-1 * J^T * lambda) + softness * lambda + b = 0
72 //
73 // Now collect coefficients of lambda:
74 //
75 // (J * M^-1 * J^T + softness) * lambda = - J * v1 - b
76 //
77 // Now we define:
78 //
79 // K = J * M^-1 * J^T + softness
80 //
81 // So our new effective mass is K^-1
82 outEffectiveMass = 1.0f / (inInvEffectiveMass + mSoftness);
83 }
84 else
85 {
86 mSoftness = 0.0f;
87 mBias = inBias;
88 }
89 }
90
92 inline bool IsActive() const
93 {
94 return mSoftness != 0.0f;
95 }
96
98 inline float GetBias(float inTotalLambda) const
99 {
100 // Remainder of post by Erin Catto: http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=4&t=1354
101 //
102 // Each iteration we are not computing the whole impulse, we are computing an increment to the impulse and we are updating the velocity.
103 // Also, as we solve each constraint we get a perfect v2, but then some other constraint will come along and mess it up.
104 // So we want to patch up the constraint while acknowledging the accumulated impulse and the damaged velocity.
105 // To help with that we use P for the accumulated impulse and lambda as the update. Mathematically we have:
106 //
107 // M * (v2new - v2damaged) = J^T * lambda
108 // J * v2new + softness * (total_lambda + lambda) + b = 0
109 //
110 // If we solve this we get:
111 //
112 // v2new = v2damaged + M^-1 * J^T * lambda
113 // J * (v2damaged + M^-1 * J^T * lambda) + softness * total_lambda + softness * lambda + b = 0
114 //
115 // (J * M^-1 * J^T + softness) * lambda = -(J * v2damaged + softness * total_lambda + b)
116 //
117 // So our lagrange multiplier becomes:
118 //
119 // lambda = -K^-1 (J v + softness * total_lambda + b)
120 //
121 // So we return the bias: softness * total_lambda + b
122 return mSoftness * inTotalLambda + mBias;
123 }
124
125private:
126 float mBias = 0.0f;
127 float mSoftness = 0.0f;
128};
129
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_MSVC_SUPPRESS_WARNING(w)
Definition: Core.h:154
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
constexpr T Square(T inV)
Square a value.
Definition: Math.h:52
Class used in other constraint parts to calculate the required bias factor in the lagrange multiplier...
Definition: SpringPart.h:14
void CalculateSpringProperties(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, float inFrequency, float inDamping, float &outEffectiveMass)
Definition: SpringPart.h:25
float GetBias(float inTotalLambda) const
Get total bias b, including supplied bias and bias for spring: lambda = J v + b.
Definition: SpringPart.h:98
bool IsActive() const
Returns if this spring is active.
Definition: SpringPart.h:92