distance_joint.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 "test.h"
24 #include "imgui/imgui.h"
25 
26 // This tests distance joints, body destruction, and joint destruction.
27 class DistanceJoint : public Test
28 {
29 public:
31  {
32  b2Body* ground = NULL;
33  {
34  b2BodyDef bd;
35  ground = m_world->CreateBody(&bd);
36 
37  b2EdgeShape shape;
38  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
39  ground->CreateFixture(&shape, 0.0f);
40  }
41 
42  {
43  b2BodyDef bd;
44  bd.type = b2_dynamicBody;
45  bd.angularDamping = 0.1f;
46 
47  bd.position.Set(0.0f, 5.0f);
48  b2Body* body = m_world->CreateBody(&bd);
49 
50  b2PolygonShape shape;
51  shape.SetAsBox(0.5f, 0.5f);
52  body->CreateFixture(&shape, 5.0f);
53 
54  m_hertz = 1.0f;
55  m_dampingRatio = 0.7f;
56 
58  jd.Initialize(ground, body, b2Vec2(0.0f, 15.0f), bd.position);
59  jd.collideConnected = true;
60  m_length = jd.length;
65  }
66  }
67 
68  void UpdateUI() override
69  {
70  ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
71  ImGui::SetNextWindowSize(ImVec2(260.0f, 150.0f));
73 
74  if (ImGui::SliderFloat("Length", &m_length, 0.0f, 20.0f, "%.0f"))
75  {
77  }
78 
79  if (ImGui::SliderFloat("Min Length", &m_minLength, 0.0f, 20.0f, "%.0f"))
80  {
82  }
83 
84  if (ImGui::SliderFloat("Max Length", &m_maxLength, 0.0f, 20.0f, "%.0f"))
85  {
87  }
88 
89  if (ImGui::SliderFloat("Hertz", &m_hertz, 0.0f, 10.0f, "%.1f"))
90  {
91  float stiffness;
92  float damping;
94  m_joint->SetStiffness(stiffness);
95  m_joint->SetDamping(damping);
96  }
97 
98  if (ImGui::SliderFloat("Damping Ratio", &m_dampingRatio, 0.0f, 2.0f, "%.1f"))
99  {
100  float stiffness;
101  float damping;
103  m_joint->SetStiffness(stiffness);
104  m_joint->SetDamping(damping);
105  }
106 
107  ImGui::End();
108  }
109 
110  static Test* Create()
111  {
112  return new DistanceJoint;
113  }
114 
116  float m_length;
117  float m_minLength;
118  float m_maxLength;
119  float m_hertz;
121 };
122 
123 static int testIndex = RegisterTest("Joints", "Distance Joint", DistanceJoint::Create);
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
float SetMinLength(float minLength)
float SetMaxLength(float maxLength)
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
f
b2Body * GetBodyA()
Get the first body attached to this joint.
Definition: b2_joint.h:198
Definition: imgui.h:164
b2DistanceJoint * m_joint
Definition: test.h:80
float stiffness
The linear stiffness in N/m.
float angularDamping
Definition: b2_body.h:99
A 2D column vector.
Definition: b2_math.h:41
b2Body * GetBodyB()
Get the second body attached to this joint.
Definition: b2_joint.h:203
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
void SetAsBox(float hx, float hy)
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:4736
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
void UpdateUI() override
static Test * Create()
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchorA, const b2Vec2 &anchorB)
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
bool collideConnected
Set this flag to true if the attached bodies should collide.
Definition: b2_joint.h:95
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
IMGUI_API void End()
Definition: imgui.cpp:5371
b2World * m_world
Definition: test.h:128
B2_API void b2LinearStiffness(float &stiffness, float &damping, float frequencyHertz, float dampingRatio, const b2Body *bodyA, const b2Body *bodyB)
Utility to compute linear stiffness values from frequency and damping ratio.
Definition: b2_joint.cpp:40
float length
The rest length of this joint. Clamped to a stable minimum value.
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiCond cond=0, const ImVec2 &pivot=ImVec2(0, 0))
Definition: imgui.cpp:6045
b2Vec2 position
Definition: b2_body.h:78
IMGUI_API bool SliderFloat(const char *label, float *v, float v_min, float v_max, const char *format="%.3f", float power=1.0f)
static int testIndex
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
float SetLength(float length)
void SetDamping(float damping)
Set/get linear damping in N*s/m.
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
float damping
The linear damping in N*s/m.
void SetStiffness(float stiffness)
Set/get the linear stiffness in N/m.
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
b2Body * bodyB
The second attached body.
Definition: b2_joint.h:92


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