slider_crank_2.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 
26 // A motor driven slider crank with joint friction.
27 
28 class SliderCrank2 : 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  {
44  b2Body* prevBody = ground;
45 
46  // Define crank.
47  {
48  b2PolygonShape shape;
49  shape.SetAsBox(0.5f, 2.0f);
50 
51  b2BodyDef bd;
52  bd.type = b2_dynamicBody;
53  bd.position.Set(0.0f, 7.0f);
54  b2Body* body = m_world->CreateBody(&bd);
55  body->CreateFixture(&shape, 2.0f);
56 
58  rjd.Initialize(prevBody, body, b2Vec2(0.0f, 5.0f));
59  rjd.motorSpeed = 1.0f * b2_pi;
60  rjd.maxMotorTorque = 10000.0f;
61  rjd.enableMotor = true;
63 
64  prevBody = body;
65  }
66 
67  // Define follower.
68  {
69  b2PolygonShape shape;
70  shape.SetAsBox(0.5f, 4.0f);
71 
72  b2BodyDef bd;
73  bd.type = b2_dynamicBody;
74  bd.position.Set(0.0f, 13.0f);
75  b2Body* body = m_world->CreateBody(&bd);
76  body->CreateFixture(&shape, 2.0f);
77 
79  rjd.Initialize(prevBody, body, b2Vec2(0.0f, 9.0f));
80  rjd.enableMotor = false;
81  m_world->CreateJoint(&rjd);
82 
83  prevBody = body;
84  }
85 
86  // Define piston
87  {
88  b2PolygonShape shape;
89  shape.SetAsBox(1.5f, 1.5f);
90 
91  b2BodyDef bd;
92  bd.type = b2_dynamicBody;
93  bd.fixedRotation = true;
94  bd.position.Set(0.0f, 17.0f);
95  b2Body* body = m_world->CreateBody(&bd);
96  body->CreateFixture(&shape, 2.0f);
97 
99  rjd.Initialize(prevBody, body, b2Vec2(0.0f, 17.0f));
100  m_world->CreateJoint(&rjd);
101 
103  pjd.Initialize(ground, body, b2Vec2(0.0f, 17.0f), b2Vec2(0.0f, 1.0f));
104 
105  pjd.maxMotorForce = 1000.0f;
106  pjd.enableMotor = true;
107 
109  }
110 
111  // Create a payload
112  {
113  b2PolygonShape shape;
114  shape.SetAsBox(1.5f, 1.5f);
115 
116  b2BodyDef bd;
117  bd.type = b2_dynamicBody;
118  bd.position.Set(0.0f, 23.0f);
119  b2Body* body = m_world->CreateBody(&bd);
120  body->CreateFixture(&shape, 2.0f);
121  }
122  }
123  }
124 
125  void Keyboard(int key) override
126  {
127  switch (key)
128  {
129  case GLFW_KEY_F:
131  m_joint2->GetBodyB()->SetAwake(true);
132  break;
133 
134  case GLFW_KEY_M:
136  m_joint1->GetBodyB()->SetAwake(true);
137  break;
138  }
139  }
140 
141  void Step(Settings& settings) override
142  {
143  Test::Step(settings);
144  g_debugDraw.DrawString(5, m_textLine, "Keys: (f) toggle friction, (m) toggle motor");
146  float torque = m_joint1->GetMotorTorque(settings.m_hertz);
147  g_debugDraw.DrawString(5, m_textLine, "Motor Torque = %5.0f", (float) torque);
149  }
150 
151  static Test* Create()
152  {
153  return new SliderCrank2;
154  }
155 
158 };
159 
160 static int testIndex = RegisterTest("Examples", "Slider Crank 2", SliderCrank2::Create);
bool fixedRotation
Should this body be prevented from rotating? Useful for characters.
Definition: b2_body.h:109
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
#define GLFW_KEY_F
Definition: glfw3.h:383
void Step(Settings &settings) override
f
bool IsMotorEnabled() const
Is the joint motor enabled?
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
int32 m_textLine
Definition: test.h:127
float GetMotorTorque(float inv_dt) const
float m_hertz
Definition: settings.h:64
Definition: test.h:80
A 2D column vector.
Definition: b2_math.h:41
b2Body * GetBodyB()
Get the second body attached to this joint.
Definition: b2_joint.h:203
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.
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
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor, const b2Vec2 &axis)
int32 m_textIncrement
Definition: test.h:135
bool enableMotor
A flag to enable the joint motor.
b2World * m_world
Definition: test.h:128
b2PrismaticJoint * m_joint2
void Keyboard(int key) override
#define b2_pi
Definition: b2_common.h:41
#define GLFW_KEY_M
Definition: glfw3.h:390
b2Vec2 position
Definition: b2_body.h:78
float motorSpeed
The desired motor speed. Usually in radians per second.
b2RevoluteJoint * m_joint1
static int testIndex
static Test * Create()
bool enableMotor
Enable/disable the joint motor.
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
void EnableMotor(bool flag)
Enable/disable the joint motor.
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
virtual void Step(Settings &settings)
Definition: test.cpp:278
bool IsMotorEnabled() const
Is the joint motor enabled?
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
void SetAwake(bool flag)
Definition: b2_body.h:638


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