1 /*
2 * Copyright (c) 2011 Erin Catto http://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 ROPE_H
20 #define ROPE_H
21 
22 ///
23 class Rope : public Test
24 {
25 public:
Rope()26     Rope()
27     {
28         const int32 N = 40;
29         b2Vec2 vertices[N];
30         float32 masses[N];
31 
32         for (int32 i = 0; i < N; ++i)
33         {
34             vertices[i].Set(0.0f, 20.0f - 0.25f * i);
35             masses[i] = 1.0f;
36         }
37         masses[0] = 0.0f;
38         masses[1] = 0.0f;
39 
40         b2RopeDef def;
41         def.vertices = vertices;
42         def.count = N;
43         def.gravity.Set(0.0f, -10.0f);
44         def.masses = masses;
45         def.damping = 0.1f;
46         def.k2 = 1.0f;
47         def.k3 = 0.5f;
48 
49         m_rope.Initialize(&def);
50 
51         m_angle = 0.0f;
52         m_rope.SetAngle(m_angle);
53     }
54 
Keyboard(unsigned char key)55     void Keyboard(unsigned char key)
56     {
57         switch (key)
58         {
59         case 'q':
60             m_angle = b2Max(-b2_pi, m_angle - 0.05f * b2_pi);
61             m_rope.SetAngle(m_angle);
62             break;
63 
64         case 'e':
65             m_angle = b2Min(b2_pi, m_angle + 0.05f * b2_pi);
66             m_rope.SetAngle(m_angle);
67             break;
68         }
69     }
70 
Step(Settings * settings)71     void Step(Settings* settings)
72     {
73         float32 dt = settings->hz > 0.0f ? 1.0f / settings->hz : 0.0f;
74 
75         if (settings->pause == 1 && settings->singleStep == 0)
76         {
77             dt = 0.0f;
78         }
79 
80         m_rope.Step(dt, 1);
81 
82         Test::Step(settings);
83 
84         m_rope.Draw(&m_debugDraw);
85 
86         m_debugDraw.DrawString(5, m_textLine, "Press (q,e) to adjust target angle");
87         m_textLine += 15;
88         m_debugDraw.DrawString(5, m_textLine, "Target angle = %g degrees", m_angle * 180.0f / b2_pi);
89         m_textLine += 15;
90     }
91 
Create()92     static Test* Create()
93     {
94         return new Rope;
95     }
96 
97     b2Rope m_rope;
98     float32 m_angle;
99 };
100 
101 #endif
102