2
* Copyright (c) 2006-2009 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.
19
// Inspired by a contribution by roman_m
20
// Dimensions scooped from APE (http://www.cove.org/ape/index.htm)
25
class TheoJansen : public Test
29
void CreateLeg(float32 s, const b2Vec2& wheelAnchor)
31
b2Vec2 p1(5.4f * s, -6.1f);
32
b2Vec2 p2(7.2f * s, -1.2f);
33
b2Vec2 p3(4.3f * s, -1.9f);
34
b2Vec2 p4(3.1f * s, 0.8f);
35
b2Vec2 p5(6.0f * s, 1.5f);
36
b2Vec2 p6(2.5f * s, 3.7f);
38
b2FixtureDef fd1, fd2;
39
fd1.filter.groupIndex = -1;
40
fd2.filter.groupIndex = -1;
44
b2PolygonShape poly1, poly2;
53
poly1.Set(vertices, 3);
55
vertices[0] = b2Vec2_zero;
56
vertices[1] = p5 - p4;
57
vertices[2] = p6 - p4;
58
poly2.Set(vertices, 3);
67
poly1.Set(vertices, 3);
69
vertices[0] = b2Vec2_zero;
70
vertices[1] = p6 - p4;
71
vertices[2] = p5 - p4;
72
poly2.Set(vertices, 3);
79
bd1.type = b2_dynamicBody;
80
bd2.type = b2_dynamicBody;
81
bd1.position = m_offset;
82
bd2.position = p4 + m_offset;
84
bd1.angularDamping = 10.0f;
85
bd2.angularDamping = 10.0f;
87
b2Body* body1 = m_world->CreateBody(&bd1);
88
b2Body* body2 = m_world->CreateBody(&bd2);
90
body1->CreateFixture(&fd1);
91
body2->CreateFixture(&fd2);
93
b2DistanceJointDef djd;
95
// Using a soft distance constraint can reduce some jitter.
96
// It also makes the structure seem a bit more fluid by
97
// acting like a suspension system.
98
djd.dampingRatio = 0.5f;
99
djd.frequencyHz = 10.0f;
101
djd.Initialize(body1, body2, p2 + m_offset, p5 + m_offset);
102
m_world->CreateJoint(&djd);
104
djd.Initialize(body1, body2, p3 + m_offset, p4 + m_offset);
105
m_world->CreateJoint(&djd);
107
djd.Initialize(body1, m_wheel, p3 + m_offset, wheelAnchor + m_offset);
108
m_world->CreateJoint(&djd);
110
djd.Initialize(body2, m_wheel, p6 + m_offset, wheelAnchor + m_offset);
111
m_world->CreateJoint(&djd);
113
b2RevoluteJointDef rjd;
115
rjd.Initialize(body2, m_chassis, p4 + m_offset);
116
m_world->CreateJoint(&rjd);
121
m_offset.Set(0.0f, 8.0f);
124
b2Vec2 pivot(0.0f, 0.8f);
129
b2Body* ground = m_world->CreateBody(&bd);
132
shape.Set(b2Vec2(-50.0f, 0.0f), b2Vec2(50.0f, 0.0f));
133
ground->CreateFixture(&shape, 0.0f);
135
shape.Set(b2Vec2(-50.0f, 0.0f), b2Vec2(-50.0f, 10.0f));
136
ground->CreateFixture(&shape, 0.0f);
138
shape.Set(b2Vec2(50.0f, 0.0f), b2Vec2(50.0f, 10.0f));
139
ground->CreateFixture(&shape, 0.0f);
143
for (int32 i = 0; i < 40; ++i)
146
shape.m_radius = 0.25f;
149
bd.type = b2_dynamicBody;
150
bd.position.Set(-40.0f + 2.0f * i, 0.5f);
152
b2Body* body = m_world->CreateBody(&bd);
153
body->CreateFixture(&shape, 1.0f);
158
b2PolygonShape shape;
159
shape.SetAsBox(2.5f, 1.0f);
164
sd.filter.groupIndex = -1;
166
bd.type = b2_dynamicBody;
167
bd.position = pivot + m_offset;
168
m_chassis = m_world->CreateBody(&bd);
169
m_chassis->CreateFixture(&sd);
174
shape.m_radius = 1.6f;
179
sd.filter.groupIndex = -1;
181
bd.type = b2_dynamicBody;
182
bd.position = pivot + m_offset;
183
m_wheel = m_world->CreateBody(&bd);
184
m_wheel->CreateFixture(&sd);
188
b2RevoluteJointDef jd;
189
jd.Initialize(m_wheel, m_chassis, pivot + m_offset);
190
jd.collideConnected = false;
191
jd.motorSpeed = m_motorSpeed;
192
jd.maxMotorTorque = 400.0f;
193
jd.enableMotor = m_motorOn;
194
m_motorJoint = (b2RevoluteJoint*)m_world->CreateJoint(&jd);
199
wheelAnchor = pivot + b2Vec2(0.0f, -0.8f);
201
CreateLeg(-1.0f, wheelAnchor);
202
CreateLeg(1.0f, wheelAnchor);
204
m_wheel->SetTransform(m_wheel->GetPosition(), 120.0f * b2_pi / 180.0f);
205
CreateLeg(-1.0f, wheelAnchor);
206
CreateLeg(1.0f, wheelAnchor);
208
m_wheel->SetTransform(m_wheel->GetPosition(), -120.0f * b2_pi / 180.0f);
209
CreateLeg(-1.0f, wheelAnchor);
210
CreateLeg(1.0f, wheelAnchor);
213
void Step(Settings* settings)
215
m_debugDraw.DrawString(5, m_textLine, "Keys: left = a, brake = s, right = d, toggle motor = m");
218
Test::Step(settings);
221
void Keyboard(unsigned char key)
226
m_motorJoint->SetMotorSpeed(-m_motorSpeed);
230
m_motorJoint->SetMotorSpeed(0.0f);
234
m_motorJoint->SetMotorSpeed(m_motorSpeed);
238
m_motorJoint->EnableMotor(!m_motorJoint->IsMotorEnabled());
243
static Test* Create()
245
return new TheoJansen;
251
b2RevoluteJoint* m_motorJoint;
253
float32 m_motorSpeed;
256
#endif // THEO_JANSEN_H