2
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
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.
22
// This is a fun demo that shows off the wheel joint
23
class Car : public Test
32
b2Body* ground = NULL;
35
ground = m_world->CreateBody(&bd);
44
shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
45
ground->CreateFixture(&fd);
47
float32 hs[10] = {0.25f, 1.0f, 4.0f, 0.0f, 0.0f, -1.0f, -2.0f, -2.0f, -1.25f, 0.0f};
49
float32 x = 20.0f, y1 = 0.0f, dx = 5.0f;
51
for (int32 i = 0; i < 10; ++i)
54
shape.Set(b2Vec2(x, y1), b2Vec2(x + dx, y2));
55
ground->CreateFixture(&fd);
60
for (int32 i = 0; i < 10; ++i)
63
shape.Set(b2Vec2(x, y1), b2Vec2(x + dx, y2));
64
ground->CreateFixture(&fd);
69
shape.Set(b2Vec2(x, 0.0f), b2Vec2(x + 40.0f, 0.0f));
70
ground->CreateFixture(&fd);
73
shape.Set(b2Vec2(x, 0.0f), b2Vec2(x + 40.0f, 0.0f));
74
ground->CreateFixture(&fd);
77
shape.Set(b2Vec2(x, 0.0f), b2Vec2(x + 10.0f, 5.0f));
78
ground->CreateFixture(&fd);
81
shape.Set(b2Vec2(x, 0.0f), b2Vec2(x + 40.0f, 0.0f));
82
ground->CreateFixture(&fd);
85
shape.Set(b2Vec2(x, 0.0f), b2Vec2(x, 20.0f));
86
ground->CreateFixture(&fd);
92
bd.position.Set(140.0f, 1.0f);
93
bd.type = b2_dynamicBody;
94
b2Body* body = m_world->CreateBody(&bd);
97
box.SetAsBox(10.0f, 0.25f);
98
body->CreateFixture(&box, 1.0f);
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);
107
body->ApplyAngularImpulse(100.0f);
113
b2PolygonShape shape;
114
shape.SetAsBox(1.0f, 0.125f);
121
b2RevoluteJointDef jd;
123
b2Body* prevBody = ground;
124
for (int32 i = 0; i < N; ++i)
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);
132
b2Vec2 anchor(160.0f + 2.0f * i, -0.125f);
133
jd.Initialize(prevBody, body, anchor);
134
m_world->CreateJoint(&jd);
139
b2Vec2 anchor(160.0f + 2.0f * N, -0.125f);
140
jd.Initialize(prevBody, ground, anchor);
141
m_world->CreateJoint(&jd);
147
box.SetAsBox(0.5f, 0.5f);
151
bd.type = b2_dynamicBody;
153
bd.position.Set(230.0f, 0.5f);
154
body = m_world->CreateBody(&bd);
155
body->CreateFixture(&box, 0.5f);
157
bd.position.Set(230.0f, 1.5f);
158
body = m_world->CreateBody(&bd);
159
body->CreateFixture(&box, 0.5f);
161
bd.position.Set(230.0f, 2.5f);
162
body = m_world->CreateBody(&bd);
163
body->CreateFixture(&box, 0.5f);
165
bd.position.Set(230.0f, 3.5f);
166
body = m_world->CreateBody(&bd);
167
body->CreateFixture(&box, 0.5f);
169
bd.position.Set(230.0f, 4.5f);
170
body = m_world->CreateBody(&bd);
171
body->CreateFixture(&box, 0.5f);
176
b2PolygonShape chassis;
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);
186
b2CircleShape circle;
187
circle.m_radius = 0.4f;
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);
200
bd.position.Set(-1.0f, 0.35f);
201
m_wheel1 = m_world->CreateBody(&bd);
202
m_wheel1->CreateFixture(&fd);
204
bd.position.Set(1.0f, 0.4f);
205
m_wheel2 = m_world->CreateBody(&bd);
206
m_wheel2->CreateFixture(&fd);
209
b2Vec2 axis(0.0f, 1.0f);
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);
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);
229
void Keyboard(unsigned char key)
234
m_spring1->SetMotorSpeed(m_speed);
238
m_spring1->SetMotorSpeed(0.0f);
242
m_spring1->SetMotorSpeed(-m_speed);
246
m_hz = b2Max(0.0f, m_hz - 1.0f);
247
m_spring1->SetSpringFrequencyHz(m_hz);
248
m_spring2->SetSpringFrequencyHz(m_hz);
253
m_spring1->SetSpringFrequencyHz(m_hz);
254
m_spring2->SetSpringFrequencyHz(m_hz);
259
void Step(Settings* settings)
261
m_debugDraw.DrawString(5, m_textLine, "Keys: left = a, brake = s, right = d, hz down = q, hz up = e");
263
m_debugDraw.DrawString(5, m_textLine, "frequency = %g hz, damping ratio = %g", m_hz, m_zeta);
266
settings->viewCenter.x = m_car->GetPosition().x;
267
Test::Step(settings);
270
static Test* Create()
282
b2WheelJoint* m_spring1;
283
b2WheelJoint* m_spring2;