~ubuntu-branches/ubuntu/saucy/emscripten/saucy-proposed

« back to all changes in this revision

Viewing changes to tests/box2d/Testbed/Tests/Car.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-2011 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 CAR_H
 
20
#define CAR_H
 
21
 
 
22
// This is a fun demo that shows off the wheel joint
 
23
class Car : public Test
 
24
{
 
25
public:
 
26
        Car()
 
27
        {               
 
28
                m_hz = 4.0f;
 
29
                m_zeta = 0.7f;
 
30
                m_speed = 50.0f;
 
31
 
 
32
                b2Body* ground = NULL;
 
33
                {
 
34
                        b2BodyDef bd;
 
35
                        ground = m_world->CreateBody(&bd);
 
36
 
 
37
                        b2EdgeShape shape;
 
38
 
 
39
                        b2FixtureDef fd;
 
40
                        fd.shape = &shape;
 
41
                        fd.density = 0.0f;
 
42
                        fd.friction = 0.6f;
 
43
 
 
44
                        shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
 
45
                        ground->CreateFixture(&fd);
 
46
 
 
47
                        float32 hs[10] = {0.25f, 1.0f, 4.0f, 0.0f, 0.0f, -1.0f, -2.0f, -2.0f, -1.25f, 0.0f};
 
48
 
 
49
                        float32 x = 20.0f, y1 = 0.0f, dx = 5.0f;
 
50
 
 
51
                        for (int32 i = 0; i < 10; ++i)
 
52
                        {
 
53
                                float32 y2 = hs[i];
 
54
                                shape.Set(b2Vec2(x, y1), b2Vec2(x + dx, y2));
 
55
                                ground->CreateFixture(&fd);
 
56
                                y1 = y2;
 
57
                                x += dx;
 
58
                        }
 
59
 
 
60
                        for (int32 i = 0; i < 10; ++i)
 
61
                        {
 
62
                                float32 y2 = hs[i];
 
63
                                shape.Set(b2Vec2(x, y1), b2Vec2(x + dx, y2));
 
64
                                ground->CreateFixture(&fd);
 
65
                                y1 = y2;
 
66
                                x += dx;
 
67
                        }
 
68
 
 
69
                        shape.Set(b2Vec2(x, 0.0f), b2Vec2(x + 40.0f, 0.0f));
 
70
                        ground->CreateFixture(&fd);
 
71
 
 
72
                        x += 80.0f;
 
73
                        shape.Set(b2Vec2(x, 0.0f), b2Vec2(x + 40.0f, 0.0f));
 
74
                        ground->CreateFixture(&fd);
 
75
 
 
76
                        x += 40.0f;
 
77
                        shape.Set(b2Vec2(x, 0.0f), b2Vec2(x + 10.0f, 5.0f));
 
78
                        ground->CreateFixture(&fd);
 
79
 
 
80
                        x += 20.0f;
 
81
                        shape.Set(b2Vec2(x, 0.0f), b2Vec2(x + 40.0f, 0.0f));
 
82
                        ground->CreateFixture(&fd);
 
83
 
 
84
                        x += 40.0f;
 
85
                        shape.Set(b2Vec2(x, 0.0f), b2Vec2(x, 20.0f));
 
86
                        ground->CreateFixture(&fd);
 
87
                }
 
88
 
 
89
                // Teeter
 
90
                {
 
91
                        b2BodyDef bd;
 
92
                        bd.position.Set(140.0f, 1.0f);
 
93
                        bd.type = b2_dynamicBody;
 
94
                        b2Body* body = m_world->CreateBody(&bd);
 
95
 
 
96
                        b2PolygonShape box;
 
97
                        box.SetAsBox(10.0f, 0.25f);
 
98
                        body->CreateFixture(&box, 1.0f);
 
99
 
 
100
                        b2RevoluteJointDef jd;
 
101
                        jd.Initialize(ground, body, body->GetPosition());
 
102
                        jd.lowerAngle = -8.0f * b2_pi / 180.0f;
 
103
                        jd.upperAngle = 8.0f * b2_pi / 180.0f;
 
104
                        jd.enableLimit = true;
 
105
                        m_world->CreateJoint(&jd);
 
106
 
 
107
                        body->ApplyAngularImpulse(100.0f);
 
108
                }
 
109
 
 
110
                // Bridge
 
111
                {
 
112
                        int32 N = 20;
 
113
                        b2PolygonShape shape;
 
114
                        shape.SetAsBox(1.0f, 0.125f);
 
115
 
 
116
                        b2FixtureDef fd;
 
117
                        fd.shape = &shape;
 
118
                        fd.density = 1.0f;
 
119
                        fd.friction = 0.6f;
 
120
 
 
121
                        b2RevoluteJointDef jd;
 
122
 
 
123
                        b2Body* prevBody = ground;
 
124
                        for (int32 i = 0; i < N; ++i)
 
125
                        {
 
126
                                b2BodyDef bd;
 
127
                                bd.type = b2_dynamicBody;
 
128
                                bd.position.Set(161.0f + 2.0f * i, -0.125f);
 
129
                                b2Body* body = m_world->CreateBody(&bd);
 
130
                                body->CreateFixture(&fd);
 
131
 
 
132
                                b2Vec2 anchor(160.0f + 2.0f * i, -0.125f);
 
133
                                jd.Initialize(prevBody, body, anchor);
 
134
                                m_world->CreateJoint(&jd);
 
135
 
 
136
                                prevBody = body;
 
137
                        }
 
138
 
 
139
                        b2Vec2 anchor(160.0f + 2.0f * N, -0.125f);
 
140
                        jd.Initialize(prevBody, ground, anchor);
 
141
                        m_world->CreateJoint(&jd);
 
142
                }
 
143
 
 
144
                // Boxes
 
145
                {
 
146
                        b2PolygonShape box;
 
147
                        box.SetAsBox(0.5f, 0.5f);
 
148
 
 
149
                        b2Body* body = NULL;
 
150
                        b2BodyDef bd;
 
151
                        bd.type = b2_dynamicBody;
 
152
 
 
153
                        bd.position.Set(230.0f, 0.5f);
 
154
                        body = m_world->CreateBody(&bd);
 
155
                        body->CreateFixture(&box, 0.5f);
 
156
 
 
157
                        bd.position.Set(230.0f, 1.5f);
 
158
                        body = m_world->CreateBody(&bd);
 
159
                        body->CreateFixture(&box, 0.5f);
 
160
 
 
161
                        bd.position.Set(230.0f, 2.5f);
 
162
                        body = m_world->CreateBody(&bd);
 
163
                        body->CreateFixture(&box, 0.5f);
 
164
 
 
165
                        bd.position.Set(230.0f, 3.5f);
 
166
                        body = m_world->CreateBody(&bd);
 
167
                        body->CreateFixture(&box, 0.5f);
 
168
 
 
169
                        bd.position.Set(230.0f, 4.5f);
 
170
                        body = m_world->CreateBody(&bd);
 
171
                        body->CreateFixture(&box, 0.5f);
 
172
                }
 
173
 
 
174
                // Car
 
175
                {
 
176
                        b2PolygonShape chassis;
 
177
                        b2Vec2 vertices[8];
 
178
                        vertices[0].Set(-1.5f, -0.5f);
 
179
                        vertices[1].Set(1.5f, -0.5f);
 
180
                        vertices[2].Set(1.5f, 0.0f);
 
181
                        vertices[3].Set(0.0f, 0.9f);
 
182
                        vertices[4].Set(-1.15f, 0.9f);
 
183
                        vertices[5].Set(-1.5f, 0.2f);
 
184
                        chassis.Set(vertices, 6);
 
185
 
 
186
                        b2CircleShape circle;
 
187
                        circle.m_radius = 0.4f;
 
188
 
 
189
                        b2BodyDef bd;
 
190
                        bd.type = b2_dynamicBody;
 
191
                        bd.position.Set(0.0f, 1.0f);
 
192
                        m_car = m_world->CreateBody(&bd);
 
193
                        m_car->CreateFixture(&chassis, 1.0f);
 
194
 
 
195
                        b2FixtureDef fd;
 
196
                        fd.shape = &circle;
 
197
                        fd.density = 1.0f;
 
198
                        fd.friction = 0.9f;
 
199
 
 
200
                        bd.position.Set(-1.0f, 0.35f);
 
201
                        m_wheel1 = m_world->CreateBody(&bd);
 
202
                        m_wheel1->CreateFixture(&fd);
 
203
 
 
204
                        bd.position.Set(1.0f, 0.4f);
 
205
                        m_wheel2 = m_world->CreateBody(&bd);
 
206
                        m_wheel2->CreateFixture(&fd);
 
207
 
 
208
                        b2WheelJointDef jd;
 
209
                        b2Vec2 axis(0.0f, 1.0f);
 
210
 
 
211
                        jd.Initialize(m_car, m_wheel1, m_wheel1->GetPosition(), axis);
 
212
                        jd.motorSpeed = 0.0f;
 
213
                        jd.maxMotorTorque = 20.0f;
 
214
                        jd.enableMotor = true;
 
215
                        jd.frequencyHz = m_hz;
 
216
                        jd.dampingRatio = m_zeta;
 
217
                        m_spring1 = (b2WheelJoint*)m_world->CreateJoint(&jd);
 
218
 
 
219
                        jd.Initialize(m_car, m_wheel2, m_wheel2->GetPosition(), axis);
 
220
                        jd.motorSpeed = 0.0f;
 
221
                        jd.maxMotorTorque = 10.0f;
 
222
                        jd.enableMotor = false;
 
223
                        jd.frequencyHz = m_hz;
 
224
                        jd.dampingRatio = m_zeta;
 
225
                        m_spring2 = (b2WheelJoint*)m_world->CreateJoint(&jd);
 
226
                }
 
227
        }
 
228
 
 
229
        void Keyboard(unsigned char key)
 
230
        {
 
231
                switch (key)
 
232
                {
 
233
                case 'a':
 
234
                        m_spring1->SetMotorSpeed(m_speed);
 
235
                        break;
 
236
 
 
237
                case 's':
 
238
                        m_spring1->SetMotorSpeed(0.0f);
 
239
                        break;
 
240
 
 
241
                case 'd':
 
242
                        m_spring1->SetMotorSpeed(-m_speed);
 
243
                        break;
 
244
 
 
245
                case 'q':
 
246
                        m_hz = b2Max(0.0f, m_hz - 1.0f);
 
247
                        m_spring1->SetSpringFrequencyHz(m_hz);
 
248
                        m_spring2->SetSpringFrequencyHz(m_hz);
 
249
                        break;
 
250
 
 
251
                case 'e':
 
252
                        m_hz += 1.0f;
 
253
                        m_spring1->SetSpringFrequencyHz(m_hz);
 
254
                        m_spring2->SetSpringFrequencyHz(m_hz);
 
255
                        break;
 
256
                }
 
257
        }
 
258
 
 
259
        void Step(Settings* settings)
 
260
        {
 
261
                m_debugDraw.DrawString(5, m_textLine, "Keys: left = a, brake = s, right = d, hz down = q, hz up = e");
 
262
                m_textLine += 15;
 
263
                m_debugDraw.DrawString(5, m_textLine, "frequency = %g hz, damping ratio = %g", m_hz, m_zeta);
 
264
                m_textLine += 15;
 
265
 
 
266
                settings->viewCenter.x = m_car->GetPosition().x;
 
267
                Test::Step(settings);
 
268
        }
 
269
 
 
270
        static Test* Create()
 
271
        {
 
272
                return new Car;
 
273
        }
 
274
 
 
275
        b2Body* m_car;
 
276
        b2Body* m_wheel1;
 
277
        b2Body* m_wheel2;
 
278
 
 
279
        float32 m_hz;
 
280
        float32 m_zeta;
 
281
        float32 m_speed;
 
282
        b2WheelJoint* m_spring1;
 
283
        b2WheelJoint* m_spring2;
 
284
};
 
285
 
 
286
#endif