slider_crank_1.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "test.h"
24 
25 // A basic slider crank created for GDC tutorial: Understanding Constraints
26 class SliderCrank1 : public Test
27 {
28 public:
30  {
31  b2Body* ground = NULL;
32  {
33  b2BodyDef bd;
34  bd.position.Set(0.0f, 17.0f);
35  ground = m_world->CreateBody(&bd);
36  }
37 
38  {
39  b2Body* prevBody = ground;
40 
41  // Define crank.
42  {
43  b2PolygonShape shape;
44  shape.SetAsBox(4.0f, 1.0f);
45 
46  b2BodyDef bd;
47  bd.type = b2_dynamicBody;
48  bd.position.Set(-8.0f, 20.0f);
49  b2Body* body = m_world->CreateBody(&bd);
50  body->CreateFixture(&shape, 2.0f);
51 
53  rjd.Initialize(prevBody, body, b2Vec2(-12.0f, 20.0f));
54  m_world->CreateJoint(&rjd);
55 
56  prevBody = body;
57  }
58 
59  // Define connecting rod
60  {
61  b2PolygonShape shape;
62  shape.SetAsBox(8.0f, 1.0f);
63 
64  b2BodyDef bd;
65  bd.type = b2_dynamicBody;
66  bd.position.Set(4.0f, 20.0f);
67  b2Body* body = m_world->CreateBody(&bd);
68  body->CreateFixture(&shape, 2.0f);
69 
71  rjd.Initialize(prevBody, body, b2Vec2(-4.0f, 20.0f));
72  m_world->CreateJoint(&rjd);
73 
74  prevBody = body;
75  }
76 
77  // Define piston
78  {
79  b2PolygonShape shape;
80  shape.SetAsBox(3.0f, 3.0f);
81 
82  b2BodyDef bd;
83  bd.type = b2_dynamicBody;
84  bd.fixedRotation = true;
85  bd.position.Set(12.0f, 20.0f);
86  b2Body* body = m_world->CreateBody(&bd);
87  body->CreateFixture(&shape, 2.0f);
88 
90  rjd.Initialize(prevBody, body, b2Vec2(12.0f, 20.0f));
91  m_world->CreateJoint(&rjd);
92 
94  pjd.Initialize(ground, body, b2Vec2(12.0f, 17.0f), b2Vec2(1.0f, 0.0f));
95  m_world->CreateJoint(&pjd);
96  }
97  }
98  }
99 
100  static Test* Create()
101  {
102  return new SliderCrank1;
103  }
104 };
105 
106 static int testIndex = RegisterTest("Examples", "Slider Crank 1", SliderCrank1::Create);
bool fixedRotation
Should this body be prevented from rotating? Useful for characters.
Definition: b2_body.h:109
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
f
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
Definition: test.h:80
A 2D column vector.
Definition: b2_math.h:41
void SetAsBox(float hx, float hy)
static Test * Create()
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor, const b2Vec2 &axis)
b2World * m_world
Definition: test.h:128
b2Vec2 position
Definition: b2_body.h:78
static int testIndex
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21