1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
// MIT License
// Copyright (c) 2019 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef B2_ROPE_H
#define B2_ROPE_H
#include "b2_api.h"
#include "b2_math.h"
class b2Draw;
struct b2RopeStretch;
struct b2RopeBend;
enum b2StretchingModel
{
b2_pbdStretchingModel,
b2_xpbdStretchingModel
};
enum b2BendingModel
{
b2_springAngleBendingModel = 0,
b2_pbdAngleBendingModel,
b2_xpbdAngleBendingModel,
b2_pbdDistanceBendingModel,
b2_pbdHeightBendingModel,
b2_pbdTriangleBendingModel
};
///
struct B2_API b2RopeTuning
{
b2RopeTuning()
{
stretchingModel = b2_pbdStretchingModel;
bendingModel = b2_pbdAngleBendingModel;
damping = 0.0f;
stretchStiffness = 1.0f;
bendStiffness = 0.5f;
bendHertz = 1.0f;
bendDamping = 0.0f;
isometric = false;
fixedEffectiveMass = false;
warmStart = false;
}
b2StretchingModel stretchingModel;
b2BendingModel bendingModel;
float damping;
float stretchStiffness;
float stretchHertz;
float stretchDamping;
float bendStiffness;
float bendHertz;
float bendDamping;
bool isometric;
bool fixedEffectiveMass;
bool warmStart;
};
///
struct B2_API b2RopeDef
{
b2RopeDef()
{
position.SetZero();
vertices = nullptr;
count = 0;
masses = nullptr;
gravity.SetZero();
}
b2Vec2 position;
b2Vec2* vertices;
int32 count;
float* masses;
b2Vec2 gravity;
b2RopeTuning tuning;
};
///
class B2_API b2Rope
{
public:
b2Rope();
~b2Rope();
///
void Create(const b2RopeDef& def);
///
void SetTuning(const b2RopeTuning& tuning);
///
void Step(float timeStep, int32 iterations, const b2Vec2& position);
///
void Reset(const b2Vec2& position);
///
void Draw(b2Draw* draw) const;
private:
void SolveStretch_PBD();
void SolveStretch_XPBD(float dt);
void SolveBend_PBD_Angle();
void SolveBend_XPBD_Angle(float dt);
void SolveBend_PBD_Distance();
void SolveBend_PBD_Height();
void SolveBend_PBD_Triangle();
void ApplyBendForces(float dt);
b2Vec2 m_position;
int32 m_count;
int32 m_stretchCount;
int32 m_bendCount;
b2RopeStretch* m_stretchConstraints;
b2RopeBend* m_bendConstraints;
b2Vec2* m_bindPositions;
b2Vec2* m_ps;
b2Vec2* m_p0s;
b2Vec2* m_vs;
float* m_invMasses;
b2Vec2 m_gravity;
b2RopeTuning m_tuning;
};
#endif
|