Box2D: /build/box2d/src/box2d-adaedf1772ede7420bc9ee569c8cba7952db91a0/include/box2d/b2_rope.h Source File

| Box2D 2.4.0 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 };
47
49 struct B2_API b2RopeTuning
50 {
51 b2RopeTuning()
52 {
53 stretchingModel = b2_pbdStretchingModel;
54 bendingModel = b2_pbdAngleBendingModel;
55 damping = 0.0f;
56 stretchStiffness = 1.0f;
57 bendStiffness = 0.5f;
58 bendHertz = 1.0f;
59 bendDamping = 0.0f;
60 isometric = false;
61 fixedEffectiveMass = false;
62 warmStart = false;
63 }
64
65 b2StretchingModel stretchingModel;
66 b2BendingModel bendingModel;
67 float damping;
68 float stretchStiffness;
69 float stretchHertz;
70 float stretchDamping;
71 float bendStiffness;
72 float bendHertz;
73 float bendDamping;
74 bool isometric;
75 bool fixedEffectiveMass;
76 bool warmStart;
77 };
78
80 struct B2_API b2RopeDef
81 {
82 b2RopeDef()
83 {
84 position.SetZero();
85 vertices = nullptr;
86 count = 0;
87 masses = nullptr;
88 gravity.SetZero();
89 }
90
91 b2Vec2 position;
92 b2Vec2* vertices;
93 int32 count;
94 float* masses;
95 b2Vec2 gravity;
96 b2RopeTuning tuning;
97 };
98
100 class B2_API b2Rope
101 {
102 public:
103 b2Rope();
104 ~b2Rope();
105
107 void Create(const b2RopeDef& def);
108
110 void SetTuning(const b2RopeTuning& tuning);
111
113 void Step(float timeStep, int32 iterations, const b2Vec2& position);
114
116 void Reset(const b2Vec2& position);
117
119 void Draw(b2Draw* draw) const;
120
121 private:
122
123 void SolveStretch_PBD();
124 void SolveStretch_XPBD(float dt);
125 void SolveBend_PBD_Angle();
126 void SolveBend_XPBD_Angle(float dt);
127 void SolveBend_PBD_Distance();
128 void SolveBend_PBD_Height();
129 void ApplyBendForces(float dt);
130
131 b2Vec2 m_position;
132
133 int32 m_count;
134 int32 m_stretchCount;
135 int32 m_bendCount;
136
137 b2RopeStretch* m_stretchConstraints;
138 b2RopeBend* m_bendConstraints;
139
140 b2Vec2* m_bindPositions;
141 b2Vec2* m_ps;
142 b2Vec2* m_p0s;
143 b2Vec2* m_vs;
144
145 float* m_invMasses;
146 b2Vec2 m_gravity;
147
148 b2RopeTuning m_tuning;
149 };
150
151 #endif
b2Vec2
A 2D column vector.
Definition: b2_math.h:42
b2RopeDef
Definition: b2_rope.h:81
b2Draw
Definition: b2_draw.h:49
b2RopeTuning
Definition: b2_rope.h:50
b2Rope
Definition: b2_rope.h:101