rope.cpp
Go to the documentation of this file.
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 #include "settings.h"
24 #include "test.h"
25 #include "box2d/b2_rope.h"
26 #include "imgui/imgui.h"
27 
29 class Rope : public Test
30 {
31 public:
32  Rope()
33  {
34  const int32 N = 20;
35  const float L = 0.5f;
36  b2Vec2 vertices[N];
37  float masses[N];
38 
39  for (int32 i = 0; i < N; ++i)
40  {
41  vertices[i].Set(0.0f, L * (N - i));
42  masses[i] = 1.0f;
43  }
44  masses[0] = 0.0f;
45  masses[1] = 0.0f;
46 
47  m_tuning1.bendHertz = 30.0f;
48  m_tuning1.bendDamping = 4.0f;
49  m_tuning1.bendStiffness = 1.0f;
51  m_tuning1.isometric = true;
52 
53  m_tuning1.stretchHertz = 30.0f;
57 
58  m_tuning2.bendHertz = 30.0f;
59  m_tuning2.bendDamping = 0.7f;
60  m_tuning2.bendStiffness = 1.0f;
62  m_tuning2.isometric = true;
63 
64  m_tuning2.stretchHertz = 30.0f;
68 
69  m_position1.Set(-5.0f, 15.0f);
70  m_position2.Set(5.0f, 15.0f);
71 
72  b2RopeDef def;
73  def.vertices = vertices;
74  def.count = N;
75  def.gravity.Set(0.0f, -10.0f);
76  def.masses = masses;
77 
78  def.position = m_position1;
79  def.tuning = m_tuning1;
80  m_rope1.Create(def);
81 
82  def.position = m_position2;
83  def.tuning = m_tuning2;
84  m_rope2.Create(def);
85 
86  m_iterations1 = 8;
87  m_iterations2 = 8;
88 
89  m_speed = 10.0f;
90  }
91 
92  void UpdateUI() override
93  {
94  ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
95  ImGui::SetNextWindowSize(ImVec2(200.0f, 700.0f));
97 
99 
101 
102  const ImGuiComboFlags comboFlags = 0;
103  const char* bendModels[] = { "Spring", "PBD Ang", "XPBD Ang", "PBD Dist", "PBD Height", "PBD Triangle" };
104  const char* stretchModels[] = { "PBD", "XPBD" };
105 
106  ImGui::Text("Rope 1");
107  static int bendModel1 = m_tuning1.bendingModel;
108  if (ImGui::BeginCombo("Bend Model##1", bendModels[bendModel1], comboFlags))
109  {
110  for (int i = 0; i < IM_ARRAYSIZE(bendModels); ++i)
111  {
112  bool isSelected = (bendModel1 == i);
113  if (ImGui::Selectable(bendModels[i], isSelected))
114  {
115  bendModel1 = i;
117  }
118 
119  if (isSelected)
120  {
122  }
123  }
124  ImGui::EndCombo();
125  }
126 
127  ImGui::SliderFloat("Damping##B1", &m_tuning1.bendDamping, 0.0f, 4.0f, "%.1f");
128  ImGui::SliderFloat("Hertz##B1", &m_tuning1.bendHertz, 0.0f, 60.0f, "%.0f");
129  ImGui::SliderFloat("Stiffness##B1", &m_tuning1.bendStiffness, 0.0f, 1.0f, "%.1f");
130 
131  ImGui::Checkbox("Isometric##1", &m_tuning1.isometric);
132  ImGui::Checkbox("Fixed Mass##1", &m_tuning1.fixedEffectiveMass);
133  ImGui::Checkbox("Warm Start##1", &m_tuning1.warmStart);
134 
135  static int stretchModel1 = m_tuning1.stretchingModel;
136  if (ImGui::BeginCombo("Stretch Model##1", stretchModels[stretchModel1], comboFlags))
137  {
138  for (int i = 0; i < IM_ARRAYSIZE(stretchModels); ++i)
139  {
140  bool isSelected = (stretchModel1 == i);
141  if (ImGui::Selectable(stretchModels[i], isSelected))
142  {
143  stretchModel1 = i;
145  }
146 
147  if (isSelected)
148  {
150  }
151  }
152  ImGui::EndCombo();
153  }
154 
155  ImGui::SliderFloat("Damping##S1", &m_tuning1.stretchDamping, 0.0f, 4.0f, "%.1f");
156  ImGui::SliderFloat("Hertz##S1", &m_tuning1.stretchHertz, 0.0f, 60.0f, "%.0f");
157  ImGui::SliderFloat("Stiffness##S1", &m_tuning1.stretchStiffness, 0.0f, 1.0f, "%.1f");
158 
159  ImGui::SliderInt("Iterations##1", &m_iterations1, 1, 100, "%d");
160 
162 
163  ImGui::Text("Rope 2");
164  static int bendModel2 = m_tuning2.bendingModel;
165  if (ImGui::BeginCombo("Bend Model##2", bendModels[bendModel2], comboFlags))
166  {
167  for (int i = 0; i < IM_ARRAYSIZE(bendModels); ++i)
168  {
169  bool isSelected = (bendModel2 == i);
170  if (ImGui::Selectable(bendModels[i], isSelected))
171  {
172  bendModel2 = i;
174  }
175 
176  if (isSelected)
177  {
179  }
180  }
181  ImGui::EndCombo();
182  }
183 
184  ImGui::SliderFloat("Damping##B2", &m_tuning2.bendDamping, 0.0f, 4.0f, "%.1f");
185  ImGui::SliderFloat("Hertz##B2", &m_tuning2.bendHertz, 0.0f, 60.0f, "%.0f");
186  ImGui::SliderFloat("Stiffness##B2", &m_tuning2.bendStiffness, 0.0f, 1.0f, "%.1f");
187 
188  ImGui::Checkbox("Isometric##2", &m_tuning2.isometric);
189  ImGui::Checkbox("Fixed Mass##2", &m_tuning2.fixedEffectiveMass);
190  ImGui::Checkbox("Warm Start##2", &m_tuning2.warmStart);
191 
192  static int stretchModel2 = m_tuning2.stretchingModel;
193  if (ImGui::BeginCombo("Stretch Model##2", stretchModels[stretchModel2], comboFlags))
194  {
195  for (int i = 0; i < IM_ARRAYSIZE(stretchModels); ++i)
196  {
197  bool isSelected = (stretchModel2 == i);
198  if (ImGui::Selectable(stretchModels[i], isSelected))
199  {
200  stretchModel2 = i;
202  }
203 
204  if (isSelected)
205  {
207  }
208  }
209  ImGui::EndCombo();
210  }
211 
212  ImGui::SliderFloat("Damping##S2", &m_tuning2.stretchDamping, 0.0f, 4.0f, "%.1f");
213  ImGui::SliderFloat("Hertz##S2", &m_tuning2.stretchHertz, 0.0f, 60.0f, "%.0f");
214  ImGui::SliderFloat("Stiffness##S2", &m_tuning2.stretchStiffness, 0.0f, 1.0f, "%.1f");
215 
216  ImGui::SliderInt("Iterations##2", &m_iterations2, 1, 100, "%d");
217 
219 
220  ImGui::SliderFloat("Speed", &m_speed, 10.0f, 100.0f, "%.0f");
221 
222  if (ImGui::Button("Reset"))
223  {
224  m_position1.Set(-5.0f, 15.0f);
225  m_position2.Set(5.0f, 15.0f);
228  }
229 
231 
232  ImGui::End();
233  }
234 
235  void Step(Settings& settings) override
236  {
237  float dt = settings.m_hertz > 0.0f ? 1.0f / settings.m_hertz : 0.0f;
238 
239  if (settings.m_pause == 1 && settings.m_singleStep == 0)
240  {
241  dt = 0.0f;
242  }
243 
245  {
246  m_position1.x -= m_speed * dt;
247  m_position2.x -= m_speed * dt;
248  }
249 
251  {
252  m_position1.x += m_speed * dt;
253  m_position2.x += m_speed * dt;
254  }
255 
260 
261  Test::Step(settings);
262 
265 
266  g_debugDraw.DrawString(5, m_textLine, "Press comma and period to move left and right");
268  }
269 
270  static Test* Create()
271  {
272  return new Rope;
273  }
274 
283  float m_speed;
284 };
285 
286 static int testIndex = RegisterTest("Rope", "Bending", Rope::Create);
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
bool m_singleStep
Definition: settings.h:82
b2BendingModel bendingModel
Definition: b2_rope.h:67
bool m_pause
Definition: settings.h:81
#define GLFW_KEY_PERIOD
Definition: glfw3.h:364
f
IMGUI_API bool BeginCombo(const char *label, const char *preview_value, ImGuiComboFlags flags=0)
Definition: imgui.h:164
void Reset(const b2Vec2 &position)
Definition: b2_rope.cpp:332
float bendDamping
Definition: b2_rope.h:74
float * masses
Definition: b2_rope.h:95
float m_speed
Definition: rope.cpp:283
float x
Definition: b2_math.h:128
int32 m_textLine
Definition: test.h:127
b2StretchingModel stretchingModel
Definition: b2_rope.h:66
void Step(Settings &settings) override
Definition: rope.cpp:235
IMGUI_API void SetItemDefaultFocus()
Definition: imgui.cpp:6318
void UpdateUI() override
Definition: rope.cpp:92
b2RopeTuning m_tuning2
Definition: rope.cpp:278
int32 m_iterations2
Definition: rope.cpp:280
float m_hertz
Definition: settings.h:64
Definition: test.h:80
float stretchDamping
Definition: b2_rope.h:71
#define IM_ARRAYSIZE(_ARR)
Definition: imgui.h:73
b2Vec2 gravity
Definition: b2_rope.h:96
b2Vec2 m_position1
Definition: rope.cpp:281
IMGUI_API float GetWindowWidth()
Definition: imgui.cpp:5882
b2Vec2 m_position2
Definition: rope.cpp:282
A 2D column vector.
Definition: b2_math.h:41
signed int int32
Definition: b2_types.h:28
b2Rope m_rope2
Definition: rope.cpp:276
Rope()
Definition: rope.cpp:32
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:4736
b2BendingModel
Definition: b2_rope.h:39
void SetTuning(const b2RopeTuning &tuning)
Definition: b2_rope.cpp:187
static int testIndex
Definition: rope.cpp:286
void Draw(b2Draw *draw) const
Definition: b2_rope.cpp:793
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2Rope m_rope1
Definition: rope.cpp:275
IMGUI_API void EndCombo()
IMGUI_API void PushItemWidth(float item_width)
Definition: imgui.cpp:5496
float bendStiffness
Definition: b2_rope.h:72
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0))
int32 m_iterations1
Definition: rope.cpp:279
int32 m_textIncrement
Definition: test.h:135
IMGUI_API void Separator()
IMGUI_API void End()
Definition: imgui.cpp:5371
b2Vec2 position
Definition: b2_rope.h:92
bool isometric
Definition: b2_rope.h:75
void Step(float timeStep, int32 iterations, const b2Vec2 &position)
Definition: b2_rope.cpp:244
int ImGuiComboFlags
Definition: imgui.h:135
b2RopeTuning tuning
Definition: b2_rope.h:97
b2StretchingModel
Definition: b2_rope.h:33
static Test * Create()
Definition: rope.cpp:270
b2Vec2 * vertices
Definition: b2_rope.h:93
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiCond cond=0, const ImVec2 &pivot=ImVec2(0, 0))
Definition: imgui.cpp:6045
int32 count
Definition: b2_rope.h:94
IMGUI_API bool SliderInt(const char *label, int *v, int v_min, int v_max, const char *format="%d")
IMGUI_API void Text(const char *fmt,...) IM_FMTARGS(1)
bool warmStart
Definition: b2_rope.h:77
IMGUI_API bool SliderFloat(const char *label, float *v, float v_min, float v_max, const char *format="%.3f", float power=1.0f)
IMGUI_API void PopItemWidth()
Definition: imgui.cpp:5517
#define GLFW_KEY_COMMA
Definition: glfw3.h:362
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
IMGUI_API bool Selectable(const char *label, bool selected=false, ImGuiSelectableFlags flags=0, const ImVec2 &size=ImVec2(0, 0))
IMGUI_API bool Checkbox(const char *label, bool *v)
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
Definition: rope.cpp:29
b2RopeTuning m_tuning1
Definition: rope.cpp:277
GLFWwindow * g_mainWindow
Definition: main.cpp:42
virtual void Step(Settings &settings)
Definition: test.cpp:278
void Create(const b2RopeDef &def)
Definition: b2_rope.cpp:77
DebugDraw g_debugDraw
Definition: draw.cpp:32
bool fixedEffectiveMass
Definition: b2_rope.h:76
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
GLFWAPI int glfwGetKey(GLFWwindow *window, int key)
Returns the last reported state of a keyboard key for the specified window.
Definition: input.c:591
float bendHertz
Definition: b2_rope.h:73
float stretchStiffness
Definition: b2_rope.h:69
float stretchHertz
Definition: b2_rope.h:70


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21