revolute_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 "settings.h"
24 #include "test.h"
25 #include "imgui/imgui.h"
26 
27 class RevoluteJoint : 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 
40  b2FixtureDef fd;
41  fd.shape = &shape;
42  //fd.filter.categoryBits = 2;
43 
44  ground->CreateFixture(&fd);
45  }
46 
47  m_enableLimit = true;
48  m_enableMotor = false;
49  m_motorSpeed = 1.0f;
50 
51  {
52  b2PolygonShape shape;
53  shape.SetAsBox(0.25f, 3.0f, b2Vec2(0.0f, 3.0f), 0.0f);
54 
55  b2BodyDef bd;
56  bd.type = b2_dynamicBody;
57  bd.position.Set(-10.0f, 20.0f);
58  b2Body* body = m_world->CreateBody(&bd);
59  body->CreateFixture(&shape, 5.0f);
60 
62  jd.Initialize(ground, body, b2Vec2(-10.0f, 20.5f));
64  jd.maxMotorTorque = 10000.0f;
66  jd.lowerAngle = -0.25f * b2_pi;
67  jd.upperAngle = 0.5f * b2_pi;
69 
71  }
72 
73  {
74  b2CircleShape circle_shape;
75  circle_shape.m_radius = 2.0f;
76 
77  b2BodyDef circle_bd;
78  circle_bd.type = b2_dynamicBody;
79  circle_bd.position.Set(5.0f, 30.0f);
80 
81  b2FixtureDef fd;
82  fd.density = 5.0f;
83  fd.filter.maskBits = 1;
84  fd.shape = &circle_shape;
85 
86  m_ball = m_world->CreateBody(&circle_bd);
87  m_ball->CreateFixture(&fd);
88 
89  b2PolygonShape polygon_shape;
90  polygon_shape.SetAsBox(10.0f, 0.5f, b2Vec2 (-10.0f, 0.0f), 0.0f);
91 
92  b2BodyDef polygon_bd;
93  polygon_bd.position.Set(20.0f, 10.0f);
94  polygon_bd.type = b2_dynamicBody;
95  polygon_bd.bullet = true;
96  b2Body* polygon_body = m_world->CreateBody(&polygon_bd);
97  polygon_body->CreateFixture(&polygon_shape, 2.0f);
98 
100  jd.Initialize(ground, polygon_body, b2Vec2(19.0f, 10.0f));
101  jd.lowerAngle = -0.25f * b2_pi;
102  jd.upperAngle = 0.0f * b2_pi;
103  jd.enableLimit = true;
104  jd.enableMotor = true;
105  jd.motorSpeed = 0.0f;
106  jd.maxMotorTorque = 10000.0f;
107 
109  }
110  }
111 
112  void UpdateUI() override
113  {
114  ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
115  ImGui::SetNextWindowSize(ImVec2(200.0f, 100.0f));
116  ImGui::Begin("Joint Controls", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
117 
118  if (ImGui::Checkbox("Limit", &m_enableLimit))
119  {
121  }
122 
123  if (ImGui::Checkbox("Motor", &m_enableMotor))
124  {
126  }
127 
128  if (ImGui::SliderFloat("Speed", &m_motorSpeed, -20.0f, 20.0f, "%.0f"))
129  {
131  }
132 
133  ImGui::End();
134  }
135 
136  void Step(Settings& settings) override
137  {
138  Test::Step(settings);
139 
140  float torque1 = m_joint1->GetMotorTorque(settings.m_hertz);
141  g_debugDraw.DrawString(5, m_textLine, "Motor Torque 1= %4.0f", torque1);
143 
144  float torque2 = m_joint2->GetMotorTorque(settings.m_hertz);
145  g_debugDraw.DrawString(5, m_textLine, "Motor Torque 2= %4.0f", torque2);
147  }
148 
149  static Test* Create()
150  {
151  return new RevoluteJoint;
152  }
153 
160 };
161 
162 static int testIndex = RegisterTest("Joints", "Revolute", RevoluteJoint::Create);
const b2Shape * shape
Definition: b2_fixture.h:76
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
float lowerAngle
The lower angle for the joint limit (radians).
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
void SetMotorSpeed(float speed)
Set the motor speed in radians per second.
f
Definition: imgui.h:164
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
int32 m_textLine
Definition: test.h:127
void Step(Settings &settings) override
float GetMotorTorque(float inv_dt) const
float m_hertz
Definition: settings.h:64
Definition: test.h:80
bool bullet
Definition: b2_body.h:115
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
b2RevoluteJoint * m_joint1
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
void SetAsBox(float hx, float hy)
void EnableMotor(bool flag)
Enable/disable the joint motor.
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
b2RevoluteJoint * m_joint2
float m_radius
Definition: b2_shape.h:102
b2Filter filter
Contact filtering data.
Definition: b2_fixture.h:99
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
static Test * Create()
int32 m_textIncrement
Definition: test.h:135
IMGUI_API void End()
Definition: imgui.cpp:5371
bool enableMotor
A flag to enable the joint motor.
b2World * m_world
Definition: test.h:128
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiCond cond=0, const ImVec2 &pivot=ImVec2(0, 0))
Definition: imgui.cpp:6045
static int testIndex
#define b2_pi
Definition: b2_common.h:41
b2Vec2 position
Definition: b2_body.h:78
float motorSpeed
The desired motor speed. Usually in radians per second.
float upperAngle
The upper angle for the joint limit (radians).
IMGUI_API bool SliderFloat(const char *label, float *v, float v_min, float v_max, const char *format="%.3f", float power=1.0f)
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
IMGUI_API bool Checkbox(const char *label, bool *v)
bool enableLimit
A flag to enable joint limits.
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
void EnableLimit(bool flag)
Enable/disable the joint limit.
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
uint16 maskBits
Definition: b2_fixture.h:51
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115


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