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