1 /*
2 * Copyright (c) 2006-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 PRISMATIC_H
20 #define PRISMATIC_H
21 
22 // The motor in this test gets smoother with higher velocity iterations.
23 class Prismatic : public Test
24 {
25 public:
Prismatic()26     Prismatic()
27     {
28         b2Body* ground = NULL;
29         {
30             b2BodyDef bd;
31             ground = m_world->CreateBody(&bd);
32 
33             b2EdgeShape shape;
34             shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
35             ground->CreateFixture(&shape, 0.0f);
36         }
37 
38         {
39             b2PolygonShape shape;
40             shape.SetAsBox(2.0f, 0.5f);
41 
42             b2BodyDef bd;
43             bd.type = b2_dynamicBody;
44             bd.position.Set(-10.0f, 10.0f);
45             bd.angle = 0.5f * b2_pi;
46             bd.allowSleep = false;
47             b2Body* body = m_world->CreateBody(&bd);
48             body->CreateFixture(&shape, 5.0f);
49 
50             b2PrismaticJointDef pjd;
51 
52             // Bouncy limit
53             b2Vec2 axis(2.0f, 1.0f);
54             axis.Normalize();
55             pjd.Initialize(ground, body, b2Vec2(0.0f, 0.0f), axis);
56 
57             // Non-bouncy limit
58             //pjd.Initialize(ground, body, b2Vec2(-10.0f, 10.0f), b2Vec2(1.0f, 0.0f));
59 
60             pjd.motorSpeed = 10.0f;
61             pjd.maxMotorForce = 10000.0f;
62             pjd.enableMotor = true;
63             pjd.lowerTranslation = 0.0f;
64             pjd.upperTranslation = 20.0f;
65             pjd.enableLimit = true;
66 
67             m_joint = (b2PrismaticJoint*)m_world->CreateJoint(&pjd);
68         }
69     }
70 
Keyboard(unsigned char key)71     void Keyboard(unsigned char key)
72     {
73         switch (key)
74         {
75         case 'l':
76             m_joint->EnableLimit(!m_joint->IsLimitEnabled());
77             break;
78 
79         case 'm':
80             m_joint->EnableMotor(!m_joint->IsMotorEnabled());
81             break;
82 
83         case 's':
84             m_joint->SetMotorSpeed(-m_joint->GetMotorSpeed());
85             break;
86         }
87     }
88 
Step(Settings * settings)89     void Step(Settings* settings)
90     {
91         Test::Step(settings);
92         m_debugDraw.DrawString(5, m_textLine, "Keys: (l) limits, (m) motors, (s) speed");
93         m_textLine += 15;
94         float32 force = m_joint->GetMotorForce(settings->hz);
95         m_debugDraw.DrawString(5, m_textLine, "Motor Force = %4.0f", (float) force);
96         m_textLine += 15;
97     }
98 
Create()99     static Test* Create()
100     {
101         return new Prismatic;
102     }
103 
104     b2PrismaticJoint* m_joint;
105 };
106 
107 #endif
108