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