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);
Test::m_textIncrement
int32 m_textIncrement
Definition: test.h:135
b2EdgeShape
Definition: b2_edge_shape.h:32
b2PrismaticJoint::EnableMotor
void EnableMotor(bool flag)
Enable/disable the joint motor.
Definition: b2_prismatic_joint.cpp:551
b2PrismaticJoint
Definition: b2_prismatic_joint.h:91
g_debugDraw
DebugDraw g_debugDraw
Definition: draw.cpp:32
NULL
#define NULL
b2PrismaticJoint::EnableLimit
void EnableLimit(bool flag)
Enable/disable the joint limit.
Definition: b2_prismatic_joint.cpp:510
settings.h
testIndex
static int testIndex
Definition: prismatic_joint.cpp:118
ImGui::SetNextWindowPos
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiCond cond=0, const ImVec2 &pivot=ImVec2(0, 0))
Definition: imgui.cpp:6045
PrismaticJoint::m_enableMotor
bool m_enableMotor
Definition: prismatic_joint.cpp:114
PrismaticJoint::m_motorSpeed
float m_motorSpeed
Definition: prismatic_joint.cpp:113
b2PrismaticJointDef::lowerTranslation
float lowerTranslation
The lower translation limit, usually in meters.
Definition: b2_prismatic_joint.h:72
imgui.h
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
PrismaticJoint::Create
static Test * Create()
Definition: prismatic_joint.cpp:107
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
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
b2_pi
#define b2_pi
Definition: b2_common.h:41
PrismaticJoint::PrismaticJoint
PrismaticJoint()
Definition: prismatic_joint.cpp:31
b2_dynamicBody
@ b2_dynamicBody
Definition: b2_body.h:47
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
PrismaticJoint::UpdateUI
void UpdateUI() override
Definition: prismatic_joint.cpp:75
b2BodyDef::angle
float angle
The world angle of the body in radians.
Definition: b2_body.h:81
Settings::m_hertz
float m_hertz
Definition: settings.h:64
b2PrismaticJointDef::enableLimit
bool enableLimit
Enable/disable the joint limit.
Definition: b2_prismatic_joint.h:69
ImGui::Checkbox
IMGUI_API bool Checkbox(const char *label, bool *v)
Definition: imgui_widgets.cpp:876
b2PrismaticJointDef
Definition: b2_prismatic_joint.h:35
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition: b2_polygon_shape.cpp:36
PrismaticJoint::m_joint
b2PrismaticJoint * m_joint
Definition: prismatic_joint.cpp:112
ImGui::SetNextWindowSize
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
b2BodyDef
Definition: b2_body.h:52
Settings
Definition: settings.h:25
PrismaticJoint
Definition: prismatic_joint.cpp:28
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
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
b2PrismaticJointDef::upperTranslation
float upperTranslation
The upper translation limit, usually in meters.
Definition: b2_prismatic_joint.h:75
PrismaticJoint::m_enableLimit
bool m_enableLimit
Definition: prismatic_joint.cpp:115
Test
Definition: test.h:80
b2BodyDef::position
b2Vec2 position
Definition: b2_body.h:78
b2PrismaticJointDef::motorSpeed
float motorSpeed
The desired motor speed in radians per second.
Definition: b2_prismatic_joint.h:84
b2PrismaticJoint::GetMotorForce
float GetMotorForce(float inv_dt) const
Get the current motor force given the inverse time step, usually in N.
Definition: b2_prismatic_joint.cpp:581
b2PrismaticJoint::SetMotorSpeed
void SetMotorSpeed(float speed)
Set the motor speed, usually in meters per second.
Definition: b2_prismatic_joint.cpp:561
b2BodyDef::allowSleep
bool allowSleep
Definition: b2_body.h:103
b2PrismaticJointDef::enableMotor
bool enableMotor
Enable/disable the joint motor.
Definition: b2_prismatic_joint.h:78
Test::Step
virtual void Step(Settings &settings)
Definition: test.cpp:278
ImGuiWindowFlags_NoResize
@ ImGuiWindowFlags_NoResize
Definition: imgui.h:687
b2PrismaticJointDef::maxMotorForce
float maxMotorForce
The maximum motor torque, usually in N-m.
Definition: b2_prismatic_joint.h:81
Test::m_world
b2World * m_world
Definition: test.h:128
b2PrismaticJointDef::Initialize
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor, const b2Vec2 &axis)
Definition: b2_prismatic_joint.cpp:73
PrismaticJoint::Step
void Step(Settings &settings) override
Definition: prismatic_joint.cpp:99
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165


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