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);
Test::m_textIncrement
int32 m_textIncrement
Definition: test.h:135
b2EdgeShape
Definition: b2_edge_shape.h:32
b2Vec2::y
float y
Definition: b2_math.h:128
testIndex
static int testIndex
Definition: wheel_joint.cpp:126
g_debugDraw
DebugDraw g_debugDraw
Definition: draw.cpp:32
WheelJoint::UpdateUI
void UpdateUI() override
Definition: wheel_joint.cpp:91
NULL
#define NULL
b2WheelJoint::EnableMotor
void EnableMotor(bool flag)
Enable/disable the joint motor.
Definition: b2_wheel_joint.cpp:561
settings.h
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
b2WheelJoint
Definition: b2_wheel_joint.h:95
b2WheelJoint::GetMotorTorque
float GetMotorTorque(float inv_dt) const
Get the current motor torque given the inverse time step, usually in N-m.
Definition: b2_wheel_joint.cpp:591
b2WheelJointDef
Definition: b2_wheel_joint.h:35
b2WheelJointDef::enableLimit
bool enableLimit
Enable/disable the joint limit.
Definition: b2_wheel_joint.h:67
b2WheelJoint::GetReactionForce
b2Vec2 GetReactionForce(float inv_dt) const override
Get the reaction force on bodyB at the joint anchor in Newtons.
Definition: b2_wheel_joint.cpp:456
imgui.h
WheelJoint::WheelJoint
WheelJoint()
Definition: wheel_joint.cpp:31
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2CircleShape
A solid circle shape.
Definition: b2_circle_shape.h:30
ImVec2
Definition: imgui.h:164
b2WheelJoint::EnableLimit
void EnableLimit(bool flag)
Enable/disable the joint translation limit.
Definition: b2_wheel_joint.cpp:520
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
b2WheelJointDef::motorSpeed
float motorSpeed
The desired motor speed in radians per second.
Definition: b2_wheel_joint.h:82
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
WheelJoint::m_joint
b2WheelJoint * m_joint
Definition: wheel_joint.cpp:120
ImGui::End
IMGUI_API void End()
Definition: imgui.cpp:5371
b2BodyDef::type
b2BodyType type
Definition: b2_body.h:74
b2World::CreateJoint
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
WheelJoint
Definition: wheel_joint.cpp:28
WheelJoint::m_enableMotor
bool m_enableMotor
Definition: wheel_joint.cpp:122
b2WheelJointDef::stiffness
float stiffness
Suspension stiffness. Typically in units N/m.
Definition: b2_wheel_joint.h:85
b2_dynamicBody
@ b2_dynamicBody
Definition: b2_body.h:47
b2WheelJointDef::damping
float damping
Suspension damping. Typically in units of N*s/m.
Definition: b2_wheel_joint.h:88
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
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
b2Shape::m_radius
float m_radius
Definition: b2_shape.h:102
b2WheelJointDef::upperTranslation
float upperTranslation
The upper translation limit, usually in meters.
Definition: b2_wheel_joint.h:73
Settings::m_hertz
float m_hertz
Definition: settings.h:64
ImGui::Checkbox
IMGUI_API bool Checkbox(const char *label, bool *v)
Definition: imgui_widgets.cpp:876
WheelJoint::Create
static Test * Create()
Definition: wheel_joint.cpp:115
WheelJoint::m_motorSpeed
float m_motorSpeed
Definition: wheel_joint.cpp:121
ImGui::SetNextWindowSize
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
b2Vec2::x
float x
Definition: b2_math.h:128
b2BodyDef
Definition: b2_body.h:52
WheelJoint::Step
void Step(Settings &settings) override
Definition: wheel_joint.cpp:78
Settings
Definition: settings.h:25
b2WheelJoint::SetMotorSpeed
void SetMotorSpeed(float speed)
Set the motor speed, usually in radians per second.
Definition: b2_wheel_joint.cpp:571
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
b2WheelJointDef::Initialize
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor, const b2Vec2 &axis)
Definition: b2_wheel_joint.cpp:44
ImGuiWindowFlags_NoMove
@ ImGuiWindowFlags_NoMove
Definition: imgui.h:688
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
Test::m_textLine
int32 m_textLine
Definition: test.h:127
ImGui::Begin
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:4736
b2WheelJointDef::lowerTranslation
float lowerTranslation
The lower translation limit, usually in meters.
Definition: b2_wheel_joint.h:70
WheelJoint::m_enableLimit
bool m_enableLimit
Definition: wheel_joint.cpp:123
Test
Definition: test.h:80
b2BodyDef::position
b2Vec2 position
Definition: b2_body.h:78
b2BodyDef::allowSleep
bool allowSleep
Definition: b2_body.h:103
Test::Step
virtual void Step(Settings &settings)
Definition: test.cpp:278
ImGuiWindowFlags_NoResize
@ ImGuiWindowFlags_NoResize
Definition: imgui.h:687
Test::m_world
b2World * m_world
Definition: test.h:128
b2WheelJointDef::maxMotorTorque
float maxMotorTorque
The maximum motor torque, usually in N-m.
Definition: b2_wheel_joint.h:79
b2WheelJointDef::enableMotor
bool enableMotor
Enable/disable the joint motor.
Definition: b2_wheel_joint.h:76
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165


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