1 /*
2 * Copyright (c) 2007-2009 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #ifndef PULLEYS_H
20 #define PULLEYS_H
21 
22 class Pulleys : public Test
23 {
24 public:
Pulleys()25     Pulleys()
26     {
27         float32 y = 16.0f;
28         float32 L = 12.0f;
29         float32 a = 1.0f;
30         float32 b = 2.0f;
31 
32         b2Body* ground = NULL;
33         {
34             b2BodyDef bd;
35             ground = m_world->CreateBody(&bd);
36 
37             b2EdgeShape edge;
38             edge.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
39             //ground->CreateFixture(&shape, 0.0f);
40 
41             b2CircleShape circle;
42             circle.m_radius = 2.0f;
43 
44             circle.m_p.Set(-10.0f, y + b + L);
45             ground->CreateFixture(&circle, 0.0f);
46 
47             circle.m_p.Set(10.0f, y + b + L);
48             ground->CreateFixture(&circle, 0.0f);
49         }
50 
51         {
52 
53             b2PolygonShape shape;
54             shape.SetAsBox(a, b);
55 
56             b2BodyDef bd;
57             bd.type = b2_dynamicBody;
58 
59             //bd.fixedRotation = true;
60             bd.position.Set(-10.0f, y);
61             b2Body* body1 = m_world->CreateBody(&bd);
62             body1->CreateFixture(&shape, 5.0f);
63 
64             bd.position.Set(10.0f, y);
65             b2Body* body2 = m_world->CreateBody(&bd);
66             body2->CreateFixture(&shape, 5.0f);
67 
68             b2PulleyJointDef pulleyDef;
69             b2Vec2 anchor1(-10.0f, y + b);
70             b2Vec2 anchor2(10.0f, y + b);
71             b2Vec2 groundAnchor1(-10.0f, y + b + L);
72             b2Vec2 groundAnchor2(10.0f, y + b + L);
73             pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f);
74 
75             m_joint1 = (b2PulleyJoint*)m_world->CreateJoint(&pulleyDef);
76         }
77     }
78 
Keyboard(unsigned char key)79     void Keyboard(unsigned char key)
80     {
81         switch (key)
82         {
83         case 0:
84             break;
85         }
86     }
87 
Step(Settings * settings)88     void Step(Settings* settings)
89     {
90         Test::Step(settings);
91 
92         float32 ratio = m_joint1->GetRatio();
93         float32 L = m_joint1->GetLengthA() + ratio * m_joint1->GetLengthB();
94         m_debugDraw.DrawString(5, m_textLine, "L1 + %4.2f * L2 = %4.2f", (float) ratio, (float) L);
95         m_textLine += 15;
96     }
97 
Create()98     static Test* Create()
99     {
100         return new Pulleys;
101     }
102 
103     b2PulleyJoint* m_joint1;
104 };
105 
106 #endif
107