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

« back to all changes in this revision

Viewing changes to tests/box2d/Testbed/Tests/Confined.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) 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 CONFINED_H
 
20
#define CONFINED_H
 
21
 
 
22
class Confined : public Test
 
23
{
 
24
public:
 
25
 
 
26
        enum
 
27
        {
 
28
                e_columnCount = 0,
 
29
                e_rowCount = 0
 
30
        };
 
31
 
 
32
        Confined()
 
33
        {
 
34
                {
 
35
                        b2BodyDef bd;
 
36
                        b2Body* ground = m_world->CreateBody(&bd);
 
37
 
 
38
                        b2EdgeShape shape;
 
39
 
 
40
                        // Floor
 
41
                        shape.Set(b2Vec2(-10.0f, 0.0f), b2Vec2(10.0f, 0.0f));
 
42
                        ground->CreateFixture(&shape, 0.0f);
 
43
 
 
44
                        // Left wall
 
45
                        shape.Set(b2Vec2(-10.0f, 0.0f), b2Vec2(-10.0f, 20.0f));
 
46
                        ground->CreateFixture(&shape, 0.0f);
 
47
 
 
48
                        // Right wall
 
49
                        shape.Set(b2Vec2(10.0f, 0.0f), b2Vec2(10.0f, 20.0f));
 
50
                        ground->CreateFixture(&shape, 0.0f);
 
51
 
 
52
                        // Roof
 
53
                        shape.Set(b2Vec2(-10.0f, 20.0f), b2Vec2(10.0f, 20.0f));
 
54
                        ground->CreateFixture(&shape, 0.0f);
 
55
                }
 
56
 
 
57
                float32 radius = 0.5f;
 
58
                b2CircleShape shape;
 
59
                shape.m_p.SetZero();
 
60
                shape.m_radius = radius;
 
61
 
 
62
                b2FixtureDef fd;
 
63
                fd.shape = &shape;
 
64
                fd.density = 1.0f;
 
65
                fd.friction = 0.1f;
 
66
 
 
67
                for (int32 j = 0; j < e_columnCount; ++j)
 
68
                {
 
69
                        for (int i = 0; i < e_rowCount; ++i)
 
70
                        {
 
71
                                b2BodyDef bd;
 
72
                                bd.type = b2_dynamicBody;
 
73
                                bd.position.Set(-10.0f + (2.1f * j + 1.0f + 0.01f * i) * radius, (2.0f * i + 1.0f) * radius);
 
74
                                b2Body* body = m_world->CreateBody(&bd);
 
75
 
 
76
                                body->CreateFixture(&fd);
 
77
                        }
 
78
                }
 
79
 
 
80
                m_world->SetGravity(b2Vec2(0.0f, 0.0f));
 
81
        }
 
82
 
 
83
        void CreateCircle()
 
84
        {
 
85
                float32 radius = 2.0f;
 
86
                b2CircleShape shape;
 
87
                shape.m_p.SetZero();
 
88
                shape.m_radius = radius;
 
89
 
 
90
                b2FixtureDef fd;
 
91
                fd.shape = &shape;
 
92
                fd.density = 1.0f;
 
93
                fd.friction = 0.0f;
 
94
 
 
95
                b2Vec2 p(RandomFloat(), 3.0f + RandomFloat());
 
96
                b2BodyDef bd;
 
97
                bd.type = b2_dynamicBody;
 
98
                bd.position = p;
 
99
                //bd.allowSleep = false;
 
100
                b2Body* body = m_world->CreateBody(&bd);
 
101
 
 
102
                body->CreateFixture(&fd);
 
103
        }
 
104
 
 
105
        void Keyboard(unsigned char key)
 
106
        {
 
107
                switch (key)
 
108
                {
 
109
                case 'c':
 
110
                        CreateCircle();
 
111
                        break;
 
112
                }
 
113
        }
 
114
 
 
115
        void Step(Settings* settings)
 
116
        {
 
117
                bool sleeping = true;
 
118
                for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext())
 
119
                {
 
120
                        if (b->GetType() != b2_dynamicBody)
 
121
                        {
 
122
                                continue;
 
123
                        }
 
124
 
 
125
                        if (b->IsAwake())
 
126
                        {
 
127
                                sleeping = false;
 
128
                        }
 
129
                }
 
130
 
 
131
                if (m_stepCount == 180)
 
132
                {
 
133
                        m_stepCount += 0;
 
134
                }
 
135
 
 
136
                //if (sleeping)
 
137
                //{
 
138
                //      CreateCircle();
 
139
                //}
 
140
 
 
141
                Test::Step(settings);
 
142
 
 
143
                for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext())
 
144
                {
 
145
                        if (b->GetType() != b2_dynamicBody)
 
146
                        {
 
147
                                continue;
 
148
                        }
 
149
 
 
150
                        b2Vec2 p = b->GetPosition();
 
151
                        if (p.x <= -10.0f || 10.0f <= p.x || p.y <= 0.0f || 20.0f <= p.y)
 
152
                        {
 
153
                                p.x += 0.0;
 
154
                        }
 
155
                }
 
156
 
 
157
                m_debugDraw.DrawString(5, m_textLine, "Press 'c' to create a circle.");
 
158
                m_textLine += 15;
 
159
        }
 
160
 
 
161
        static Test* Create()
 
162
        {
 
163
                return new Confined;
 
164
        }
 
165
};
 
166
 
 
167
#endif