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);
b2EdgeShape
Definition: b2_edge_shape.h:32
NULL
#define NULL
b2LinearStiffness
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
ImGui::SetNextWindowPos
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiCond cond=0, const ImVec2 &pivot=ImVec2(0, 0))
Definition: imgui.cpp:6045
DistanceJoint
Definition: distance_joint.cpp:27
b2Joint::GetBodyB
b2Body * GetBodyB()
Get the second body attached to this joint.
Definition: b2_joint.h:203
b2BodyDef::angularDamping
float angularDamping
Definition: b2_body.h:99
DistanceJoint::Create
static Test * Create()
Definition: distance_joint.cpp:110
DistanceJoint::m_maxLength
float m_maxLength
Definition: distance_joint.cpp:118
imgui.h
b2DistanceJointDef::damping
float damping
The linear damping in N*s/m.
Definition: b2_distance_joint.h:71
b2DistanceJointDef::length
float length
The rest length of this joint. Clamped to a stable minimum value.
Definition: b2_distance_joint.h:59
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
ImVec2
Definition: imgui.h:164
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2DistanceJoint::SetMaxLength
float SetMaxLength(float maxLength)
Definition: b2_distance_joint.cpp:352
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
b2EdgeShape::SetTwoSided
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
Definition: b2_edge_shape.cpp:36
f
f
b2Joint::GetBodyA
b2Body * GetBodyA()
Get the first body attached to this joint.
Definition: b2_joint.h:198
ImGui::End
IMGUI_API void End()
Definition: imgui.cpp:5371
b2BodyDef::type
b2BodyType type
Definition: b2_body.h:74
b2DistanceJointDef::stiffness
float stiffness
The linear stiffness in N/m.
Definition: b2_distance_joint.h:68
b2World::CreateJoint
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
DistanceJoint::m_joint
b2DistanceJoint * m_joint
Definition: distance_joint.cpp:115
b2_dynamicBody
@ b2_dynamicBody
Definition: b2_body.h:47
ImGui::SliderFloat
IMGUI_API bool SliderFloat(const char *label, float *v, float v_min, float v_max, const char *format="%.3f", float power=1.0f)
Definition: imgui_widgets.cpp:2394
b2DistanceJoint::SetDamping
void SetDamping(float damping)
Set/get linear damping in N*s/m.
Definition: b2_distance_joint.h:126
DistanceJoint::m_length
float m_length
Definition: distance_joint.cpp:116
b2JointDef::collideConnected
bool collideConnected
Set this flag to true if the attached bodies should collide.
Definition: b2_joint.h:95
DistanceJoint::UpdateUI
void UpdateUI() override
Definition: distance_joint.cpp:68
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition: b2_polygon_shape.cpp:36
b2DistanceJoint::SetStiffness
void SetStiffness(float stiffness)
Set/get the linear stiffness in N/m.
Definition: b2_distance_joint.h:122
ImGui::SetNextWindowSize
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
b2DistanceJointDef::Initialize
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchorA, const b2Vec2 &anchorB)
Definition: b2_distance_joint.cpp:44
b2BodyDef
Definition: b2_body.h:52
DistanceJoint::DistanceJoint
DistanceJoint()
Definition: distance_joint.cpp:30
testIndex
static int testIndex
Definition: distance_joint.cpp:123
b2DistanceJoint::SetLength
float SetLength(float length)
Definition: b2_distance_joint.cpp:338
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
b2PolygonShape
Definition: b2_polygon_shape.h:32
ImGuiWindowFlags_NoMove
@ ImGuiWindowFlags_NoMove
Definition: imgui.h:688
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
ImGui::Begin
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:4736
Test
Definition: test.h:80
b2DistanceJointDef
Definition: b2_distance_joint.h:33
b2BodyDef::position
b2Vec2 position
Definition: b2_body.h:78
b2DistanceJoint
Definition: b2_distance_joint.h:76
ImGuiWindowFlags_NoResize
@ ImGuiWindowFlags_NoResize
Definition: imgui.h:687
DistanceJoint::m_dampingRatio
float m_dampingRatio
Definition: distance_joint.cpp:120
DistanceJoint::m_hertz
float m_hertz
Definition: distance_joint.cpp:119
b2DistanceJoint::SetMinLength
float SetMinLength(float minLength)
Definition: b2_distance_joint.cpp:345
DistanceJoint::m_minLength
float m_minLength
Definition: distance_joint.cpp:117
b2JointDef::bodyA
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
Test::m_world
b2World * m_world
Definition: test.h:128
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
b2JointDef::bodyB
b2Body * bodyB
The second attached body.
Definition: b2_joint.h:92


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:07