pulley_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 PulleyJoint : public Test
26 {
27 public:
29  {
30  float y = 16.0f;
31  float L = 12.0f;
32  float a = 1.0f;
33  float b = 2.0f;
34 
35  b2Body* ground = NULL;
36  {
37  b2BodyDef bd;
38  ground = m_world->CreateBody(&bd);
39 
40  b2CircleShape circle;
41  circle.m_radius = 2.0f;
42 
43  circle.m_p.Set(-10.0f, y + b + L);
44  ground->CreateFixture(&circle, 0.0f);
45 
46  circle.m_p.Set(10.0f, y + b + L);
47  ground->CreateFixture(&circle, 0.0f);
48  }
49 
50  {
51 
52  b2PolygonShape shape;
53  shape.SetAsBox(a, b);
54 
55  b2BodyDef bd;
56  bd.type = b2_dynamicBody;
57 
58  //bd.fixedRotation = true;
59  bd.position.Set(-10.0f, y);
60  b2Body* body1 = m_world->CreateBody(&bd);
61  body1->CreateFixture(&shape, 5.0f);
62 
63  bd.position.Set(10.0f, y);
64  b2Body* body2 = m_world->CreateBody(&bd);
65  body2->CreateFixture(&shape, 5.0f);
66 
67  b2PulleyJointDef pulleyDef;
68  b2Vec2 anchor1(-10.0f, y + b);
69  b2Vec2 anchor2(10.0f, y + b);
70  b2Vec2 groundAnchor1(-10.0f, y + b + L);
71  b2Vec2 groundAnchor2(10.0f, y + b + L);
72  pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f);
73 
74  m_joint1 = (b2PulleyJoint*)m_world->CreateJoint(&pulleyDef);
75  }
76  }
77 
78  void Step(Settings& settings) override
79  {
80  Test::Step(settings);
81 
82  float ratio = m_joint1->GetRatio();
83  float L = m_joint1->GetCurrentLengthA() + ratio * m_joint1->GetCurrentLengthB();
84  g_debugDraw.DrawString(5, m_textLine, "L1 + %4.2f * L2 = %4.2f", (float) ratio, (float) L);
86  }
87 
88  static Test* Create()
89  {
90  return new PulleyJoint;
91  }
92 
94 };
95 
96 static int testIndex = RegisterTest("Joints", "Pulley", PulleyJoint::Create);
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
f
int32 m_textLine
Definition: test.h:127
Definition: test.h:80
float GetCurrentLengthB() const
Get the current length of the segment attached to bodyB.
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
b2Vec2 m_p
Position.
void SetAsBox(float hx, float hy)
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
float m_radius
Definition: b2_shape.h:102
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 &groundAnchorA, const b2Vec2 &groundAnchorB, const b2Vec2 &anchorA, const b2Vec2 &anchorB, float ratio)
Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors.
int32 m_textIncrement
Definition: test.h:135
float GetRatio() const
Get the pulley ratio.
static Test * Create()
static int testIndex
b2World * m_world
Definition: test.h:128
b2Vec2 position
Definition: b2_body.h:78
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
void Step(Settings &settings) override
float GetCurrentLengthA() const
Get the current length of the segment attached to bodyA.
b2PulleyJoint * m_joint1
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115


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