prismatic_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 prismatic joint with limits and motor options.
28 class PrismaticJoint : 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  b2PolygonShape shape;
49  shape.SetAsBox(1.0f, 1.0f);
50 
51  b2BodyDef bd;
52  bd.type = b2_dynamicBody;
53  bd.position.Set(0.0f, 10.0f);
54  bd.angle = 0.5f * b2_pi;
55  bd.allowSleep = false;
56  b2Body* body = m_world->CreateBody(&bd);
57  body->CreateFixture(&shape, 5.0f);
58 
60 
61  // Horizontal
62  pjd.Initialize(ground, body, bd.position, b2Vec2(1.0f, 0.0f));
63 
65  pjd.maxMotorForce = 10000.0f;
67  pjd.lowerTranslation = -10.0f;
68  pjd.upperTranslation = 10.0f;
70 
72  }
73  }
74 
75  void UpdateUI() override
76  {
77  ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
78  ImGui::SetNextWindowSize(ImVec2(200.0f, 100.0f));
80 
81  if (ImGui::Checkbox("Limit", &m_enableLimit))
82  {
84  }
85 
86  if (ImGui::Checkbox("Motor", &m_enableMotor))
87  {
89  }
90 
91  if (ImGui::SliderFloat("Speed", &m_motorSpeed, -100.0f, 100.0f, "%.0f"))
92  {
94  }
95 
96  ImGui::End();
97  }
98 
99  void Step(Settings& settings) override
100  {
101  Test::Step(settings);
102  float force = m_joint->GetMotorForce(settings.m_hertz);
103  g_debugDraw.DrawString(5, m_textLine, "Motor Force = %4.0f", force);
105  }
106 
107  static Test* Create()
108  {
109  return new PrismaticJoint;
110  }
111 
116 };
117 
118 static int testIndex = RegisterTest("Joints", "Prismatic", PrismaticJoint::Create);
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
f
Definition: imgui.h:164
int32 m_textLine
Definition: test.h:127
float m_hertz
Definition: settings.h:64
Definition: test.h:80
void SetMotorSpeed(float speed)
Set the motor speed, usually in meters per second.
A 2D column vector.
Definition: b2_math.h:41
float lowerTranslation
The lower translation limit, usually in meters.
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
b2PrismaticJoint * m_joint
void SetAsBox(float hx, float hy)
float GetMotorForce(float inv_dt) const
Get the current motor force given the inverse time step, usually in N.
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
float maxMotorForce
The maximum motor torque, usually in N-m.
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
float motorSpeed
The desired motor speed in radians per second.
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
bool enableLimit
Enable/disable the joint limit.
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor, const b2Vec2 &axis)
int32 m_textIncrement
Definition: test.h:135
IMGUI_API void End()
Definition: imgui.cpp:5371
b2World * m_world
Definition: test.h:128
static Test * Create()
float angle
The world angle of the body in radians.
Definition: b2_body.h:81
void EnableLimit(bool flag)
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
#define b2_pi
Definition: b2_common.h:41
float upperTranslation
The upper translation limit, usually in meters.
b2Vec2 position
Definition: b2_body.h:78
void UpdateUI() override
IMGUI_API bool SliderFloat(const char *label, float *v, float v_min, float v_max, const char *format="%.3f", float power=1.0f)
bool enableMotor
Enable/disable the joint motor.
static int testIndex
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
IMGUI_API bool Checkbox(const char *label, bool *v)
void EnableMotor(bool flag)
Enable/disable the joint motor.
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
bool allowSleep
Definition: b2_body.h:103
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
void Step(Settings &settings) override
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115


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