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
#ifndef SLIDER_CRANK_H
20
#define SLIDER_CRANK_H
22
// A motor driven slider crank with joint friction.
24
class SliderCrank : public Test
29
b2Body* ground = NULL;
32
ground = m_world->CreateBody(&bd);
35
shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
36
ground->CreateFixture(&shape, 0.0f);
40
b2Body* prevBody = ground;
45
shape.SetAsBox(0.5f, 2.0f);
48
bd.type = b2_dynamicBody;
49
bd.position.Set(0.0f, 7.0f);
50
b2Body* body = m_world->CreateBody(&bd);
51
body->CreateFixture(&shape, 2.0f);
53
b2RevoluteJointDef rjd;
54
rjd.Initialize(prevBody, body, b2Vec2(0.0f, 5.0f));
55
rjd.motorSpeed = 1.0f * b2_pi;
56
rjd.maxMotorTorque = 10000.0f;
57
rjd.enableMotor = true;
58
m_joint1 = (b2RevoluteJoint*)m_world->CreateJoint(&rjd);
66
shape.SetAsBox(0.5f, 4.0f);
69
bd.type = b2_dynamicBody;
70
bd.position.Set(0.0f, 13.0f);
71
b2Body* body = m_world->CreateBody(&bd);
72
body->CreateFixture(&shape, 2.0f);
74
b2RevoluteJointDef rjd;
75
rjd.Initialize(prevBody, body, b2Vec2(0.0f, 9.0f));
76
rjd.enableMotor = false;
77
m_world->CreateJoint(&rjd);
85
shape.SetAsBox(1.5f, 1.5f);
88
bd.type = b2_dynamicBody;
89
bd.fixedRotation = true;
90
bd.position.Set(0.0f, 17.0f);
91
b2Body* body = m_world->CreateBody(&bd);
92
body->CreateFixture(&shape, 2.0f);
94
b2RevoluteJointDef rjd;
95
rjd.Initialize(prevBody, body, b2Vec2(0.0f, 17.0f));
96
m_world->CreateJoint(&rjd);
98
b2PrismaticJointDef pjd;
99
pjd.Initialize(ground, body, b2Vec2(0.0f, 17.0f), b2Vec2(0.0f, 1.0f));
101
pjd.maxMotorForce = 1000.0f;
102
pjd.enableMotor = true;
104
m_joint2 = (b2PrismaticJoint*)m_world->CreateJoint(&pjd);
109
b2PolygonShape shape;
110
shape.SetAsBox(1.5f, 1.5f);
113
bd.type = b2_dynamicBody;
114
bd.position.Set(0.0f, 23.0f);
115
b2Body* body = m_world->CreateBody(&bd);
116
body->CreateFixture(&shape, 2.0f);
121
void Keyboard(unsigned char key)
126
m_joint2->EnableMotor(!m_joint2->IsMotorEnabled());
127
m_joint2->GetBodyB()->SetAwake(true);
131
m_joint1->EnableMotor(!m_joint1->IsMotorEnabled());
132
m_joint1->GetBodyB()->SetAwake(true);
137
void Step(Settings* settings)
139
Test::Step(settings);
140
m_debugDraw.DrawString(5, m_textLine, "Keys: (f) toggle friction, (m) toggle motor");
142
float32 torque = m_joint1->GetMotorTorque(settings->hz);
143
m_debugDraw.DrawString(5, m_textLine, "Motor Torque = %5.0f", (float) torque);
147
static Test* Create()
149
return new SliderCrank;
152
b2RevoluteJoint* m_joint1;
153
b2PrismaticJoint* m_joint2;