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 DOMINOS_H 20 #define DOMINOS_H 21 22 class Dominos : public Test 23 { 24 public: 25 Dominos()26 Dominos() 27 { 28 b2Body* b1; 29 { 30 b2EdgeShape shape; 31 shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 32 33 b2BodyDef bd; 34 b1 = m_world->CreateBody(&bd); 35 b1->CreateFixture(&shape, 0.0f); 36 } 37 38 { 39 b2PolygonShape shape; 40 shape.SetAsBox(6.0f, 0.25f); 41 42 b2BodyDef bd; 43 bd.position.Set(-1.5f, 10.0f); 44 b2Body* ground = m_world->CreateBody(&bd); 45 ground->CreateFixture(&shape, 0.0f); 46 } 47 48 { 49 b2PolygonShape shape; 50 shape.SetAsBox(0.1f, 1.0f); 51 52 b2FixtureDef fd; 53 fd.shape = &shape; 54 fd.density = 20.0f; 55 fd.friction = 0.1f; 56 57 for (int i = 0; i < 10; ++i) 58 { 59 b2BodyDef bd; 60 bd.type = b2_dynamicBody; 61 bd.position.Set(-6.0f + 1.0f * i, 11.25f); 62 b2Body* body = m_world->CreateBody(&bd); 63 body->CreateFixture(&fd); 64 } 65 } 66 67 { 68 b2PolygonShape shape; 69 shape.SetAsBox(7.0f, 0.25f, b2Vec2_zero, 0.3f); 70 71 b2BodyDef bd; 72 bd.position.Set(1.0f, 6.0f); 73 b2Body* ground = m_world->CreateBody(&bd); 74 ground->CreateFixture(&shape, 0.0f); 75 } 76 77 b2Body* b2; 78 { 79 b2PolygonShape shape; 80 shape.SetAsBox(0.25f, 1.5f); 81 82 b2BodyDef bd; 83 bd.position.Set(-7.0f, 4.0f); 84 b2 = m_world->CreateBody(&bd); 85 b2->CreateFixture(&shape, 0.0f); 86 } 87 88 b2Body* b3; 89 { 90 b2PolygonShape shape; 91 shape.SetAsBox(6.0f, 0.125f); 92 93 b2BodyDef bd; 94 bd.type = b2_dynamicBody; 95 bd.position.Set(-0.9f, 1.0f); 96 bd.angle = -0.15f; 97 98 b3 = m_world->CreateBody(&bd); 99 b3->CreateFixture(&shape, 10.0f); 100 } 101 102 b2RevoluteJointDef jd; 103 b2Vec2 anchor; 104 105 anchor.Set(-2.0f, 1.0f); 106 jd.Initialize(b1, b3, anchor); 107 jd.collideConnected = true; 108 m_world->CreateJoint(&jd); 109 110 b2Body* b4; 111 { 112 b2PolygonShape shape; 113 shape.SetAsBox(0.25f, 0.25f); 114 115 b2BodyDef bd; 116 bd.type = b2_dynamicBody; 117 bd.position.Set(-10.0f, 15.0f); 118 b4 = m_world->CreateBody(&bd); 119 b4->CreateFixture(&shape, 10.0f); 120 } 121 122 anchor.Set(-7.0f, 15.0f); 123 jd.Initialize(b2, b4, anchor); 124 m_world->CreateJoint(&jd); 125 126 b2Body* b5; 127 { 128 b2BodyDef bd; 129 bd.type = b2_dynamicBody; 130 bd.position.Set(6.5f, 3.0f); 131 b5 = m_world->CreateBody(&bd); 132 133 b2PolygonShape shape; 134 b2FixtureDef fd; 135 136 fd.shape = &shape; 137 fd.density = 10.0f; 138 fd.friction = 0.1f; 139 140 shape.SetAsBox(1.0f, 0.1f, b2Vec2(0.0f, -0.9f), 0.0f); 141 b5->CreateFixture(&fd); 142 143 shape.SetAsBox(0.1f, 1.0f, b2Vec2(-0.9f, 0.0f), 0.0f); 144 b5->CreateFixture(&fd); 145 146 shape.SetAsBox(0.1f, 1.0f, b2Vec2(0.9f, 0.0f), 0.0f); 147 b5->CreateFixture(&fd); 148 } 149 150 anchor.Set(6.0f, 2.0f); 151 jd.Initialize(b1, b5, anchor); 152 m_world->CreateJoint(&jd); 153 154 b2Body* b6; 155 { 156 b2PolygonShape shape; 157 shape.SetAsBox(1.0f, 0.1f); 158 159 b2BodyDef bd; 160 bd.type = b2_dynamicBody; 161 bd.position.Set(6.5f, 4.1f); 162 b6 = m_world->CreateBody(&bd); 163 b6->CreateFixture(&shape, 30.0f); 164 } 165 166 anchor.Set(7.5f, 4.0f); 167 jd.Initialize(b5, b6, anchor); 168 m_world->CreateJoint(&jd); 169 170 b2Body* b7; 171 { 172 b2PolygonShape shape; 173 shape.SetAsBox(0.1f, 1.0f); 174 175 b2BodyDef bd; 176 bd.type = b2_dynamicBody; 177 bd.position.Set(7.4f, 1.0f); 178 179 b7 = m_world->CreateBody(&bd); 180 b7->CreateFixture(&shape, 10.0f); 181 } 182 183 b2DistanceJointDef djd; 184 djd.bodyA = b3; 185 djd.bodyB = b7; 186 djd.localAnchorA.Set(6.0f, 0.0f); 187 djd.localAnchorB.Set(0.0f, -1.0f); 188 b2Vec2 d = djd.bodyB->GetWorldPoint(djd.localAnchorB) - djd.bodyA->GetWorldPoint(djd.localAnchorA); 189 djd.length = d.Length(); 190 m_world->CreateJoint(&djd); 191 192 { 193 float32 radius = 0.2f; 194 195 b2CircleShape shape; 196 shape.m_radius = radius; 197 198 for (int i = 0; i < 4; ++i) 199 { 200 b2BodyDef bd; 201 bd.type = b2_dynamicBody; 202 bd.position.Set(5.9f + 2.0f * radius * i, 2.4f); 203 b2Body* body = m_world->CreateBody(&bd); 204 body->CreateFixture(&shape, 10.0f); 205 } 206 } 207 } 208 Create()209 static Test* Create() 210 { 211 return new Dominos; 212 } 213 }; 214 215 #endif 216