wheel_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 // Test the wheel joint with motor, spring, and limit options.
28 class WheelJoint : public Test
29 {
30 public:
32  {
33  b2Body* ground = NULL;
34  {
35  b2BodyDef bd;
36  ground = m_world->CreateBody(&bd);
37 
38  b2EdgeShape shape;
39  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
40  ground->CreateFixture(&shape, 0.0f);
41  }
42 
43  m_enableLimit = true;
44  m_enableMotor = false;
45  m_motorSpeed = 10.0f;
46 
47  {
48  b2CircleShape shape;
49  shape.m_radius = 2.0f;
50 
51  b2BodyDef bd;
52  bd.type = b2_dynamicBody;
53  bd.position.Set(0.0f, 10.0f);
54  bd.allowSleep = false;
55  b2Body* body = m_world->CreateBody(&bd);
56  body->CreateFixture(&shape, 5.0f);
57 
58  b2WheelJointDef jd;
59 
60  // Horizontal
61  jd.Initialize(ground, body, bd.position, b2Vec2(0.0f, 1.0f));
62 
64  jd.maxMotorTorque = 10000.0f;
66  jd.lowerTranslation = -3.0f;
67  jd.upperTranslation = 3.0f;
69 
70  float hertz = 1.0f;
71  float dampingRatio = 0.7f;
72  b2LinearStiffness(jd.stiffness, jd.damping, hertz, dampingRatio, ground, body);
73 
75  }
76  }
77 
78  void Step(Settings& settings) override
79  {
80  Test::Step(settings);
81 
82  float torque = m_joint->GetMotorTorque(settings.m_hertz);
83  g_debugDraw.DrawString(5, m_textLine, "Motor Torque = %4.0f", torque);
85 
86  b2Vec2 F = m_joint->GetReactionForce(settings.m_hertz);
87  g_debugDraw.DrawString(5, m_textLine, "Reaction Force = (%4.1f, %4.1f)", F.x, F.y);
89  }
90 
91  void UpdateUI() override
92  {
93  ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
94  ImGui::SetNextWindowSize(ImVec2(200.0f, 100.0f));
96 
97  if (ImGui::Checkbox("Limit", &m_enableLimit))
98  {
100  }
101 
102  if (ImGui::Checkbox("Motor", &m_enableMotor))
103  {
105  }
106 
107  if (ImGui::SliderFloat("Speed", &m_motorSpeed, -100.0f, 100.0f, "%.0f"))
108  {
110  }
111 
112  ImGui::End();
113  }
114 
115  static Test* Create()
116  {
117  return new WheelJoint;
118  }
119 
124 };
125 
126 static int testIndex = RegisterTest("Joints", "Wheel", WheelJoint::Create);
bool m_enableMotor
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
float m_motorSpeed
void Step(Settings &settings) override
Definition: wheel_joint.cpp:78
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
f
static Test * Create()
Definition: imgui.h:164
float x
Definition: b2_math.h:128
float y
Definition: b2_math.h:128
int32 m_textLine
Definition: test.h:127
float lowerTranslation
The lower translation limit, usually in meters.
float m_hertz
Definition: settings.h:64
Definition: test.h:80
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor, const b2Vec2 &axis)
static int testIndex
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
bool m_enableLimit
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
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
void SetMotorSpeed(float speed)
Set the motor speed, usually in radians per second.
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
float maxMotorTorque
The maximum motor torque, usually in N-m.
float m_radius
Definition: b2_shape.h:102
float GetMotorTorque(float inv_dt) const
Get the current motor torque given the inverse time step, usually in N-m.
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
bool enableMotor
Enable/disable the joint motor.
int32 m_textIncrement
Definition: test.h:135
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
void UpdateUI() override
Definition: wheel_joint.cpp:91
bool enableLimit
Enable/disable the joint limit.
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)
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
IMGUI_API bool Checkbox(const char *label, bool *v)
b2WheelJoint * m_joint
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
void EnableMotor(bool flag)
Enable/disable the joint motor.
bool allowSleep
Definition: b2_body.h:103
float motorSpeed
The desired motor speed in radians per second.
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2Vec2 GetReactionForce(float inv_dt) const override
Get the reaction force on bodyB at the joint anchor in Newtons.
DebugDraw g_debugDraw
Definition: draw.cpp:32
void EnableLimit(bool flag)
Enable/disable the joint translation limit.
float damping
Suspension damping. Typically in units of N*s/m.
float stiffness
Suspension stiffness. Typically in units N/m.
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
float upperTranslation
The upper translation limit, usually in meters.


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