gear_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 "test.h"
24 
25 class GearJoint : public Test
26 {
27 public:
29  {
30  b2Body* ground = NULL;
31  {
32  b2BodyDef bd;
33  ground = m_world->CreateBody(&bd);
34 
35  b2EdgeShape shape;
36  shape.SetTwoSided(b2Vec2(50.0f, 0.0f), b2Vec2(-50.0f, 0.0f));
37  ground->CreateFixture(&shape, 0.0f);
38  }
39 
40  {
41  b2CircleShape circle1;
42  circle1.m_radius = 1.0f;
43 
44  b2PolygonShape box;
45  box.SetAsBox(0.5f, 5.0f);
46 
47  b2CircleShape circle2;
48  circle2.m_radius = 2.0f;
49 
50  b2BodyDef bd1;
51  bd1.type = b2_staticBody;
52  bd1.position.Set(10.0f, 9.0f);
53  b2Body* body1 = m_world->CreateBody(&bd1);
54  body1->CreateFixture(&circle1, 5.0f);
55 
56  b2BodyDef bd2;
57  bd2.type = b2_dynamicBody;
58  bd2.position.Set(10.0f, 8.0f);
59  b2Body* body2 = m_world->CreateBody(&bd2);
60  body2->CreateFixture(&box, 5.0f);
61 
62  b2BodyDef bd3;
63  bd3.type = b2_dynamicBody;
64  bd3.position.Set(10.0f, 6.0f);
65  b2Body* body3 = m_world->CreateBody(&bd3);
66  body3->CreateFixture(&circle2, 5.0f);
67 
69  jd1.Initialize(body1, body2, bd1.position);
70  b2Joint* joint1 = m_world->CreateJoint(&jd1);
71 
73  jd2.Initialize(body2, body3, bd3.position);
74  b2Joint* joint2 = m_world->CreateJoint(&jd2);
75 
76  b2GearJointDef jd4;
77  jd4.bodyA = body1;
78  jd4.bodyB = body3;
79  jd4.joint1 = joint1;
80  jd4.joint2 = joint2;
81  jd4.ratio = circle2.m_radius / circle1.m_radius;
82  m_world->CreateJoint(&jd4);
83  }
84 
85  {
86  b2CircleShape circle1;
87  circle1.m_radius = 1.0f;
88 
89  b2CircleShape circle2;
90  circle2.m_radius = 2.0f;
91 
92  b2PolygonShape box;
93  box.SetAsBox(0.5f, 5.0f);
94 
95  b2BodyDef bd1;
96  bd1.type = b2_dynamicBody;
97  bd1.position.Set(-3.0f, 12.0f);
98  b2Body* body1 = m_world->CreateBody(&bd1);
99  body1->CreateFixture(&circle1, 5.0f);
100 
101  b2RevoluteJointDef jd1;
102  jd1.bodyA = ground;
103  jd1.bodyB = body1;
104  jd1.localAnchorA = ground->GetLocalPoint(bd1.position);
105  jd1.localAnchorB = body1->GetLocalPoint(bd1.position);
106  jd1.referenceAngle = body1->GetAngle() - ground->GetAngle();
108 
109  b2BodyDef bd2;
110  bd2.type = b2_dynamicBody;
111  bd2.position.Set(0.0f, 12.0f);
112  b2Body* body2 = m_world->CreateBody(&bd2);
113  body2->CreateFixture(&circle2, 5.0f);
114 
115  b2RevoluteJointDef jd2;
116  jd2.Initialize(ground, body2, bd2.position);
118 
119  b2BodyDef bd3;
120  bd3.type = b2_dynamicBody;
121  bd3.position.Set(2.5f, 12.0f);
122  b2Body* body3 = m_world->CreateBody(&bd3);
123  body3->CreateFixture(&box, 5.0f);
124 
126  jd3.Initialize(ground, body3, bd3.position, b2Vec2(0.0f, 1.0f));
127  jd3.lowerTranslation = -5.0f;
128  jd3.upperTranslation = 5.0f;
129  jd3.enableLimit = true;
130 
132 
133  b2GearJointDef jd4;
134  jd4.bodyA = body1;
135  jd4.bodyB = body2;
136  jd4.joint1 = m_joint1;
137  jd4.joint2 = m_joint2;
138  jd4.ratio = circle2.m_radius / circle1.m_radius;
140 
141  b2GearJointDef jd5;
142  jd5.bodyA = body2;
143  jd5.bodyB = body3;
144  jd5.joint1 = m_joint2;
145  jd5.joint2 = m_joint3;
146  jd5.ratio = -1.0f / circle2.m_radius;
148  }
149  }
150 
151  void Step(Settings& settings) override
152  {
153  Test::Step(settings);
154 
155  float ratio, value;
156 
157  ratio = m_joint4->GetRatio();
158  value = m_joint1->GetJointAngle() + ratio * m_joint2->GetJointAngle();
159  g_debugDraw.DrawString(5, m_textLine, "theta1 + %4.2f * theta2 = %4.2f", (float) ratio, (float) value);
161 
162  ratio = m_joint5->GetRatio();
163  value = m_joint2->GetJointAngle() + ratio * m_joint3->GetJointTranslation();
164  g_debugDraw.DrawString(5, m_textLine, "theta2 + %4.2f * delta = %4.2f", (float) ratio, (float) value);
166  }
167 
168  static Test* Create()
169  {
170  return new GearJoint;
171  }
172 
178 };
179 
180 static int testIndex = RegisterTest("Joints", "Gear", GearJoint::Create);
b2Joint * joint2
The second revolute/prismatic joint attached to the gear joint.
Definition: b2_gear_joint.h:45
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
void Step(Settings &settings) override
Definition: gear_joint.cpp:151
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
f
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
int32 m_textLine
Definition: test.h:127
b2GearJoint * m_joint4
Definition: gear_joint.cpp:176
Definition: test.h:80
b2PrismaticJoint * m_joint3
Definition: gear_joint.cpp:175
static int testIndex
Definition: gear_joint.cpp:180
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
float lowerTranslation
The lower translation limit, usually in meters.
static Test * Create()
Definition: gear_joint.cpp:168
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
void SetAsBox(float hx, float hy)
float GetRatio() const
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2RevoluteJoint * m_joint1
Definition: gear_joint.cpp:173
float m_radius
Definition: b2_shape.h:102
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
float GetJointAngle() const
Get the current joint angle in radians.
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
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)
float referenceAngle
The bodyB angle minus bodyA angle in the reference state (radians).
b2Joint * joint1
The first revolute/prismatic joint attached to the gear joint.
Definition: b2_gear_joint.h:42
int32 m_textIncrement
Definition: test.h:135
float GetAngle() const
Definition: b2_body.h:489
b2World * m_world
Definition: test.h:128
float upperTranslation
The upper translation limit, usually in meters.
b2Vec2 position
Definition: b2_body.h:78
b2Vec2 GetLocalPoint(const b2Vec2 &worldPoint) const
Definition: b2_body.h:571
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2RevoluteJoint * m_joint2
Definition: gear_joint.cpp:174
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
float GetJointTranslation() const
Get the current joint translation, usually in meters.
b2Body * bodyB
The second attached body.
Definition: b2_joint.h:92
b2GearJoint * m_joint5
Definition: gear_joint.cpp:177


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