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 CANTILEVER_H 20 #define CANTILEVER_H 21 22 // It is difficult to make a cantilever made of links completely rigid with weld joints. 23 // You will have to use a high number of iterations to make them stiff. 24 // So why not go ahead and use soft weld joints? They behave like a revolute 25 // joint with a rotational spring. 26 class Cantilever : public Test 27 { 28 public: 29 30 enum 31 { 32 e_count = 8 33 }; 34 Cantilever()35 Cantilever() 36 { 37 b2Body* ground = NULL; 38 { 39 b2BodyDef bd; 40 ground = m_world->CreateBody(&bd); 41 42 b2EdgeShape shape; 43 shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 44 ground->CreateFixture(&shape, 0.0f); 45 } 46 47 { 48 b2PolygonShape shape; 49 shape.SetAsBox(0.5f, 0.125f); 50 51 b2FixtureDef fd; 52 fd.shape = &shape; 53 fd.density = 20.0f; 54 55 b2WeldJointDef jd; 56 57 b2Body* prevBody = ground; 58 for (int32 i = 0; i < e_count; ++i) 59 { 60 b2BodyDef bd; 61 bd.type = b2_dynamicBody; 62 bd.position.Set(-14.5f + 1.0f * i, 5.0f); 63 b2Body* body = m_world->CreateBody(&bd); 64 body->CreateFixture(&fd); 65 66 b2Vec2 anchor(-15.0f + 1.0f * i, 5.0f); 67 jd.Initialize(prevBody, body, anchor); 68 m_world->CreateJoint(&jd); 69 70 prevBody = body; 71 } 72 } 73 74 { 75 b2PolygonShape shape; 76 shape.SetAsBox(1.0f, 0.125f); 77 78 b2FixtureDef fd; 79 fd.shape = &shape; 80 fd.density = 20.0f; 81 82 b2WeldJointDef jd; 83 jd.frequencyHz = 5.0f; 84 jd.dampingRatio = 0.7f; 85 86 b2Body* prevBody = ground; 87 for (int32 i = 0; i < 3; ++i) 88 { 89 b2BodyDef bd; 90 bd.type = b2_dynamicBody; 91 bd.position.Set(-14.0f + 2.0f * i, 15.0f); 92 b2Body* body = m_world->CreateBody(&bd); 93 body->CreateFixture(&fd); 94 95 b2Vec2 anchor(-15.0f + 2.0f * i, 15.0f); 96 jd.Initialize(prevBody, body, anchor); 97 m_world->CreateJoint(&jd); 98 99 prevBody = body; 100 } 101 } 102 103 { 104 b2PolygonShape shape; 105 shape.SetAsBox(0.5f, 0.125f); 106 107 b2FixtureDef fd; 108 fd.shape = &shape; 109 fd.density = 20.0f; 110 111 b2WeldJointDef jd; 112 113 b2Body* prevBody = ground; 114 for (int32 i = 0; i < e_count; ++i) 115 { 116 b2BodyDef bd; 117 bd.type = b2_dynamicBody; 118 bd.position.Set(-4.5f + 1.0f * i, 5.0f); 119 b2Body* body = m_world->CreateBody(&bd); 120 body->CreateFixture(&fd); 121 122 if (i > 0) 123 { 124 b2Vec2 anchor(-5.0f + 1.0f * i, 5.0f); 125 jd.Initialize(prevBody, body, anchor); 126 m_world->CreateJoint(&jd); 127 } 128 129 prevBody = body; 130 } 131 } 132 133 { 134 b2PolygonShape shape; 135 shape.SetAsBox(0.5f, 0.125f); 136 137 b2FixtureDef fd; 138 fd.shape = &shape; 139 fd.density = 20.0f; 140 141 b2WeldJointDef jd; 142 jd.frequencyHz = 8.0f; 143 jd.dampingRatio = 0.7f; 144 145 b2Body* prevBody = ground; 146 for (int32 i = 0; i < e_count; ++i) 147 { 148 b2BodyDef bd; 149 bd.type = b2_dynamicBody; 150 bd.position.Set(5.5f + 1.0f * i, 10.0f); 151 b2Body* body = m_world->CreateBody(&bd); 152 body->CreateFixture(&fd); 153 154 if (i > 0) 155 { 156 b2Vec2 anchor(5.0f + 1.0f * i, 10.0f); 157 jd.Initialize(prevBody, body, anchor); 158 m_world->CreateJoint(&jd); 159 } 160 161 prevBody = body; 162 } 163 } 164 165 for (int32 i = 0; i < 2; ++i) 166 { 167 b2Vec2 vertices[3]; 168 vertices[0].Set(-0.5f, 0.0f); 169 vertices[1].Set(0.5f, 0.0f); 170 vertices[2].Set(0.0f, 1.5f); 171 172 b2PolygonShape shape; 173 shape.Set(vertices, 3); 174 175 b2FixtureDef fd; 176 fd.shape = &shape; 177 fd.density = 1.0f; 178 179 b2BodyDef bd; 180 bd.type = b2_dynamicBody; 181 bd.position.Set(-8.0f + 8.0f * i, 12.0f); 182 b2Body* body = m_world->CreateBody(&bd); 183 body->CreateFixture(&fd); 184 } 185 186 for (int32 i = 0; i < 2; ++i) 187 { 188 b2CircleShape shape; 189 shape.m_radius = 0.5f; 190 191 b2FixtureDef fd; 192 fd.shape = &shape; 193 fd.density = 1.0f; 194 195 b2BodyDef bd; 196 bd.type = b2_dynamicBody; 197 bd.position.Set(-6.0f + 6.0f * i, 10.0f); 198 b2Body* body = m_world->CreateBody(&bd); 199 body->CreateFixture(&fd); 200 } 201 } 202 Create()203 static Test* Create() 204 { 205 return new Cantilever; 206 } 207 208 b2Body* m_middle; 209 }; 210 211 #endif 212