Rube Goldberg Machine 1.0
This is the base code for Rube Goldberg designed for the CS296 Software Systems Lab
|
00001 //Documenting the Main Documentation page. 00002 00011 /* 00012 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 00013 * This software is provided 'as-is', without any express or implied 00014 * warranty. In no event will the authors be held liable for any damages 00015 * arising from the use of this software. 00016 * Permission is granted to anyone to use this software for any purpose, 00017 * including commercial applications, and to alter it and redistribute it 00018 * freely, subject to the following restrictions: 00019 * 1. The origin of this software must not be misrepresented; you must not 00020 * claim that you wrote the original software. If you use this software 00021 * in a product, an acknowledgment in the product documentation would be 00022 * appreciated but is not required. 00023 * 2. Altered source versions must be plainly marked as such, and must not be 00024 * misrepresented as being the original software. 00025 * 3. This notice may not be removed or altered from any source distribution. 00026 */ 00027 00028 /* 00029 * Base code for CS 296 Software Systems Lab 00030 * Department of Computer Science and Engineering, IIT Bombay 00031 * Instructor: Parag Chaudhuri 00032 */ 00033 00034 00035 00036 #include "cs296_base.hpp" 00037 #include "render.hpp" 00038 00039 #ifdef __APPLE__ 00040 #include <GLUT/glut.h> 00041 #else 00042 #include "GL/freeglut.h" 00043 #endif 00044 00045 #include <cstring> 00046 using namespace std; 00047 00048 #include "dominos.hpp" 00049 00050 00051 namespace cs296 00052 { 00053 00054 dominos_t::dominos_t() 00055 { 00071 00072 //The two Grounds of the World. 00073 b2Body* b1; 00074 { 00075 //There is a hole in the higher ground where the sphere lodges itself 00076 { 00077 b2EdgeShape shape; // line segment used to define the higher ground. 00078 shape.Set(b2Vec2(-90.0f, 0.0f), b2Vec2(-20.0f, 0.0f)); // The end points of the ground is set. by the function set(). The parameters 00079 // specified are the end points of the ground. 00080 00081 b2BodyDef bd; //Used to define the body. 00082 b1 = m_world->CreateBody(&bd); // Body created in the Box2D world. 00083 b1->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. 00084 } 00085 { 00086 b2EdgeShape shape; // line segment used to define the higher ground. 00087 shape.Set(b2Vec2(-10.0f, 0.0f), b2Vec2(90.0f, 0.0f)); // The end points of the ground is set. by the function set(). The parameters 00088 // specified are the end points of the ground. 00089 00090 b2BodyDef bd; //Used to define the body. 00091 b1 = m_world->CreateBody(&bd); // Body created in the Box2D world. 00092 b1->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. 00093 } 00094 { 00095 b2EdgeShape shape; // line segment used to define the lower ground. 00096 shape.Set(b2Vec2(-90.0f, -2.0f), b2Vec2(90.0f, -2.0f)); // The end points of the ground is set. by the function set(). The parameters 00097 // specified are the end points of the ground. 00098 00099 b2BodyDef bd; //Used to define the body. 00100 b1 = m_world->CreateBody(&bd); // Body created in the Box2D world. 00101 b1->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. 00102 } 00103 } 00105 //Top horizontal shelf 00106 { 00107 b2PolygonShape shape; // used to define a polygon shape. 00108 shape.SetAsBox(6.0f, 0.25f); // Create a box of half-length 6f and half-width 0.25f 00109 00110 b2BodyDef bd; // Used to define the shelf. 00111 bd.position.Set(-30.5f, 30.0f); // Set the position of the shelf. 00112 b2Body* ground = m_world->CreateBody(&bd); // Create the shelf in the Box2D world. 00113 ground->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. 00114 } 00115 00116 00118 //The trigger balloon which rises in the beginning of the Simulation. 00119 { 00120 b2Body* sbody; // The heavy sphere object 00121 b2CircleShape circle; // Defining the shape of the sphere(circle) 00122 circle.m_radius = 1.0; // radius of the sphere 00123 00124 b2FixtureDef ballfd; // FIxture of the heavy sphere 00125 ballfd.shape = &circle; // Assigning the shape to the fixture 00126 ballfd.density = 1.0f; // Setting the density 00127 ballfd.friction = 0.0f; // Frictionless ball 00128 ballfd.restitution = 0.0f; // Collision of Restitution = 0 (Inelastic Collision) 00129 b2BodyDef ballbd; // Defining the heavy sphere 00130 ballbd.type = b2_dynamicBody; // Setting the sphere to dynamic 00131 ballbd.position.Set(-38.5f, 22.0f); 00132 sbody = m_world->CreateBody(&ballbd); // Creating the sphere in the world 00133 sbody -> SetGravityScale(-0.2f); // Setting the Gravity Scale(SInce its a balloon, it has negative gravity to rise) 00134 sbody->CreateFixture(&ballfd); // Assigning the fixture to the sphere. 00135 } 00136 00138 //The first revolving horizontal platform which is hit by the balloon. 00139 { 00140 //The Plank which revolves. 00141 b2PolygonShape shape; // Creating a shape for the platform. 00142 shape.SetAsBox(2.4f, 0.2f); // Defining the dimensions of the platform 00143 b2BodyDef bd; 00144 bd.position.Set(-37.0f, 35.0f); // Setting position of the platform. 00145 bd.type = b2_dynamicBody; // Setting type to dynamic 00146 b2Body* body = m_world->CreateBody(&bd); // Creating the platform in the world 00147 b2FixtureDef *fd = new b2FixtureDef; // Creating a new Fixture to set the platform's physical properties. 00148 fd->density = 1.f; // Setting density of the platform. 00149 fd->shape = new b2PolygonShape; 00150 fd->shape = &shape; 00151 body->CreateFixture(fd); // Assigning fixture to the body. 00152 00153 // Creating an invisible hinge to rotate the platform: Static object. 00154 b2PolygonShape shape2; 00155 shape2.SetAsBox(0.2f, 1.0f); //Dimensions of the hinge. 00156 b2BodyDef bd2; 00157 bd2.position.Set(-37.0, 35.0f); 00158 b2Body* body2 = m_world->CreateBody(&bd2); //Static Object,so default fixture. 00159 00160 // Revolute joint between the plank and the hinge. 00161 b2RevoluteJointDef jointDef; // Creating a revolute joint to rotate the platform 00162 jointDef.bodyA = body; // by joining the bar and the hinge. 00163 jointDef.bodyB = body2; 00164 jointDef.localAnchorA.Set(0,0); // Setting Local Anchor 00165 jointDef.localAnchorB.Set(0,0); 00166 jointDef.collideConnected = false; // The bodies will not collide with each other 00167 m_world->CreateJoint(&jointDef); // Creating the joint in the world. 00168 } 00169 00171 //The ball that falls on the dominoes 00172 { 00173 b2Body* spherebody; // Creating the spheres 00174 b2CircleShape circle; // Defining the shape of the sphere 00175 circle.m_radius = 1.0; // Circle of radius 1.0f 00176 00177 b2FixtureDef ballfd; // Defining the fixture of the sphere 00178 ballfd.shape = &circle; // Assigning the shape to the fixture 00179 ballfd.density = 1.0f; // Assigning the density 00180 ballfd.friction = 0.0f; // Assigning the friction 00181 00182 ballfd.restitution = 0.0f; 00183 b2BodyDef ballbd; // Defining the sphere 00184 00185 ballbd.type = b2_dynamicBody; // Setting the type to dynamic 00186 00187 ballbd.position.Set(-37.0f, 37.0f); // Setting the position of the spheres 00188 00189 spherebody = m_world->CreateBody(&ballbd); // Creating the spheres in the Box2D world 00190 spherebody->CreateFixture(&ballfd); // Creating the spheres with fixture defined 00191 00192 00193 } 00194 00196 //Dominos on the horizontal shelf which topple one after the other. 00197 { 00198 b2PolygonShape shape; // used to define a polygon shape. 00199 shape.SetAsBox(0.1f, 1.0f); // Create the domino of half-width 0.1f and half-height 1.0f 00200 00201 b2FixtureDef fd; // Create a fixture object to change the properties of the domino(dynamic body in motion) 00202 fd.shape = &shape; // Set the shape of the fixture. 00203 fd.density = 20.0f; // Set the density of the fixture. 00204 fd.friction = 0.1f; // Set the friction of the fixture. 00205 00206 for (int i = 1; i < 10; ++i) // For-loop for the 10 dominoes 00207 { 00208 b2BodyDef bd; // Used to define the body. 00209 bd.type = b2_dynamicBody; // Set the type of the domino to dynamic( so that it detects collision) 00210 bd.position.Set(-35.5f + 1.0f * i, 31.25f); // set the position of the domino 00211 b2Body* body = m_world->CreateBody(&bd); // Create the domino in the Box2D world. 00212 body->CreateFixture(&fd); // Create the domino fixture as set above. 00213 } 00214 } 00215 00217 //Newton's Pendulums which are triggered by the Dominos. 00218 { 00219 //Creating the ten pendulums. 00220 for( int i = 0; i < 10 ; i++) 00221 { 00222 00223 // the top support of the pendulum 00224 b2Body* b2; 00225 { 00226 b2PolygonShape shape; // Defining the shape of the support 00227 shape.SetAsBox(0.25f, 0.5f); // Setting the box dimensions. 00228 00229 b2BodyDef bd; 00230 bd.position.Set(-25.5f + 0.5*i, 35.0f); // Set the position of the body 00231 b2 = m_world->CreateBody(&bd); // Creating the top support in the world. 00232 b2->CreateFixture(&shape, 10.0f); // Creating the fixture of the top support 00233 } 00234 00235 //Bob of the Pendulum 00236 b2Body* b4; // the bob of the pendulum 00237 { 00238 b2PolygonShape shape; // Defining the shape of the bob. 00239 shape.SetAsBox(0.25f, 0.25f); // Setting the bob dimensions. 00240 00241 00242 00243 b2BodyDef bd; 00244 bd.type = b2_dynamicBody; // The bob type is set to dynamic. 00245 bd.position.Set(-25.5f + 0.5*i, 31.0f); // The positions of the bob are set. 00246 b4 = m_world->CreateBody(&bd); // Creating the bob in the Box2D world. 00247 b4->CreateFixture(&shape, 2.0f); // Creating the fixture of the bob. 00248 } 00249 00250 //The revolute joint between the bob and the support. 00251 b2RevoluteJointDef jd; 00252 b2Vec2 anchor; // Creating the anchor that links the bob and the base support. Center of rotation 00253 anchor.Set(-25.5f + 0.5*i, 35.0f); 00254 jd.Initialize(b2, b4, anchor); // Creating the revolute joint. The bob can only revolve about the anchor 00255 m_world->CreateJoint(&jd); // Creating the joint in the world. 00256 } 00257 } 00258 00259 00261 // Sphere hit by newton's Pendulum which resides on the platform and falls on the platform 00262 { 00263 //The shelf. 00264 { 00265 b2PolygonShape shape; // used to define a polygon shape. 00266 shape.SetAsBox(1.0f, 0.025f); // Create a box of half-length 6f and half-width 0.25f 00267 00268 b2BodyDef bd; // Used to define the shelf. 00269 bd.position.Set(-17.0f, 31.0f); // Set the position of the shelf. 00270 b2Body* ground = m_world->CreateBody(&bd); // Create the shelf in the Box2D world. 00271 ground->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. 00272 } 00273 //The sphere on the shelf. 00274 { 00275 b2Body* spherebody; // Creating the spheres 00276 b2CircleShape circle; // Defining the shape of the sphere 00277 circle.m_radius = 2.0; // Circle of radius 2.0f 00278 00279 b2FixtureDef ballfd; // Defining the fixture of the sphere 00280 ballfd.shape = &circle; // Assigning the shape to the fixture 00281 ballfd.density = 1.0f; // Assigning the density 00282 ballfd.friction = 0.0f; // Assigning the friction 00283 00284 ballfd.restitution = 0.0f; 00285 b2BodyDef ballbd; // Defining the sphere 00286 00287 ballbd.type = b2_dynamicBody; // Setting the type to dynamic 00288 00289 ballbd.position.Set(-16.0f, 32.0f); // Setting the position of the spheres 00290 00291 spherebody = m_world->CreateBody(&ballbd); // Creating the spheres in the Box2D world 00292 spherebody->CreateFixture(&ballfd); // Creating the spheres with fixture defined 00293 00294 spherebody-> SetGravityScale(1.0f); //Normal Garvity Scale. 00295 00296 } 00297 } 00298 00300 //The see-saw system at the bottom. Contains a triangular wedge and a plank balanced on top of the wedge. 00301 { 00302 //The triangle wedge 00303 b2Body* sbody; 00304 b2PolygonShape poly; // Defining a polygon shape by defining the points. 00305 b2Vec2 vertices[3]; //Using an array of vertices to define the three endpoints of the triangle 00306 vertices[0].Set(-5,0); 00307 vertices[1].Set(5,0); 00308 vertices[2].Set(0,7.5); 00309 poly.Set(vertices, 3); // Setting the shape of the triangle wedge. 00310 b2FixtureDef wedgefd; // Defining the fixture for the wedge. 00311 wedgefd.shape = &poly; // Assigning the shape to the fixture 00312 wedgefd.density = 10.0f; // Setting the density 00313 wedgefd.friction = 0.0f; // Frictionless wedge 00314 wedgefd.restitution = 0.0f; 00315 b2BodyDef wedgebd; 00316 wedgebd.position.Set(-4.0f, 0.0f); 00317 sbody = m_world->CreateBody(&wedgebd); // Creating the wedge in the world. Static Body 00318 sbody->CreateFixture(&wedgefd); // Assigning the fixture to the wedge 00319 00320 // The plank on top of the wedge 00321 b2PolygonShape shape; // Defining the shape of the plank 00322 shape.SetAsBox(15.0f, 0.2f); // Setting the dimensions 00323 b2BodyDef bd2; 00324 bd2.position.Set(-4.0f, 7.0f); 00325 bd2.type = b2_dynamicBody; // Setting the plank to dynamic body. 00326 b2Body* body = m_world->CreateBody(&bd2); // Creating the plank in the world 00327 b2FixtureDef *fd2 = new b2FixtureDef; // Creating the fixture of the plank 00328 fd2->density = 1.f; 00329 fd2->shape = new b2PolygonShape; // Defining the shape of the plank 00330 fd2->shape = &shape; // Passing the shape by reference. 00331 body->CreateFixture(fd2); 00332 00333 b2RevoluteJointDef jd; // Creating a revolute joint between the wedge and plank 00334 b2Vec2 anchor; 00335 anchor.Set(-4.0f, 7.0f); // the center of rotation is set to the center of the plank 00336 jd.Initialize(sbody, body, anchor); // Initializing the revolute joint. 00337 m_world->CreateJoint(&jd); 00338 } 00339 00342 //The pulley system. Contains an open box on one side, which resides on a revolving platform. 00343 //On the other side of the pulley is the lid to an open box which contains a balloon. 00344 { 00345 b2BodyDef *bd = new b2BodyDef; // Creating a new Body definition 00346 bd->type = b2_dynamicBody; // Setting the pulley system to be dynamic. 00347 bd->position.Set(14,15); 00348 bd->fixedRotation = true; // Setting fixed rotation to true. The pulley system will not rotate. 00349 00350 //The open box 00351 //Creating the box by 3 planks which form the open box. 00352 //The three fixtures are assigned to same body object to create an open box 00353 00354 // Plank 1 00355 b2FixtureDef *fd1 = new b2FixtureDef; // Creating a fixture of the plank. 00356 fd1->density = 10.0; // Assigning the properties to the box(density) 00357 fd1->friction = 0.5; // Friction 00358 fd1->restitution = 0.f; // Restitution 00359 fd1->shape = new b2PolygonShape; // Creating a new b2PolygonShape for the fixture fd1 00360 b2PolygonShape bs1; 00361 bs1.SetAsBox(2,0.2, b2Vec2(0.f,-1.9f), 0); 00362 fd1->shape = &bs1; // Assigning reference to the shape defined above. 00363 00364 // Plank 2 00365 b2FixtureDef *fd2 = new b2FixtureDef; 00366 fd2->density = 10.0; 00367 fd2->friction = 0.5; 00368 fd2->restitution = 0.f; 00369 fd2->shape = new b2PolygonShape; 00370 b2PolygonShape bs2; 00371 bs2.SetAsBox(0.2,2, b2Vec2(2.0f,0.f), 0); 00372 fd2->shape = &bs2; 00373 00374 // Plank 3 00375 b2FixtureDef *fd3 = new b2FixtureDef; 00376 fd3->density = 10.0; 00377 fd3->friction = 0.5; 00378 fd3->restitution = 0.f; 00379 fd3->shape = new b2PolygonShape; 00380 b2PolygonShape bs3; 00381 bs3.SetAsBox(0.2,2, b2Vec2(-2.0f,0.f), 0); 00382 fd3->shape = &bs3; 00383 00384 00385 b2Body* box1 = m_world->CreateBody(bd); // Creating the body in the Box2D world. 00386 //Assigning the three fixtures to box1 to create a open box 00387 box1->CreateFixture(fd1); 00388 box1->CreateFixture(fd2); 00389 box1->CreateFixture(fd3); 00390 00391 //The bar which acts a lid to the box with the balloon. 00392 bd->position.Set(30,20); // Setting the position of the bar 00393 fd1->density = 34.0; 00394 b2Body* box2 = m_world->CreateBody(bd); // Creating the bar in the world 00395 b2FixtureDef *fd4 = new b2FixtureDef; // Creating a fixture of the bar. 00396 fd4->density = 10.0; // Assigning the properties to the bar(density) 00397 fd4->friction = 0.5; // Friction 00398 fd4->restitution = 0.f; // Restitution 00399 fd4->shape = new b2PolygonShape; 00400 b2PolygonShape bs4; 00401 bs4.SetAsBox(5,0.2, b2Vec2(0.f,-1.9f), 0); 00402 fd4->shape = &bs4; 00403 box2->CreateFixture(fd4); 00404 00405 //Shelf on which the box lies 00406 { 00407 b2PolygonShape shape; // used to define a polygon shape. 00408 shape.SetAsBox(2.0f, 0.25f); // Create a box 00409 b2BodyDef bd; // Used to define the shelf. 00410 bd.position.Set(30.0f, 14.0f); // Set the position of the shelf. 00411 b2Body* ground = m_world->CreateBody(&bd); // Create the shelf in the Box2D world. 00412 ground->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. 00413 } 00414 00415 // The pulley joint 00416 b2PulleyJointDef* myjoint = new b2PulleyJointDef(); // Creating a pulley joint. 00417 //b2Vec2 worldAnchorOnBody1(0, 15); // Anchor point on body 1 in world axis 00418 //b2Vec2 worldAnchorOnBody2(15, 15); // Anchor point on body 2 in world axis 00419 b2Vec2 worldAnchorGround1(14, 20); // Anchor point for ground 1 in world axis 00420 b2Vec2 worldAnchorGround2(20, 20); // Anchor point for ground 2 in world axis 00421 float32 ratio = 1.0f; // Define ratio 00422 myjoint->Initialize(box1, box2, worldAnchorGround1, worldAnchorGround2, box1->GetWorldCenter(), box2->GetWorldCenter(), ratio); 00423 m_world->CreateJoint(myjoint); // Initializing the joint with box1, box2 and the coordinates. 00424 } 00425 00427 //The center revolving horizontal platform on which the box connected to the pulley rests. 00428 { 00429 b2PolygonShape shape; 00430 shape.SetAsBox(2.4f, 0.2f); 00431 00432 b2BodyDef bd; 00433 bd.position.Set(12.25f, 12.0f); 00434 bd.type = b2_dynamicBody; // Setting type to dynamic 00435 b2Body* body = m_world->CreateBody(&bd); // Creating the platform in the world 00436 b2FixtureDef *fd = new b2FixtureDef; 00437 fd->density = 1.f; 00438 fd->shape = new b2PolygonShape; 00439 fd->shape = &shape; 00440 body->CreateFixture(fd); 00441 00442 // Creating an invisible hinge to rotate the platform 00443 b2PolygonShape shape2; 00444 shape2.SetAsBox(0.2f, 1.0f); 00445 b2BodyDef bd2; 00446 bd2.position.Set(12.25f, 12.0f); 00447 b2Body* body2 = m_world->CreateBody(&bd2); 00448 00449 b2RevoluteJointDef jointDef; // Creating a revolute joint to rotate the platform. 00450 jointDef.bodyA = body; 00451 jointDef.bodyB = body2; 00452 jointDef.localAnchorA.Set(0,0); 00453 jointDef.localAnchorB.Set(0,0); 00454 jointDef.collideConnected = false; // The bodies will not collide with each other 00455 m_world->CreateJoint(&jointDef); // Creating the joint in the world. 00456 } 00457 00459 //The box with the balloon which is closed by the lid which is connected to pulley. 00460 { 00461 b2BodyDef *bd = new b2BodyDef; // Creating a new Body definition 00462 bd->type = b2_dynamicBody; // Setting the box to be dynamic. 00463 bd->position.Set(30,16); 00464 00465 //The open box 00466 //Creating the box by 3 planks which form the open box. 00467 //The three fixtures are assigned to same body object to create an open box 00468 00469 // Plank 1 00470 b2FixtureDef *fd1 = new b2FixtureDef; // Creating a fixture of the plank. 00471 fd1->density = 10.0; // Assigning the properties to the box(density) 00472 fd1->friction = 0.5; // Friction 00473 fd1->restitution = 0.f; // Restitution 00474 fd1->shape = new b2PolygonShape; // Creating a new b2PolygonShape for the fixture fd1 00475 b2PolygonShape bs1; 00476 bs1.SetAsBox(5,0.2, b2Vec2(0.f,-1.9f), 0); 00477 fd1->shape = &bs1; // Assigning reference to the shape defined above. 00478 00479 // Plank 2 00480 b2FixtureDef *fd2 = new b2FixtureDef; // Creating a fixture of the plank. 00481 fd2->density = 10.0; // Assigning the properties to the box(density) 00482 fd2->friction = 0.5; // Friction 00483 fd2->restitution = 0.f; // Restitution 00484 fd2->shape = new b2PolygonShape; // Creating a new b2PolygonShape for the fixture fd2 00485 b2PolygonShape bs2; 00486 bs2.SetAsBox(0.2,2, b2Vec2(5.0f,0.f), 0); 00487 fd2->shape = &bs2; 00488 00489 // Plank 3 00490 b2FixtureDef *fd3 = new b2FixtureDef; 00491 fd3->density = 10.0; 00492 fd3->friction = 0.5; 00493 fd3->restitution = 0.f; 00494 fd3->shape = new b2PolygonShape; 00495 b2PolygonShape bs3; 00496 bs3.SetAsBox(0.2,2, b2Vec2(-5.0f,0.f), 0); 00497 fd3->shape = &bs3; 00498 00499 00500 b2Body* box1 = m_world->CreateBody(bd); // Creating the body in the Box2D world. 00501 //Assigning the three fixtures to box1 to create a open box 00502 //The three fixtures define the 3 planks. Assigning them to box1 creates a fixed box with 3 planks. 00503 box1->CreateFixture(fd1); 00504 box1->CreateFixture(fd2); 00505 box1->CreateFixture(fd3); 00506 } 00507 00509 //The balloon in the box 00510 { 00511 b2Body* sbody; // The balloon object 00512 b2CircleShape circle; // Defining the shape of the balloon(circle) 00513 circle.m_radius = 1.0; // radius of the balloon 00514 00515 b2FixtureDef ballfd; // FIxture of the balloon 00516 ballfd.shape = &circle; // Assigning the shape to the fixture 00517 ballfd.density = 1.0f; // Setting the density 00518 ballfd.friction = 0.0f; // Frictionless ball 00519 ballfd.restitution = 0.0f; // Collision of Restitution = 0 (Inelastic Collision) 00520 b2BodyDef ballbd; // Defining the balloon 00521 ballbd.type = b2_dynamicBody; // Setting the balloon to dynamic 00522 ballbd.position.Set(29.0f, 16.0f); 00523 sbody = m_world->CreateBody(&ballbd); // Creating the balloon in the world 00524 sbody -> SetGravityScale(-0.1f); // Setting Negative Gravity so that balloon rises up. 00525 sbody->CreateFixture(&ballfd); // Assigning the fixture to the balloon. 00526 } 00527 00528 00530 //The second revolving horizontal platform on the top with the top spheres on it(to balance it) 00531 { 00532 b2PolygonShape shape; 00533 shape.SetAsBox(4.0f, 0.2f); 00534 00535 b2BodyDef bd; 00536 bd.position.Set(27.5f, 42.25f); 00537 bd.type = b2_dynamicBody; // Setting type to dynamic 00538 b2Body* body = m_world->CreateBody(&bd); // Creating the platform in the world 00539 b2FixtureDef *fd = new b2FixtureDef; 00540 fd->density = 1.f; 00541 fd->shape = new b2PolygonShape; 00542 fd->shape = &shape; 00543 body->CreateFixture(fd); 00544 00545 // Creating an invisible hinge to rotate the platform 00546 b2PolygonShape shape2; 00547 shape2.SetAsBox(0.2f, 1.0f); 00548 b2BodyDef bd2; 00549 bd2.position.Set(27.5f, 42.25f); 00550 b2Body* body2 = m_world->CreateBody(&bd2); 00551 00552 b2RevoluteJointDef jointDef; // Creating a revolute joint to rotate the platform. 00553 jointDef.bodyA = body; 00554 jointDef.bodyB = body2; 00555 jointDef.localAnchorA.Set(0,0); 00556 jointDef.localAnchorB.Set(0,0); 00557 jointDef.collideConnected = false; // The bodies will not collide with each other 00558 m_world->CreateJoint(&jointDef); // Creating the joint in the world. 00559 } 00560 00561 00563 //The two small spheres on the top revolving platform. 00564 { 00565 b2Body* spherebody; // Creating the spheres 00566 b2CircleShape circle; // Defining the shape of the sphere 00567 circle.m_radius = 0.5; // Circle of radius 0.5f ; 00568 00569 b2FixtureDef ballfd; // Defining the fixture of the sphere 00570 ballfd.shape = &circle; // Assigning the shape to the fixture 00571 ballfd.density = 1.0f; // Assigning the density 00572 ballfd.friction = 0.0f; // Assigning the friction 00573 ballfd.restitution = 0.0f; 00574 00575 { 00576 b2BodyDef ballbd; // Defining the sphere 00577 ballbd.type = b2_dynamicBody; // Setting the type to dynamic 00578 ballbd.position.Set(24.0f , 42.8f); // Setting the position of the sphere 00579 spherebody = m_world->CreateBody(&ballbd); // Creating the sphere in the Box2D world 00580 spherebody->CreateFixture(&ballfd); // Creating the sphere with fixture defined 00581 } 00582 { 00583 b2BodyDef ballbd; // Defining the sphere 00584 ballbd.type = b2_dynamicBody; // Setting the type to dynamic 00585 ballbd.position.Set(31.0f , 42.8f); // Setting the position of the sphere 00586 spherebody = m_world->CreateBody(&ballbd); // Creating the sphere in the Box2D world 00587 spherebody->CreateFixture(&ballfd); // Creating the sphere with fixture defined 00588 } 00589 } 00590 00591 00593 //The slant edge on which one of the small spheres slides down. 00594 { 00595 b2EdgeShape shape; // line segment used to define the path. 00596 shape.Set(b2Vec2(-40.0f, 38.0f), b2Vec2(23.6f, 41.75f)); // The two end points of the path are specified. 00597 b2BodyDef bd; //Used to define the body. 00598 b1 = m_world->CreateBody(&bd); // Body created in the Box2D world. 00599 b1->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. 00600 } 00601 00603 //The Spherical tube(semi-circular). The balls slides down this tube. 00604 { 00605 float pi = 3.14159 ; 00606 float inrad = 8.0, outrad = 9.25, cx = -40.0, cy = 30.0 ; //Temporary positional variables. 00607 00608 //One circular edge of the tube. 00609 for(int i=0,j=2; i<90; i+=2, j+=2) 00610 { 00611 b2EdgeShape shape; // Define line segment shape 00612 shape.Set(b2Vec2(cx - inrad * sin(2*pi*i/180), cy + inrad * cos(2*pi*i/180)), b2Vec2(cx - inrad * sin(2*pi*j/180), cy + inrad * cos(2*pi*j/180))); // Position the end points of the segments 00613 // To make a circular structure of 180 degrees, divided the semi-circle 00614 // into 90 arc segments of 2 degrees each and created line segments 00615 // with end points of each of the arcs 00616 00617 b2BodyDef bd; // Used to define Box2D body 00618 b1 = m_world->CreateBody(&bd); // Used to define a fixture 00619 b1->CreateFixture(&shape, 0.0f); 00620 } 00621 00622 //The other circular edge of the tube. 00623 for(int i=0,j=2; i<90; i+=2, j+=2) 00624 { 00625 b2EdgeShape shape; 00626 shape.Set(b2Vec2(cx - outrad * sin(2*pi*i/180), cy + outrad * cos(2*pi*i/180)), b2Vec2(cx - outrad * sin(2*pi*j/180), cy + outrad * cos(2*pi*j/180))); 00627 00628 b2BodyDef bd; 00629 b1 = m_world->CreateBody(&bd); 00630 b1->CreateFixture(&shape, 0.0f); 00631 } 00632 } 00633 00635 //The horizontal parallel planks which have flaps at one end. 00636 { 00637 for(int i=0; i<6; i++) 00638 { 00639 b2EdgeShape shape; // line segment used to define the planks 00640 shape.Set(b2Vec2(-40.0f, 20.0f - 4.0f * i), b2Vec2(-22.0f, 20.0f - 4.0f * i)); 00641 b2BodyDef bd; //Used to define the body. 00642 b1 = m_world->CreateBody(&bd); // Body created in the Box2D world. 00643 b1->CreateFixture(&shape, 0.0f); 00644 } 00645 00646 } 00647 00649 //The vertical rotating flaps at the end of the horizontal planks(which hit the spheres on each floor and hit the next flap triggering a chain reaction) 00650 { 00651 //Right Flaps 00652 for(int i = 0 ; i < 3 ; i++) { 00653 b2PolygonShape shape; 00654 shape.SetAsBox(0.2f, 2.2f); // Setting Dimensions of the flap. 00655 00656 b2BodyDef bd; 00657 bd.position.Set(-21.7f, 18.3f - 8.0f*i); // Setting position of the flap. 00658 bd.type = b2_dynamicBody; // Setting type to dynamic 00659 b2Body* body = m_world->CreateBody(&bd); // Creating the flap in the world 00660 b2FixtureDef *fd = new b2FixtureDef; 00661 fd->density = 10.0f; // Assigning density to the fixture 00662 fd->restitution = 1.0f ; 00663 fd->shape = new b2PolygonShape; 00664 fd->shape = &shape; 00665 body->CreateFixture(fd); 00666 00667 // Creating an invisible hinge to rotate the platform 00668 b2PolygonShape shape2; 00669 shape2.SetAsBox(0.2f, 1.0f); 00670 b2BodyDef bd2; 00671 bd2.position.Set(-21.7f, 18.3f-8.0f*i); 00672 b2Body* body2 = m_world->CreateBody(&bd2); 00673 00674 b2RevoluteJointDef jointDef; // Creating a revolute joint to rotate the flap. 00675 jointDef.bodyA = body; 00676 jointDef.bodyB = body2; 00677 jointDef.localAnchorA.Set(0,0); 00678 jointDef.localAnchorB.Set(0,0); 00679 jointDef.collideConnected = false; // The bodies will not collide with each other 00680 m_world->CreateJoint(&jointDef); 00681 } 00682 00683 //Left Flaps 00684 for(int i = 0; i < 2; i++) { 00685 b2PolygonShape shape; 00686 shape.SetAsBox(0.2f, 2.2f); 00687 00688 b2BodyDef bd; 00689 bd.position.Set(-40.3f, 14.2f - 8.0f*i); 00690 bd.type = b2_dynamicBody; // Setting type to dynamic 00691 b2Body* body = m_world->CreateBody(&bd); // Creating the platform in the world 00692 b2FixtureDef *fd = new b2FixtureDef; 00693 fd->density = 10.0f; 00694 fd->restitution = 1.0f ; 00695 fd->shape = new b2PolygonShape; 00696 fd->shape = &shape; 00697 body->CreateFixture(fd); 00698 00699 // Creating an invisible hinge to rotate the platform 00700 b2PolygonShape shape2; 00701 shape2.SetAsBox(0.2f, 1.0f); 00702 b2BodyDef bd2; 00703 bd2.position.Set(-40.3f, 14.2f-8.0f*i); 00704 b2Body* body2 = m_world->CreateBody(&bd2); 00705 00706 b2RevoluteJointDef jointDef; // Creating a revolute joint to rotate the platform. 00707 jointDef.bodyA = body; 00708 jointDef.bodyB = body2; 00709 jointDef.localAnchorA.Set(0,0); 00710 jointDef.localAnchorB.Set(0,0); 00711 jointDef.collideConnected = false; // The bodies will not collide with each other 00712 m_world->CreateJoint(&jointDef); 00713 } 00714 } 00715 00717 //The balls on the planks(which hit the next flap and trigger the chain reaction. 00718 { 00719 //Right side balls. 00720 for(int i = 0 ; i < 3 ; i++) { 00721 b2Body* spherebody; // Creating the spheres 00722 b2CircleShape circle; // Defining the shape of the sphere 00723 circle.m_radius = 0.5; // Circle of radius 0.5f ; 00724 00725 b2FixtureDef ballfd; // Defining the fixture of the sphere 00726 ballfd.shape = &circle; // Assigning the shape to the fixture 00727 ballfd.density = 10.0f; // Assigning the density 00728 ballfd.friction = 0.0f; // Assigning the friction 00729 ballfd.restitution = 0.0f; 00730 00731 { 00732 b2BodyDef ballbd; // Defining the sphere 00733 ballbd.type = b2_dynamicBody; // Setting the type to dynamic 00734 ballbd.position.Set(-22.5f , 16.6 - 8.0f*i); // Setting the position of the spheres 00735 spherebody = m_world->CreateBody(&ballbd); // Creating the spheres in the Box2D world 00736 spherebody->CreateFixture(&ballfd); // Creating the spheres with fixture defined 00737 } 00738 } 00739 //Left side balls. 00740 for(int i = 0 ; i < 2 ; i++) { 00741 b2Body* spherebody; // Creating the spheres 00742 b2CircleShape circle; // Defining the shape of the sphere 00743 circle.m_radius = 0.5; // Circle of radius 0.5f ; 00744 00745 b2FixtureDef ballfd; // Defining the fixture of the sphere 00746 ballfd.shape = &circle; // Assigning the shape to the fixture 00747 ballfd.density = 10.0f; // Assigning the density 00748 ballfd.friction = 0.0f; // Assigning the friction 00749 ballfd.restitution = 0.0f; 00750 00751 { 00752 b2BodyDef ballbd; // Defining the sphere 00753 ballbd.type = b2_dynamicBody; // Setting the type to dynamic 00754 ballbd.position.Set(-39.5f , 12.6f - 8.0f*i); // Setting the position of the spheres 00755 spherebody = m_world->CreateBody(&ballbd); // Creating the spheres in the Box2D world 00756 spherebody->CreateFixture(&ballfd); // Creating the spheres with fixture defined 00757 } 00758 } 00759 } 00760 00762 // The small blockade which stops the rocket from take-off. 00763 { 00764 b2BodyDef *bd = new b2BodyDef; // Creating a new Body definition 00765 bd->position.Set(-47,10); 00766 00767 // Horizontal Plank 00768 b2FixtureDef *fd2 = new b2FixtureDef; // Defining a fixture for the blockade. 00769 fd2->density = 10.0; // Assigning density. 00770 fd2->friction = 0.0; // Assigning friction 00771 fd2->restitution = 0.f; 00772 fd2->shape = new b2PolygonShape; 00773 b2PolygonShape bs2; 00774 bs2.SetAsBox(0.2,0.2, b2Vec2(-1.5f,0.0f), 0); // Setting dimensions of the blockade. 00775 fd2->shape = &bs2; 00776 b2Body* box1 = m_world->CreateBody(bd); // Creating the body in the Box2D world. 00777 box1->CreateFixture(fd2); 00778 00779 00780 } 00781 00782 00784 //The rocket which flies off after its base is displaced slightly. 00785 { 00786 b2BodyDef *rocket = new b2BodyDef; // Creating a new Body definition 00787 rocket->type = b2_dynamicBody; // Setting the rocket to be dynamic. 00788 rocket->position.Set(-48.4,8); 00789 00790 //The base of the rocket. 00791 b2FixtureDef *fd2 = new b2FixtureDef; // Defining a fixture for the rocket. 00792 fd2->density = 0.5f; // Low density rocket. 00793 fd2->friction = 0.88; // Friction Cofficient is set a large value to provide torque to rocket. 00794 fd2->restitution = 0.f; 00795 fd2->shape = new b2PolygonShape; 00796 b2PolygonShape bs2; 00797 bs2.SetAsBox(1.0,1.5, b2Vec2(0.0f,-1.5f), 0); 00798 fd2->shape = &bs2; 00799 00800 //The triangular head of the rocket. 00801 b2PolygonShape poly; 00802 b2Vec2 vertices[3]; //Using an array of vertices to define the three endpoints of the triangle 00803 vertices[0].Set(-1,0); 00804 vertices[1].Set(1,0); 00805 vertices[2].Set(0,1.73); 00806 poly.Set(vertices, 3); // Setting the shape of the triangle rocket. 00807 b2FixtureDef rocketfd; // Defining the fixture for the rocket. 00808 rocketfd.shape = &poly; // Assigning the shape to the fixture 00809 rocketfd.density = 0.5f; // Setting the density 00810 rocketfd.friction = 0.88f; // Frictionless rocket 00811 rocketfd.restitution = 0.0f; 00812 00813 b2Body* rocket_body; 00814 rocket_body = m_world->CreateBody(rocket); // Creating the rocket in the world. Static Body 00815 rocket_body -> SetGravityScale(-10.0f); // Setting the Gravity Scale(SInce its a rocket, it has large negative gravity to fly off) 00816 rocket_body->CreateFixture(&rocketfd); // Assigning the fixture to the rocket 00817 rocket_body->CreateFixture(fd2); 00818 00819 } 00820 00822 //The block which acts as a base for the rocket. When the block is hit by the sphere, the base provides torque and the rocket frees from the blockade and flies off. 00823 { 00824 b2PolygonShape shape; 00825 shape.SetAsBox(1.0f, 2.5f); 00826 00827 b2BodyDef bd; 00828 bd.position.Set(-48.2f, 2.0f); 00829 bd.type = b2_dynamicBody; // Setting type to dynamic 00830 b2Body* body = m_world->CreateBody(&bd); // Creating the block in the world 00831 b2FixtureDef *fd = new b2FixtureDef; 00832 fd->density = 0.5f; // Low density so sphere can displace it. 00833 fd->friction = 0.87f; // High cofficient of friction to provide torque to the rocket. 00834 fd->shape = new b2PolygonShape; 00835 fd->shape = &shape; 00836 body->CreateFixture(fd); 00837 00838 } 00839 00840 } 00841 00842 sim_t *sim = new sim_t("Dominos", dominos_t::create); 00843 }