1 /* 2 * Copyright (c) 2006-2009 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 BRIDGE_H 20 #define BRIDGE_H 21 22 class Bridge : public Test 23 { 24 public: 25 26 enum 27 { 28 e_count = 30 29 }; 30 Bridge()31 Bridge() 32 { 33 b2Body* ground = NULL; 34 { 35 b2BodyDef bd; 36 ground = m_world->CreateBody(&bd); 37 38 b2EdgeShape shape; 39 shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 40 ground->CreateFixture(&shape, 0.0f); 41 } 42 43 { 44 b2PolygonShape shape; 45 shape.SetAsBox(0.5f, 0.125f); 46 47 b2FixtureDef fd; 48 fd.shape = &shape; 49 fd.density = 20.0f; 50 fd.friction = 0.2f; 51 52 b2RevoluteJointDef jd; 53 54 b2Body* prevBody = ground; 55 for (int32 i = 0; i < e_count; ++i) 56 { 57 b2BodyDef bd; 58 bd.type = b2_dynamicBody; 59 bd.position.Set(-14.5f + 1.0f * i, 5.0f); 60 b2Body* body = m_world->CreateBody(&bd); 61 body->CreateFixture(&fd); 62 63 b2Vec2 anchor(-15.0f + 1.0f * i, 5.0f); 64 jd.Initialize(prevBody, body, anchor); 65 m_world->CreateJoint(&jd); 66 67 if (i == (e_count >> 1)) 68 { 69 m_middle = body; 70 } 71 prevBody = body; 72 } 73 74 b2Vec2 anchor(-15.0f + 1.0f * e_count, 5.0f); 75 jd.Initialize(prevBody, ground, anchor); 76 m_world->CreateJoint(&jd); 77 } 78 79 for (int32 i = 0; i < 2; ++i) 80 { 81 b2Vec2 vertices[3]; 82 vertices[0].Set(-0.5f, 0.0f); 83 vertices[1].Set(0.5f, 0.0f); 84 vertices[2].Set(0.0f, 1.5f); 85 86 b2PolygonShape shape; 87 shape.Set(vertices, 3); 88 89 b2FixtureDef fd; 90 fd.shape = &shape; 91 fd.density = 1.0f; 92 93 b2BodyDef bd; 94 bd.type = b2_dynamicBody; 95 bd.position.Set(-8.0f + 8.0f * i, 12.0f); 96 b2Body* body = m_world->CreateBody(&bd); 97 body->CreateFixture(&fd); 98 } 99 100 for (int32 i = 0; i < 3; ++i) 101 { 102 b2CircleShape shape; 103 shape.m_radius = 0.5f; 104 105 b2FixtureDef fd; 106 fd.shape = &shape; 107 fd.density = 1.0f; 108 109 b2BodyDef bd; 110 bd.type = b2_dynamicBody; 111 bd.position.Set(-6.0f + 6.0f * i, 10.0f); 112 b2Body* body = m_world->CreateBody(&bd); 113 body->CreateFixture(&fd); 114 } 115 } 116 Create()117 static Test* Create() 118 { 119 return new Bridge; 120 } 121 122 b2Body* m_middle; 123 }; 124 125 #endif 126