~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/box2d/Testbed/Tests/RopeJoint.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* Copyright (c) 2006-2010 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 ROPE_JOINT_H
 
20
#define ROPE_JOINT_H
 
21
 
 
22
/// This test shows how a rope joint can be used to stabilize a chain of
 
23
/// bodies with a heavy payload. Notice that the rope joint just prevents
 
24
/// excessive stretching and has no other effect.
 
25
/// By disabling the rope joint you can see that the Box2D solver has trouble
 
26
/// supporting heavy bodies with light bodies. Try playing around with the
 
27
/// densities, time step, and iterations to see how they affect stability.
 
28
/// This test also shows how to use contact filtering. Filtering is configured
 
29
/// so that the payload does not collide with the chain.
 
30
class RopeJoint : public Test
 
31
{
 
32
public:
 
33
        RopeJoint()
 
34
        {
 
35
                b2Body* ground = NULL;
 
36
                {
 
37
                        b2BodyDef bd;
 
38
                        ground = m_world->CreateBody(&bd);
 
39
 
 
40
                        b2EdgeShape shape;
 
41
                        shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
 
42
                        ground->CreateFixture(&shape, 0.0f);
 
43
                }
 
44
 
 
45
                {
 
46
                        b2PolygonShape shape;
 
47
                        shape.SetAsBox(0.5f, 0.125f);
 
48
 
 
49
                        b2FixtureDef fd;
 
50
                        fd.shape = &shape;
 
51
                        fd.density = 20.0f;
 
52
                        fd.friction = 0.2f;
 
53
                        fd.filter.categoryBits = 0x0001;
 
54
                        fd.filter.maskBits = 0xFFFF & ~0x0002;
 
55
 
 
56
                        b2RevoluteJointDef jd;
 
57
                        jd.collideConnected = false;
 
58
 
 
59
                        const int32 N = 10;
 
60
                        const float32 y = 15.0f;
 
61
                        m_ropeDef.localAnchorA.Set(0.0f, y);
 
62
 
 
63
                        b2Body* prevBody = ground;
 
64
                        for (int32 i = 0; i < N; ++i)
 
65
                        {
 
66
                                b2BodyDef bd;
 
67
                                bd.type = b2_dynamicBody;
 
68
                                bd.position.Set(0.5f + 1.0f * i, y);
 
69
                                if (i == N - 1)
 
70
                                {
 
71
                                        shape.SetAsBox(1.5f, 1.5f);
 
72
                                        fd.density = 100.0f;
 
73
                                        fd.filter.categoryBits = 0x0002;
 
74
                                        bd.position.Set(1.0f * i, y);
 
75
                                        bd.angularDamping = 0.4f;
 
76
                                }
 
77
 
 
78
                                b2Body* body = m_world->CreateBody(&bd);
 
79
 
 
80
                                body->CreateFixture(&fd);
 
81
 
 
82
                                b2Vec2 anchor(float32(i), y);
 
83
                                jd.Initialize(prevBody, body, anchor);
 
84
                                m_world->CreateJoint(&jd);
 
85
 
 
86
                                prevBody = body;
 
87
                        }
 
88
 
 
89
                        m_ropeDef.localAnchorB.SetZero();
 
90
 
 
91
                        float32 extraLength = 0.01f;
 
92
                        m_ropeDef.maxLength = N - 1.0f + extraLength;
 
93
                        m_ropeDef.bodyB = prevBody;
 
94
                }
 
95
 
 
96
                {
 
97
                        m_ropeDef.bodyA = ground;
 
98
                        m_rope = m_world->CreateJoint(&m_ropeDef);
 
99
                }
 
100
        }
 
101
 
 
102
        void Keyboard(unsigned char key)
 
103
        {
 
104
                switch (key)
 
105
                {
 
106
                case 'j':
 
107
                        if (m_rope)
 
108
                        {
 
109
                                m_world->DestroyJoint(m_rope);
 
110
                                m_rope = NULL;
 
111
                        }
 
112
                        else
 
113
                        {
 
114
                                m_rope = m_world->CreateJoint(&m_ropeDef);
 
115
                        }
 
116
                        break;
 
117
                }
 
118
        }
 
119
 
 
120
        void Step(Settings* settings)
 
121
        {
 
122
                Test::Step(settings);
 
123
                m_debugDraw.DrawString(5, m_textLine, "Press (j) to toggle the rope joint.");
 
124
                m_textLine += 15;
 
125
                if (m_rope)
 
126
                {
 
127
                        m_debugDraw.DrawString(5, m_textLine, "Rope ON");
 
128
                }
 
129
                else
 
130
                {
 
131
                        m_debugDraw.DrawString(5, m_textLine, "Rope OFF");
 
132
                }
 
133
                m_textLine += 15;
 
134
        }
 
135
 
 
136
        static Test* Create()
 
137
        {
 
138
                return new RopeJoint;
 
139
        }
 
140
 
 
141
        b2RopeJointDef m_ropeDef;
 
142
        b2Joint* m_rope;
 
143
};
 
144
 
 
145
#endif